Back to index

lightning-sunbird  0.9+nobinonly
vsnprintf.c
Go to the documentation of this file.
00001 #ifndef WIN32
00002 #include "config.h"
00003 #endif
00004 #ifndef HAVE_SNPRINTF
00005 /*
00006  * Revision 12: http://theos.com/~deraadt/snprintf.c
00007  *
00008  * Copyright (c) 1997 Theo de Raadt
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions
00012  * are met:
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00020  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00021  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00022  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00023  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00024  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00028  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 #ifndef WIN32
00032 #include <sys/param.h>
00033 #include <sys/mman.h>
00034 #include <unistd.h>
00035 #endif
00036 #include <sys/types.h>
00037 #include <signal.h>
00038 #include <stdio.h>
00039 #include <string.h>
00040 #if __STDC__
00041 #include <stdarg.h>
00042 #include <stdlib.h>
00043 #else
00044 #include <varargs.h>
00045 #endif
00046 #include <setjmp.h>
00047 
00048 #ifndef roundup
00049 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
00050 #endif
00051 
00052 static int pgsize;
00053 static char *curobj;
00054 static sigjmp_buf bail;
00055 
00056 #define EXTRABYTES   2      /* XXX: why 2? you don't want to know */
00057 
00058 static char *
00059 msetup(str, n)
00060        char *str;
00061        size_t n;
00062 {
00063        char *e;
00064 
00065        if (n == 0)
00066               return NULL;
00067        if (pgsize == 0)
00068               pgsize = getpagesize();
00069        curobj = (char *)malloc(n + EXTRABYTES + pgsize * 2);
00070        if (curobj == NULL)
00071               return NULL;
00072        e = curobj + n + EXTRABYTES;
00073        e = (char *)roundup((unsigned long)e, pgsize);
00074        if (mprotect(e, pgsize, PROT_NONE) == -1) {
00075               free(curobj);
00076               curobj = NULL;
00077               return NULL;
00078        }
00079        e = e - n - EXTRABYTES;
00080        *e = '\0';
00081        return (e);
00082 }
00083 
00084 static void
00085 mcatch(int i)
00086 {
00087        siglongjmp(bail, 1);
00088 }
00089 
00090 static void
00091 mcleanup(str, n, p)
00092        char *str;
00093        size_t n;
00094        char *p;
00095 {
00096        strncpy(str, p, n-1);
00097        str[n-1] = '\0';
00098        if (mprotect((caddr_t)(p + n + EXTRABYTES), pgsize,
00099            PROT_READ|PROT_WRITE|PROT_EXEC) == -1)
00100               mprotect((caddr_t)(p + n + EXTRABYTES), pgsize,
00101                   PROT_READ|PROT_WRITE);
00102        free(curobj);
00103 }
00104 
00105 int
00106 #if __STDC__
00107 vsnprintf(char *str, size_t n, char const *fmt, va_list ap)
00108 #else
00109 vsnprintf(str, n, fmt, ap)
00110        char *str;
00111        size_t n;
00112        char *fmt;
00113        char *ap;
00114 #endif
00115 {
00116        struct sigaction osa, nsa;
00117        char *p;
00118        int ret = n + 1;     /* if we bail, indicated we overflowed */
00119 
00120        memset(&nsa, 0, sizeof nsa);
00121        nsa.sa_handler = mcatch;
00122        sigemptyset(&nsa.sa_mask);
00123 
00124        p = msetup(str, n);
00125        if (p == NULL) {
00126               *str = '\0';
00127               return 0;
00128        }
00129        if (sigsetjmp(bail, 1) == 0) {
00130               if (sigaction(SIGSEGV, &nsa, &osa) == -1) {
00131                      mcleanup(str, n, p);
00132                      return (0);
00133               }
00134               ret = vsprintf(p, fmt, ap);
00135        }
00136        mcleanup(str, n, p);
00137        (void) sigaction(SIGSEGV, &osa, NULL);
00138        return (ret);
00139 }
00140 
00141 int
00142 #if __STDC__
00143 snprintf(char *str, size_t n, char const *fmt, ...)
00144 #else
00145 snprintf(str, n, fmt, va_alist)
00146        char *str;
00147        size_t n;
00148        char *fmt;
00149        va_dcl
00150 #endif
00151 {
00152        va_list ap;
00153 #if __STDC__
00154        va_start(ap, fmt);
00155 #else
00156        va_start(ap);
00157 #endif
00158 
00159        return (vsnprintf(str, n, fmt, ap));
00160        va_end(ap);
00161 }
00162 
00163 
00164 #else
00165 
00166 /* ANSI C forbids an empty source file... */
00167 
00168 static void vsnprintf_dummy_func() {
00169    vsnprintf_dummy_func();
00170 }
00171 
00172 #endif