1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.83 2017/05/04 14:34:18 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
226 *********************************************************************/
227 void write_pid_file(void)
232 * If no --pidfile option was given,
233 * we can live without one.
235 if (pidfile == NULL) return;
237 if ((fp = fopen(pidfile, "w")) == NULL)
239 log_error(LOG_LEVEL_INFO, "can't open pidfile '%s': %E", pidfile);
243 fprintf(fp, "%u\n", (unsigned int) getpid());
249 #endif /* def unix */
252 /*********************************************************************
254 * Function : hash_string
256 * Description : Take a string and compute a (hopefuly) unique numeric
257 * integer value. This is useful to "switch" a string.
260 * 1 : s : string to be hashed.
262 * Returns : The string's hash
264 *********************************************************************/
265 unsigned int hash_string(const char* s)
271 h = 5 * h + (unsigned int)*s;
279 /*********************************************************************
281 * Function : strcmpic
283 * Description : Case insensitive string comparison
286 * 1 : s1 = string 1 to compare
287 * 2 : s2 = string 2 to compare
289 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
291 *********************************************************************/
292 int strcmpic(const char *s1, const char *s2)
299 if ((*s1 != *s2) && (privoxy_tolower(*s1) != privoxy_tolower(*s2)))
305 return(privoxy_tolower(*s1) - privoxy_tolower(*s2));
310 /*********************************************************************
312 * Function : strncmpic
314 * Description : Case insensitive string comparison (up to n characters)
317 * 1 : s1 = string 1 to compare
318 * 2 : s2 = string 2 to compare
319 * 3 : n = maximum characters to compare
321 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
323 *********************************************************************/
324 int strncmpic(const char *s1, const char *s2, size_t n)
326 if (n <= (size_t)0) return(0);
332 if ((*s1 != *s2) && (privoxy_tolower(*s1) != privoxy_tolower(*s2)))
337 if (--n <= (size_t)0) break;
341 return(privoxy_tolower(*s1) - privoxy_tolower(*s2));
346 /*********************************************************************
350 * Description : In-situ-eliminate all leading and trailing whitespace
354 * 1 : s : string to be chomped.
356 * Returns : chomped string
358 *********************************************************************/
359 char *chomp(char *string)
364 * strip trailing whitespace
366 p = string + strlen(string);
367 while (p > string && privoxy_isspace(*(p-1)))
374 * find end of leading whitespace
377 while (*q && privoxy_isspace(*q))
383 * if there was any, move the rest forwards
398 /*********************************************************************
400 * Function : string_append
402 * Description : Reallocate target_string and append text to it.
403 * This makes it easier to append to malloc'd strings.
404 * This is similar to the (removed) strsav(), but
405 * running out of memory isn't catastrophic.
409 * The following style provides sufficient error
410 * checking for this routine, with minimal clutter
411 * in the source code. It is recommended if you
412 * have many calls to this function:
414 * char * s = strdup(...); // don't check for error
415 * string_append(&s, ...); // don't check for error
416 * string_append(&s, ...); // don't check for error
417 * string_append(&s, ...); // don't check for error
418 * if (NULL == s) { ... handle error ... }
422 * char * s = strdup(...); // don't check for error
423 * string_append(&s, ...); // don't check for error
424 * string_append(&s, ...); // don't check for error
425 * if (string_append(&s, ...)) {... handle error ...}
428 * 1 : target_string = Pointer to old text that is to be
429 * extended. *target_string will be free()d by this
430 * routine. target_string must be non-NULL.
431 * If *target_string is NULL, this routine will
432 * do nothing and return with an error - this allows
433 * you to make many calls to this routine and only
434 * check for errors after the last one.
435 * 2 : text_to_append = Text to be appended to old.
438 * Returns : JB_ERR_OK on success, and sets *target_string
439 * to newly malloc'ed appended string. Caller
440 * must free(*target_string).
441 * JB_ERR_MEMORY on out-of-memory. (And free()s
442 * *target_string and sets it to NULL).
443 * JB_ERR_MEMORY if *target_string is NULL.
445 *********************************************************************/
446 jb_err string_append(char **target_string, const char *text_to_append)
452 assert(target_string);
453 assert(text_to_append);
455 if (*target_string == NULL)
457 return JB_ERR_MEMORY;
460 if (*text_to_append == '\0')
465 old_len = strlen(*target_string);
467 new_size = strlen(text_to_append) + old_len + 1;
469 if (NULL == (new_string = realloc(*target_string, new_size)))
471 free(*target_string);
473 *target_string = NULL;
474 return JB_ERR_MEMORY;
477 strlcpy(new_string + old_len, text_to_append, new_size - old_len);
479 *target_string = new_string;
484 /*********************************************************************
486 * Function : string_join
488 * Description : Join two strings together. Frees BOTH the original
489 * strings. If either or both input strings are NULL,
490 * fails as if it had run out of memory.
492 * For comparison, string_append requires that the
493 * second string is non-NULL, and doesn't free it.
495 * Rationale: Too often, we want to do
496 * string_append(s, html_encode(s2)). That assert()s
497 * if s2 is NULL or if html_encode() runs out of memory.
498 * It also leaks memory. Proper checking is cumbersome.
499 * The solution: string_join(s, html_encode(s2)) is safe,
500 * and will free the memory allocated by html_encode().
503 * 1 : target_string = Pointer to old text that is to be
504 * extended. *target_string will be free()d by this
505 * routine. target_string must be non-NULL.
506 * 2 : text_to_append = Text to be appended to old.
508 * Returns : JB_ERR_OK on success, and sets *target_string
509 * to newly malloc'ed appended string. Caller
510 * must free(*target_string).
511 * JB_ERR_MEMORY on out-of-memory, or if
512 * *target_string or text_to_append is NULL. (In
513 * this case, frees *target_string and text_to_append,
514 * sets *target_string to NULL).
516 *********************************************************************/
517 jb_err string_join(char **target_string, char *text_to_append)
521 assert(target_string);
523 if (text_to_append == NULL)
525 freez(*target_string);
526 return JB_ERR_MEMORY;
529 err = string_append(target_string, text_to_append);
531 freez(text_to_append);
537 /*********************************************************************
539 * Function : string_toupper
541 * Description : Produce a copy of string with all convertible
542 * characters converted to uppercase.
545 * 1 : string = string to convert
547 * Returns : Uppercase copy of string if possible,
548 * NULL on out-of-memory or if string was NULL.
550 *********************************************************************/
551 char *string_toupper(const char *string)
556 if (!string || ((result = (char *) zalloc(strlen(string) + 1)) == NULL))
566 *p++ = (char)toupper((int) *q++);
574 /*********************************************************************
576 * Function : string_move
578 * Description : memmove wrapper to move the last part of a string
579 * towards the beginning, overwriting the part in
580 * the middle. strlcpy() can't be used here as the
584 * 1 : dst = Destination to overwrite
585 * 2 : src = Source to move.
589 *********************************************************************/
590 void string_move(char *dst, char *src)
594 /* +1 to copy the terminating nul as well. */
595 memmove(dst, src, strlen(src)+1);
599 /*********************************************************************
603 * Description : Duplicate the first n characters of a string that may
604 * contain '\0' characters.
607 * 1 : string = string to be duplicated
608 * 2 : len = number of bytes to duplicate
610 * Returns : pointer to copy, or NULL if failiure
612 *********************************************************************/
613 char *bindup(const char *string, size_t len)
617 duplicate = (char *)malloc(len);
618 if (NULL != duplicate)
620 memcpy(duplicate, string, len);
628 /*********************************************************************
630 * Function : make_path
632 * Description : Takes a directory name and a file name, returns
633 * the complete path. Handles windows/unix differences.
634 * If the file name is already an absolute path, or if
635 * the directory name is NULL or empty, it returns
639 * 1 : dir: Name of directory or NULL for none.
640 * 2 : file: Name of file. Should not be NULL or empty.
642 * Returns : "dir/file" (Or on windows, "dir\file").
643 * It allocates the string on the heap. Caller frees.
644 * Returns NULL in error (i.e. NULL file or out of
647 *********************************************************************/
648 char * make_path(const char * dir, const char * file)
659 strncpy(path,dir+2,512);
663 strncpy(path,dir+1,512);
668 strncpy(path,dir,512);
676 if (AddPart(path,file,512))
684 #else /* ndef AMIGA */
686 if ((file == NULL) || (*file == '\0'))
688 return NULL; /* Error */
691 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
692 #if defined(_WIN32) || defined(__OS2__)
693 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
694 #else /* ifndef _WIN32 || __OS2__ */
695 || (*file == '/') /* Absolute path (U*ix) */
696 #endif /* ifndef _WIN32 || __OS2__ */
704 size_t path_size = strlen(dir) + strlen(file) + 2; /* +2 for trailing (back)slash and \0 */
707 if (*dir != '/' && basedir && *basedir)
710 * Relative path, so start with the base directory.
712 path_size += strlen(basedir) + 1; /* +1 for the slash */
713 path = malloc(path_size);
714 if (!path) log_error(LOG_LEVEL_FATAL, "malloc failed!");
715 strlcpy(path, basedir, path_size);
716 strlcat(path, "/", path_size);
717 strlcat(path, dir, path_size);
720 #endif /* defined unix */
722 path = malloc(path_size);
723 if (!path) log_error(LOG_LEVEL_FATAL, "malloc failed!");
724 strlcpy(path, dir, path_size);
727 assert(NULL != path);
728 #if defined(_WIN32) || defined(__OS2__)
729 if (path[strlen(path)-1] != '\\')
731 strlcat(path, "\\", path_size);
733 #else /* ifndef _WIN32 || __OS2__ */
734 if (path[strlen(path)-1] != '/')
736 strlcat(path, "/", path_size);
738 #endif /* ifndef _WIN32 || __OS2__ */
739 strlcat(path, file, path_size);
743 #endif /* ndef AMIGA */
747 /*********************************************************************
749 * Function : pick_from_range
751 * Description : Pick a positive number out of a given range.
752 * Should only be used if randomness would be nice,
753 * but isn't really necessary.
756 * 1 : range: Highest possible number to pick.
758 * Returns : Picked number.
760 *********************************************************************/
761 long int pick_from_range(long int range)
765 static unsigned long seed = 0;
766 #endif /* def _WIN32 */
771 if (range <= 0) return 0;
773 #ifdef HAVE_ARC4RANDOM
774 number = arc4random() % range + 1;
775 #elif defined(HAVE_RANDOM)
776 number = random() % range + 1;
777 #elif defined(MUTEX_LOCKS_AVAILABLE)
778 privoxy_mutex_lock(&rand_mutex);
782 seed = (unsigned long)(GetCurrentThreadId()+GetTickCount());
785 seed = (unsigned long)((rand() << 16) + rand());
786 #endif /* def _WIN32 */
787 number = (unsigned long)((rand() << 16) + (rand())) % (unsigned long)(range + 1);
788 privoxy_mutex_unlock(&rand_mutex);
791 * XXX: Which platforms reach this and are there
792 * better options than just using rand() and hoping
795 log_error(LOG_LEVEL_INFO, "No thread-safe PRNG available? Header time randomization "
796 "might cause crashes, predictable results or even combine these fine options.");
797 number = rand() % (long int)(range + 1);
799 #endif /* (def HAVE_ARC4RANDOM) */
805 #ifdef USE_PRIVOXY_STRLCPY
806 /*********************************************************************
808 * Function : privoxy_strlcpy
810 * Description : strlcpy(3) look-alike for those without decent libc.
813 * 1 : destination: buffer to copy into.
814 * 2 : source: String to copy.
815 * 3 : size: Size of destination buffer.
817 * Returns : The length of the string that privoxy_strlcpy() tried to create.
819 *********************************************************************/
820 size_t privoxy_strlcpy(char *destination, const char *source, const size_t size)
824 snprintf(destination, size, "%s", source);
826 * Platforms that lack strlcpy() also tend to have
827 * a broken snprintf implementation that doesn't
828 * guarantee nul termination.
830 * XXX: the configure script should detect and reject those.
832 destination[size-1] = '\0';
834 return strlen(source);
836 #endif /* def USE_PRIVOXY_STRLCPY */
840 /*********************************************************************
842 * Function : privoxy_strlcat
844 * Description : strlcat(3) look-alike for those without decent libc.
847 * 1 : destination: C string.
848 * 2 : source: String to copy.
849 * 3 : size: Size of destination buffer.
851 * Returns : The length of the string that privoxy_strlcat() tried to create.
853 *********************************************************************/
854 size_t privoxy_strlcat(char *destination, const char *source, const size_t size)
856 const size_t old_length = strlen(destination);
857 return old_length + strlcpy(destination + old_length, source, size - old_length);
859 #endif /* ndef HAVE_STRLCAT */
862 #if !defined(HAVE_TIMEGM) && defined(HAVE_TZSET) && defined(HAVE_PUTENV)
863 /*********************************************************************
867 * Description : libc replacement function for the inverse of gmtime().
868 * Copyright (C) 2004 Free Software Foundation, Inc.
870 * Code originally copied from GnuPG, modifications done
871 * for Privoxy: style changed, #ifdefs for _WIN32 added
872 * to have it work on mingw32.
874 * XXX: It's very unlikely to happen, but if the malloc()
875 * call fails the time zone will be permanently set to UTC.
878 * 1 : tm: Broken-down time struct.
880 * Returns : tm converted into time_t seconds.
882 *********************************************************************/
883 time_t timegm(struct tm *tm)
896 old_zone = malloc(3 + strlen(zone) + 1);
899 strcpy(old_zone, "TZ=");
900 strcat(old_zone, zone);
903 /* http://man7.org/linux/man-pages/man3/putenv.3.html
904 * int putenv(char *string);
905 * The string pointed to by string becomes part of the environment, so altering the
906 * string changes the environment.
907 * In other words, the memory pointed to by *string is used until
908 * a) another call to putenv() with the same e-var name
909 * b) the program exits
911 * Windows e-vars don't work that way, so let's not leak memory.
914 #endif /* def _WIN32 */
921 #elif defined(_WIN32)
931 #endif /* !defined(HAVE_TIMEGM) && defined(HAVE_TZSET) && defined(HAVE_PUTENV) */
934 #ifndef HAVE_SNPRINTF
936 * What follows is a portable snprintf routine, written by Mark Martinec.
937 * See: http://www.ijs.si/software/snprintf/
940 - a portable implementation of snprintf,
941 including vsnprintf.c, asnprintf, vasnprintf, asprintf, vasprintf
943 snprintf is a routine to convert numeric and string arguments to
944 formatted strings. It is similar to sprintf(3) provided in a system's
945 C library, yet it requires an additional argument - the buffer size -
946 and it guarantees never to store anything beyond the given buffer,
947 regardless of the format or arguments to be formatted. Some newer
948 operating systems do provide snprintf in their C library, but many do
949 not or do provide an inadequate (slow or idiosyncratic) version, which
950 calls for a portable implementation of this routine.
954 Mark Martinec <mark.martinec@ijs.si>, April 1999, June 2000
955 Copyright © 1999, Mark Martinec
959 #define PORTABLE_SNPRINTF_VERSION_MAJOR 2
960 #define PORTABLE_SNPRINTF_VERSION_MINOR 2
962 #if defined(NEED_ASPRINTF) || defined(NEED_ASNPRINTF) || defined(NEED_VASPRINTF) || defined(NEED_VASNPRINTF)
963 # if defined(NEED_SNPRINTF_ONLY)
964 # undef NEED_SNPRINTF_ONLY
966 # if !defined(PREFER_PORTABLE_SNPRINTF)
967 # define PREFER_PORTABLE_SNPRINTF
971 #if defined(SOLARIS_BUG_COMPATIBLE) && !defined(SOLARIS_COMPATIBLE)
972 #define SOLARIS_COMPATIBLE
975 #if defined(HPUX_BUG_COMPATIBLE) && !defined(HPUX_COMPATIBLE)
976 #define HPUX_COMPATIBLE
979 #if defined(DIGITAL_UNIX_BUG_COMPATIBLE) && !defined(DIGITAL_UNIX_COMPATIBLE)
980 #define DIGITAL_UNIX_COMPATIBLE
983 #if defined(PERL_BUG_COMPATIBLE) && !defined(PERL_COMPATIBLE)
984 #define PERL_COMPATIBLE
987 #if defined(LINUX_BUG_COMPATIBLE) && !defined(LINUX_COMPATIBLE)
988 #define LINUX_COMPATIBLE
991 #include <sys/types.h>
1002 #define isdigit(c) ((c) >= '0' && (c) <= '9')
1004 /* For copying strings longer or equal to 'breakeven_point'
1005 * it is more efficient to call memcpy() than to do it inline.
1006 * The value depends mostly on the processor architecture,
1007 * but also on the compiler and its optimization capabilities.
1008 * The value is not critical, some small value greater than zero
1009 * will be just fine if you don't care to squeeze every drop
1010 * of performance out of the code.
1012 * Small values favor memcpy, large values favor inline code.
1014 #if defined(__alpha__) || defined(__alpha)
1015 # define breakeven_point 2 /* AXP (DEC Alpha) - gcc or cc or egcs */
1017 #if defined(__i386__) || defined(__i386)
1018 # define breakeven_point 12 /* Intel Pentium/Linux - gcc 2.96 */
1021 # define breakeven_point 10 /* HP-PA - gcc */
1023 #if defined(__sparc__) || defined(__sparc)
1024 # define breakeven_point 33 /* Sun Sparc 5 - gcc 2.8.1 */
1027 /* some other values of possible interest: */
1028 /* #define breakeven_point 8 */ /* VAX 4000 - vaxc */
1029 /* #define breakeven_point 19 */ /* VAX 4000 - gcc 2.7.0 */
1031 #ifndef breakeven_point
1032 # define breakeven_point 6 /* some reasonable one-size-fits-all value */
1035 #define fast_memcpy(d,s,n) \
1036 { register size_t nn = (size_t)(n); \
1037 if (nn >= breakeven_point) memcpy((d), (s), nn); \
1038 else if (nn > 0) { /* proc call overhead is worth only for large strings*/\
1039 register char *dd; register const char *ss; \
1040 for (ss=(s), dd=(d); nn>0; nn--) *dd++ = *ss++; } }
1042 #define fast_memset(d,c,n) \
1043 { register size_t nn = (size_t)(n); \
1044 if (nn >= breakeven_point) memset((d), (int)(c), nn); \
1045 else if (nn > 0) { /* proc call overhead is worth only for large strings*/\
1046 register char *dd; register const int cc=(int)(c); \
1047 for (dd=(d); nn>0; nn--) *dd++ = cc; } }
1051 #if defined(NEED_ASPRINTF)
1052 int asprintf (char **ptr, const char *fmt, /*args*/ ...);
1054 #if defined(NEED_VASPRINTF)
1055 int vasprintf (char **ptr, const char *fmt, va_list ap);
1057 #if defined(NEED_ASNPRINTF)
1058 int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
1060 #if defined(NEED_VASNPRINTF)
1061 int vasnprintf (char **ptr, size_t str_m, const char *fmt, va_list ap);
1064 #if defined(HAVE_SNPRINTF)
1065 /* declare our portable snprintf routine under name portable_snprintf */
1066 /* declare our portable vsnprintf routine under name portable_vsnprintf */
1068 /* declare our portable routines under names snprintf and vsnprintf */
1069 #define portable_snprintf snprintf
1070 #if !defined(NEED_SNPRINTF_ONLY)
1071 #define portable_vsnprintf vsnprintf
1075 #if !defined(HAVE_SNPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
1076 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...);
1077 #if !defined(NEED_SNPRINTF_ONLY)
1078 int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap);
1084 static char credits[] = "\n\
1085 @(#)snprintf.c, v2.2: Mark Martinec, <mark.martinec@ijs.si>\n\
1086 @(#)snprintf.c, v2.2: Copyright 1999, Mark Martinec. Frontier Artistic License applies.\n\
1087 @(#)snprintf.c, v2.2: http://www.ijs.si/software/snprintf/\n";
1089 #if defined(NEED_ASPRINTF)
1090 int asprintf(char **ptr, const char *fmt, /*args*/ ...) {
1096 va_start(ap, fmt); /* measure the required size */
1097 str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap);
1099 assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */
1100 *ptr = (char *) malloc(str_m = (size_t)str_l + 1);
1101 if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
1105 str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
1107 assert(str_l2 == str_l);
1113 #if defined(NEED_VASPRINTF)
1114 int vasprintf(char **ptr, const char *fmt, va_list ap) {
1120 va_copy(ap2, ap); /* don't consume the original ap, we'll need it again */
1121 str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap2);/*get required size*/
1124 assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */
1125 *ptr = (char *) malloc(str_m = (size_t)str_l + 1);
1126 if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
1128 int str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
1129 assert(str_l2 == str_l);
1135 #if defined(NEED_ASNPRINTF)
1136 int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...) {
1141 va_start(ap, fmt); /* measure the required size */
1142 str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap);
1144 assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */
1145 if ((size_t)str_l + 1 < str_m) str_m = (size_t)str_l + 1; /* truncate */
1146 /* if str_m is 0, no buffer is allocated, just set *ptr to NULL */
1147 if (str_m == 0) { /* not interested in resulting string, just return size */
1149 *ptr = (char *) malloc(str_m);
1150 if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
1154 str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
1156 assert(str_l2 == str_l);
1163 #if defined(NEED_VASNPRINTF)
1164 int vasnprintf (char **ptr, size_t str_m, const char *fmt, va_list ap) {
1169 va_copy(ap2, ap); /* don't consume the original ap, we'll need it again */
1170 str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap2);/*get required size*/
1173 assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */
1174 if ((size_t)str_l + 1 < str_m) str_m = (size_t)str_l + 1; /* truncate */
1175 /* if str_m is 0, no buffer is allocated, just set *ptr to NULL */
1176 if (str_m == 0) { /* not interested in resulting string, just return size */
1178 *ptr = (char *) malloc(str_m);
1179 if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
1181 int str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
1182 assert(str_l2 == str_l);
1190 * If the system does have snprintf and the portable routine is not
1191 * specifically required, this module produces no code for snprintf/vsnprintf.
1193 #if !defined(HAVE_SNPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
1195 #if !defined(NEED_SNPRINTF_ONLY)
1196 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...) {
1201 str_l = portable_vsnprintf(str, str_m, fmt, ap);
1207 #if defined(NEED_SNPRINTF_ONLY)
1208 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...) {
1210 int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) {
1213 #if defined(NEED_SNPRINTF_ONLY)
1217 const char *p = fmt;
1219 /* In contrast with POSIX, the ISO C99 now says
1220 * that str can be NULL and str_m can be 0.
1221 * This is more useful than the old: if (str_m < 1) return -1; */
1223 #if defined(NEED_SNPRINTF_ONLY)
1229 /* if (str_l < str_m) str[str_l++] = *p++; -- this would be sufficient */
1230 /* but the following code achieves better performance for cases
1231 * where format string is long and contains few conversions */
1232 const char *q = strchr(p+1,'%');
1233 size_t n = !q ? strlen(p) : (q-p);
1234 if (str_l < str_m) {
1235 size_t avail = str_m-str_l;
1236 fast_memcpy(str+str_l, p, (n>avail?avail:n));
1240 const char *starting_p;
1241 size_t min_field_width = 0, precision = 0;
1242 int zero_padding = 0, precision_specified = 0, justify_left = 0;
1243 int alternate_form = 0, force_sign = 0;
1244 int space_for_positive = 1; /* If both the ' ' and '+' flags appear,
1245 the ' ' flag should be ignored. */
1246 char length_modifier = '\0'; /* allowed values: \0, h, l, L */
1247 char tmp[32];/* temporary buffer for simple numeric->string conversion */
1249 const char *str_arg; /* string address in case of string argument */
1250 size_t str_arg_l; /* natural field width of arg without padding
1252 unsigned char uchar_arg;
1253 /* unsigned char argument value - only defined for c conversion.
1254 N.B. standard explicitly states the char argument for
1255 the c conversion is unsigned */
1257 size_t number_of_zeros_to_pad = 0;
1258 /* number of zeros to be inserted for numeric conversions
1259 as required by the precision or minimal field width */
1261 size_t zero_padding_insertion_ind = 0;
1262 /* index into tmp where zero padding is to be inserted */
1264 char fmt_spec = '\0';
1265 /* current conversion specifier character */
1267 str_arg = credits;/* just to make compiler happy (defined but not used)*/
1269 starting_p = p; p++; /* skip '%' */
1271 while (*p == '0' || *p == '-' || *p == '+' ||
1272 *p == ' ' || *p == '#' || *p == '\'') {
1274 case '0': zero_padding = 1; break;
1275 case '-': justify_left = 1; break;
1276 case '+': force_sign = 1; space_for_positive = 0; break;
1277 case ' ': force_sign = 1;
1278 /* If both the ' ' and '+' flags appear, the ' ' flag should be ignored */
1279 #ifdef PERL_COMPATIBLE
1280 /* ... but in Perl the last of ' ' and '+' applies */
1281 space_for_positive = 1;
1284 case '#': alternate_form = 1; break;
1289 /* If the '0' and '-' flags both appear, the '0' flag should be ignored. */
1291 /* parse field width */
1294 p++; j = va_arg(ap, int);
1295 if (j >= 0) min_field_width = j;
1296 else { min_field_width = -j; justify_left = 1; }
1297 } else if (isdigit((int)(*p))) {
1298 /* size_t could be wider than unsigned int;
1299 make sure we treat argument like common implementations do */
1300 unsigned int uj = *p++ - '0';
1301 while (isdigit((int)(*p))) uj = 10*uj + (unsigned int)(*p++ - '0');
1302 min_field_width = uj;
1304 /* parse precision */
1306 p++; precision_specified = 1;
1308 int j = va_arg(ap, int);
1310 if (j >= 0) precision = j;
1312 precision_specified = 0; precision = 0;
1314 * Solaris 2.6 man page claims that in this case the precision
1315 * should be set to 0. Digital Unix 4.0, HPUX 10 and BSD man page
1316 * claim that this case should be treated as unspecified precision,
1317 * which is what we do here.
1320 } else if (isdigit((int)(*p))) {
1321 /* size_t could be wider than unsigned int;
1322 make sure we treat argument like common implementations do */
1323 unsigned int uj = *p++ - '0';
1324 while (isdigit((int)(*p))) uj = 10*uj + (unsigned int)(*p++ - '0');
1328 /* parse 'h', 'l' and 'll' length modifiers */
1329 if (*p == 'h' || *p == 'l') {
1330 length_modifier = *p; p++;
1331 if (length_modifier == 'l' && *p == 'l') { /* double l = long long */
1332 #ifdef SNPRINTF_LONGLONG_SUPPORT
1333 length_modifier = '2'; /* double l encoded as '2' */
1335 length_modifier = 'l'; /* treat it as a single 'l' */
1341 /* common synonyms: */
1343 case 'i': fmt_spec = 'd'; break;
1344 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
1345 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
1346 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
1349 /* get parameter value, do initial processing */
1351 case '%': /* % behaves similar to 's' regarding flags and field widths */
1352 case 'c': /* c behaves similar to 's' regarding flags and field widths */
1354 length_modifier = '\0'; /* wint_t and wchar_t not supported */
1355 /* the result of zero padding flag with non-numeric conversion specifier*/
1356 /* is undefined. Solaris and HPUX 10 does zero padding in this case, */
1357 /* Digital Unix and Linux does not. */
1358 #if !defined(SOLARIS_COMPATIBLE) && !defined(HPUX_COMPATIBLE)
1359 zero_padding = 0; /* turn zero padding off for string conversions */
1366 int j = va_arg(ap, int);
1367 uchar_arg = (unsigned char) j; /* standard demands unsigned char */
1368 str_arg = (const char *) &uchar_arg;
1372 str_arg = va_arg(ap, const char *);
1373 if (!str_arg) str_arg_l = 0;
1374 /* make sure not to address string beyond the specified precision !!! */
1375 else if (!precision_specified) str_arg_l = strlen(str_arg);
1376 /* truncate string if necessary as requested by precision */
1377 else if (precision == 0) str_arg_l = 0;
1379 /* memchr on HP does not like n > 2^31 !!! */
1380 const char *q = memchr(str_arg, '\0',
1381 precision <= 0x7fffffff ? precision : 0x7fffffff);
1382 str_arg_l = !q ? precision : (q-str_arg);
1388 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p': {
1389 /* NOTE: the u, o, x, X and p conversion specifiers imply
1390 the value is unsigned; d implies a signed value */
1393 /* 0 if numeric argument is zero (or if pointer is NULL for 'p'),
1394 +1 if greater than zero (or nonzero for unsigned arguments),
1395 -1 if negative (unsigned argument is never negative) */
1397 int int_arg = 0; unsigned int uint_arg = 0;
1398 /* only defined for length modifier h, or for no length modifiers */
1400 long int long_arg = 0; unsigned long int ulong_arg = 0;
1401 /* only defined for length modifier l */
1403 void *ptr_arg = NULL;
1404 /* pointer argument value -only defined for p conversion */
1406 #ifdef SNPRINTF_LONGLONG_SUPPORT
1407 long long int long_long_arg = 0;
1408 unsigned long long int ulong_long_arg = 0;
1409 /* only defined for length modifier ll */
1411 if (fmt_spec == 'p') {
1412 /* HPUX 10: An l, h, ll or L before any other conversion character
1413 * (other than d, i, u, o, x, or X) is ignored.
1415 * not specified, but seems to behave as HPUX does.
1416 * Solaris: If an h, l, or L appears before any other conversion
1417 * specifier (other than d, i, u, o, x, or X), the behavior
1418 * is undefined. (Actually %hp converts only 16-bits of address
1419 * and %llp treats address as 64-bit data which is incompatible
1420 * with (void *) argument on a 32-bit system).
1422 #ifdef SOLARIS_COMPATIBLE
1423 # ifdef SOLARIS_BUG_COMPATIBLE
1424 /* keep length modifiers even if it represents 'll' */
1426 if (length_modifier == '2') length_modifier = '\0';
1429 length_modifier = '\0';
1431 ptr_arg = va_arg(ap, void *);
1432 if (ptr_arg != NULL) arg_sign = 1;
1433 } else if (fmt_spec == 'd') { /* signed */
1434 switch (length_modifier) {
1437 /* It is non-portable to specify a second argument of char or short
1438 * to va_arg, because arguments seen by the called function
1439 * are not char or short. C converts char and short arguments
1440 * to int before passing them to a function.
1442 int_arg = va_arg(ap, int);
1443 if (int_arg > 0) arg_sign = 1;
1444 else if (int_arg < 0) arg_sign = -1;
1447 long_arg = va_arg(ap, long int);
1448 if (long_arg > 0) arg_sign = 1;
1449 else if (long_arg < 0) arg_sign = -1;
1451 #ifdef SNPRINTF_LONGLONG_SUPPORT
1453 long_long_arg = va_arg(ap, long long int);
1454 if (long_long_arg > 0) arg_sign = 1;
1455 else if (long_long_arg < 0) arg_sign = -1;
1459 } else { /* unsigned */
1460 switch (length_modifier) {
1463 uint_arg = va_arg(ap, unsigned int);
1464 if (uint_arg) arg_sign = 1;
1467 ulong_arg = va_arg(ap, unsigned long int);
1468 if (ulong_arg) arg_sign = 1;
1470 #ifdef SNPRINTF_LONGLONG_SUPPORT
1472 ulong_long_arg = va_arg(ap, unsigned long long int);
1473 if (ulong_long_arg) arg_sign = 1;
1478 str_arg = tmp; str_arg_l = 0;
1480 * For d, i, u, o, x, and X conversions, if precision is specified,
1481 * the '0' flag should be ignored. This is so with Solaris 2.6,
1482 * Digital UNIX 4.0, HPUX 10, Linux, FreeBSD, NetBSD; but not with Perl.
1484 #ifndef PERL_COMPATIBLE
1485 if (precision_specified) zero_padding = 0;
1487 if (fmt_spec == 'd') {
1488 if (force_sign && arg_sign >= 0)
1489 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
1490 /* leave negative numbers for sprintf to handle,
1491 to avoid handling tricky cases like (short int)(-32768) */
1492 #ifdef LINUX_COMPATIBLE
1493 } else if (fmt_spec == 'p' && force_sign && arg_sign > 0) {
1494 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
1496 } else if (alternate_form) {
1497 if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X') )
1498 { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = fmt_spec; }
1499 /* alternate form should have no effect for p conversion, but ... */
1500 #ifdef HPUX_COMPATIBLE
1501 else if (fmt_spec == 'p'
1502 /* HPUX 10: for an alternate form of p conversion,
1503 * a nonzero result is prefixed by 0x. */
1504 #ifndef HPUX_BUG_COMPATIBLE
1505 /* Actually it uses 0x prefix even for a zero value. */
1508 ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; }
1511 zero_padding_insertion_ind = str_arg_l;
1512 if (!precision_specified) precision = 1; /* default precision is 1 */
1513 if (precision == 0 && arg_sign == 0
1514 #if defined(HPUX_BUG_COMPATIBLE) || defined(LINUX_COMPATIBLE)
1516 /* HPUX 10 man page claims: With conversion character p the result of
1517 * converting a zero value with a precision of zero is a null string.
1518 * Actually HP returns all zeroes, and Linux returns "(nil)". */
1521 /* converted to null string */
1522 /* When zero value is formatted with an explicit precision 0,
1523 the resulting formatted string is empty (d, i, u, o, x, X, p). */
1525 char f[5]; int f_l = 0;
1526 f[f_l++] = '%'; /* construct a simple format string for sprintf */
1527 if (!length_modifier) { }
1528 else if (length_modifier=='2') { f[f_l++] = 'l'; f[f_l++] = 'l'; }
1529 else f[f_l++] = length_modifier;
1530 f[f_l++] = fmt_spec; f[f_l++] = '\0';
1531 if (fmt_spec == 'p') str_arg_l += sprintf(tmp+str_arg_l, f, ptr_arg);
1532 else if (fmt_spec == 'd') { /* signed */
1533 switch (length_modifier) {
1535 case 'h': str_arg_l+=sprintf(tmp+str_arg_l, f, int_arg); break;
1536 case 'l': str_arg_l+=sprintf(tmp+str_arg_l, f, long_arg); break;
1537 #ifdef SNPRINTF_LONGLONG_SUPPORT
1538 case '2': str_arg_l+=sprintf(tmp+str_arg_l,f,long_long_arg); break;
1541 } else { /* unsigned */
1542 switch (length_modifier) {
1544 case 'h': str_arg_l+=sprintf(tmp+str_arg_l, f, uint_arg); break;
1545 case 'l': str_arg_l+=sprintf(tmp+str_arg_l, f, ulong_arg); break;
1546 #ifdef SNPRINTF_LONGLONG_SUPPORT
1547 case '2': str_arg_l+=sprintf(tmp+str_arg_l,f,ulong_long_arg);break;
1551 /* include the optional minus sign and possible "0x"
1552 in the region before the zero padding insertion point */
1553 if (zero_padding_insertion_ind < str_arg_l &&
1554 tmp[zero_padding_insertion_ind] == '-') {
1555 zero_padding_insertion_ind++;
1557 if (zero_padding_insertion_ind+1 < str_arg_l &&
1558 tmp[zero_padding_insertion_ind] == '0' &&
1559 (tmp[zero_padding_insertion_ind+1] == 'x' ||
1560 tmp[zero_padding_insertion_ind+1] == 'X') ) {
1561 zero_padding_insertion_ind += 2;
1564 { size_t num_of_digits = str_arg_l - zero_padding_insertion_ind;
1565 if (alternate_form && fmt_spec == 'o'
1566 #ifdef HPUX_COMPATIBLE /* ("%#.o",0) -> "" */
1569 #ifdef DIGITAL_UNIX_BUG_COMPATIBLE /* ("%#o",0) -> "00" */
1571 /* unless zero is already the first character */
1572 && !(zero_padding_insertion_ind < str_arg_l
1573 && tmp[zero_padding_insertion_ind] == '0')
1575 ) { /* assure leading zero for alternate-form octal numbers */
1576 if (!precision_specified || precision < num_of_digits+1) {
1577 /* precision is increased to force the first character to be zero,
1578 except if a zero value is formatted with an explicit precision
1580 precision = num_of_digits+1; precision_specified = 1;
1583 /* zero padding to specified precision? */
1584 if (num_of_digits < precision)
1585 number_of_zeros_to_pad = precision - num_of_digits;
1587 /* zero padding to specified minimal field width? */
1588 if (!justify_left && zero_padding) {
1589 int n = min_field_width - (str_arg_l+number_of_zeros_to_pad);
1590 if (n > 0) number_of_zeros_to_pad += n;
1594 default: /* unrecognized conversion specifier, keep format string as-is*/
1595 zero_padding = 0; /* turn zero padding off for non-numeric convers. */
1596 #ifndef DIGITAL_UNIX_COMPATIBLE
1597 justify_left = 1; min_field_width = 0; /* reset flags */
1599 #if defined(PERL_COMPATIBLE) || defined(LINUX_COMPATIBLE)
1600 /* keep the entire format string unchanged */
1601 str_arg = starting_p; str_arg_l = p - starting_p;
1602 /* well, not exactly so for Linux, which does something between,
1603 * and I don't feel an urge to imitate it: "%+++++hy" -> "%+y" */
1605 /* discard the unrecognized conversion, just keep *
1606 * the unrecognized conversion character */
1607 str_arg = p; str_arg_l = 0;
1609 if (*p) str_arg_l++; /* include invalid conversion specifier unchanged
1610 if not at end-of-string */
1613 if (*p) p++; /* step over the just processed conversion specifier */
1614 /* insert padding to the left as requested by min_field_width;
1615 this does not include the zero padding in case of numerical conversions*/
1616 if (!justify_left) { /* left padding with blank or zero */
1617 int n = min_field_width - (str_arg_l+number_of_zeros_to_pad);
1619 if (str_l < str_m) {
1620 size_t avail = str_m-str_l;
1621 fast_memset(str+str_l, (zero_padding?'0':' '), (n>avail?avail:n));
1626 /* zero padding as requested by the precision or by the minimal field width
1627 * for numeric conversions required? */
1628 if (number_of_zeros_to_pad <= 0) {
1629 /* will not copy first part of numeric right now, *
1630 * force it to be copied later in its entirety */
1631 zero_padding_insertion_ind = 0;
1633 /* insert first part of numerics (sign or '0x') before zero padding */
1634 int n = zero_padding_insertion_ind;
1636 if (str_l < str_m) {
1637 size_t avail = str_m-str_l;
1638 fast_memcpy(str+str_l, str_arg, (n>avail?avail:n));
1642 /* insert zero padding as requested by the precision or min field width */
1643 n = number_of_zeros_to_pad;
1645 if (str_l < str_m) {
1646 size_t avail = str_m-str_l;
1647 fast_memset(str+str_l, '0', (n>avail?avail:n));
1652 /* insert formatted string
1653 * (or as-is conversion specifier for unknown conversions) */
1654 { int n = str_arg_l - zero_padding_insertion_ind;
1656 if (str_l < str_m) {
1657 size_t avail = str_m-str_l;
1658 fast_memcpy(str+str_l, str_arg+zero_padding_insertion_ind,
1664 /* insert right padding */
1665 if (justify_left) { /* right blank padding to the field width */
1666 int n = min_field_width - (str_arg_l+number_of_zeros_to_pad);
1668 if (str_l < str_m) {
1669 size_t avail = str_m-str_l;
1670 fast_memset(str+str_l, ' ', (n>avail?avail:n));
1677 #if defined(NEED_SNPRINTF_ONLY)
1680 if (str_m > 0) { /* make sure the string is null-terminated
1681 even at the expense of overwriting the last character
1682 (shouldn't happen, but just in case) */
1683 str[str_l <= str_m-1 ? str_l : str_m-1] = '\0';
1685 /* Return the number of characters formatted (excluding trailing null
1686 * character), that is, the number of characters that would have been
1687 * written to the buffer if it were large enough.
1689 * The value of str_l should be returned, but str_l is of unsigned type
1690 * size_t, and snprintf is int, possibly leading to an undetected
1691 * integer overflow, resulting in a negative return value, which is illegal.
1692 * Both XSH5 and ISO C99 (at least the draft) are silent on this issue.
1693 * Should errno be set to EOVERFLOW and EOF returned in this case???
1698 #endif /* ndef HAVE_SNPRINTF */