title: How do I write directly to a USB drive with over 4GB data?
body: <p>I have a research project where I need to be able to fill a USB stick with a known pattern of data directly, no files or file system. My goal is to fill a drive from top to bottom with my pattern; I have written a program for writing to /dev/sd* but it is slow and does not work if the drive is over 4GB in size. The writing will stop at offset oxFFFFFFF or 2^32.</p>
  
  <p>My code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;linux/fs.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;inttypes.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/ioctl.h&gt;
  #include &lt;ctype.h&gt;
  
  
  
  int main(int argc, char **argv)
  {
      int fd;
      uint64_t numbytes=0;
      char response[4];
      long int nblocks = 0; // number of blocks up to 4GB
  
  
      int error;
      size_t fill_length = 8;
      char buf[512];
      char fill[] = "ABCDEFGH";
  
      printf("\n\n");
      fd = open(argv[1], O_RDWR);
      if(error = ioctl(fd, BLKGETSIZE64, &amp;numbytes) != 0){
          printf("Failed to read block device, ioctl returned: %i\n", error);
          exit(0);
      }
      if (numbytes &gt; 8589934592){ // Exit if more than 8 GB drive
          printf("Drive is too large.l\n");
          exit(0);
      }
      printf("Number of bytes: %lu, i.e. %.3f GiB\n", numbytes,
      (double)numbytes / (1024 * 1024 * 1024));
      nblocks = numbytes / 512;
      printf("Number of blocks on device: %lu\n", nblocks);
  
      strcpy(buf, fill); // fills with pattern, one time
      for(int i =0; i &lt; (512 - fill_length); i += fill_length){ // Fills up the rest of the buffer
          strcat(buf, fill); // with the pattern to be repeated.
      } // 512 is the default &amp; smallest block size
      printf("buf is:\n%s\n", buf);
  
      printf("\n*** The device at %s will be completely overwritten ***\n", argv[1]);
      printf("\nAre you sure you want to proceed? (Type:&lt;Ctrl&gt;-C to exit)\n");
  
      // printf("\n nblocks: %lu", nblocks);
      fgets(response, 3, stdin);
      printf("writting to: %s\n", argv[1]);
  
      for (int i = 0; i &lt;= nblocks; i++)
      {
          write(fd, buf, 512);
      }
  
  
      printf("Closing...\n");
      close(fd);
      printf("Closed.\n");
      return 0;
  
  } 
  </code></pre>
  
  <p>I realize my program isn't great and is dangerous as I could wipe out a HDD, but all I am looking for at this point is tips to make this work on drives over 4GB and hopefully make the process faster. It will have limited use by myself and possibly another.</p>
  
  <p>A push in the right direction would be appreciated.</p>