Sender: richdawe AT bigfoot DOT com Message-ID: <3A0F14F3.DF321E50@bigfoot.com> Date: Sun, 12 Nov 2000 22:08:51 +0000 From: Richard Dawe X-Mailer: Mozilla 4.51 [en] (X11; I; Linux 2.2.17 i586) X-Accept-Language: de,fr MIME-Version: 1.0 To: DJGPP workers Subject: snprintf() diff Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Reply-To: djgpp-workers AT delorie DOT com Hello. Please find below a first diff for adding snprintf(), based on Alain Magloire's code. Changes from Alain's code drop: * snprintf() & vsnprintf() avoid wrap-around in FILE's _cnt by returning EFBIG if 'n' is greater than INT_MAX. * Rewrote the docs a little, to make them clearer. (Hope you don't mind Alain. ;) ) * Extracted the test program from snprintf.c and put it in the test hierachy. * Added "What's Changed" entries, suggestions to sprintf(), vsprintf() pages that one should use snprintf(), vsnprintf() instead. Bye, Rich =] *** /develop/djgpp/include/libc/file.h Sun Jun 27 17:27:44 1999 --- /develop/djgpp.dev/include/libc/file.h Sun Nov 12 21:20:44 2000 *************** *** 1,3 **** --- 1,4 ---- + /* Copyright (C) 2000 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ *************** *** 52,57 **** --- 53,63 ---- { p->_cnt--; return((unsigned char)(*(p->_ptr++)=(unsigned char)x)); + } + else if (p->_flag & _IOSTRG) + { + /* noop for strings */ + return 0; } return(_flsbuf((unsigned char)x,p)); } *** /develop/djgpp/include/stdio.h Sun Nov 15 13:37:28 1998 --- /develop/djgpp.dev/include/stdio.h Sun Nov 12 21:18:46 2000 *************** *** 1,3 **** --- 1,4 ---- + /* Copyright (C) 2000 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ *************** *** 94,99 **** --- 95,101 ---- int scanf(const char *_format, ...); void setbuf(FILE *_stream, char *_buf); int setvbuf(FILE *_stream, char *_buf, int _mode, size_t _size); + int snprintf(char *str, size_t n, const char *fmt, ...); int sprintf(char *_s, const char *_format, ...); int sscanf(const char *_s, const char *_format, ...); FILE * tmpfile(void); *************** *** 101,106 **** --- 103,109 ---- int ungetc(int _c, FILE *_stream); int vfprintf(FILE *_stream, const char *_format, va_list _ap); int vprintf(const char *_format, va_list _ap); + int vsnprintf(char *str, size_t n, const char *fmt, va_list ap); int vsprintf(char *_s, const char *_format, va_list _ap); #ifndef __STRICT_ANSI__ *** /develop/djgpp/src/docs/kb/wc204.txi Fri Oct 6 23:53:22 2000 --- /develop/djgpp.dev/src/docs/kb/wc204.txi Sun Nov 12 21:34:42 2000 *************** *** 131,133 **** --- 131,136 ---- @code{end}. The profiling code was also changed to not pollute the name space with @code{etext}. + @findex snprintf + @findex vsnprintf + New functions @code{snprintf} and @code{vsnprintf} added. *** /develop/djgpp/src/libc/ansi/stdio/makefile Sun Jun 28 18:44:16 1998 --- /develop/djgpp.dev/src/libc/ansi/stdio/makefile Sun Nov 12 21:57:50 2000 *************** *** 1,3 **** --- 1,4 ---- + # Copyright (C) 2000 DJ Delorie, see COPYING.DJ for details # Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details # Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details TOP=../.. *************** *** 61,65 **** --- 62,68 ---- SRC += vfprintf.c SRC += vprintf.c SRC += vsprintf.c + SRC += snprintf.c + SRC += vsnprntf.c include $(TOP)/../makefile.inc *** /develop/djgpp/src/libc/ansi/stdio/snprintf.c Sun Nov 12 21:57:18 2000 --- /develop/djgpp.dev/src/libc/ansi/stdio/snprintf.c Sun Nov 12 21:58:06 2000 *************** *** 1 **** --- 1,28 ---- + /* Copyright (C) 2000 DJ Delorie see COPYING.DJ for details */ + #include + #include + #include + #include + int + snprintf(char *str, size_t n, const char *fmt, ...) + { + FILE _strbuf; + int len; + + /* _cnt is an int in the FILE structure. To prevent wrap-around, we limit + * n to between 0 and INT_MAX inclusively. */ + if (n > INT_MAX) + { + errno = EFBIG; + return -1; + } + + _strbuf._flag = _IOWRT | _IOSTRG | _IONTERM; + _strbuf._ptr = str; + _strbuf._cnt = n - 1; + len = _doprnt(fmt, &(fmt)+1, &_strbuf); + if (n != 0) + *_strbuf._ptr = 0; + return len; + } *** /develop/djgpp/src/libc/ansi/stdio/snprintf.txh Sun Nov 12 21:57:18 2000 --- /develop/djgpp.dev/src/libc/ansi/stdio/snprintf.txh Sun Nov 12 21:41:04 2000 *************** *** 1 **** --- 1,25 ---- + @node snprintf, stdio + @subheading Syntax + @example + #include + + int snprintf (char *@var{buffer}, size_t @var{n}, const char *@var{format}, + @dots{}); + @end example + + @subheading Description + + This function works similarly to @xref{sprintf}, but the size @var{n} of + the @var{buffer} is also taken into account. This function will write + @var{n} - 1 characters. The @var{n}th character is used for the terminating + nul. If @var{n} is zero, @var{buffer} is not touched. + + @subheading Return Value + + The number of characters that would have been written (excluding the trailing + nul) is returned; otherwise -1 is returned to flag encoding errors. + + @subheading Portability + + @portability ansi *** /develop/djgpp/src/libc/ansi/stdio/sprintf.txh Sun Sep 27 16:20:50 1998 --- /develop/djgpp.dev/src/libc/ansi/stdio/sprintf.txh Sun Nov 12 21:37:08 2000 *************** *** 12,17 **** --- 12,20 ---- Sends formatted output from the arguments (@dots{}) to the @var{buffer}. @xref{printf}. + To avoid buffer overruns, it is safer to use @code{snprintf()} + (@pxref{snprintf}). + @subheading Return Value The number of characters written. *** /develop/djgpp/src/libc/ansi/stdio/vsnprntf.c Sun Nov 12 21:57:18 2000 --- /develop/djgpp.dev/src/libc/ansi/stdio/vsnprntf.c Sun Nov 12 21:58:16 2000 *************** *** 1 **** --- 1,29 ---- + /* Copyright (C) 2000 DJ Delorie, see COPYING.DJ for details */ + #include + #include + #include + #include + #include + int + vsnprintf(char *str, size_t n, const char *fmt, va_list ap) + { + FILE _strbuf; + int len; + + /* _cnt is an int in the FILE structure. To prevent wrap-around, we limit + * n to between 0 and INT_MAX inclusively. */ + if (n > INT_MAX) + { + errno = EFBIG; + return -1; + } + + _strbuf._flag = _IOWRT | _IOSTRG | _IONTERM; + _strbuf._ptr = str; + _strbuf._cnt = n - 1; + len = _doprnt(fmt, ap, &_strbuf); + if (n != 0) + *_strbuf._ptr = 0; + return len; + } *** /develop/djgpp/src/libc/ansi/stdio/vsnprntf.txh Sun Nov 12 21:57:18 2000 --- /develop/djgpp.dev/src/libc/ansi/stdio/vsnprntf.txh Sun Nov 12 21:31:38 2000 *************** *** 1 **** --- 1,26 ---- + @node vsnprintf, stdio + @subheading Syntax + @example + #include + #include + + int vsnprintf (char *@var{buffer}, size_t @var{n}, const char *@var{format}, + va_list @var{ap}); + @end example + + @subheading Description + + This function works similarly to @xref{vsprintf}, but the size @var{n} of + the @var{buffer} is also taken into account. This function will write + @var{n} - 1 characters. The @var{n}th character is used for the terminating + nul. If @var{n} is zero, @var{buffer} is not touched. + + @subheading Return Value + + The number of characters that would have been written (excluding the trailing + nul) is returned; otherwise -1 is returned to flag encoding errors. + + @subheading Portability + + @portability ansi *** /develop/djgpp/src/libc/ansi/stdio/vsprintf.txh Sun Sep 27 16:20:50 1998 --- /develop/djgpp.dev/src/libc/ansi/stdio/vsprintf.txh Sun Nov 12 21:37:00 2000 *************** *** 13,18 **** --- 13,21 ---- Sends formatted output from the @var{arguments} to the @var{buffer}. @xref{printf}. @xref{vfprintf}. + To avoid buffer overruns, it is safer to use @code{vsnprintf()} + (@pxref{vsnprintf}). + @subheading Return Value The number of characters written. *** /develop/djgpp/tests/libc/ansi/stdio/snprintf.c Sun Nov 12 21:57:18 2000 --- /develop/djgpp.dev/tests/libc/ansi/stdio/snprintf.c Sun Nov 12 21:46:14 2000 *************** *** 1 **** --- 1,58 ---- + #include + int main () + { + char holder[24]; + unsigned i, j; + + #define BIG "Hello this is a too big string for the buffer" + i = snprintf (holder, sizeof (holder), "%s\n", BIG); + printf ("%s\n", BIG); + printf ("%s\n", holder); + /* + * We are expecting : + * i == strlen (BIG) + 1 + * meaning the number that would have been written if the buffer was + * large enough (see C9X). + */ + if (i != strlen (BIG) + 1/* NL */) + { + fprintf (stderr, "FAILED snprintf\n"); + fprintf (stderr, + "sizeof (%d), snprintf(%d), strlen(%d)\n", + sizeof (holder), i, strlen (BIG)) ; + exit (1); + } + + /* We may have broken sscanf since it is also a string stream + * Lets do a basic test. + */ + { + static char *line = "25 December 2000\n"; + int day, year; + char month[24]; + + /* we are expectinc to read 3 variables */ + if ((i = sscanf (line, "%d %s %d\n", &day, month, &year)) == 3) + { + i = snprintf (holder, sizeof(holder), "%d %s %d\n", day, month, year); + printf (line); + j = printf (holder); + if (i != j) + { + fprintf (stderr, "FAILED snprintf\n"); + fprintf (stderr, "snprintf (%d) != printf (%d)\n", i, j); + exit (1); + } + } + else + { + printf ("sscanf (%d)\n", i); + printf ("FAILED sscanf\n"); + exit (1); + } + } + /* signal success */ + printf ("SUCCESS\n"); + return 0; + }