2 /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
4 /* a function similar to vsnprintf() but it just handles %m */
8 #include <stddef.h> /* ptrdiff_t */
9 #include <ctype.h> /* isdigit() */
16 #include <sys/utsname.h>
17 #include <time.h> /* time_t, strftime() */
18 #include <sys/time.h> /* gettimeofday() */
25 #include "snprinterr.h"
27 #define HAVE_STRERROR_R 0
28 /* replace %m in format with actual strerror() message, write result to *str.
29 keep other % formats unchanged!
30 writes at most size chars including the terminating \0 to *str
31 returns the number of bytes in the output without terminating \0
32 result is always \0 terminated except when size==0
34 int snprinterr(char *str
, size_t size
, const char *format
) {
36 int full
= 0; /* 1 means: there is no space left in * str for more data or \0 */
38 if (size
== 0) return 0;
39 if (count
>= size
) full
= 1;
40 while (c
= *format
++) {
45 ++count
; if (!full
) { (*str
++ = '%'); if (count
+1 >= size
) full
= 1; }
48 ++count
; if (!full
) { (*str
++ = '%'); if (count
+1 >= size
) full
= 1; }
49 ++count
; if (!full
) { (*str
++ = c
); if (count
+1 >= size
) full
= 1; }
55 char buf
[BUFLEN
] = "";
56 #endif /* HAVE_STRERROR_R */
59 bufp
= strerror(errno
);
61 /* there are two versions floating around... */
62 # if 1 /* GNU version */
63 bufp
= strerror_r(errno
, buf
, BUFLEN
);
64 # else /* standard version */
65 strerror_r(errno
, buf
, BUFLEN
);
68 #endif /* HAVE_STRERROR_R */
69 while ((c
= *bufp
++) != '\0') {
70 ++count
; if (!full
) { (*str
++ = c
); if (count
+1 >= size
) full
= 1; }
73 c
= ' '; /* not \0 ! */
78 ++count
; if (!full
) { (*str
++ = c
); if (count
+1 >= size
) full
= 1; }
81 *str
++ = '\0'; /* always write terminating \0 */