GRASS Programmer's Manual  6.4.2(2012)
writ_zeros.c
Go to the documentation of this file.
00001 
00017 #include <unistd.h>
00018 #include <grass/gis.h>
00019 
00020 
00029 int G_write_zeros(int fd, size_t n)
00030 {
00031     char zeros[1024];
00032     char *z;
00033     int i;
00034 
00035     if (n <= 0)
00036         return 0;
00037 
00038     /* There is a subtle gotcha to be avoided here.
00039      *
00040      * i must be an int for the write, but n (size_t) can be long or larger.
00041      * Must be careful not to cast long to int, hence
00042      * avoid i = n unless n is within range of int */
00043 
00044     /* fill zeros buffer with zeros */
00045     if (n > sizeof(zeros))
00046         i = sizeof(zeros);
00047     else
00048         i = n;                  /* this is ok here */
00049 
00050     z = zeros;
00051     while (i--)
00052         *z++ = 0;
00053 
00054     /* write n zeros to fd */
00055     while (n > 0) {
00056         if (n > sizeof(zeros))
00057             i = sizeof(zeros);
00058         else
00059             i = n;              /* this is ok here */
00060 
00061         write(fd, zeros, i);
00062         n -= i;
00063     }
00064 
00065     return 0;
00066 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines