GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
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 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 void G_write_zeros(int fd, size_t n)
30 {
31  char zeros[1024];
32  char *z;
33  size_t i;
34 
35  if (n <= 0)
36  return;
37 
38  /* fill zeros buffer with zeros */
39  if (n > sizeof(zeros))
40  i = sizeof(zeros);
41  else
42  i = n;
43 
44  z = zeros;
45  while (i--)
46  *z++ = 0;
47 
48  /* write n zeros to fd */
49  while (n > 0) {
50  if (n > sizeof(zeros))
51  i = sizeof(zeros);
52  else
53  i = n;
54 
55  if (write(fd, zeros, i) < 0)
56  G_fatal_error(_("File writing error in %s() %d:%s"), __func__,
57  errno, strerror(errno));
58  n -= i;
59  }
60 }
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
#define write
Definition: unistd.h:6
void G_write_zeros(int fd, size_t n)
Writes n bytes of zero to file descriptor fd
Definition: writ_zeros.c:29