snprintf.c

Go to the documentation of this file.
00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2002
00005  *      Sleepycat Software.  All rights reserved.
00006  */
00007 
00008 #include "db_config.h"
00009 
00010 #ifndef lint
00011 static const char revid[] = "$Id: snprintf.c,v 11.10 2002/01/11 15:51:28 bostic Exp $";
00012 #endif /* not lint */
00013 
00014 #ifndef NO_SYSTEM_INCLUDES
00015 #include <sys/types.h>
00016 
00017 #include <stdio.h>
00018 #endif
00019 
00020 #include "db_int.h"
00021 
00022 /*
00023  * snprintf --
00024  *      Bounded version of sprintf.
00025  *
00026  * PUBLIC: #ifndef HAVE_SNPRINTF
00027  * PUBLIC: int snprintf __P((char *, size_t, const char *, ...));
00028  * PUBLIC: #endif
00029  */
00030 #ifndef HAVE_SNPRINTF
00031 int
00032 #ifdef __STDC__
00033 snprintf(char *str, size_t n, const char *fmt, ...)
00034 #else
00035 snprintf(str, n, fmt, va_alist)
00036         char *str;
00037         size_t n;
00038         const char *fmt;
00039         va_dcl
00040 #endif
00041 {
00042         static int ret_charpnt = -1;
00043         va_list ap;
00044         int len;
00045 
00046         COMPQUIET(n, 0);
00047 
00048         /*
00049          * Some old versions of sprintf return a pointer to the first argument
00050          * instead of a character count.  Assume the return value of snprintf,
00051          * vsprintf, etc. will be the same as sprintf, and check the easy one.
00052          *
00053          * We do this test at run-time because it's not a test we can do in a
00054          * cross-compilation environment.
00055          */
00056         if (ret_charpnt == -1) {
00057                 char buf[10];
00058 
00059                 ret_charpnt =
00060                     sprintf(buf, "123") != 3 ||
00061                     sprintf(buf, "123456789") != 9 ||
00062                     sprintf(buf, "1234") != 4;
00063         }
00064 
00065 #ifdef __STDC__
00066         va_start(ap, fmt);
00067 #else
00068         va_start(ap);
00069 #endif
00070         len = vsprintf(str, fmt, ap);
00071         va_end(ap);
00072         return (ret_charpnt ? (int)strlen(str) : len);
00073 }
00074 #endif

Generated on Wed Jul 20 21:03:02 2005 for MySQL 5.0.9 Beta by  doxygen 1.4.3