GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
snprintf.c
Go to the documentation of this file.
1 
2 /*!
3  * \file lib/gis/snprintf.c
4  *
5  * \brief GIS Library - snprintf() clone functions.
6  *
7  *
8  * \todo if needed, implement alternative versions for portability.
9  * potential code source:
10  * - http://www.ijs.si/software/snprintf/
11  * - openssh's snprintf() implementation: bsd-snprintf.c
12  *
13  * (C) 2001-2014 by the GRASS Development Team
14  *
15  * This program is free software under the GNU General Public License
16  * (>=v2). Read the file COPYING that comes with GRASS for details.
17  *
18  * \author Markus Neteler
19  *
20  * \date 2006-2008
21  */
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <unistd.h>
27 #include <assert.h>
28 #include <grass/gis.h>
29 
30 /**
31  * \brief snprintf() clone.
32  *
33  * <b>Note:</b> The use of <i>snprintf()</i>/<i>G_snprintf()</i> is
34  * discouraged in favour of calculating how long the string will be and
35  * allocating enough memory!
36  *
37  * \param[in] str input string
38  * \param[in] size length of string
39  * \param[in] fmt
40  * \return numer of chars written
41  */
42 
43 int G_snprintf(char *str, size_t size, const char *fmt, ...)
44 {
45  va_list ap;
46  int count;
47 
48  va_start(ap, fmt);
49  count = vsnprintf(str, size, fmt, ap);
50  va_end(ap);
51 
52  /* Windows' vsnprintf() doesn't always NUL-terminate the buffer */
53  if (count == size)
54  str[--count] = '\0';
55 
56  return count;
57 }
int count
int G_snprintf(char *str, size_t size, const char *fmt,...)
snprintf() clone.
Definition: snprintf.c:43