|
|
c83c6d |
diff -up xsane-0.999/lib/snprintf.c.OLD xsane-0.999/lib/snprintf.c
|
|
|
c83c6d |
--- xsane-0.999/lib/snprintf.c.OLD 2014-05-29 10:42:26.795419830 -0400
|
|
|
c83c6d |
+++ xsane-0.999/lib/snprintf.c 2014-05-29 10:48:58.544431002 -0400
|
|
|
c83c6d |
@@ -2,180 +2,616 @@
|
|
|
c83c6d |
|
|
|
c83c6d |
#ifndef HAVE_SNPRINTF
|
|
|
c83c6d |
|
|
|
c83c6d |
-/***************************************************************************
|
|
|
c83c6d |
- * LPRng - An Extended Print Spooler System
|
|
|
c83c6d |
- *
|
|
|
c83c6d |
- * Copyright 1988-1997, Patrick Powell, San Diego, CA
|
|
|
c83c6d |
- * papowell@sdsu.edu
|
|
|
c83c6d |
- * See below for conditions of use.
|
|
|
c83c6d |
- *
|
|
|
c83c6d |
- ***************************************************************************
|
|
|
c83c6d |
- * MODULE: snprintf.c
|
|
|
c83c6d |
- * PURPOSE: LPRng version of printf - absolutely bombproof (hopefully!)
|
|
|
c83c6d |
+/**************************************************************************
|
|
|
c83c6d |
+ * Copyright 1994-2003 Patrick Powell, San Diego, CA <papowell@lprng.com>
|
|
|
c83c6d |
**************************************************************************/
|
|
|
c83c6d |
-#if 0
|
|
|
c83c6d |
-
|
|
|
c83c6d |
- The "Artistic License"
|
|
|
c83c6d |
-
|
|
|
c83c6d |
- Preamble
|
|
|
c83c6d |
-
|
|
|
c83c6d |
-The intent of this document is to state the conditions under which a
|
|
|
c83c6d |
-Package may be copied, such that the Copyright Holder maintains some
|
|
|
c83c6d |
-semblance of artistic control over the development of the package,
|
|
|
c83c6d |
-while giving the users of the package the right to use and distribute
|
|
|
c83c6d |
-the Package in a more-or-less customary fashion, plus the right to make
|
|
|
c83c6d |
-reasonable modifications.
|
|
|
c83c6d |
-
|
|
|
c83c6d |
-Definitions:
|
|
|
c83c6d |
-
|
|
|
c83c6d |
- "Package" refers to the collection of files distributed by the
|
|
|
c83c6d |
- Copyright Holder, and derivatives of that collection of files
|
|
|
c83c6d |
- created through textual modification.
|
|
|
c83c6d |
-
|
|
|
c83c6d |
- "Standard Version" refers to such a Package if it has not been
|
|
|
c83c6d |
- modified, or has been modified in accordance with the wishes
|
|
|
c83c6d |
- of the Copyright Holder as specified below.
|
|
|
c83c6d |
-
|
|
|
c83c6d |
- "Copyright Holder" is whoever is named in the copyright or
|
|
|
c83c6d |
- copyrights for the package.
|
|
|
c83c6d |
-
|
|
|
c83c6d |
- "You" is you, if you are thinking about copying or distributing
|
|
|
c83c6d |
- this Package.
|
|
|
c83c6d |
-
|
|
|
c83c6d |
- "Reasonable copying fee" is whatever you can justify on the
|
|
|
c83c6d |
- basis of media cost, duplication charges, time of people involved,
|
|
|
c83c6d |
- and so on. (You will not be required to justify it to the
|
|
|
c83c6d |
- Copyright Holder, but only to the computing community at large
|
|
|
c83c6d |
- as a market that must bear the fee.)
|
|
|
c83c6d |
|
|
|
c83c6d |
- "Freely Available" means that no fee is charged for the item
|
|
|
c83c6d |
- itself, though there may be fees involved in handling the item.
|
|
|
c83c6d |
- It also means that recipients of the item may redistribute it
|
|
|
c83c6d |
- under the same conditions they received it.
|
|
|
c83c6d |
-
|
|
|
c83c6d |
-1. You may make and give away verbatim copies of the source form of the
|
|
|
c83c6d |
-Standard Version of this Package without restriction, provided that you
|
|
|
c83c6d |
-duplicate all of the original copyright notices and associated disclaimers.
|
|
|
c83c6d |
-
|
|
|
c83c6d |
-2. You may apply bug fixes, portability fixes and other modifications
|
|
|
c83c6d |
-derived from the Public Domain or from the Copyright Holder. A Package
|
|
|
c83c6d |
-modified in such a way shall still be considered the Standard Version.
|
|
|
c83c6d |
-
|
|
|
c83c6d |
-3. You may otherwise modify your copy of this Package in any way, provided
|
|
|
c83c6d |
-that you insert a prominent notice in each changed file stating how and
|
|
|
c83c6d |
-when you changed that file, and provided that you do at least ONE of the
|
|
|
c83c6d |
-following:
|
|
|
c83c6d |
-
|
|
|
c83c6d |
- a) place your modifications in the Public Domain or otherwise make them
|
|
|
c83c6d |
- Freely Available, such as by posting said modifications to Usenet or
|
|
|
c83c6d |
- an equivalent medium, or placing the modifications on a major archive
|
|
|
c83c6d |
- site such as uunet.uu.net, or by allowing the Copyright Holder to include
|
|
|
c83c6d |
- your modifications in the Standard Version of the Package.
|
|
|
c83c6d |
-
|
|
|
c83c6d |
- b) use the modified Package only within your corporation or organization.
|
|
|
c83c6d |
-
|
|
|
c83c6d |
- c) rename any non-standard executables so the names do not conflict
|
|
|
c83c6d |
- with standard executables, which must also be provided, and provide
|
|
|
c83c6d |
- a separate manual page for each non-standard executable that clearly
|
|
|
c83c6d |
- documents how it differs from the Standard Version.
|
|
|
c83c6d |
-
|
|
|
c83c6d |
- d) make other distribution arrangements with the Copyright Holder.
|
|
|
c83c6d |
-
|
|
|
c83c6d |
-4. You may distribute the programs of this Package in object code or
|
|
|
c83c6d |
-executable form, provided that you do at least ONE of the following:
|
|
|
c83c6d |
+/*
|
|
|
c83c6d |
+ Overview:
|
|
|
c83c6d |
|
|
|
c83c6d |
- a) distribute a Standard Version of the executables and library files,
|
|
|
c83c6d |
- together with instructions (in the manual page or equivalent) on where
|
|
|
c83c6d |
- to get the Standard Version.
|
|
|
c83c6d |
+ snprintf( char *buffer, int len, const char *format,...)
|
|
|
c83c6d |
+ plp_unsafe_snprintf( char *buffer, int len, const char *format,...)
|
|
|
c83c6d |
+ its horribly unsafe companion that does NOT protect you from
|
|
|
c83c6d |
+ the printing of evil control characters, but may be necessary
|
|
|
c83c6d |
+ See the man page documentation below
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ This version of snprintf was developed originally for printing
|
|
|
c83c6d |
+ on a motley collection of specialized hardware that had NO IO
|
|
|
c83c6d |
+ library. Due to contractual restrictions, a clean room implementation
|
|
|
c83c6d |
+ of the printf() code had to be developed.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ The method chosen for printf was to be as paranoid as possible,
|
|
|
c83c6d |
+ as these platforms had NO memory protection, and very small
|
|
|
c83c6d |
+ address spaces. This made it possible to try to print
|
|
|
c83c6d |
+ very long strings, i.e. - all of memory, very easily. To guard
|
|
|
c83c6d |
+ against this, all printing was done via a buffer, generous enough
|
|
|
c83c6d |
+ to hold strings, but small enough to protect against overruns,
|
|
|
c83c6d |
+ etc.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ Strangely enough, this proved to be of immense importance when
|
|
|
c83c6d |
+ SPRINTFing to a buffer on a stack... The rest, of course, is
|
|
|
c83c6d |
+ well known, as buffer overruns in the stack are a common way to
|
|
|
c83c6d |
+ do horrible things to operating systems, security, etc etc.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ This version of snprintf is VERY limited by modern standards.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ Revision History:
|
|
|
c83c6d |
+ First Released Version - 1994. This version had NO comments.
|
|
|
c83c6d |
+ First Released Version - 1994. This version had NO comments.
|
|
|
c83c6d |
+ Second Major Released Version - Tue May 23 10:43:44 PDT 2000
|
|
|
c83c6d |
+ Configuration and other items changed. Read this doc.
|
|
|
c83c6d |
+ Treat this as a new version.
|
|
|
c83c6d |
+ Minor Revision - Mon Apr 1 09:41:28 PST 2002
|
|
|
c83c6d |
+ - fixed up some constants and casts
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ COPYRIGHT AND TERMS OF USE:
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ You may use, copy, distribute, or otherwise incorporate this software
|
|
|
c83c6d |
+ and documentation into any product or other item, provided that
|
|
|
c83c6d |
+ the copyright in the documentation and source code as well as the
|
|
|
c83c6d |
+ source code generated constant strings in the object, executable
|
|
|
c83c6d |
+ or other code remain in place and are present in executable modules
|
|
|
c83c6d |
+ or objects.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ You may modify this code as appropriate to your usage; however the
|
|
|
c83c6d |
+ modified version must be identified by changing the various source
|
|
|
c83c6d |
+ and object code identification strings as is appropriately noted
|
|
|
c83c6d |
+ in the source code.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ You can use this with the GNU CONFIGURE utility.
|
|
|
c83c6d |
+ This should define the following macros appropriately:
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ HAVE_STDARG_H - if the <stdargs.h> include file is available
|
|
|
c83c6d |
+ HAVE_VARARG_H - if the <varargs.h> include file is available
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ HAVE_STRERROR - if the strerror() routine is available.
|
|
|
c83c6d |
+ If it is not available, then examine the lines containing
|
|
|
c83c6d |
+ the tests below.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ HAVE_SYS_ERRLIST - have sys_errlist available
|
|
|
c83c6d |
+ HAVE_DECL_SYS_ERRLIST - sys_errlist declaration in include files
|
|
|
c83c6d |
+ HAVE_SYS_NERR - have sys_nerr available
|
|
|
c83c6d |
+ HAVE_DECL_SYS_NERR - sys_nerr declaration in include files
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ HAVE_QUAD_T - if the quad_t type is defined
|
|
|
c83c6d |
+ HAVE_LONG_LONG - if the long long type is defined
|
|
|
c83c6d |
+ HAVE_LONG_DOUBLE - if the long double type is defined
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ If you are using the GNU configure (autoconf) facility, add the
|
|
|
c83c6d |
+ following line to the configure.in file, to force checking for the
|
|
|
c83c6d |
+ quad_t and long long data types:
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ AC_CHECK_HEADERS(stdlib.h,stdio.h,unistd.h,errno.h)
|
|
|
c83c6d |
+ AC_CHECK_FUNCS(strerror)
|
|
|
c83c6d |
+ AC_CACHE_CHECK(for errno,
|
|
|
c83c6d |
+ ac_cv_errno,
|
|
|
c83c6d |
+ [
|
|
|
c83c6d |
+ AC_TRY_LINK(,[extern int errno; return (errno);],
|
|
|
c83c6d |
+ ac_cv_errno=yes, ac_cv_errno=no)
|
|
|
c83c6d |
+ ])
|
|
|
c83c6d |
+ if test "$ac_cv_errno" = yes; then
|
|
|
c83c6d |
+ AC_DEFINE(HAVE_ERRNO)
|
|
|
c83c6d |
+ AC_CACHE_CHECK(for errno declaration,
|
|
|
c83c6d |
+ ac_cv_decl_errno,
|
|
|
c83c6d |
+ [
|
|
|
c83c6d |
+ AC_TRY_COMPILE([
|
|
|
c83c6d |
+ #include <stdio.h>
|
|
|
c83c6d |
+ #ifdef HAVE_STDLIB_H
|
|
|
c83c6d |
+ #include <stdlib.h>
|
|
|
c83c6d |
+ #endif
|
|
|
c83c6d |
+ #ifdef HAVE_UNISTD_H
|
|
|
c83c6d |
+ #include <unistd.h>
|
|
|
c83c6d |
+ #endif
|
|
|
c83c6d |
+ #ifdef HAVE_ERRNO_H
|
|
|
c83c6d |
+ #include <errno.h>
|
|
|
c83c6d |
+ ],[return(sys_nerr);],
|
|
|
c83c6d |
+ ac_cv_decl_errno=yes, ac_cv_decl_errno=no)
|
|
|
c83c6d |
+ ])
|
|
|
c83c6d |
+ if test "$ac_cv_decl_errno" = yes; then
|
|
|
c83c6d |
+ AC_DEFINE(HAVE_DECL_ERRNO)
|
|
|
c83c6d |
+ fi;
|
|
|
c83c6d |
+ fi
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ AC_CACHE_CHECK(for sys_nerr,
|
|
|
c83c6d |
+ ac_cv_sys_nerr,
|
|
|
c83c6d |
+ [
|
|
|
c83c6d |
+ AC_TRY_LINK(,[extern int sys_nerr; return (sys_nerr);],
|
|
|
c83c6d |
+ ac_cv_sys_nerr=yes, ac_cv_sys_nerr=no)
|
|
|
c83c6d |
+ ])
|
|
|
c83c6d |
+ if test "$ac_cv_sys_nerr" = yes; then
|
|
|
c83c6d |
+ AC_DEFINE(HAVE_SYS_NERR)
|
|
|
c83c6d |
+ AC_CACHE_CHECK(for sys_nerr declaration,
|
|
|
c83c6d |
+ ac_cv_decl_sys_nerr,
|
|
|
c83c6d |
+ [
|
|
|
c83c6d |
+ AC_TRY_COMPILE([
|
|
|
c83c6d |
+ #include <stdio.h>
|
|
|
c83c6d |
+ #ifdef HAVE_STDLIB_H
|
|
|
c83c6d |
+ #include <stdlib.h>
|
|
|
c83c6d |
+ #endif
|
|
|
c83c6d |
+ #ifdef HAVE_UNISTD_H
|
|
|
c83c6d |
+ #include <unistd.h>
|
|
|
c83c6d |
+ #endif],[return(sys_nerr);],
|
|
|
c83c6d |
+ ac_cv_decl_sys_nerr_def=yes, ac_cv_decl_sys_nerr_def=no)
|
|
|
c83c6d |
+ ])
|
|
|
c83c6d |
+ if test "$ac_cv_decl_sys_nerr" = yes; then
|
|
|
c83c6d |
+ AC_DEFINE(HAVE_DECL_SYS_NERR)
|
|
|
c83c6d |
+ fi
|
|
|
c83c6d |
+ fi
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ AC_CACHE_CHECK(for sys_errlist array,
|
|
|
c83c6d |
+ ac_cv_sys_errlist,
|
|
|
c83c6d |
+ [AC_TRY_LINK(,[extern char *sys_errlist[];
|
|
|
c83c6d |
+ sys_errlist[0];],
|
|
|
c83c6d |
+ ac_cv_sys_errlist=yes, ac_cv_sys_errlist=no)
|
|
|
c83c6d |
+ ])
|
|
|
c83c6d |
+ if test "$ac_cv_sys_errlist" = yes; then
|
|
|
c83c6d |
+ AC_DEFINE(HAVE_SYS_ERRLIST)
|
|
|
c83c6d |
+ AC_CACHE_CHECK(for sys_errlist declaration,
|
|
|
c83c6d |
+ ac_cv_sys_errlist_def,
|
|
|
c83c6d |
+ [AC_TRY_COMPILE([
|
|
|
c83c6d |
+ #include <stdio.h>
|
|
|
c83c6d |
+ #include <errno.h>
|
|
|
c83c6d |
+ #ifdef HAVE_STDLIB_H
|
|
|
c83c6d |
+ #include <stdlib.h>
|
|
|
c83c6d |
+ #endif
|
|
|
c83c6d |
+ #ifdef HAVE_UNISTD_H
|
|
|
c83c6d |
+ #include <unistd.h>
|
|
|
c83c6d |
+ #endif],[char *s = sys_errlist[0]; return(*s);],
|
|
|
c83c6d |
+ ac_cv_decl_sys_errlist=yes, ac_cv_decl_sys_errlist=no)
|
|
|
c83c6d |
+ ])
|
|
|
c83c6d |
+ if test "$ac_cv_decl_sys_errlist" = yes; then
|
|
|
c83c6d |
+ AC_DEFINE(HAVE_DECL_SYS_ERRLIST)
|
|
|
c83c6d |
+ fi
|
|
|
c83c6d |
+ fi
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ AC_CACHE_CHECK(checking for long long,
|
|
|
c83c6d |
+ ac_cv_long_long,
|
|
|
c83c6d |
+ [
|
|
|
c83c6d |
+ AC_TRY_COMPILE([
|
|
|
c83c6d |
+ #include <stdio.h>
|
|
|
c83c6d |
+ #include <sys/types.h>
|
|
|
c83c6d |
+ ], [printf("%d",sizeof(long long));],
|
|
|
c83c6d |
+ ac_cv_long_long=yes, ac_cv_long_long=no)
|
|
|
c83c6d |
+ ])
|
|
|
c83c6d |
+ if test $ac_cv_long_long = yes; then
|
|
|
c83c6d |
+ AC_DEFINE(HAVE_LONG_LONG)
|
|
|
c83c6d |
+ fi
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ AC_CACHE_CHECK(checking for long double,
|
|
|
c83c6d |
+ ac_cv_long_double,
|
|
|
c83c6d |
+ [
|
|
|
c83c6d |
+ AC_TRY_COMPILE([
|
|
|
c83c6d |
+ #include <stdio.h>
|
|
|
c83c6d |
+ #include <sys/types.h>
|
|
|
c83c6d |
+ ], [printf("%d",sizeof(long double));],
|
|
|
c83c6d |
+ ac_cv_long_double=yes, ac_cv_long_double=no)
|
|
|
c83c6d |
+ ])
|
|
|
c83c6d |
+ if test $ac_cv_long_double = yes; then
|
|
|
c83c6d |
+ AC_DEFINE(HAVE_LONG_DOUBLE)
|
|
|
c83c6d |
+ fi
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ AC_CACHE_CHECK(checking for quad_t,
|
|
|
c83c6d |
+ ac_cv_quad_t,
|
|
|
c83c6d |
+ [
|
|
|
c83c6d |
+ AC_TRY_COMPILE([
|
|
|
c83c6d |
+ #include <stdio.h>
|
|
|
c83c6d |
+ #include <sys/types.h>
|
|
|
c83c6d |
+ ], [printf("%d",sizeof(quad_t));],
|
|
|
c83c6d |
+ ac_cv_quad_t=yes, ac_cv_quad_t=no)
|
|
|
c83c6d |
+ ])
|
|
|
c83c6d |
+ if test $ac_cv_quad_t = yes; then
|
|
|
c83c6d |
+ AC_DEFINE(HAVE_QUAD_T)
|
|
|
c83c6d |
+ fi
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ NAME
|
|
|
c83c6d |
+ snprintf, plp_vsnprintf - formatted output conversion
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ SYNOPSIS
|
|
|
c83c6d |
+ #include <stdio.h>
|
|
|
c83c6d |
+ #include <stdarg.h>
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ int
|
|
|
c83c6d |
+ snprintf(const char *format, size_t size, va_list ap);
|
|
|
c83c6d |
+ int
|
|
|
c83c6d |
+ plp_unsafe_snprintf(const char *format, size_t size, va_list ap);
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ AKA snprintf and unsafe_snprintf in the documentation below
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ int
|
|
|
c83c6d |
+ vsnprintf(char *str, size_t size, const char *format, va_list ap);
|
|
|
c83c6d |
+ int
|
|
|
c83c6d |
+ unsafe_vsnprintf(char *str, size_t size, const char *format, va_list ap);
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ AKA vsnprintf and unsafe_vsnprintf in the documentation below
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ (Multithreaded Safe)
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ DESCRIPTION
|
|
|
c83c6d |
+ The printf() family of functions produces output according to
|
|
|
c83c6d |
+ a format as described below. Snprintf(), and vsnprintf()
|
|
|
c83c6d |
+ write to the character string str. These functions write the
|
|
|
c83c6d |
+ output under the control of a format string that specifies
|
|
|
c83c6d |
+ how subsequent arguments (or arguments accessed via the
|
|
|
c83c6d |
+ variable-length argument facilities of stdarg(3)) are converted
|
|
|
c83c6d |
+ for output. These functions return the number of characters
|
|
|
c83c6d |
+ printed (not including the trailing `\0' used to end output
|
|
|
c83c6d |
+ to strings). Snprintf() and vsnprintf() will write at most
|
|
|
c83c6d |
+ size-1 of the characters printed into the output string (the
|
|
|
c83c6d |
+ size'th character then gets the terminating `\0'); if the
|
|
|
c83c6d |
+ return value is greater than or equal to the size argument,
|
|
|
c83c6d |
+ the string was too short and some of the printed characters
|
|
|
c83c6d |
+ were discarded. The size or str may be given as zero to find
|
|
|
c83c6d |
+ out how many characters are needed; in this case, the str
|
|
|
c83c6d |
+ argument is ignored.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ By default, the snprintf function will not format control
|
|
|
c83c6d |
+ characters (except new line and tab) in strings. This is a
|
|
|
c83c6d |
+ safety feature that has proven to be extremely critical when
|
|
|
c83c6d |
+ using snprintf for secure applications and when debugging.
|
|
|
c83c6d |
+ If you MUST have control characters formatted or printed,
|
|
|
c83c6d |
+ then use the unsafe_snprintf() and unsafe_vsnprintf() and on
|
|
|
c83c6d |
+ your own head be the consequences. You have been warned.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ There is one exception to the comments above, and that is
|
|
|
c83c6d |
+ the "%c" (character) format. It brutally assumes that the
|
|
|
c83c6d |
+ user will have performed the necessary 'isprint()' or other
|
|
|
c83c6d |
+ checks and uses the integer value as a character.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ The format string is composed of zero or more directives:
|
|
|
c83c6d |
+ ordinary characters (not %), which are copied unchanged to
|
|
|
c83c6d |
+ the output stream; and conversion specifications, each
|
|
|
c83c6d |
+ of which results in fetching zero or more subsequent arguments.
|
|
|
c83c6d |
+ Each conversion specification is introduced by the character
|
|
|
c83c6d |
+ %. The arguments must correspond properly (after type promotion)
|
|
|
c83c6d |
+ with the conversion specifier. After the %, the following
|
|
|
c83c6d |
+ appear in sequence:
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ o Zero or more of the following flags:
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ - A zero `0' character specifying zero padding. For
|
|
|
c83c6d |
+ all conversions except n, the converted value is padded
|
|
|
c83c6d |
+ on the left with zeros rather than blanks. If a
|
|
|
c83c6d |
+ precision is given with a numeric conversion (d, i,
|
|
|
c83c6d |
+ o, u, i, x, and X), the `0' flag is ignored.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ - A negative field width flag `-' indicates the converted
|
|
|
c83c6d |
+ value is to be left adjusted on the field boundary. Except
|
|
|
c83c6d |
+ for n conversions, the converted value is padded on
|
|
|
c83c6d |
+ the right with blanks, rather than on the left with
|
|
|
c83c6d |
+ blanks or zeros. A `-' overrides a `0' if both are
|
|
|
c83c6d |
+ given.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ - A space, specifying that a blank should be left before
|
|
|
c83c6d |
+ a positive number produced by a signed conversion (d, e, E, f,
|
|
|
c83c6d |
+ g, G, or i).
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ - A `+' character specifying that a sign always be placed
|
|
|
c83c6d |
+ before a number produced by a signed conversion. A `+' overrides
|
|
|
c83c6d |
+ a space if both are used.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ o An optional decimal digit string specifying a minimum
|
|
|
c83c6d |
+ field width. If the converted value has fewer
|
|
|
c83c6d |
+ characters than the field width, it will be padded
|
|
|
c83c6d |
+ with spaces on the left (or right, if the
|
|
|
c83c6d |
+ left-adjustment flag has been given) to fill out
|
|
|
c83c6d |
+ the field width.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ o An optional precision, in the form of a period `.' followed
|
|
|
c83c6d |
+ by an optional digit string. If the digit string
|
|
|
c83c6d |
+ is omitted, the precision is taken as zero. This
|
|
|
c83c6d |
+ gives the minimum number of digits to appear for
|
|
|
c83c6d |
+ d, i, o, u, x, and X conversions, the number of
|
|
|
c83c6d |
+ digits to appear after the decimal-point for e,
|
|
|
c83c6d |
+ E, and f conversions, the maximum number of
|
|
|
c83c6d |
+ significant digits for g and G conversions, or
|
|
|
c83c6d |
+ the maximum number of characters to be printed
|
|
|
c83c6d |
+ from a string for s conversions.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ o The optional character h, specifying that a following d,
|
|
|
c83c6d |
+ i, o, u, x, or X conversion corresponds to a short
|
|
|
c83c6d |
+ int or unsigned short int argument, or that a
|
|
|
c83c6d |
+ following n conversion corresponds to a pointer
|
|
|
c83c6d |
+ to a short int argument.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ o The optional character l (ell) specifying that a following
|
|
|
c83c6d |
+ d, i, o, u, x, or X conversion applies to a pointer
|
|
|
c83c6d |
+ to a long int or unsigned long int argument, or
|
|
|
c83c6d |
+ that a following n conversion corresponds to a
|
|
|
c83c6d |
+ pointer to a long int argument.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ o The optional character q, specifying that a following d,
|
|
|
c83c6d |
+ i, o, u, x, or X conversion corresponds to a quad_t
|
|
|
c83c6d |
+ or u_quad_t argument, or that a following n
|
|
|
c83c6d |
+ conversion corresponds to a quad_t argument.
|
|
|
c83c6d |
+ This value is always printed in HEX notation. Tough.
|
|
|
c83c6d |
+ quad_t's are an OS system implementation, and should
|
|
|
c83c6d |
+ not be allowed.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ o The character L specifying that a following e, E, f, g,
|
|
|
c83c6d |
+ or G conversion corresponds to a long double
|
|
|
c83c6d |
+ argument.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ o A character that specifies the type of conversion to be applied.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ A field width or precision, or both, may be indicated by an asterisk `*'
|
|
|
c83c6d |
+ instead of a digit string. In this case, an int argument supplies the
|
|
|
c83c6d |
+ field width or precision. A negative field width is treated as a left
|
|
|
c83c6d |
+ adjustment flag followed by a positive field width; a negative precision
|
|
|
c83c6d |
+ is treated as though it were missing.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ The conversion specifiers and their meanings are:
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ diouxX The int (or appropriate variant) argument is converted to signed
|
|
|
c83c6d |
+ decimal (d and i), unsigned octal (o), unsigned decimal
|
|
|
c83c6d |
+ (u), or unsigned hexadecimal (x and X) notation. The
|
|
|
c83c6d |
+ letters abcdef are used for x conversions; the letters
|
|
|
c83c6d |
+ ABCDEF are used for X conversions. The precision, if
|
|
|
c83c6d |
+ any, gives the minimum number of digits that must
|
|
|
c83c6d |
+ appear; if the converted value requires fewer digits,
|
|
|
c83c6d |
+ it is padded on the left with zeros.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ eE The double argument is rounded and converted in the style
|
|
|
c83c6d |
+ [-]d.ddde+-dd where there is one digit before the decimal-point
|
|
|
c83c6d |
+ character and the number of digits after it is equal
|
|
|
c83c6d |
+ to the precision; if the precision is missing, it is
|
|
|
c83c6d |
+ taken as 6; if the precision is zero, no decimal-point
|
|
|
c83c6d |
+ character appears. An E conversion uses the letter
|
|
|
c83c6d |
+ E (rather than e) to introduce the exponent.
|
|
|
c83c6d |
+ The exponent always contains at least two digits; if
|
|
|
c83c6d |
+ the value is zero, the exponent is 00.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ f The double argument is rounded and converted to decimal notation
|
|
|
c83c6d |
+ in the style [-]ddd.ddd, where the number of digits after the
|
|
|
c83c6d |
+ decimal-point character is equal to the precision specification.
|
|
|
c83c6d |
+ If the precision is missing, it is taken as 6; if the precision
|
|
|
c83c6d |
+ is explicitly zero, no decimal-point character appears. If a
|
|
|
c83c6d |
+ decimal point appears, at least one digit appears before it.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ g The double argument is converted in style f or e (or
|
|
|
c83c6d |
+ E for G conversions). The precision specifies the
|
|
|
c83c6d |
+ number of significant digits. If the precision is
|
|
|
c83c6d |
+ missing, 6 digits are given; if the precision is zero,
|
|
|
c83c6d |
+ it is treated as 1. Style e is used if the exponent
|
|
|
c83c6d |
+ from its conversion is less than -4 or greater than
|
|
|
c83c6d |
+ or equal to the precision. Trailing zeros are removed
|
|
|
c83c6d |
+ from the fractional part of the result; a decimal
|
|
|
c83c6d |
+ point appears only if it is followed by at least one
|
|
|
c83c6d |
+ digit.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ c The int argument is converted to an unsigned char,
|
|
|
c83c6d |
+ and the resulting character is written.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ s The ``char *'' argument is expected to be a pointer to an array
|
|
|
c83c6d |
+ of character type (pointer to a string). Characters
|
|
|
c83c6d |
+ from the array are written up to (but not including)
|
|
|
c83c6d |
+ a terminating NUL character; if a precision is
|
|
|
c83c6d |
+ specified, no more than the number specified are
|
|
|
c83c6d |
+ written. If a precision is given, no null character
|
|
|
c83c6d |
+ need be present; if the precision is not specified,
|
|
|
c83c6d |
+ or is greater than the size of the array, the array
|
|
|
c83c6d |
+ must contain a terminating NUL character.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ % A `%' is written. No argument is converted. The complete
|
|
|
c83c6d |
+ conversion specification is `%%'.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ In no case does a non-existent or small field width cause truncation of a
|
|
|
c83c6d |
+ field; if the result of a conversion is wider than the field width, the
|
|
|
c83c6d |
+ field is expanded to contain the conversion result.
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ EXAMPLES
|
|
|
c83c6d |
+ To print a date and time in the form `Sunday, July 3, 10:02', where
|
|
|
c83c6d |
+ weekday and month are pointers to strings:
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ #include <stdio.h>
|
|
|
c83c6d |
+ fprintf(stdout, "%s, %s %d, %.2d:%.2d\n",
|
|
|
c83c6d |
+ weekday, month, day, hour, min);
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ To print pi to five decimal places:
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ #include <math.h>
|
|
|
c83c6d |
+ #include <stdio.h>
|
|
|
c83c6d |
+ fprintf(stdout, "pi = %.5f\n", 4 * atan(1.0));
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ To allocate a 128 byte string and print into it:
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ #include <stdio.h>
|
|
|
c83c6d |
+ #include <stdlib.h>
|
|
|
c83c6d |
+ #include <stdarg.h>
|
|
|
c83c6d |
+ char *newfmt(const char *fmt, ...)
|
|
|
c83c6d |
+ {
|
|
|
c83c6d |
+ char *p;
|
|
|
c83c6d |
+ va_list ap;
|
|
|
c83c6d |
+ if ((p = malloc(128)) == NULL)
|
|
|
c83c6d |
+ return (NULL);
|
|
|
c83c6d |
+ va_start(ap, fmt);
|
|
|
c83c6d |
+ (void) vsnprintf(p, 128, fmt, ap);
|
|
|
c83c6d |
+ va_end(ap);
|
|
|
c83c6d |
+ return (p);
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ SEE ALSO
|
|
|
c83c6d |
+ printf(1), scanf(3)
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ STANDARDS
|
|
|
c83c6d |
+ Turkey C Standardization and wimpy POSIX folks did not define
|
|
|
c83c6d |
+ snprintf or vsnprintf().
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ BUGS
|
|
|
c83c6d |
+ The conversion formats %D, %O, and %U are not standard and are provided
|
|
|
c83c6d |
+ only for backward compatibility. The effect of padding the %p format
|
|
|
c83c6d |
+ with zeros (either by the `0' flag or by specifying a precision), and the
|
|
|
c83c6d |
+ benign effect (i.e., none) of the `#' flag on %n and %p conversions, as
|
|
|
c83c6d |
+ well as other nonsensical combinations such as %Ld, are not standard;
|
|
|
c83c6d |
+ such combinations should be avoided.
|
|
|
c83c6d |
|
|
|
c83c6d |
- b) accompany the distribution with the machine-readable source of
|
|
|
c83c6d |
- the Package with your modifications.
|
|
|
c83c6d |
+ The typedef names quad_t and u_quad_t are infelicitous.
|
|
|
c83c6d |
|
|
|
c83c6d |
- c) give non-standard executables non-standard names, and clearly
|
|
|
c83c6d |
- document the differences in manual pages (or equivalent), together
|
|
|
c83c6d |
- with instructions on where to get the Standard Version.
|
|
|
c83c6d |
+*/
|
|
|
c83c6d |
|
|
|
c83c6d |
- d) make other distribution arrangements with the Copyright Holder.
|
|
|
c83c6d |
|
|
|
c83c6d |
-5. You may charge a reasonable copying fee for any distribution of this
|
|
|
c83c6d |
-Package. You may charge any fee you choose for support of this
|
|
|
c83c6d |
-Package. You may not charge a fee for this Package itself. However,
|
|
|
c83c6d |
-you may distribute this Package in aggregate with other (possibly
|
|
|
c83c6d |
-commercial) programs as part of a larger (possibly commercial) software
|
|
|
c83c6d |
-distribution provided that you do not advertise this Package as a
|
|
|
c83c6d |
-product of your own.
|
|
|
c83c6d |
+#include <sys/types.h>
|
|
|
c83c6d |
+#include <ctype.h>
|
|
|
c83c6d |
+#include <stdlib.h>
|
|
|
c83c6d |
+#include <stdio.h>
|
|
|
c83c6d |
+#if defined(HAVE_STRING_H)
|
|
|
c83c6d |
+# include <string.h>
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+#if defined(HAVE_STRINGS_H)
|
|
|
c83c6d |
+# include <strings.h>
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+#if defined(HAVE_ERRNO_H)
|
|
|
c83c6d |
+#include <errno.h>
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
|
|
|
c83c6d |
-6. The name of the Copyright Holder may not be used to endorse or promote
|
|
|
c83c6d |
-products derived from this software without specific prior written permission.
|
|
|
c83c6d |
+/*
|
|
|
c83c6d |
+ * For testing, define these values
|
|
|
c83c6d |
+ */
|
|
|
c83c6d |
+#if 0
|
|
|
c83c6d |
+#define HAVE_STDARG_H 1
|
|
|
c83c6d |
+#define TEST 1
|
|
|
c83c6d |
+#define HAVE_QUAD_T 1
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
|
|
|
c83c6d |
-7. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
|
|
c83c6d |
-IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
c83c6d |
-WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
c83c6d |
+/**** ENDINCLUDE ****/
|
|
|
c83c6d |
|
|
|
c83c6d |
- The End
|
|
|
c83c6d |
-#include "lp.h"
|
|
|
c83c6d |
+/*************************************************
|
|
|
c83c6d |
+ * KEEP THIS STRING - MODIFY AT THE END WITH YOUR REVISIONS
|
|
|
c83c6d |
+ * i.e. - the LOCAL REVISIONS part is for your use
|
|
|
c83c6d |
+ *************************************************/
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ static char *const _id = "plp_snprintf V2000.08.18 Copyright Patrick Powell 1988-2000 "
|
|
|
c83c6d |
+ "$Id: plp_snprintf.c,v 1.4 2005/04/14 20:05:19 papowell Exp $"
|
|
|
c83c6d |
+ " LOCAL REVISIONS: renamed plp_snprintf to snprintf, conditionalized everything on HAVE_SNPRINTF";
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+/* varargs declarations: */
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+# undef HAVE_STDARGS /* let's hope that works everywhere (mj) */
|
|
|
c83c6d |
+# undef VA_LOCAL_DECL
|
|
|
c83c6d |
+# undef VA_START
|
|
|
c83c6d |
+# undef VA_SHIFT
|
|
|
c83c6d |
+# undef VA_END
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+#if defined(HAVE_STDARG_H)
|
|
|
c83c6d |
+# include <stdarg.h>
|
|
|
c83c6d |
+# define HAVE_STDARGS /* let's hope that works everywhere (mj) */
|
|
|
c83c6d |
+# define VA_LOCAL_DECL va_list ap;
|
|
|
c83c6d |
+# define VA_START(f) va_start(ap, f)
|
|
|
c83c6d |
+# define VA_SHIFT(v,t) ; /* no-op for ANSI */
|
|
|
c83c6d |
+# define VA_END va_end(ap)
|
|
|
c83c6d |
+#else
|
|
|
c83c6d |
+# if defined(HAVE_VARARGS_H)
|
|
|
c83c6d |
+# include <varargs.h>
|
|
|
c83c6d |
+# undef HAVE_STDARGS
|
|
|
c83c6d |
+# define VA_LOCAL_DECL va_list ap;
|
|
|
c83c6d |
+# define VA_START(f) va_start(ap) /* f is ignored! */
|
|
|
c83c6d |
+# define VA_SHIFT(v,t) v = va_arg(ap,t)
|
|
|
c83c6d |
+# define VA_END va_end(ap)
|
|
|
c83c6d |
+# else
|
|
|
c83c6d |
+ XX ** NO VARARGS ** XX
|
|
|
c83c6d |
+# endif
|
|
|
c83c6d |
#endif
|
|
|
c83c6d |
|
|
|
c83c6d |
-#include <stdarg.h>
|
|
|
c83c6d |
-#include <errno.h>
|
|
|
c83c6d |
-#include <sys/types.h>
|
|
|
c83c6d |
-#define HAVE_STDARGS /* let's hope that works everywhere (mj) */
|
|
|
c83c6d |
-#define VA_LOCAL_DECL va_list ap;
|
|
|
c83c6d |
-#define VA_START(f) va_start(ap, f)
|
|
|
c83c6d |
-#define VA_SHIFT(v,t) ; /* no-op for ANSI */
|
|
|
c83c6d |
-#define VA_END va_end(ap)
|
|
|
c83c6d |
-
|
|
|
c83c6d |
-/**** ENDINCLUDE ****/
|
|
|
c83c6d |
+ union value {
|
|
|
c83c6d |
+#if defined(HAVE_QUAD_T)
|
|
|
c83c6d |
+ quad_t qvalue;
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+#if defined(HAVE_LONG_LONG)
|
|
|
c83c6d |
+ long long value;
|
|
|
c83c6d |
+#else
|
|
|
c83c6d |
+ long value;
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+ double dvalue;
|
|
|
c83c6d |
+};
|
|
|
c83c6d |
|
|
|
c83c6d |
-static char *const _id = "$Id: snprintf.c,v 3.2 1997/01/19 14:34:56 papowell Exp $";
|
|
|
c83c6d |
+#undef CVAL
|
|
|
c83c6d |
+#define CVAL(s) (*((unsigned char *)s))
|
|
|
c83c6d |
+#define safestrlen(s) ((s)?strlen(s):0)
|
|
|
c83c6d |
|
|
|
c83c6d |
-/*
|
|
|
c83c6d |
- * dopr(): poor man's version of doprintf
|
|
|
c83c6d |
- */
|
|
|
c83c6d |
|
|
|
c83c6d |
-static char * plp_Errormsg ( int err );
|
|
|
c83c6d |
-static void dopr( char *buffer, const char *format, va_list args );
|
|
|
c83c6d |
-static void fmtstr( char *value, int ljust, int len, int zpad, int precision );
|
|
|
c83c6d |
-static void fmtnum( long value, int base, int dosign,
|
|
|
c83c6d |
+ static char * plp_Errormsg ( int err, char *buffer );
|
|
|
c83c6d |
+ static void dopr( int visible_control, char **buffer, int *left,
|
|
|
c83c6d |
+ const char *format, va_list args );
|
|
|
c83c6d |
+ static void fmtstr( int visible_control, char **buffer, int *left,
|
|
|
c83c6d |
+ char *value, int ljust, int len, int zpad, int precision );
|
|
|
c83c6d |
+ static void fmtnum( char **buffer, int *left,
|
|
|
c83c6d |
+ union value *value, int base, int dosign,
|
|
|
c83c6d |
+ int ljust, int len, int zpad, int precision );
|
|
|
c83c6d |
+#if defined(HAVE_QUAD_T)
|
|
|
c83c6d |
+ static void fmtquad( char **buffer, int *left,
|
|
|
c83c6d |
+ union value *value, int base, int dosign,
|
|
|
c83c6d |
int ljust, int len, int zpad, int precision );
|
|
|
c83c6d |
-static void fmtdouble( int fmt, double value,
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+ static void fmtdouble( char **bufer, int *left,
|
|
|
c83c6d |
+ int fmt, double value,
|
|
|
c83c6d |
int ljust, int len, int zpad, int precision );
|
|
|
c83c6d |
-static void dostr( char * );
|
|
|
c83c6d |
-static char *output;
|
|
|
c83c6d |
-static void dopr_outch( int c );
|
|
|
c83c6d |
-static char *end;
|
|
|
c83c6d |
-int visible_control = 1;
|
|
|
c83c6d |
-
|
|
|
c83c6d |
-/**************************************************************
|
|
|
c83c6d |
- * Original:
|
|
|
c83c6d |
- * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
|
|
|
c83c6d |
- * A bombproof version of doprnt (dopr) included.
|
|
|
c83c6d |
- * Sigh. This sort of thing is always nasty do deal with. Note that
|
|
|
c83c6d |
- * the version here does not include floating point...
|
|
|
c83c6d |
- *
|
|
|
c83c6d |
- * plp_snprintf() is used instead of sprintf() as it does limit checks
|
|
|
c83c6d |
- * for string length. This covers a nasty loophole.
|
|
|
c83c6d |
- *
|
|
|
c83c6d |
- * The other functions are there to prevent NULL pointers from
|
|
|
c83c6d |
- * causing nast effects.
|
|
|
c83c6d |
- **************************************************************/
|
|
|
c83c6d |
-
|
|
|
c83c6d |
-int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
|
|
|
c83c6d |
-{
|
|
|
c83c6d |
- str[0] = 0;
|
|
|
c83c6d |
- end = str+count-1;
|
|
|
c83c6d |
- dopr( str, fmt, args );
|
|
|
c83c6d |
- if( count>0 ){
|
|
|
c83c6d |
- end[0] = 0;
|
|
|
c83c6d |
+ static void dostr( char **buffer, int *left, char *str );
|
|
|
c83c6d |
+ static void dopr_outch( char **buffer, int *left, int c );
|
|
|
c83c6d |
+/* VARARGS3 */
|
|
|
c83c6d |
+#ifdef HAVE_STDARGS
|
|
|
c83c6d |
+ int plp_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
|
|
|
c83c6d |
+#else
|
|
|
c83c6d |
+ int plp_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+{
|
|
|
c83c6d |
+ int left;
|
|
|
c83c6d |
+ char *buffer;
|
|
|
c83c6d |
+ if( (int)count < 0 ) count = 0;
|
|
|
c83c6d |
+ left = count;
|
|
|
c83c6d |
+ if( count == 0 ) str = 0;
|
|
|
c83c6d |
+ buffer = str;
|
|
|
c83c6d |
+ dopr( 1, &buffer, &left, fmt, args );
|
|
|
c83c6d |
+ /* fprintf(stderr,"str 0x%x, buffer 0x%x, count %d, left %d\n",
|
|
|
c83c6d |
+ (int)str, (int)buffer, count, left ); */
|
|
|
c83c6d |
+ if( str && count > 0 ){
|
|
|
c83c6d |
+ if( left > 0 ){
|
|
|
c83c6d |
+ str[count-left] = 0;
|
|
|
c83c6d |
+ } else {
|
|
|
c83c6d |
+ str[count-1] = 0;
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+ return(count - left);
|
|
|
c83c6d |
+}
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+/* VARARGS3 */
|
|
|
c83c6d |
+#ifdef HAVE_STDARGS
|
|
|
c83c6d |
+ int plp_unsafe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
|
|
|
c83c6d |
+#else
|
|
|
c83c6d |
+ int plp_unsafe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+{
|
|
|
c83c6d |
+ int left;
|
|
|
c83c6d |
+ char *buffer;
|
|
|
c83c6d |
+ if( (int)count < 0 ) count = 0;
|
|
|
c83c6d |
+ left = count;
|
|
|
c83c6d |
+ if( count == 0 ) str = 0;
|
|
|
c83c6d |
+ buffer = str;
|
|
|
c83c6d |
+ dopr( 0, &buffer, &left, fmt, args );
|
|
|
c83c6d |
+ /* fprintf(stderr,"str 0x%x, buffer 0x%x, count %d, left %d\n",
|
|
|
c83c6d |
+ (int)str, (int)buffer, count, left ); */
|
|
|
c83c6d |
+ if( str && count > 0 ){
|
|
|
c83c6d |
+ if( left > 0 ){
|
|
|
c83c6d |
+ str[count-left] = 0;
|
|
|
c83c6d |
+ } else {
|
|
|
c83c6d |
+ str[count-1] = 0;
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
}
|
|
|
c83c6d |
- return(strlen(str));
|
|
|
c83c6d |
+ return(count - left);
|
|
|
c83c6d |
}
|
|
|
c83c6d |
|
|
|
c83c6d |
/* VARARGS3 */
|
|
|
c83c6d |
#ifdef HAVE_STDARGS
|
|
|
c83c6d |
-int snprintf (char *str,size_t count,const char *fmt,...)
|
|
|
c83c6d |
+ int snprintf (char *str,size_t count,const char *fmt,...)
|
|
|
c83c6d |
#else
|
|
|
c83c6d |
-int snprintf (va_alist) va_dcl
|
|
|
c83c6d |
+ int snprintf (va_alist) va_dcl
|
|
|
c83c6d |
#endif
|
|
|
c83c6d |
{
|
|
|
c83c6d |
#ifndef HAVE_STDARGS
|
|
|
c83c6d |
@@ -183,22 +619,48 @@ int snprintf (va_alist) va_dcl
|
|
|
c83c6d |
size_t count;
|
|
|
c83c6d |
char *fmt;
|
|
|
c83c6d |
#endif
|
|
|
c83c6d |
+ int n = 0;
|
|
|
c83c6d |
VA_LOCAL_DECL
|
|
|
c83c6d |
|
|
|
c83c6d |
VA_START (fmt);
|
|
|
c83c6d |
VA_SHIFT (str, char *);
|
|
|
c83c6d |
VA_SHIFT (count, size_t );
|
|
|
c83c6d |
VA_SHIFT (fmt, char *);
|
|
|
c83c6d |
- (void) vsnprintf ( str, count, fmt, ap);
|
|
|
c83c6d |
+ n = plp_vsnprintf ( str, count, fmt, ap);
|
|
|
c83c6d |
VA_END;
|
|
|
c83c6d |
- return( strlen( str ) );
|
|
|
c83c6d |
+ return( n );
|
|
|
c83c6d |
}
|
|
|
c83c6d |
|
|
|
c83c6d |
-static void dopr( char *buffer, const char *format, va_list args )
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+/* VARARGS3 */
|
|
|
c83c6d |
+#ifdef HAVE_STDARGS
|
|
|
c83c6d |
+ int plp_unsafe_snprintf (char *str,size_t count,const char *fmt,...)
|
|
|
c83c6d |
+#else
|
|
|
c83c6d |
+ int plp_unsafe_snprintf (va_alist) va_dcl
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+{
|
|
|
c83c6d |
+#ifndef HAVE_STDARGS
|
|
|
c83c6d |
+ char *str;
|
|
|
c83c6d |
+ size_t count;
|
|
|
c83c6d |
+ char *fmt;
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+ int n = 0;
|
|
|
c83c6d |
+ VA_LOCAL_DECL
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ VA_START (fmt);
|
|
|
c83c6d |
+ VA_SHIFT (str, char *);
|
|
|
c83c6d |
+ VA_SHIFT (count, size_t );
|
|
|
c83c6d |
+ VA_SHIFT (fmt, char *);
|
|
|
c83c6d |
+ n = plp_unsafe_vsnprintf ( str, count, fmt, ap);
|
|
|
c83c6d |
+ VA_END;
|
|
|
c83c6d |
+ return( n );
|
|
|
c83c6d |
+}
|
|
|
c83c6d |
+ static void dopr( int visible_control, char **buffer, int *left, const char *format, va_list args )
|
|
|
c83c6d |
{
|
|
|
c83c6d |
int ch;
|
|
|
c83c6d |
- long value;
|
|
|
c83c6d |
+ union value value;
|
|
|
c83c6d |
int longflag = 0;
|
|
|
c83c6d |
+ int quadflag = 0;
|
|
|
c83c6d |
char *strvalue;
|
|
|
c83c6d |
int ljust;
|
|
|
c83c6d |
int len;
|
|
|
c83c6d |
@@ -207,22 +669,30 @@ static void dopr( char *buffer, const ch
|
|
|
c83c6d |
int set_precision;
|
|
|
c83c6d |
double dval;
|
|
|
c83c6d |
int err = errno;
|
|
|
c83c6d |
+ int base = 0;
|
|
|
c83c6d |
+ int signed_val = 0;
|
|
|
c83c6d |
|
|
|
c83c6d |
- output = buffer;
|
|
|
c83c6d |
while( (ch = *format++) ){
|
|
|
c83c6d |
switch( ch ){
|
|
|
c83c6d |
case '%':
|
|
|
c83c6d |
- ljust = len = zpad = 0;
|
|
|
c83c6d |
+ longflag = quadflag =
|
|
|
c83c6d |
+ ljust = len = zpad = base = signed_val = 0;
|
|
|
c83c6d |
precision = -1; set_precision = 0;
|
|
|
c83c6d |
nextch:
|
|
|
c83c6d |
ch = *format++;
|
|
|
c83c6d |
switch( ch ){
|
|
|
c83c6d |
case 0:
|
|
|
c83c6d |
- dostr( "**end of format**" );
|
|
|
c83c6d |
+ dostr( buffer, left, "**end of format**" );
|
|
|
c83c6d |
return;
|
|
|
c83c6d |
case '-': ljust = 1; goto nextch;
|
|
|
c83c6d |
case '.': set_precision = 1; precision = 0; goto nextch;
|
|
|
c83c6d |
- case '*': len = va_arg( args, int ); goto nextch;
|
|
|
c83c6d |
+ case '*':
|
|
|
c83c6d |
+ if( set_precision ){
|
|
|
c83c6d |
+ precision = va_arg( args, int );
|
|
|
c83c6d |
+ } else {
|
|
|
c83c6d |
+ len = va_arg( args, int );
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+ goto nextch;
|
|
|
c83c6d |
case '0': /* set zero padding if len not set */
|
|
|
c83c6d |
if(len==0 && set_precision == 0 ) zpad = '0';
|
|
|
c83c6d |
case '1': case '2': case '3':
|
|
|
c83c6d |
@@ -234,76 +704,91 @@ static void dopr( char *buffer, const ch
|
|
|
c83c6d |
len = len*10 + ch - '0';
|
|
|
c83c6d |
}
|
|
|
c83c6d |
goto nextch;
|
|
|
c83c6d |
- case 'l': longflag = 1; goto nextch;
|
|
|
c83c6d |
+ case 'l': ++longflag; goto nextch;
|
|
|
c83c6d |
+ case 'q':
|
|
|
c83c6d |
+#if !defined( HAVE_QUAD_T )
|
|
|
c83c6d |
+ dostr( buffer, left, "*no quad_t support *");
|
|
|
c83c6d |
+ return;
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+ quadflag = 1;
|
|
|
c83c6d |
+ goto nextch;
|
|
|
c83c6d |
case 'u': case 'U':
|
|
|
c83c6d |
- /*fmtnum(value,base,dosign,ljust,len, zpad, precision) */
|
|
|
c83c6d |
- if( longflag ){
|
|
|
c83c6d |
- value = va_arg( args, long );
|
|
|
c83c6d |
- } else {
|
|
|
c83c6d |
- value = va_arg( args, int );
|
|
|
c83c6d |
- }
|
|
|
c83c6d |
- fmtnum( value, 10,0, ljust, len, zpad, precision ); break;
|
|
|
c83c6d |
+ if( base == 0 ){ base = 10; signed_val = 0; }
|
|
|
c83c6d |
case 'o': case 'O':
|
|
|
c83c6d |
- /*fmtnum(value,base,dosign,ljust,len, zpad, precision) */
|
|
|
c83c6d |
- if( longflag ){
|
|
|
c83c6d |
- value = va_arg( args, long );
|
|
|
c83c6d |
- } else {
|
|
|
c83c6d |
- value = va_arg( args, int );
|
|
|
c83c6d |
- }
|
|
|
c83c6d |
- fmtnum( value, 8,0, ljust, len, zpad, precision ); break;
|
|
|
c83c6d |
+ if( base == 0 ){ base = 8; signed_val = 0; }
|
|
|
c83c6d |
case 'd': case 'D':
|
|
|
c83c6d |
- if( longflag ){
|
|
|
c83c6d |
- value = va_arg( args, long );
|
|
|
c83c6d |
- } else {
|
|
|
c83c6d |
- value = va_arg( args, int );
|
|
|
c83c6d |
- }
|
|
|
c83c6d |
- fmtnum( value, 10,1, ljust, len, zpad, precision ); break;
|
|
|
c83c6d |
+ if( base == 0 ){ base = 10; signed_val = 1; }
|
|
|
c83c6d |
case 'x':
|
|
|
c83c6d |
- if( longflag ){
|
|
|
c83c6d |
- value = va_arg( args, long );
|
|
|
c83c6d |
- } else {
|
|
|
c83c6d |
- value = va_arg( args, int );
|
|
|
c83c6d |
- }
|
|
|
c83c6d |
- fmtnum( value, 16,0, ljust, len, zpad, precision ); break;
|
|
|
c83c6d |
+ if( base == 0 ){ base = 16; signed_val = 0; }
|
|
|
c83c6d |
case 'X':
|
|
|
c83c6d |
- if( longflag ){
|
|
|
c83c6d |
- value = va_arg( args, long );
|
|
|
c83c6d |
+ if( base == 0 ){ base = -16; signed_val = 0; }
|
|
|
c83c6d |
+#if defined( HAVE_QUAD_T )
|
|
|
c83c6d |
+ if( quadflag ){
|
|
|
c83c6d |
+ value.qvalue = va_arg( args, quad_t );
|
|
|
c83c6d |
+ fmtquad( buffer, left, &value,base,signed_val, ljust, len, zpad, precision );
|
|
|
c83c6d |
+ break;
|
|
|
c83c6d |
+ } else
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+ if( longflag > 1 ){
|
|
|
c83c6d |
+#if defined(HAVE_LONG_LONG)
|
|
|
c83c6d |
+ if( signed_val ){
|
|
|
c83c6d |
+ value.value = va_arg( args, long long );
|
|
|
c83c6d |
+ } else {
|
|
|
c83c6d |
+ value.value = va_arg( args, unsigned long long );
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+#else
|
|
|
c83c6d |
+ if( signed_val ){
|
|
|
c83c6d |
+ value.value = va_arg( args, long );
|
|
|
c83c6d |
+ } else {
|
|
|
c83c6d |
+ value.value = va_arg( args, unsigned long );
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+ } else if( longflag ){
|
|
|
c83c6d |
+ if( signed_val ){
|
|
|
c83c6d |
+ value.value = va_arg( args, long );
|
|
|
c83c6d |
+ } else {
|
|
|
c83c6d |
+ value.value = va_arg( args, unsigned long );
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
} else {
|
|
|
c83c6d |
- value = va_arg( args, int );
|
|
|
c83c6d |
+ if( signed_val ){
|
|
|
c83c6d |
+ value.value = va_arg( args, int );
|
|
|
c83c6d |
+ } else {
|
|
|
c83c6d |
+ value.value = va_arg( args, unsigned int );
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
}
|
|
|
c83c6d |
- fmtnum( value,-16,0, ljust, len, zpad, precision ); break;
|
|
|
c83c6d |
+ fmtnum( buffer, left, &value,base,signed_val, ljust, len, zpad, precision ); break;
|
|
|
c83c6d |
case 's':
|
|
|
c83c6d |
strvalue = va_arg( args, char *);
|
|
|
c83c6d |
- fmtstr( strvalue,ljust,len, zpad, precision );
|
|
|
c83c6d |
+ fmtstr( visible_control, buffer, left, strvalue,ljust,len, zpad, precision );
|
|
|
c83c6d |
break;
|
|
|
c83c6d |
case 'c':
|
|
|
c83c6d |
ch = va_arg( args, int );
|
|
|
c83c6d |
{ char b[2];
|
|
|
c83c6d |
- int vsb = visible_control;
|
|
|
c83c6d |
b[0] = ch;
|
|
|
c83c6d |
b[1] = 0;
|
|
|
c83c6d |
- visible_control = 0;
|
|
|
c83c6d |
- fmtstr( b,ljust,len, zpad, precision );
|
|
|
c83c6d |
- visible_control = vsb;
|
|
|
c83c6d |
+ fmtstr( 0, buffer, left, b,ljust,len, zpad, precision );
|
|
|
c83c6d |
}
|
|
|
c83c6d |
break;
|
|
|
c83c6d |
- case 'f': case 'g':
|
|
|
c83c6d |
+ case 'f': case 'g': case 'e':
|
|
|
c83c6d |
dval = va_arg( args, double );
|
|
|
c83c6d |
- fmtdouble( ch, dval,ljust,len, zpad, precision ); break;
|
|
|
c83c6d |
+ fmtdouble( buffer, left, ch, dval,ljust,len, zpad, precision ); break;
|
|
|
c83c6d |
case 'm':
|
|
|
c83c6d |
- fmtstr( plp_Errormsg(err),ljust,len, zpad, precision ); break;
|
|
|
c83c6d |
- case '%': dopr_outch( ch ); continue;
|
|
|
c83c6d |
+ { char shortbuffer[32];
|
|
|
c83c6d |
+ fmtstr( visible_control, buffer, left,
|
|
|
c83c6d |
+ plp_Errormsg(err, shortbuffer),ljust,len, zpad, precision );
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+ break;
|
|
|
c83c6d |
+ case '%': dopr_outch( buffer, left, ch ); continue;
|
|
|
c83c6d |
default:
|
|
|
c83c6d |
- dostr( "???????" );
|
|
|
c83c6d |
+ dostr( buffer, left, "???????" );
|
|
|
c83c6d |
}
|
|
|
c83c6d |
longflag = 0;
|
|
|
c83c6d |
break;
|
|
|
c83c6d |
default:
|
|
|
c83c6d |
- dopr_outch( ch );
|
|
|
c83c6d |
+ dopr_outch( buffer, left, ch );
|
|
|
c83c6d |
break;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
}
|
|
|
c83c6d |
- *output = 0;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
|
|
|
c83c6d |
/*
|
|
|
c83c6d |
@@ -312,63 +797,69 @@ static void dopr( char *buffer, const ch
|
|
|
c83c6d |
* len = minimum length
|
|
|
c83c6d |
* precision = numbers of chars in string to use
|
|
|
c83c6d |
*/
|
|
|
c83c6d |
-static void
|
|
|
c83c6d |
-fmtstr( char *value, int ljust, int len, int zpad, int precision )
|
|
|
c83c6d |
+ static void
|
|
|
c83c6d |
+ fmtstr( int visible_control, char **buffer, int *left,
|
|
|
c83c6d |
+ char *value, int ljust, int len, int zpad, int precision )
|
|
|
c83c6d |
{
|
|
|
c83c6d |
- int padlen, strlen, i, c; /* amount to pad */
|
|
|
c83c6d |
+ int padlen, strlenv, i, c; /* amount to pad */
|
|
|
c83c6d |
|
|
|
c83c6d |
if( value == 0 ){
|
|
|
c83c6d |
value = "<NULL>";
|
|
|
c83c6d |
}
|
|
|
c83c6d |
- if( precision > 0 ){
|
|
|
c83c6d |
- strlen = precision;
|
|
|
c83c6d |
- } else {
|
|
|
c83c6d |
- /* cheap strlen so you do not have library call */
|
|
|
c83c6d |
- for( strlen = 0; (c=value[strlen]); ++ strlen ){
|
|
|
c83c6d |
- if( visible_control && iscntrl( c ) && !isspace( c ) ){
|
|
|
c83c6d |
- ++strlen;
|
|
|
c83c6d |
- }
|
|
|
c83c6d |
+ /* cheap strlen so you do not have library call */
|
|
|
c83c6d |
+ for( strlenv = i = 0; (c=CVAL(value+i)); ++i ){
|
|
|
c83c6d |
+ if( visible_control && iscntrl( c ) && c != '\t' && c != '\n' ){
|
|
|
c83c6d |
+ ++strlenv;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
+ ++strlenv;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
- padlen = len - strlen;
|
|
|
c83c6d |
+ if( precision > 0 && strlenv > precision ){
|
|
|
c83c6d |
+ strlenv = precision;
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+ padlen = len - strlenv;
|
|
|
c83c6d |
if( padlen < 0 ) padlen = 0;
|
|
|
c83c6d |
if( ljust ) padlen = -padlen;
|
|
|
c83c6d |
while( padlen > 0 ) {
|
|
|
c83c6d |
- dopr_outch( ' ' );
|
|
|
c83c6d |
+ dopr_outch( buffer, left, ' ' );
|
|
|
c83c6d |
--padlen;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
/* output characters */
|
|
|
c83c6d |
- for( i = 0; (c = value[i]); ++i ){
|
|
|
c83c6d |
- if( visible_control && iscntrl( c ) && !isspace( c ) ){
|
|
|
c83c6d |
- dopr_outch('^');
|
|
|
c83c6d |
+ for( i = 0; i < strlenv && (c = CVAL(value+i)); ++i ){
|
|
|
c83c6d |
+ if( visible_control && iscntrl( c ) && c != '\t' && c != '\n' ){
|
|
|
c83c6d |
+ dopr_outch(buffer, left, '^');
|
|
|
c83c6d |
c = ('@' | (c & 0x1F));
|
|
|
c83c6d |
}
|
|
|
c83c6d |
- dopr_outch(c);
|
|
|
c83c6d |
+ dopr_outch(buffer, left, c);
|
|
|
c83c6d |
}
|
|
|
c83c6d |
while( padlen < 0 ) {
|
|
|
c83c6d |
- dopr_outch( ' ' );
|
|
|
c83c6d |
+ dopr_outch( buffer, left, ' ' );
|
|
|
c83c6d |
++padlen;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
}
|
|
|
c83c6d |
|
|
|
c83c6d |
-static void
|
|
|
c83c6d |
-fmtnum( long value, int base, int dosign, int ljust,
|
|
|
c83c6d |
+ static void
|
|
|
c83c6d |
+ fmtnum( char **buffer, int *left,
|
|
|
c83c6d |
+ union value *value, int base, int dosign, int ljust,
|
|
|
c83c6d |
int len, int zpad, int precision )
|
|
|
c83c6d |
{
|
|
|
c83c6d |
int signvalue = 0;
|
|
|
c83c6d |
+#if defined(HAVE_LONG_LONG)
|
|
|
c83c6d |
+ unsigned long long uvalue;
|
|
|
c83c6d |
+#else
|
|
|
c83c6d |
unsigned long uvalue;
|
|
|
c83c6d |
- char convert[20];
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+ char convert[sizeof( union value) * 8 + 16];
|
|
|
c83c6d |
int place = 0;
|
|
|
c83c6d |
int padlen = 0; /* amount to pad */
|
|
|
c83c6d |
int caps = 0;
|
|
|
c83c6d |
|
|
|
c83c6d |
- /* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
|
|
|
c83c6d |
- value, base, dosign, ljust, len, zpad )); */
|
|
|
c83c6d |
- uvalue = value;
|
|
|
c83c6d |
+ /* fprintf(stderr,"value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
|
|
|
c83c6d |
+ value, base, dosign, ljust, len, zpad );/ **/
|
|
|
c83c6d |
+ uvalue = value->value;
|
|
|
c83c6d |
if( dosign ){
|
|
|
c83c6d |
- if( value < 0 ) {
|
|
|
c83c6d |
+ if( value->value < 0 ) {
|
|
|
c83c6d |
signvalue = '-';
|
|
|
c83c6d |
- uvalue = -value;
|
|
|
c83c6d |
+ uvalue = -value->value;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
}
|
|
|
c83c6d |
if( base < 0 ){
|
|
|
c83c6d |
@@ -385,71 +876,148 @@ fmtnum( long value, int base, int dosig
|
|
|
c83c6d |
padlen = len - place;
|
|
|
c83c6d |
if( padlen < 0 ) padlen = 0;
|
|
|
c83c6d |
if( ljust ) padlen = -padlen;
|
|
|
c83c6d |
- /* DEBUGP(( "str '%s', place %d, sign %c, padlen %d\n",
|
|
|
c83c6d |
- convert,place,signvalue,padlen)); */
|
|
|
c83c6d |
+ /* fprintf( stderr, "str '%s', place %d, sign %c, padlen %d\n",
|
|
|
c83c6d |
+ convert,place,signvalue,padlen); / **/
|
|
|
c83c6d |
if( zpad && padlen > 0 ){
|
|
|
c83c6d |
if( signvalue ){
|
|
|
c83c6d |
- dopr_outch( signvalue );
|
|
|
c83c6d |
+ dopr_outch( buffer, left, signvalue );
|
|
|
c83c6d |
--padlen;
|
|
|
c83c6d |
signvalue = 0;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
while( padlen > 0 ){
|
|
|
c83c6d |
- dopr_outch( zpad );
|
|
|
c83c6d |
+ dopr_outch( buffer, left, zpad );
|
|
|
c83c6d |
--padlen;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
}
|
|
|
c83c6d |
while( padlen > 0 ) {
|
|
|
c83c6d |
- dopr_outch( ' ' );
|
|
|
c83c6d |
+ dopr_outch( buffer, left, ' ' );
|
|
|
c83c6d |
--padlen;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
- if( signvalue ) dopr_outch( signvalue );
|
|
|
c83c6d |
- while( place > 0 ) dopr_outch( convert[--place] );
|
|
|
c83c6d |
+ if( signvalue ) dopr_outch( buffer, left, signvalue );
|
|
|
c83c6d |
+ while( place > 0 ) dopr_outch( buffer, left, convert[--place] );
|
|
|
c83c6d |
while( padlen < 0 ){
|
|
|
c83c6d |
- dopr_outch( ' ' );
|
|
|
c83c6d |
+ dopr_outch( buffer, left, ' ' );
|
|
|
c83c6d |
++padlen;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
}
|
|
|
c83c6d |
|
|
|
c83c6d |
-static void
|
|
|
c83c6d |
-fmtdouble( int fmt, double value, int ljust, int len, int zpad, int precision )
|
|
|
c83c6d |
+#if defined(HAVE_QUAD_T)
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ static void
|
|
|
c83c6d |
+ fmtquad( char **buffer, int *left,
|
|
|
c83c6d |
+ union value *value, int base, int dosign, int ljust,
|
|
|
c83c6d |
+ int len, int zpad, int precision )
|
|
|
c83c6d |
{
|
|
|
c83c6d |
- char convert[128];
|
|
|
c83c6d |
- char fmtstr[128];
|
|
|
c83c6d |
- int l;
|
|
|
c83c6d |
+ int signvalue = 0;
|
|
|
c83c6d |
+ int place = 0;
|
|
|
c83c6d |
+ int padlen = 0; /* amount to pad */
|
|
|
c83c6d |
+ int caps = 0;
|
|
|
c83c6d |
+ int i, c;
|
|
|
c83c6d |
+ union {
|
|
|
c83c6d |
+ quad_t qvalue;
|
|
|
c83c6d |
+ unsigned char qconvert[sizeof(quad_t)];
|
|
|
c83c6d |
+ } vvalue;
|
|
|
c83c6d |
+ char convert[2*sizeof(quad_t)+1];
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ /* fprintf(stderr,"value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
|
|
|
c83c6d |
+ value, base, dosign, ljust, len, zpad );/ **/
|
|
|
c83c6d |
+ vvalue.qvalue = value->qvalue;
|
|
|
c83c6d |
|
|
|
c83c6d |
- if( len == 0 ) len = 10;
|
|
|
c83c6d |
- if( len > sizeof(convert) - 10 ){
|
|
|
c83c6d |
- len = sizeof(convert) - 10;
|
|
|
c83c6d |
+ if( base < 0 ){
|
|
|
c83c6d |
+ caps = 1;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
- if( precision > sizeof(convert) - 10 ){
|
|
|
c83c6d |
- precision = sizeof(convert) - 10;
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ for( i = 0; i < (int)sizeof(quad_t); ++i ){
|
|
|
c83c6d |
+ c = vvalue.qconvert[i];
|
|
|
c83c6d |
+ convert[2*i] =
|
|
|
c83c6d |
+ (caps? "0123456789ABCDEF":"0123456789abcdef")[ (c >> 4) & 0xF];
|
|
|
c83c6d |
+ convert[2*i+1] =
|
|
|
c83c6d |
+ (caps? "0123456789ABCDEF":"0123456789abcdef")[ c & 0xF];
|
|
|
c83c6d |
}
|
|
|
c83c6d |
- if( precision > len ) precision = len;
|
|
|
c83c6d |
- strcpy( fmtstr, "%" );
|
|
|
c83c6d |
- if( ljust ) strcat(fmtstr, "-" );
|
|
|
c83c6d |
- if( len ){
|
|
|
c83c6d |
- sprintf( fmtstr+strlen(fmtstr), "%d", len );
|
|
|
c83c6d |
+ convert[2*i] = 0;
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ place = strlen(convert);
|
|
|
c83c6d |
+ padlen = len - place;
|
|
|
c83c6d |
+ if( padlen < 0 ) padlen = 0;
|
|
|
c83c6d |
+ if( ljust ) padlen = -padlen;
|
|
|
c83c6d |
+ /* fprintf( stderr, "str '%s', place %d, sign %c, padlen %d\n",
|
|
|
c83c6d |
+ convert,place,signvalue,padlen); / **/
|
|
|
c83c6d |
+ if( zpad && padlen > 0 ){
|
|
|
c83c6d |
+ if( signvalue ){
|
|
|
c83c6d |
+ dopr_outch( buffer, left, signvalue );
|
|
|
c83c6d |
+ --padlen;
|
|
|
c83c6d |
+ signvalue = 0;
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+ while( padlen > 0 ){
|
|
|
c83c6d |
+ dopr_outch( buffer, left, zpad );
|
|
|
c83c6d |
+ --padlen;
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+ while( padlen > 0 ) {
|
|
|
c83c6d |
+ dopr_outch( buffer, left, ' ' );
|
|
|
c83c6d |
+ --padlen;
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+ if( signvalue ) dopr_outch( buffer, left, signvalue );
|
|
|
c83c6d |
+ while( place > 0 ) dopr_outch( buffer, left, convert[--place] );
|
|
|
c83c6d |
+ while( padlen < 0 ){
|
|
|
c83c6d |
+ dopr_outch( buffer, left, ' ' );
|
|
|
c83c6d |
+ ++padlen;
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+}
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ static void mystrcat(char *dest, char *src )
|
|
|
c83c6d |
+{
|
|
|
c83c6d |
+ if( dest && src ){
|
|
|
c83c6d |
+ dest += safestrlen(dest);
|
|
|
c83c6d |
+ strcpy(dest,src);
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+}
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ static void
|
|
|
c83c6d |
+ fmtdouble( char **buffer, int *left,
|
|
|
c83c6d |
+ int fmt, double value, int ljust, int len, int zpad, int precision )
|
|
|
c83c6d |
+{
|
|
|
c83c6d |
+ char convert[sizeof( union value) * 8 + 512];
|
|
|
c83c6d |
+ char formatstr[128];
|
|
|
c83c6d |
+
|
|
|
c83c6d |
+ /* fprintf(stderr,"len %d, precision %d\n", len, precision ); */
|
|
|
c83c6d |
+ if( len > 255 ){
|
|
|
c83c6d |
+ len = 255;
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+ if( precision > 255 ){
|
|
|
c83c6d |
+ precision = 255;
|
|
|
c83c6d |
+ }
|
|
|
c83c6d |
+ if( precision >= 0 && len > 0 && precision > len ) precision = len;
|
|
|
c83c6d |
+ strcpy( formatstr, "%" ); /* 1 */
|
|
|
c83c6d |
+ if( ljust ) mystrcat(formatstr, "-" ); /* 1 */
|
|
|
c83c6d |
+ if( zpad ) mystrcat(formatstr, "0" ); /* 1 */
|
|
|
c83c6d |
+ if( len >= 0 ){
|
|
|
c83c6d |
+ sprintf( formatstr+strlen(formatstr), "%d", len ); /* 3 */
|
|
|
c83c6d |
}
|
|
|
c83c6d |
- if( precision > 0 ){
|
|
|
c83c6d |
- sprintf( fmtstr+strlen(fmtstr), ".%d", precision );
|
|
|
c83c6d |
+ if( precision >= 0 ){
|
|
|
c83c6d |
+ sprintf( formatstr+strlen(formatstr), ".%d", precision ); /* 3 */
|
|
|
c83c6d |
}
|
|
|
c83c6d |
- l = strlen( fmtstr );
|
|
|
c83c6d |
- fmtstr[l] = fmt;
|
|
|
c83c6d |
- fmtstr[l+1] = 0;
|
|
|
c83c6d |
- sprintf( convert, fmtstr, value );
|
|
|
c83c6d |
- dostr( convert );
|
|
|
c83c6d |
+ /* format string will be at most 10 chars long ... */
|
|
|
c83c6d |
+ sprintf( formatstr+strlen(formatstr), "%c", fmt );
|
|
|
c83c6d |
+ /* this is easier than trying to do the portable dtostr */
|
|
|
c83c6d |
+ /* fprintf(stderr,"format string '%s'\n", formatstr); */
|
|
|
c83c6d |
+ sprintf( convert, formatstr, value );
|
|
|
c83c6d |
+ dostr( buffer, left, convert );
|
|
|
c83c6d |
}
|
|
|
c83c6d |
|
|
|
c83c6d |
-static void dostr( char *str )
|
|
|
c83c6d |
+ static void dostr( char **buffer, int *left, char *str )
|
|
|
c83c6d |
{
|
|
|
c83c6d |
- while(*str) dopr_outch(*str++);
|
|
|
c83c6d |
+ if(str)while(*str) dopr_outch( buffer, left, *str++ );
|
|
|
c83c6d |
}
|
|
|
c83c6d |
|
|
|
c83c6d |
-static void dopr_outch( int c )
|
|
|
c83c6d |
+ static void dopr_outch( char **buffer, int *left, int c )
|
|
|
c83c6d |
{
|
|
|
c83c6d |
- if( end == 0 || output < end ){
|
|
|
c83c6d |
- *output++ = c;
|
|
|
c83c6d |
+ if( *left > 0 ){
|
|
|
c83c6d |
+ *(*buffer)++ = c;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
+ *left -= 1;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
|
|
|
c83c6d |
|
|
|
c83c6d |
@@ -461,29 +1029,26 @@ static void dopr_outch( int c )
|
|
|
c83c6d |
* Patrick Powell Tue Apr 11 08:05:05 PDT 1995
|
|
|
c83c6d |
****************************************************************************/
|
|
|
c83c6d |
/****************************************************************************/
|
|
|
c83c6d |
-#if !defined(HAVE_STRERROR)
|
|
|
c83c6d |
-
|
|
|
c83c6d |
-# if defined(HAVE_SYS_NERR)
|
|
|
c83c6d |
-# if !defined(HAVE_SYS_NERR_DEF)
|
|
|
c83c6d |
- extern int sys_nerr;
|
|
|
c83c6d |
-# endif
|
|
|
c83c6d |
-# define num_errors (sys_nerr)
|
|
|
c83c6d |
-# else
|
|
|
c83c6d |
-# define num_errors (-1) /* always use "errno=%d" */
|
|
|
c83c6d |
-# endif
|
|
|
c83c6d |
|
|
|
c83c6d |
+#if !defined(HAVE_STRERROR)
|
|
|
c83c6d |
+# undef num_errors
|
|
|
c83c6d |
# if defined(HAVE_SYS_ERRLIST)
|
|
|
c83c6d |
-# if !defined(HAVE_SYS_ERRLIST_DEF)
|
|
|
c83c6d |
+# if !defined(HAVE_DECL_SYS_ERRLIST)
|
|
|
c83c6d |
extern const char *const sys_errlist[];
|
|
|
c83c6d |
# endif
|
|
|
c83c6d |
-# else
|
|
|
c83c6d |
-# undef num_errors
|
|
|
c83c6d |
-# define num_errors (-1) /* always use "errno=%d" */
|
|
|
c83c6d |
+# if defined(HAVE_SYS_NERR)
|
|
|
c83c6d |
+# if !defined(HAVE_DECL_SYS_NERR)
|
|
|
c83c6d |
+ extern int sys_nerr;
|
|
|
c83c6d |
+# endif
|
|
|
c83c6d |
+# define num_errors (sys_nerr)
|
|
|
c83c6d |
+# endif
|
|
|
c83c6d |
+# endif
|
|
|
c83c6d |
+# if !defined(num_errors)
|
|
|
c83c6d |
+# define num_errors (-1) /* always use "errno=%d" */
|
|
|
c83c6d |
# endif
|
|
|
c83c6d |
-
|
|
|
c83c6d |
#endif
|
|
|
c83c6d |
|
|
|
c83c6d |
-static char * plp_Errormsg ( int err )
|
|
|
c83c6d |
+ static char * plp_Errormsg ( int err, char *buffer /* int maxlen = 32 */)
|
|
|
c83c6d |
{
|
|
|
c83c6d |
char *cp;
|
|
|
c83c6d |
|
|
|
c83c6d |
@@ -496,10 +1061,8 @@ static char * plp_Errormsg ( int err )
|
|
|
c83c6d |
} else
|
|
|
c83c6d |
# endif
|
|
|
c83c6d |
{
|
|
|
c83c6d |
- static char msgbuf[32]; /* holds "errno=%d". */
|
|
|
c83c6d |
- /* SAFE use of sprintf */
|
|
|
c83c6d |
- (void) sprintf (msgbuf, "errno=%d", err);
|
|
|
c83c6d |
- cp = msgbuf;
|
|
|
c83c6d |
+ (void) sprintf (buffer, "errno=%d", err);
|
|
|
c83c6d |
+ cp = buffer;
|
|
|
c83c6d |
}
|
|
|
c83c6d |
#endif
|
|
|
c83c6d |
return (cp);
|
|
|
c83c6d |
@@ -507,23 +1070,47 @@ static char * plp_Errormsg ( int err )
|
|
|
c83c6d |
|
|
|
c83c6d |
#if defined(TEST)
|
|
|
c83c6d |
#include <stdio.h>
|
|
|
c83c6d |
-int main( void )
|
|
|
c83c6d |
+ int main( void )
|
|
|
c83c6d |
{
|
|
|
c83c6d |
char buffer[128];
|
|
|
c83c6d |
char *t;
|
|
|
c83c6d |
char *test1 = "01234";
|
|
|
c83c6d |
+ int n;
|
|
|
c83c6d |
errno = 1;
|
|
|
c83c6d |
- plp_snprintf( buffer, sizeof(buffer), (t="errno '%m'")); printf( "%s = '%s'\n", t, buffer );
|
|
|
c83c6d |
- plp_snprintf( buffer, sizeof(buffer), (t = "%s"), test1 ); printf( "%s = '%s'\n", t, buffer );
|
|
|
c83c6d |
- plp_snprintf( buffer, sizeof(buffer), (t = "%12s"), test1 ); printf( "%s = '%s'\n", t, buffer );
|
|
|
c83c6d |
- plp_snprintf( buffer, sizeof(buffer), (t = "%-12s"), test1 ); printf( "%s = '%s'\n", t, buffer );
|
|
|
c83c6d |
- plp_snprintf( buffer, sizeof(buffer), (t = "%12.2s"), test1 ); printf( "%s = '%s'\n", t, buffer );
|
|
|
c83c6d |
- plp_snprintf( buffer, sizeof(buffer), (t = "%-12.2s"), test1 ); printf( "%s = '%s'\n", t, buffer );
|
|
|
c83c6d |
- plp_snprintf( buffer, sizeof(buffer), (t = "%g"), 1.25 ); printf( "%s = '%s'\n", t, buffer );
|
|
|
c83c6d |
- plp_snprintf( buffer, sizeof(buffer), (t = "%g"), 1.2345 ); printf( "%s = '%s'\n", t, buffer );
|
|
|
c83c6d |
- plp_snprintf( buffer, sizeof(buffer), (t = "%12g"), 1.25 ); printf( "%s = '%s'\n", t, buffer );
|
|
|
c83c6d |
- plp_snprintf( buffer, sizeof(buffer), (t = "%12.2g"), 1.25 ); printf( "%s = '%s'\n", t, buffer );
|
|
|
c83c6d |
- plp_snprintf( buffer, sizeof(buffer), (t = "%0*d"), 6, 1 ); printf( "%s = '%s'\n", t, buffer );
|
|
|
c83c6d |
+ buffer[0] = 0;
|
|
|
c83c6d |
+ n = snprintf( buffer, 0, (t="test")); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t="errno '%m'")); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%12s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%-12s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%12.2s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%-12.2s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%g"), 1.2345 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%12g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%12.1g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%12.2g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%12.3g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%0*d"), 6, 1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+#if defined(HAVE_LONG_LONG)
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%llx"), 1, 2, 3, 4 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%llx"), (long long)1, (long long)2 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%qx"), 1, 2, 3, 4 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%qx"), (quad_t)1, (quad_t)2 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+#endif
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), (char *)(0x01234567), (char *)0, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), (char *)(0x01234567), (char *)0x89ABCDEF, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), t, 0, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%f"), 1.2345 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%12f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%12.2f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%0.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%1.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%1.5f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
+ n = snprintf( buffer, sizeof(buffer), (t = "%5.5f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
|
|
|
c83c6d |
return(0);
|
|
|
c83c6d |
}
|
|
|
c83c6d |
#endif
|