X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=x2p%2Futil.c;h=6994e873f753e8be9add4a1ac1c834feae7c2d85;hb=d9f30342f9de4793189d81b85a5e32057393e428;hp=7c2485aab10af9536b1c0fa8974781708d9f0ce2;hpb=d48672a2009b4897fb5bf74d6723c050cdd015e0;p=p5sagit%2Fp5-mst-13.2.git diff --git a/x2p/util.c b/x2p/util.c index 7c2485a..6994e87 100644 --- a/x2p/util.c +++ b/x2p/util.c @@ -1,47 +1,36 @@ -/* $RCSfile: util.c,v $$Revision: 4.0.1.1 $$Date: 91/06/07 12:20:35 $ +/* util.c * - * Copyright (c) 1991, Larry Wall + * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999, + * 2000, 2001, 2005 by Larry Wall and others * * You may distribute under the terms of either the GNU General Public * License or the Artistic License, as specified in the README file. - * - * $Log: util.c,v $ - * Revision 4.0.1.1 91/06/07 12:20:35 lwall - * patch4: new copyright notice - * - * Revision 4.0 91/03/20 01:58:25 lwall - * 4.0 baseline. - * */ -#include - -#include "handy.h" #include "EXTERN.h" #include "a2p.h" #include "INTERN.h" #include "util.h" +#include #define FLUSH -#define MEM_SIZE unsigned int -static char nomem[] = "Out of memory!\n"; +static const char nomem[] = "Out of memory!\n"; /* paranoid version of malloc */ -static int an = 0; -char * -safemalloc(size) -MEM_SIZE size; +Malloc_t +safemalloc(MEM_SIZE size) { - char *ptr; - char *malloc(); + Malloc_t ptr; - ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */ + /* malloc(0) is NASTY on some systems */ + ptr = malloc(size ? size : 1); #ifdef DEBUGGING if (debug & 128) - fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size); + fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",(unsigned long)ptr, + an++,(long)size); #endif if (ptr != Nullch) return ptr; @@ -50,23 +39,22 @@ MEM_SIZE size; exit(1); } /*NOTREACHED*/ + return 0; } /* paranoid version of realloc */ -char * -saferealloc(where,size) -char *where; -MEM_SIZE size; +Malloc_t +saferealloc(Malloc_t where, MEM_SIZE size) { - char *ptr; - char *realloc(); + Malloc_t ptr; - ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */ + /* realloc(0) is NASTY on some systems */ + ptr = realloc(where, size ? size : 1); #ifdef DEBUGGING if (debug & 128) { - fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++); - fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size); + fprintf(stderr,"0x%lx: (%05d) rfree\n",(unsigned long)where,an++); + fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",(unsigned long)ptr,an++,(long)size); } #endif if (ptr != Nullch) @@ -76,42 +64,25 @@ MEM_SIZE size; exit(1); } /*NOTREACHED*/ + return 0; } /* safe version of free */ -safefree(where) -char *where; +Free_t +safefree(Malloc_t where) { #ifdef DEBUGGING if (debug & 128) - fprintf(stderr,"0x%x: (%05d) free\n",where,an++); + fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++); #endif free(where); } -/* safe version of string copy */ - -char * -safecpy(to,from,len) -char *to; -register char *from; -register int len; -{ - register char *dest = to; - - if (from != Nullch) - for (len--; len && (*dest++ = *from++); len--) ; - *dest = '\0'; - return to; -} - /* copy a string up to some (non-backslashed) delimiter, if any */ char * -cpytill(to,from,delim) -register char *to, *from; -register int delim; +cpytill(register char *to, register char *from, register int delim) { for (; *from; from++,to++) { if (*from == '\\') { @@ -130,9 +101,7 @@ register int delim; char * -cpy2(to,from,delim) -register char *to, *from; -register int delim; +cpy2(register char *to, register char *from, register int delim) { for (; *from; from++,to++) { if (*from == '\\') @@ -150,9 +119,7 @@ register int delim; /* return ptr to little string in big string, NULL if not found */ char * -instr(big, little) -char *big, *little; - +instr(char *big, char *little) { register char *t, *s, *x; @@ -172,10 +139,9 @@ char *big, *little; /* copy a string to a safe spot */ char * -savestr(str) -char *str; +savestr(char *str) { - register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1)); + register char * const newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1)); (void)strcpy(newaddr,str); return newaddr; @@ -184,82 +150,46 @@ char *str; /* grow a static string to at least a certain length */ void -growstr(strptr,curlen,newlen) -char **strptr; -int *curlen; -int newlen; +growstr(char **strptr, int *curlen, int newlen) { if (newlen > *curlen) { /* need more room? */ if (*curlen) - *strptr = saferealloc(*strptr,(MEM_SIZE)newlen); + *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen); else - *strptr = safemalloc((MEM_SIZE)newlen); + *strptr = (char *) safemalloc((MEM_SIZE)newlen); *curlen = newlen; } } -/*VARARGS1*/ -fatal(pat,a1,a2,a3,a4) -char *pat; +void +fatal(const char *pat,...) { - fprintf(stderr,pat,a1,a2,a3,a4); - exit(1); -} +#if defined(HAS_VPRINTF) + va_list args; -/*VARARGS1*/ -warn(pat,a1,a2,a3,a4) -char *pat; -{ + va_start(args, pat); + vfprintf(stderr,pat,args); + va_end(args); +#else fprintf(stderr,pat,a1,a2,a3,a4); +#endif + exit(1); } -static bool firstsetenv = TRUE; -extern char **environ; - +#if defined(DARWIN) +__private_extern__ /* warn() conflicts with libc */ +#endif void -setenv(nam,val) -char *nam, *val; +warn(const char *pat,...) { - register int i=envix(nam); /* where does it go? */ +#if defined(HAS_VPRINTF) + va_list args; - if (!environ[i]) { /* does not exist yet */ - if (firstsetenv) { /* need we copy environment? */ - int j; -#ifndef lint - char **tmpenv = (char**) /* point our wand at memory */ - safemalloc((i+2) * sizeof(char*)); + va_start(args, pat); + vfprintf(stderr,pat,args); + va_end(args); #else - char **tmpenv = Null(char **); -#endif /* lint */ - - firstsetenv = FALSE; - for (j=0; j