writ_zeros.c

Go to the documentation of this file.
00001 #include <grass/gis.h>
00002 #include <unistd.h>
00003 /*******************************************************
00004  * 
00005  * G_write_zeros (fd ,n)
00006  *   int fd
00007  *   long n
00008  *
00009  * writes n bytes of zero to file descriptor fd
00010  ******************************************************/
00011 
00012 int G_write_zeros(int fd, long n)
00013 {
00014     char zeros[1024];
00015     register char *z;
00016     register int i;
00017 
00018 /*
00019  * there is a subtle gotcha to be avoided here
00020  * i must be an int for the write, but n can be long 
00021  * must be careful not to cast long to int, hence must
00022  * avoid i = n unless n is within range of int
00023  */
00024     if (n <= 0)
00025         return 0;
00026 
00027 /* fill zeros buffer with zeros */
00028     if (n > sizeof zeros)
00029         i = sizeof zeros;
00030     else
00031         i = n;  /* this is ok here */
00032 
00033     z = zeros;
00034     while (i--)
00035         *z++ = 0;
00036 
00037 /* write n zeros to fd */
00038     while (n > 0)
00039     {
00040         if (n > sizeof zeros)
00041             i = sizeof zeros;
00042         else
00043             i = n;      /* this is ok here */
00044         write (fd, zeros, i);
00045         n -= i;
00046     }
00047 
00048     return 0;
00049 }

Generated on Fri Nov 21 11:02:18 2008 for GRASS by  doxygen 1.5.1