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