GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-c7e1de7dfd
writ_zeros.c
Go to the documentation of this file.
1 /*!
2  * \file lib/gis/writ_zeros.c
3  *
4  * \brief GIS Library - Write zero functions.
5  *
6  * (C) 2001-2014 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public License
9  * (>=v2). Read the file COPYING that comes with GRASS for details.
10  *
11  * \author GRASS GIS Development Team
12  *
13  * \date 1999-2014
14  */
15 
16 #include <errno.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <grass/gis.h>
20 #include <grass/glocale.h>
21 
22 /**
23  * \brief Writes <b>n</b> bytes of zero to file descriptor <b>fd</b>
24  *
25  * \param[in] fd file descriptor
26  * \param[in] n number of bytes to write
27  * \return
28  */
29 
30 void G_write_zeros(int fd, size_t n)
31 {
32  char zeros[1024];
33  char *z;
34  size_t i;
35 
36  if (n <= 0)
37  return;
38 
39  /* fill zeros buffer with zeros */
40  if (n > sizeof(zeros))
41  i = sizeof(zeros);
42  else
43  i = n;
44 
45  z = zeros;
46  while (i--)
47  *z++ = 0;
48 
49  /* write n zeros to fd */
50  while (n > 0) {
51  if (n > sizeof(zeros))
52  i = sizeof(zeros);
53  else
54  i = n;
55 
56  if (write(fd, zeros, i) < 0)
57  G_fatal_error(_("File writing error in %s() %d:%s"), __func__,
58  errno, strerror(errno));
59  n -= i;
60  }
61 }
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
void G_write_zeros(int fd, size_t n)
Writes n bytes of zero to file descriptor fd
Definition: writ_zeros.c:30