X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=x2p%2Futil.c;h=f0875577fe07870d2972698310dc58070525c7b0;hb=158b3652342ca691c9e3b061a1d78456ae1a9b4a;hp=83adfc276b800932427e765436103420b451d167;hpb=8d063cd8450e59ea1c611a2f4f5a21059a2804f1;p=p5sagit%2Fp5-mst-13.2.git diff --git a/x2p/util.c b/x2p/util.c index 83adfc2..f087557 100644 --- a/x2p/util.c +++ b/x2p/util.c @@ -1,39 +1,37 @@ -/* $Header: util.c,v 1.0 87/12/18 13:07:34 root Exp $ +/* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $ + * + * Copyright (c) 1991-2002, Larry Wall + * + * 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 1.0 87/12/18 13:07:34 root - * Initial revision - * */ -#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"; /* 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; @@ -42,23 +40,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) @@ -68,16 +65,17 @@ 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); } @@ -85,10 +83,7 @@ char *where; /* safe version of string copy */ char * -safecpy(to,from,len) -char *to; -register char *from; -register int len; +safecpy(char *to, register char *from, register int len) { register char *dest = to; @@ -98,46 +93,18 @@ register int len; return to; } -#ifdef undef -/* safe version of string concatenate, with \n deletion and space padding */ - -char * -safecat(to,from,len) -char *to; -register char *from; -register int len; -{ - register char *dest = to; - - len--; /* leave room for null */ - if (*dest) { - while (len && *dest++) len--; - if (len) { - len--; - *(dest-1) = ' '; - } - } - if (from != Nullch) - while (len && (*dest++ = *from++)) len--; - if (len) - dest--; - if (*(dest-1) == '\n') - dest--; - *dest = '\0'; - return to; -} -#endif - /* 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 == '\\' && from[1] == delim) - *to++ = *from++; + if (*from == '\\') { + if (from[1] == delim) + from++; + else if (from[1] == '\\') + *to++ = *from++; + } else if (*from == delim) break; *to = *from; @@ -146,13 +113,12 @@ register int delim; return from; } + 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 == '\\' && from[1] == delim) + if (*from == '\\') *to++ = *from++; else if (*from == '$') *to++ = '\\'; @@ -167,9 +133,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; @@ -189,10 +153,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 *newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1)); (void)strcpy(newaddr,str); return newaddr; @@ -201,75 +164,61 @@ 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 +croak(char *pat,...) { +#if defined(HAS_VPRINTF) + va_list args; + + 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; - void -setenv(nam,val) -char *nam, *val; +fatal(char *pat,...) { - register int i=envix(nam); /* where does it go? */ - - 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*)); +#if defined(HAS_VPRINTF) + va_list args; + + va_start(args, pat); + vfprintf(stderr,pat,args); + va_end(args); #else - char **tmpenv = Null(char **); -#endif /* lint */ - - firstsetenv = FALSE; - for (j=0; j