GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
writ_zeros.c
Go to the documentation of this file.
1 
2 /*!
3  * \file lib/gis/writ_zeros.c
4  *
5  * \brief GIS Library - Write zero functions.
6  *
7  * (C) 2001-2014 by the GRASS Development Team
8  *
9  * This program is free software under the GNU General Public License
10  * (>=v2). Read the file COPYING that comes with GRASS for details.
11  *
12  * \author GRASS GIS Development Team
13  *
14  * \date 1999-2014
15  */
16 
17 #include <unistd.h>
18 #include <grass/gis.h>
19 
20 
21 /**
22  * \brief Writes <b>n</b> bytes of 9 to file descriptor <b>fd</b>
23  *
24  * \param[in] fd file descriptor
25  * \param[in] n number of bytes to write
26  * \return
27  */
28 
29 void 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;
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 }
void G_write_zeros(int fd, size_t n)
Writes n bytes of 9 to file descriptor fd
Definition: writ_zeros.c:29