GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
writ_zeros.c
Go to the documentation of this file.
1 
17 #include <unistd.h>
18 #include <grass/gis.h>
19 
20 
29 int G_write_zeros(int fd, size_t n)
30 {
31  char zeros[1024];
32  char *z;
33  int i;
34 
35  if (n <= 0)
36  return 0;
37 
38  /* There is a subtle gotcha to be avoided here.
39  *
40  * i must be an int for the write, but n (size_t) can be long or larger.
41  * Must be careful not to cast long to int, hence
42  * avoid i = n unless n is within range of int */
43 
44  /* fill zeros buffer with zeros */
45  if (n > sizeof(zeros))
46  i = sizeof(zeros);
47  else
48  i = n; /* this is ok here */
49 
50  z = zeros;
51  while (i--)
52  *z++ = 0;
53 
54  /* write n zeros to fd */
55  while (n > 0) {
56  if (n > sizeof(zeros))
57  i = sizeof(zeros);
58  else
59  i = n; /* this is ok here */
60 
61  write(fd, zeros, i);
62  n -= i;
63  }
64 
65  return 0;
66 }
FILE * fd
Definition: g3dcolor.c:368
int G_write_zeros(int fd, size_t n)
Writes n bytes of 9 to file descriptor fd
Definition: writ_zeros.c:29
int n
Definition: dataquad.c:291