1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.84 2017/05/29 10:05:46 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $
6 * Purpose : zalloc, hash_string, strcmpic, strncmpic, and
7 * MinGW32 strdup functions. These are each too small
8 * to deserve their own file but don't really fit in
11 * Copyright : Written by and Copyright (C) 2001-2016 the
12 * Privoxy team. http://www.privoxy.org/
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
18 * The timegm replacement function was taken from GnuPG,
19 * Copyright (C) 2004 Free Software Foundation, Inc.
21 * The snprintf replacement function is written by
22 * Mark Martinec who also holds the copyright. It can be
23 * used under the terms of the GPL or the terms of the
24 * "Frontier Artistic License".
26 * This program is free software; you can redistribute it
27 * and/or modify it under the terms of the GNU General
28 * Public License as published by the Free Software
29 * Foundation; either version 2 of the License, or (at
30 * your option) any later version.
32 * This program is distributed in the hope that it will
33 * be useful, but WITHOUT ANY WARRANTY; without even the
34 * implied warranty of MERCHANTABILITY or FITNESS FOR A
35 * PARTICULAR PURPOSE. See the GNU General Public
36 * License for more details.
38 * The GNU General Public License should be included with
39 * this file. If not, you can view it at
40 * http://www.gnu.org/copyleft/gpl.html
41 * or write to the Free Software Foundation, Inc., 59
42 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 *********************************************************************/
50 #include <sys/types.h>
52 #if !defined(_WIN32) && !defined(__OS2__)
54 #endif /* #if !defined(_WIN32) && !defined(__OS2__) */
59 #if !defined(HAVE_TIMEGM) && defined(HAVE_TZSET) && defined(HAVE_PUTENV)
61 #endif /* !defined(HAVE_TIMEGM) && defined(HAVE_TZSET) && defined(HAVE_PUTENV) */
68 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
70 /*********************************************************************
74 * Description : Returns allocated memory that is initialized
78 * 1 : size = Size of memory chunk to return.
80 * Returns : Pointer to newly alloc'd memory chunk.
82 *********************************************************************/
83 void *zalloc(size_t size)
88 ret = calloc(1, size);
90 if ((ret = (void *)malloc(size)) != NULL)
101 /*********************************************************************
103 * Function : zalloc_or_die
105 * Description : zalloc wrapper that either succeeds or causes
106 * program termination.
108 * Useful in situations were the string length is
109 * "small" and zalloc() failures couldn't be handled
110 * better anyway. In case of debug builds, failures
111 * trigger an assert().
114 * 1 : size = Size of memory chunk to return.
116 * Returns : Pointer to newly malloc'd memory chunk.
118 *********************************************************************/
119 void *zalloc_or_die(size_t size)
123 buffer = zalloc(size);
126 assert(buffer != NULL);
127 log_error(LOG_LEVEL_FATAL, "Out of memory in zalloc_or_die().");
135 /*********************************************************************
137 * Function : strdup_or_die
139 * Description : strdup wrapper that either succeeds or causes
140 * program termination.
142 * Useful in situations were the string length is
143 * "small" and strdup() failures couldn't be handled
144 * better anyway. In case of debug builds, failures
145 * trigger an assert().
148 * 1 : str = String to duplicate
150 * Returns : Pointer to newly strdup'd copy of the string.
152 *********************************************************************/
153 char *strdup_or_die(const char *str)
157 new_str = strdup(str);
161 assert(new_str != NULL);
162 log_error(LOG_LEVEL_FATAL, "Out of memory in strdup_or_die().");
171 /*********************************************************************
173 * Function : malloc_or_die
175 * Description : malloc wrapper that either succeeds or causes
176 * program termination.
178 * Useful in situations were the buffer size is "small"
179 * and malloc() failures couldn't be handled better
180 * anyway. In case of debug builds, failures trigger
184 * 1 : buffer_size = Size of the space to allocate
186 * Returns : Pointer to newly malloc'd memory
188 *********************************************************************/
189 void *malloc_or_die(size_t buffer_size)
193 if (buffer_size == 0)
195 log_error(LOG_LEVEL_ERROR,
196 "malloc_or_die() called with buffer size 0");
197 assert(buffer_size != 0);
201 new_buf = malloc(buffer_size);
205 assert(new_buf != NULL);
206 log_error(LOG_LEVEL_FATAL, "Out of memory in malloc_or_die().");
216 /*********************************************************************
218 * Function : write_pid_file
220 * Description : Writes a pid file with the pid of the main process.
221 * Exits if the file can't be opened
227 *********************************************************************/
228 void write_pid_file(void)
233 * If no --pidfile option was given,
234 * we can live without one.
236 if (pidfile == NULL) return;
238 if ((fp = fopen(pidfile, "w")) == NULL)
240 log_error(LOG_LEVEL_FATAL, "can't open pidfile '%s': %E", pidfile);
244 fprintf(fp, "%u\n", (unsigned int) getpid());
250 #endif /* def unix */
253 /*********************************************************************
255 * Function : hash_string
257 * Description : Take a string and compute a (hopefuly) unique numeric
258 * integer value. This is useful to "switch" a string.
261 * 1 : s : string to be hashed.
263 * Returns : The string's hash
265 *********************************************************************/
266 unsigned int hash_string(const char* s)
272 h = 5 * h + (unsigned int)*s;
280 /*********************************************************************
282 * Function : strcmpic
284 * Description : Case insensitive string comparison
287 * 1 : s1 = string 1 to compare
288 * 2 : s2 = string 2 to compare
290 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
292 *********************************************************************/
293 int strcmpic(const char *s1, const char *s2)
300 if ((*s1 != *s2) && (privoxy_tolower(*s1) != privoxy_tolower(*s2)))
306 return(privoxy_tolower(*s1) - privoxy_tolower(*s2));
311 /*********************************************************************
313 * Function : strncmpic
315 * Description : Case insensitive string comparison (up to n characters)
318 * 1 : s1 = string 1 to compare
319 * 2 : s2 = string 2 to compare
320 * 3 : n = maximum characters to compare
322 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
324 *********************************************************************/
325 int strncmpic(const char *s1, const char *s2, size_t n)
327 if (n <= (size_t)0) return(0);
333 if ((*s1 != *s2) && (privoxy_tolower(*s1) != privoxy_tolower(*s2)))
338 if (--n <= (size_t)0) break;
342 return(privoxy_tolower(*s1) - privoxy_tolower(*s2));
347 /*********************************************************************
351 * Description : In-situ-eliminate all leading and trailing whitespace
355 * 1 : s : string to be chomped.
357 * Returns : chomped string
359 *********************************************************************/
360 char *chomp(char *string)
365 * strip trailing whitespace
367 p = string + strlen(string);
368 while (p > string && privoxy_isspace(*(p-1)))
375 * find end of leading whitespace
378 while (*q && privoxy_isspace(*q))
384 * if there was any, move the rest forwards
399 /*********************************************************************
401 * Function : string_append
403 * Description : Reallocate target_string and append text to it.
404 * This makes it easier to append to malloc'd strings.
405 * This is similar to the (removed) strsav(), but
406 * running out of memory isn't catastrophic.
410 * The following style provides sufficient error
411 * checking for this routine, with minimal clutter
412 * in the source code. It is recommended if you
413 * have many calls to this function:
415 * char * s = strdup(...); // don't check for error
416 * string_append(&s, ...); // don't check for error
417 * string_append(&s, ...); // don't check for error
418 * string_append(&s, ...); // don't check for error
419 * if (NULL == s) { ... handle error ... }
423 * char * s = strdup(...); // don't check for error
424 * string_append(&s, ...); // don't check for error
425 * string_append(&s, ...); // don't check for error
426 * if (string_append(&s, ...)) {... handle error ...}
429 * 1 : target_string = Pointer to old text that is to be
430 * extended. *target_string will be free()d by this
431 * routine. target_string must be non-NULL.
432 * If *target_string is NULL, this routine will
433 * do nothing and return with an error - this allows
434 * you to make many calls to this routine and only
435 * check for errors after the last one.
436 * 2 : text_to_append = Text to be appended to old.
439 * Returns : JB_ERR_OK on success, and sets *target_string
440 * to newly malloc'ed appended string. Caller
441 * must free(*target_string).
442 * JB_ERR_MEMORY on out-of-memory. (And free()s
443 * *target_string and sets it to NULL).
444 * JB_ERR_MEMORY if *target_string is NULL.
446 *********************************************************************/
447 jb_err string_append(char **target_string, const char *text_to_append)
453 assert(target_string);
454 assert(text_to_append);
456 if (*target_string == NULL)
458 return JB_ERR_MEMORY;
461 if (*text_to_append == '\0')
466 old_len = strlen(*target_string);
468 new_size = strlen(text_to_append) + old_len + 1;
470 if (NULL == (new_string = realloc(*target_string, new_size)))
472 free(*target_string);
474 *target_string = NULL;
475 return JB_ERR_MEMORY;
478 strlcpy(new_string + old_len, text_to_append, new_size - old_len);
480 *target_string = new_string;
485 /*********************************************************************
487 * Function : string_join
489 * Description : Join two strings together. Frees BOTH the original
490 * strings. If either or both input strings are NULL,
491 * fails as if it had run out of memory.
493 * For comparison, string_append requires that the
494 * second string is non-NULL, and doesn't free it.
496 * Rationale: Too often, we want to do
497 * string_append(s, html_encode(s2)). That assert()s
498 * if s2 is NULL or if html_encode() runs out of memory.
499 * It also leaks memory. Proper checking is cumbersome.
500 * The solution: string_join(s, html_encode(s2)) is safe,
501 * and will free the memory allocated by html_encode().
504 * 1 : target_string = Pointer to old text that is to be
505 * extended. *target_string will be free()d by this
506 * routine. target_string must be non-NULL.
507 * 2 : text_to_append = Text to be appended to old.
509 * Returns : JB_ERR_OK on success, and sets *target_string
510 * to newly malloc'ed appended string. Caller
511 * must free(*target_string).
512 * JB_ERR_MEMORY on out-of-memory, or if
513 * *target_string or text_to_append is NULL. (In
514 * this case, frees *target_string and text_to_append,
515 * sets *target_string to NULL).
517 *********************************************************************/
518 jb_err string_join(char **target_string, char *text_to_append)
522 assert(target_string);
524 if (text_to_append == NULL)
526 freez(*target_string);
527 return JB_ERR_MEMORY;
530 err = string_append(target_string, text_to_append);
532 freez(text_to_append);
538 /*********************************************************************
540 * Function : string_toupper
542 * Description : Produce a copy of string with all convertible
543 * characters converted to uppercase.
546 * 1 : string = string to convert
548 * Returns : Uppercase copy of string if possible,
549 * NULL on out-of-memory or if string was NULL.
551 *********************************************************************/
552 char *string_toupper(const char *string)
557 if (!string || ((result = (char *) zalloc(strlen(string) + 1)) == NULL))
567 *p++ = (char)toupper((int) *q++);
575 /*********************************************************************
577 * Function : string_move
579 * Description : memmove wrapper to move the last part of a string
580 * towards the beginning, overwriting the part in
581 * the middle. strlcpy() can't be used here as the
585 * 1 : dst = Destination to overwrite
586 * 2 : src = Source to move.
590 *********************************************************************/
591 void string_move(char *dst, char *src)
595 /* +1 to copy the terminating nul as well. */
596 memmove(dst, src, strlen(src)+1);
600 /*********************************************************************
604 * Description : Duplicate the first n characters of a string that may
605 * contain '\0' characters.
608 * 1 : string = string to be duplicated
609 * 2 : len = number of bytes to duplicate
611 * Returns : pointer to copy, or NULL if failiure
613 *********************************************************************/
614 char *bindup(const char *string, size_t len)
618 duplicate = (char *)malloc(len);
619 if (NULL != duplicate)
621 memcpy(duplicate, string, len);
629 /*********************************************************************
631 * Function : make_path
633 * Description : Takes a directory name and a file name, returns
634 * the complete path. Handles windows/unix differences.
635 * If the file name is already an absolute path, or if
636 * the directory name is NULL or empty, it returns
640 * 1 : dir: Name of directory or NULL for none.
641 * 2 : file: Name of file. Should not be NULL or empty.
643 * Returns : "dir/file" (Or on windows, "dir\file").
644 * It allocates the string on the heap. Caller frees.
645 * Returns NULL in error (i.e. NULL file or out of
648 *********************************************************************/
649 char * make_path(const char * dir, const char * file)
660 strncpy(path,dir+2,512);
664 strncpy(path,dir+1,512);
669 strncpy(path,dir,512);
677 if (AddPart(path,file,512))
685 #else /* ndef AMIGA */
687 if ((file == NULL) || (*file == '\0'))
689 return NULL; /* Error */
692 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
693 #if defined(_WIN32) || defined(__OS2__)
694 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
695 #else /* ifndef _WIN32 || __OS2__ */
696 || (*file == '/') /* Absolute path (U*ix) */
697 #endif /* ifndef _WIN32 || __OS2__ */
705 size_t path_size = strlen(dir) + strlen(file) + 2; /* +2 for trailing (back)slash and \0 */
708 if (*dir != '/' && basedir && *basedir)
711 * Relative path, so start with the base directory.
713 path_size += strlen(basedir) + 1; /* +1 for the slash */
714 path = malloc(path_size);
715 if (!path) log_error(LOG_LEVEL_FATAL, "malloc failed!");
716 strlcpy(path, basedir, path_size);
717 strlcat(path, "/", path_size);
718 strlcat(path, dir, path_size);
721 #endif /* defined unix */
723 path = malloc(path_size);
724 if (!path) log_error(LOG_LEVEL_FATAL, "malloc failed!");
725 strlcpy(path, dir, path_size);
728 assert(NULL != path);
729 #if defined(_WIN32) || defined(__OS2__)
730 if (path[strlen(path)-1] != '\\')
732 strlcat(path, "\\", path_size);
734 #else /* ifndef _WIN32 || __OS2__ */
735 if (path[strlen(path)-1] != '/')
737 strlcat(path, "/", path_size);
739 #endif /* ifndef _WIN32 || __OS2__ */
740 strlcat(path, file, path_size);
744 #endif /* ndef AMIGA */
748 /*********************************************************************
750 * Function : pick_from_range
752 * Description : Pick a positive number out of a given range.
753 * Should only be used if randomness would be nice,
754 * but isn't really necessary.
757 * 1 : range: Highest possible number to pick.
759 * Returns : Picked number.
761 *********************************************************************/
762 long int pick_from_range(long int range)
766 static unsigned long seed = 0;
767 #endif /* def _WIN32 */
772 if (range <= 0) return 0;
774 #ifdef HAVE_ARC4RANDOM
775 number = arc4random() % range + 1;
776 #elif defined(HAVE_RANDOM)
777 number = random() % range + 1;
778 #elif defined(MUTEX_LOCKS_AVAILABLE)
779 privoxy_mutex_lock(&rand_mutex);
783 seed = (unsigned long)(GetCurrentThreadId()+GetTickCount());
786 seed = (unsigned long)((rand() << 16) + rand());
787 #endif /* def _WIN32 */
788 number = (unsigned long)((rand() << 16) + (rand())) % (unsigned long)(range + 1);
789 privoxy_mutex_unlock(&rand_mutex);
792 * XXX: Which platforms reach this and are there
793 * better options than just using rand() and hoping
796 log_error(LOG_LEVEL_INFO, "No thread-safe PRNG available? Header time randomization "
797 "might cause crashes, predictable results or even combine these fine options.");
798 number = rand() % (long int)(range + 1);
800 #endif /* (def HAVE_ARC4RANDOM) */
806 #ifdef USE_PRIVOXY_STRLCPY
807 /*********************************************************************
809 * Function : privoxy_strlcpy
811 * Description : strlcpy(3) look-alike for those without decent libc.
814 * 1 : destination: buffer to copy into.
815 * 2 : source: String to copy.
816 * 3 : size: Size of destination buffer.
818 * Returns : The length of the string that privoxy_strlcpy() tried to create.
820 *********************************************************************/
821 size_t privoxy_strlcpy(char *destination, const char *source, const size_t size)
825 snprintf(destination, size, "%s", source);
827 * Platforms that lack strlcpy() also tend to have
828 * a broken snprintf implementation that doesn't
829 * guarantee nul termination.
831 * XXX: the configure script should detect and reject those.
833 destination[size-1] = '\0';
835 return strlen(source);
837 #endif /* def USE_PRIVOXY_STRLCPY */
841 /*********************************************************************
843 * Function : privoxy_strlcat
845 * Description : strlcat(3) look-alike for those without decent libc.
848 * 1 : destination: C string.
849 * 2 : source: String to copy.
850 * 3 : size: Size of destination buffer.
852 * Returns : The length of the string that privoxy_strlcat() tried to create.
854 *********************************************************************/
855 size_t privoxy_strlcat(char *destination, const char *source, const size_t size)
857 const size_t old_length = strlen(destination);
858 return old_length + strlcpy(destination + old_length, source, size - old_length);
860 #endif /* ndef HAVE_STRLCAT */
863 #if !defined(HAVE_TIMEGM) && defined(HAVE_TZSET) && defined(HAVE_PUTENV)
864 /*********************************************************************
868 * Description : libc replacement function for the inverse of gmtime().
869 * Copyright (C) 2004 Free Software Foundation, Inc.
871 * Code originally copied from GnuPG, modifications done
872 * for Privoxy: style changed, #ifdefs for _WIN32 added
873 * to have it work on mingw32.
875 * XXX: It's very unlikely to happen, but if the malloc()
876 * call fails the time zone will be permanently set to UTC.
879 * 1 : tm: Broken-down time struct.
881 * Returns : tm converted into time_t seconds.
883 *********************************************************************/
884 time_t timegm(struct tm *tm)
897 old_zone = malloc(3 + strlen(zone) + 1);
900 strcpy(old_zone, "TZ=");
901 strcat(old_zone, zone);
904 /* http://man7.org/linux/man-pages/man3/putenv.3.html
905 * int putenv(char *string);
906 * The string pointed to by string becomes part of the environment, so altering the
907 * string changes the environment.
908 * In other words, the memory pointed to by *string is used until
909 * a) another call to putenv() with the same e-var name
910 * b) the program exits
912 * Windows e-vars don't work that way, so let's not leak memory.
915 #endif /* def _WIN32 */
922 #elif defined(_WIN32)
932 #endif /* !defined(HAVE_TIMEGM) && defined(HAVE_TZSET) && defined(HAVE_PUTENV) */
935 #ifndef HAVE_SNPRINTF
937 * What follows is a portable snprintf routine, written by Mark Martinec.
938 * See: http://www.ijs.si/software/snprintf/
941 - a portable implementation of snprintf,
942 including vsnprintf.c, asnprintf, vasnprintf, asprintf, vasprintf
944 snprintf is a routine to convert numeric and string arguments to
945 formatted strings. It is similar to sprintf(3) provided in a system's
946 C library, yet it requires an additional argument - the buffer size -
947 and it guarantees never to store anything beyond the given buffer,
948 regardless of the format or arguments to be formatted. Some newer
949 operating systems do provide snprintf in their C library, but many do
950 not or do provide an inadequate (slow or idiosyncratic) version, which
951 calls for a portable implementation of this routine.
955 Mark Martinec <mark.martinec@ijs.si>, April 1999, June 2000
956 Copyright © 1999, Mark Martinec
960 #define PORTABLE_SNPRINTF_VERSION_MAJOR 2
961 #define PORTABLE_SNPRINTF_VERSION_MINOR 2
963 #if defined(NEED_ASPRINTF) || defined(NEED_ASNPRINTF) || defined(NEED_VASPRINTF) || defined(NEED_VASNPRINTF)
964 # if defined(NEED_SNPRINTF_ONLY)
965 # undef NEED_SNPRINTF_ONLY
967 # if !defined(PREFER_PORTABLE_SNPRINTF)
968 # define PREFER_PORTABLE_SNPRINTF
972 #if defined(SOLARIS_BUG_COMPATIBLE) && !defined(SOLARIS_COMPATIBLE)
973 #define SOLARIS_COMPATIBLE
976 #if defined(HPUX_BUG_COMPATIBLE) && !defined(HPUX_COMPATIBLE)
977 #define HPUX_COMPATIBLE
980 #if defined(DIGITAL_UNIX_BUG_COMPATIBLE) && !defined(DIGITAL_UNIX_COMPATIBLE)
981 #define DIGITAL_UNIX_COMPATIBLE
984 #if defined(PERL_BUG_COMPATIBLE) && !defined(PERL_COMPATIBLE)
985 #define PERL_COMPATIBLE
988 #if defined(LINUX_BUG_COMPATIBLE) && !defined(LINUX_COMPATIBLE)
989 #define LINUX_COMPATIBLE
992 #include <sys/types.h>
1003 #define isdigit(c) ((c) >= '0' && (c) <= '9')
1005 /* For copying strings longer or equal to 'breakeven_point'
1006 * it is more efficient to call memcpy() than to do it inline.
1007 * The value depends mostly on the processor architecture,
1008 * but also on the compiler and its optimization capabilities.
1009 * The value is not critical, some small value greater than zero
1010 * will be just fine if you don't care to squeeze every drop
1011 * of performance out of the code.
1013 * Small values favor memcpy, large values favor inline code.
1015 #if defined(__alpha__) || defined(__alpha)
1016 # define breakeven_point 2 /* AXP (DEC Alpha) - gcc or cc or egcs */
1018 #if defined(__i386__) || defined(__i386)
1019 # define breakeven_point 12 /* Intel Pentium/Linux - gcc 2.96 */
1022 # define breakeven_point 10 /* HP-PA - gcc */
1024 #if defined(__sparc__) || defined(__sparc)
1025 # define breakeven_point 33 /* Sun Sparc 5 - gcc 2.8.1 */
1028 /* some other values of possible interest: */
1029 /* #define breakeven_point 8 */ /* VAX 4000 - vaxc */
1030 /* #define breakeven_point 19 */ /* VAX 4000 - gcc 2.7.0 */
1032 #ifndef breakeven_point
1033 # define breakeven_point 6 /* some reasonable one-size-fits-all value */
1036 #define fast_memcpy(d,s,n) \
1037 { register size_t nn = (size_t)(n); \
1038 if (nn >= breakeven_point) memcpy((d), (s), nn); \
1039 else if (nn > 0) { /* proc call overhead is worth only for large strings*/\
1040 register char *dd; register const char *ss; \
1041 for (ss=(s), dd=(d); nn>0; nn--) *dd++ = *ss++; } }
1043 #define fast_memset(d,c,n) \
1044 { register size_t nn = (size_t)(n); \
1045 if (nn >= breakeven_point) memset((d), (int)(c), nn); \
1046 else if (nn > 0) { /* proc call overhead is worth only for large strings*/\
1047 register char *dd; register const int cc=(int)(c); \
1048 for (dd=(d); nn>0; nn--) *dd++ = cc; } }
1052 #if defined(NEED_ASPRINTF)
1053 int asprintf (char **ptr, const char *fmt, /*args*/ ...);
1055 #if defined(NEED_VASPRINTF)
1056 int vasprintf (char **ptr, const char *fmt, va_list ap);
1058 #if defined(NEED_ASNPRINTF)
1059 int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
1061 #if defined(NEED_VASNPRINTF)
1062 int vasnprintf (char **ptr, size_t str_m, const char *fmt, va_list ap);
1065 #if defined(HAVE_SNPRINTF)
1066 /* declare our portable snprintf routine under name portable_snprintf */
1067 /* declare our portable vsnprintf routine under name portable_vsnprintf */
1069 /* declare our portable routines under names snprintf and vsnprintf */
1070 #define portable_snprintf snprintf
1071 #if !defined(NEED_SNPRINTF_ONLY)
1072 #define portable_vsnprintf vsnprintf
1076 #if !defined(HAVE_SNPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
1077 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...);
1078 #if !defined(NEED_SNPRINTF_ONLY)
1079 int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap);
1085 static char credits[] = "\n\
1086 @(#)snprintf.c, v2.2: Mark Martinec, <mark.martinec@ijs.si>\n\
1087 @(#)snprintf.c, v2.2: Copyright 1999, Mark Martinec. Frontier Artistic License applies.\n\
1088 @(#)snprintf.c, v2.2: http://www.ijs.si/software/snprintf/\n";
1090 #if defined(NEED_ASPRINTF)
1091 int asprintf(char **ptr, const char *fmt, /*args*/ ...) {
1097 va_start(ap, fmt); /* measure the required size */
1098 str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap);
1100 assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */
1101 *ptr = (char *) malloc(str_m = (size_t)str_l + 1);
1102 if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
1106 str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
1108 assert(str_l2 == str_l);
1114 #if defined(NEED_VASPRINTF)
1115 int vasprintf(char **ptr, const char *fmt, va_list ap) {
1121 va_copy(ap2, ap); /* don't consume the original ap, we'll need it again */
1122 str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap2);/*get required size*/
1125 assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */
1126 *ptr = (char *) malloc(str_m = (size_t)str_l + 1);
1127 if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
1129 int str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
1130 assert(str_l2 == str_l);
1136 #if defined(NEED_ASNPRINTF)
1137 int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...) {
1142 va_start(ap, fmt); /* measure the required size */
1143 str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap);
1145 assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */
1146 if ((size_t)str_l + 1 < str_m) str_m = (size_t)str_l + 1; /* truncate */
1147 /* if str_m is 0, no buffer is allocated, just set *ptr to NULL */
1148 if (str_m == 0) { /* not interested in resulting string, just return size */
1150 *ptr = (char *) malloc(str_m);
1151 if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
1155 str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
1157 assert(str_l2 == str_l);
1164 #if defined(NEED_VASNPRINTF)
1165 int vasnprintf (char **ptr, size_t str_m, const char *fmt, va_list ap) {
1170 va_copy(ap2, ap); /* don't consume the original ap, we'll need it again */
1171 str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap2);/*get required size*/
1174 assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */
1175 if ((size_t)str_l + 1 < str_m) str_m = (size_t)str_l + 1; /* truncate */
1176 /* if str_m is 0, no buffer is allocated, just set *ptr to NULL */
1177 if (str_m == 0) { /* not interested in resulting string, just return size */
1179 *ptr = (char *) malloc(str_m);
1180 if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
1182 int str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
1183 assert(str_l2 == str_l);
1191 * If the system does have snprintf and the portable routine is not
1192 * specifically required, this module produces no code for snprintf/vsnprintf.
1194 #if !defined(HAVE_SNPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
1196 #if !defined(NEED_SNPRINTF_ONLY)
1197 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...) {
1202 str_l = portable_vsnprintf(str, str_m, fmt, ap);
1208 #if defined(NEED_SNPRINTF_ONLY)
1209 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...) {
1211 int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) {
1214 #if defined(NEED_SNPRINTF_ONLY)
1218 const char *p = fmt;
1220 /* In contrast with POSIX, the ISO C99 now says
1221 * that str can be NULL and str_m can be 0.
1222 * This is more useful than the old: if (str_m < 1) return -1; */
1224 #if defined(NEED_SNPRINTF_ONLY)
1230 /* if (str_l < str_m) str[str_l++] = *p++; -- this would be sufficient */
1231 /* but the following code achieves better performance for cases
1232 * where format string is long and contains few conversions */
1233 const char *q = strchr(p+1,'%');
1234 size_t n = !q ? strlen(p) : (q-p);
1235 if (str_l < str_m) {
1236 size_t avail = str_m-str_l;
1237 fast_memcpy(str+str_l, p, (n>avail?avail:n));
1241 const char *starting_p;
1242 size_t min_field_width = 0, precision = 0;
1243 int zero_padding = 0, precision_specified = 0, justify_left = 0;
1244 int alternate_form = 0, force_sign = 0;
1245 int space_for_positive = 1; /* If both the ' ' and '+' flags appear,
1246 the ' ' flag should be ignored. */
1247 char length_modifier = '\0'; /* allowed values: \0, h, l, L */
1248 char tmp[32];/* temporary buffer for simple numeric->string conversion */
1250 const char *str_arg; /* string address in case of string argument */
1251 size_t str_arg_l; /* natural field width of arg without padding
1253 unsigned char uchar_arg;
1254 /* unsigned char argument value - only defined for c conversion.
1255 N.B. standard explicitly states the char argument for
1256 the c conversion is unsigned */
1258 size_t number_of_zeros_to_pad = 0;
1259 /* number of zeros to be inserted for numeric conversions
1260 as required by the precision or minimal field width */
1262 size_t zero_padding_insertion_ind = 0;
1263 /* index into tmp where zero padding is to be inserted */
1265 char fmt_spec = '\0';
1266 /* current conversion specifier character */
1268 str_arg = credits;/* just to make compiler happy (defined but not used)*/
1270 starting_p = p; p++; /* skip '%' */
1272 while (*p == '0' || *p == '-' || *p == '+' ||
1273 *p == ' ' || *p == '#' || *p == '\'') {
1275 case '0': zero_padding = 1; break;
1276 case '-': justify_left = 1; break;
1277 case '+': force_sign = 1; space_for_positive = 0; break;
1278 case ' ': force_sign = 1;
1279 /* If both the ' ' and '+' flags appear, the ' ' flag should be ignored */
1280 #ifdef PERL_COMPATIBLE
1281 /* ... but in Perl the last of ' ' and '+' applies */
1282 space_for_positive = 1;
1285 case '#': alternate_form = 1; break;
1290 /* If the '0' and '-' flags both appear, the '0' flag should be ignored. */
1292 /* parse field width */
1295 p++; j = va_arg(ap, int);
1296 if (j >= 0) min_field_width = j;
1297 else { min_field_width = -j; justify_left = 1; }
1298 } else if (isdigit((int)(*p))) {
1299 /* size_t could be wider than unsigned int;
1300 make sure we treat argument like common implementations do */
1301 unsigned int uj = *p++ - '0';
1302 while (isdigit((int)(*p))) uj = 10*uj + (unsigned int)(*p++ - '0');
1303 min_field_width = uj;
1305 /* parse precision */
1307 p++; precision_specified = 1;
1309 int j = va_arg(ap, int);
1311 if (j >= 0) precision = j;
1313 precision_specified = 0; precision = 0;
1315 * Solaris 2.6 man page claims that in this case the precision
1316 * should be set to 0. Digital Unix 4.0, HPUX 10 and BSD man page
1317 * claim that this case should be treated as unspecified precision,
1318 * which is what we do here.
1321 } else if (isdigit((int)(*p))) {
1322 /* size_t could be wider than unsigned int;
1323 make sure we treat argument like common implementations do */
1324 unsigned int uj = *p++ - '0';
1325 while (isdigit((int)(*p))) uj = 10*uj + (unsigned int)(*p++ - '0');
1329 /* parse 'h', 'l' and 'll' length modifiers */
1330 if (*p == 'h' || *p == 'l') {
1331 length_modifier = *p; p++;
1332 if (length_modifier == 'l' && *p == 'l') { /* double l = long long */
1333 #ifdef SNPRINTF_LONGLONG_SUPPORT
1334 length_modifier = '2'; /* double l encoded as '2' */
1336 length_modifier = 'l'; /* treat it as a single 'l' */
1342 /* common synonyms: */
1344 case 'i': fmt_spec = 'd'; break;
1345 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
1346 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
1347 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
1350 /* get parameter value, do initial processing */
1352 case '%': /* % behaves similar to 's' regarding flags and field widths */
1353 case 'c': /* c behaves similar to 's' regarding flags and field widths */
1355 length_modifier = '\0'; /* wint_t and wchar_t not supported */
1356 /* the result of zero padding flag with non-numeric conversion specifier*/
1357 /* is undefined. Solaris and HPUX 10 does zero padding in this case, */
1358 /* Digital Unix and Linux does not. */
1359 #if !defined(SOLARIS_COMPATIBLE) && !defined(HPUX_COMPATIBLE)
1360 zero_padding = 0; /* turn zero padding off for string conversions */
1367 int j = va_arg(ap, int);
1368 uchar_arg = (unsigned char) j; /* standard demands unsigned char */
1369 str_arg = (const char *) &uchar_arg;
1373 str_arg = va_arg(ap, const char *);
1374 if (!str_arg) str_arg_l = 0;
1375 /* make sure not to address string beyond the specified precision !!! */
1376 else if (!precision_specified) str_arg_l = strlen(str_arg);
1377 /* truncate string if necessary as requested by precision */
1378 else if (precision == 0) str_arg_l = 0;
1380 /* memchr on HP does not like n > 2^31 !!! */
1381 const char *q = memchr(str_arg, '\0',
1382 precision <= 0x7fffffff ? precision : 0x7fffffff);
1383 str_arg_l = !q ? precision : (q-str_arg);
1389 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p': {
1390 /* NOTE: the u, o, x, X and p conversion specifiers imply
1391 the value is unsigned; d implies a signed value */
1394 /* 0 if numeric argument is zero (or if pointer is NULL for 'p'),
1395 +1 if greater than zero (or nonzero for unsigned arguments),
1396 -1 if negative (unsigned argument is never negative) */
1398 int int_arg = 0; unsigned int uint_arg = 0;
1399 /* only defined for length modifier h, or for no length modifiers */
1401 long int long_arg = 0; unsigned long int ulong_arg = 0;
1402 /* only defined for length modifier l */
1404 void *ptr_arg = NULL;
1405 /* pointer argument value -only defined for p conversion */
1407 #ifdef SNPRINTF_LONGLONG_SUPPORT
1408 long long int long_long_arg = 0;
1409 unsigned long long int ulong_long_arg = 0;
1410 /* only defined for length modifier ll */
1412 if (fmt_spec == 'p') {
1413 /* HPUX 10: An l, h, ll or L before any other conversion character
1414 * (other than d, i, u, o, x, or X) is ignored.
1416 * not specified, but seems to behave as HPUX does.
1417 * Solaris: If an h, l, or L appears before any other conversion
1418 * specifier (other than d, i, u, o, x, or X), the behavior
1419 * is undefined. (Actually %hp converts only 16-bits of address
1420 * and %llp treats address as 64-bit data which is incompatible
1421 * with (void *) argument on a 32-bit system).
1423 #ifdef SOLARIS_COMPATIBLE
1424 # ifdef SOLARIS_BUG_COMPATIBLE
1425 /* keep length modifiers even if it represents 'll' */
1427 if (length_modifier == '2') length_modifier = '\0';
1430 length_modifier = '\0';
1432 ptr_arg = va_arg(ap, void *);
1433 if (ptr_arg != NULL) arg_sign = 1;
1434 } else if (fmt_spec == 'd') { /* signed */
1435 switch (length_modifier) {
1438 /* It is non-portable to specify a second argument of char or short
1439 * to va_arg, because arguments seen by the called function
1440 * are not char or short. C converts char and short arguments
1441 * to int before passing them to a function.
1443 int_arg = va_arg(ap, int);
1444 if (int_arg > 0) arg_sign = 1;
1445 else if (int_arg < 0) arg_sign = -1;
1448 long_arg = va_arg(ap, long int);
1449 if (long_arg > 0) arg_sign = 1;
1450 else if (long_arg < 0) arg_sign = -1;
1452 #ifdef SNPRINTF_LONGLONG_SUPPORT
1454 long_long_arg = va_arg(ap, long long int);
1455 if (long_long_arg > 0) arg_sign = 1;
1456 else if (long_long_arg < 0) arg_sign = -1;
1460 } else { /* unsigned */
1461 switch (length_modifier) {
1464 uint_arg = va_arg(ap, unsigned int);
1465 if (uint_arg) arg_sign = 1;
1468 ulong_arg = va_arg(ap, unsigned long int);
1469 if (ulong_arg) arg_sign = 1;
1471 #ifdef SNPRINTF_LONGLONG_SUPPORT
1473 ulong_long_arg = va_arg(ap, unsigned long long int);
1474 if (ulong_long_arg) arg_sign = 1;
1479 str_arg = tmp; str_arg_l = 0;
1481 * For d, i, u, o, x, and X conversions, if precision is specified,
1482 * the '0' flag should be ignored. This is so with Solaris 2.6,
1483 * Digital UNIX 4.0, HPUX 10, Linux, FreeBSD, NetBSD; but not with Perl.
1485 #ifndef PERL_COMPATIBLE
1486 if (precision_specified) zero_padding = 0;
1488 if (fmt_spec == 'd') {
1489 if (force_sign && arg_sign >= 0)
1490 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
1491 /* leave negative numbers for sprintf to handle,
1492 to avoid handling tricky cases like (short int)(-32768) */
1493 #ifdef LINUX_COMPATIBLE
1494 } else if (fmt_spec == 'p' && force_sign && arg_sign > 0) {
1495 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
1497 } else if (alternate_form) {
1498 if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X') )
1499 { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = fmt_spec; }
1500 /* alternate form should have no effect for p conversion, but ... */
1501 #ifdef HPUX_COMPATIBLE
1502 else if (fmt_spec == 'p'
1503 /* HPUX 10: for an alternate form of p conversion,
1504 * a nonzero result is prefixed by 0x. */
1505 #ifndef HPUX_BUG_COMPATIBLE
1506 /* Actually it uses 0x prefix even for a zero value. */
1509 ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; }
1512 zero_padding_insertion_ind = str_arg_l;
1513 if (!precision_specified) precision = 1; /* default precision is 1 */
1514 if (precision == 0 && arg_sign == 0
1515 #if defined(HPUX_BUG_COMPATIBLE) || defined(LINUX_COMPATIBLE)
1517 /* HPUX 10 man page claims: With conversion character p the result of
1518 * converting a zero value with a precision of zero is a null string.
1519 * Actually HP returns all zeroes, and Linux returns "(nil)". */
1522 /* converted to null string */
1523 /* When zero value is formatted with an explicit precision 0,
1524 the resulting formatted string is empty (d, i, u, o, x, X, p). */
1526 char f[5]; int f_l = 0;
1527 f[f_l++] = '%'; /* construct a simple format string for sprintf */
1528 if (!length_modifier) { }
1529 else if (length_modifier=='2') { f[f_l++] = 'l'; f[f_l++] = 'l'; }
1530 else f[f_l++] = length_modifier;
1531 f[f_l++] = fmt_spec; f[f_l++] = '\0';
1532 if (fmt_spec == 'p') str_arg_l += sprintf(tmp+str_arg_l, f, ptr_arg);
1533 else if (fmt_spec == 'd') { /* signed */
1534 switch (length_modifier) {
1536 case 'h': str_arg_l+=sprintf(tmp+str_arg_l, f, int_arg); break;
1537 case 'l': str_arg_l+=sprintf(tmp+str_arg_l, f, long_arg); break;
1538 #ifdef SNPRINTF_LONGLONG_SUPPORT
1539 case '2': str_arg_l+=sprintf(tmp+str_arg_l,f,long_long_arg); break;
1542 } else { /* unsigned */
1543 switch (length_modifier) {
1545 case 'h': str_arg_l+=sprintf(tmp+str_arg_l, f, uint_arg); break;
1546 case 'l': str_arg_l+=sprintf(tmp+str_arg_l, f, ulong_arg); break;
1547 #ifdef SNPRINTF_LONGLONG_SUPPORT
1548 case '2': str_arg_l+=sprintf(tmp+str_arg_l,f,ulong_long_arg);break;
1552 /* include the optional minus sign and possible "0x"
1553 in the region before the zero padding insertion point */
1554 if (zero_padding_insertion_ind < str_arg_l &&
1555 tmp[zero_padding_insertion_ind] == '-') {
1556 zero_padding_insertion_ind++;
1558 if (zero_padding_insertion_ind+1 < str_arg_l &&
1559 tmp[zero_padding_insertion_ind] == '0' &&
1560 (tmp[zero_padding_insertion_ind+1] == 'x' ||
1561 tmp[zero_padding_insertion_ind+1] == 'X') ) {
1562 zero_padding_insertion_ind += 2;
1565 { size_t num_of_digits = str_arg_l - zero_padding_insertion_ind;
1566 if (alternate_form && fmt_spec == 'o'
1567 #ifdef HPUX_COMPATIBLE /* ("%#.o",0) -> "" */
1570 #ifdef DIGITAL_UNIX_BUG_COMPATIBLE /* ("%#o",0) -> "00" */
1572 /* unless zero is already the first character */
1573 && !(zero_padding_insertion_ind < str_arg_l
1574 && tmp[zero_padding_insertion_ind] == '0')
1576 ) { /* assure leading zero for alternate-form octal numbers */
1577 if (!precision_specified || precision < num_of_digits+1) {
1578 /* precision is increased to force the first character to be zero,
1579 except if a zero value is formatted with an explicit precision
1581 precision = num_of_digits+1; precision_specified = 1;
1584 /* zero padding to specified precision? */
1585 if (num_of_digits < precision)
1586 number_of_zeros_to_pad = precision - num_of_digits;
1588 /* zero padding to specified minimal field width? */
1589 if (!justify_left && zero_padding) {
1590 int n = min_field_width - (str_arg_l+number_of_zeros_to_pad);
1591 if (n > 0) number_of_zeros_to_pad += n;
1595 default: /* unrecognized conversion specifier, keep format string as-is*/
1596 zero_padding = 0; /* turn zero padding off for non-numeric convers. */
1597 #ifndef DIGITAL_UNIX_COMPATIBLE
1598 justify_left = 1; min_field_width = 0; /* reset flags */
1600 #if defined(PERL_COMPATIBLE) || defined(LINUX_COMPATIBLE)
1601 /* keep the entire format string unchanged */
1602 str_arg = starting_p; str_arg_l = p - starting_p;
1603 /* well, not exactly so for Linux, which does something between,
1604 * and I don't feel an urge to imitate it: "%+++++hy" -> "%+y" */
1606 /* discard the unrecognized conversion, just keep *
1607 * the unrecognized conversion character */
1608 str_arg = p; str_arg_l = 0;
1610 if (*p) str_arg_l++; /* include invalid conversion specifier unchanged
1611 if not at end-of-string */
1614 if (*p) p++; /* step over the just processed conversion specifier */
1615 /* insert padding to the left as requested by min_field_width;
1616 this does not include the zero padding in case of numerical conversions*/
1617 if (!justify_left) { /* left padding with blank or zero */
1618 int n = min_field_width - (str_arg_l+number_of_zeros_to_pad);
1620 if (str_l < str_m) {
1621 size_t avail = str_m-str_l;
1622 fast_memset(str+str_l, (zero_padding?'0':' '), (n>avail?avail:n));
1627 /* zero padding as requested by the precision or by the minimal field width
1628 * for numeric conversions required? */
1629 if (number_of_zeros_to_pad <= 0) {
1630 /* will not copy first part of numeric right now, *
1631 * force it to be copied later in its entirety */
1632 zero_padding_insertion_ind = 0;
1634 /* insert first part of numerics (sign or '0x') before zero padding */
1635 int n = zero_padding_insertion_ind;
1637 if (str_l < str_m) {
1638 size_t avail = str_m-str_l;
1639 fast_memcpy(str+str_l, str_arg, (n>avail?avail:n));
1643 /* insert zero padding as requested by the precision or min field width */
1644 n = number_of_zeros_to_pad;
1646 if (str_l < str_m) {
1647 size_t avail = str_m-str_l;
1648 fast_memset(str+str_l, '0', (n>avail?avail:n));
1653 /* insert formatted string
1654 * (or as-is conversion specifier for unknown conversions) */
1655 { int n = str_arg_l - zero_padding_insertion_ind;
1657 if (str_l < str_m) {
1658 size_t avail = str_m-str_l;
1659 fast_memcpy(str+str_l, str_arg+zero_padding_insertion_ind,
1665 /* insert right padding */
1666 if (justify_left) { /* right blank padding to the field width */
1667 int n = min_field_width - (str_arg_l+number_of_zeros_to_pad);
1669 if (str_l < str_m) {
1670 size_t avail = str_m-str_l;
1671 fast_memset(str+str_l, ' ', (n>avail?avail:n));
1678 #if defined(NEED_SNPRINTF_ONLY)
1681 if (str_m > 0) { /* make sure the string is null-terminated
1682 even at the expense of overwriting the last character
1683 (shouldn't happen, but just in case) */
1684 str[str_l <= str_m-1 ? str_l : str_m-1] = '\0';
1686 /* Return the number of characters formatted (excluding trailing null
1687 * character), that is, the number of characters that would have been
1688 * written to the buffer if it were large enough.
1690 * The value of str_l should be returned, but str_l is of unsigned type
1691 * size_t, and snprintf is int, possibly leading to an undetected
1692 * integer overflow, resulting in a negative return value, which is illegal.
1693 * Both XSH5 and ISO C99 (at least the draft) are silent on this issue.
1694 * Should errno be set to EOVERFLOW and EOF returned in this case???
1699 #endif /* ndef HAVE_SNPRINTF */