Menu

/dev/zero and friends on windows?

Help
2005-04-17
2012-07-26
  • Dwaine Gonyier

    Dwaine Gonyier - 2005-04-17

    This is probably a stupid question, but it seems that there is no /dev/zero equivalent available under windows.

    I.e. I want to create a file full of null bytes.
    Under Linux:
    dd if=/dev/zero of=/tmp/foo bs=1M count=1
    works, but how do I (or can I even) do the same thing under windows with the dd for gnuwin32?

    I think cygwin emulates things like /dev/zero and /dev/null under that environment, but I don't have cygwin installed in all places and was hoping the gnuwin32 tools could help me out.

    Thanks.

     
    • Nobody/Anonymous

      I'm not aware of a gnuwin32 tool that will fill a file with a particular byte value, but here's one that will. I wrote it for the MSVC 6.0 environment; it should port to other compiler environments with minimal work. The part that might give trouble is the "setmode" call; you'll need this to prevent NL to CRLF translation, and to avoid misinterpretation of CTRL-Z.

      Enjoy,
      Robert Bassett

      / bytegen: repeat a specified byte /

      include <stdio.h>

      include <fcntl.h>

      unsigned interp (char s)
      {
      if (
      s == '\')
      switch (++s) {
      case 'x': case 'X':
      return (unsigned char) strtoul (++s, NULL, 16);
      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9':
      return (unsigned char) strtoul (s, NULL, 0);
      case '\':
      return '\';
      case 'a':
      return '\a';
      case 'b':
      return '\b';
      case 'f':
      return '\f';
      case 'n':
      return '\n';
      case 'r':
      return '\r';
      case 't':
      return '\t';
      case 'v':
      return '\v';
      }
      return (unsigned char)
      s;
      }

      int main (int argc, char **argv)
      {
      unsigned byte, rept;

      if ((argc &gt; 1) &amp;&amp; (!strcmp (argv[1], &quot;-h&quot;) || !strcmp (argv[1], &quot;--help&quot;))) {
          printf (&quot;%s: usage: %s [ char [ repeat ] ]\n&quot;, argv[0], argv[0]);
          printf (&quot;\nchar may be one of the following:\n&quot;);
          printf (&quot; \\xhh   an hexadecimal escape code\n&quot;);
          printf (&quot; \\ooo   an octal escape code (beginning with 0)\n&quot;);
          printf (&quot; \\ddd   a decimal escape code (not beginning with 0)\n&quot;);
          printf (&quot; \\\\     backslash\n&quot;);
          printf (&quot; \\a     alert (BEL)\n&quot;);
          printf (&quot; \\b     backspace\n&quot;);
          printf (&quot; \\f     form feed\n&quot;);
          printf (&quot; \\n     newline\n&quot;);
          printf (&quot; \\r     carriage return\n&quot;);
          printf (&quot; \\t     tab\n&quot;);
          printf (&quot; \\v     vertical tab\n&quot;);
          printf (&quot; c      a literal character (default is a)\n&quot;);
          printf (&quot;\nrepeat is a 32-bit unsigned number, from 0 to 4294967295, inclusive.\n&quot;);
          printf (&quot;repeat may be specified in decimal, octal (with leading 0), or 0x hex.\n&quot;);
          printf (&quot;repeat defaults to 128, for no special reason.\n&quot;);
          return 0;
      }
      if (argc &lt; 2) {
          byte = 'a';
          rept = 128;
      } else if (argc &lt; 3) {
          byte = interp (argv[1]);
          rept = 128;
      } else {
          byte = interp (argv[1]);
          rept = strtoul (argv[2], NULL, 0);
      }
      setmode (1, _O_BINARY);
      while (rept--) 
          putchar (byte);
      return 0;
      

      }

       
MongoDB Logo MongoDB