[inseparable changes from match from perl-5.003_93 to perl-5.003_94]
[p5sagit/p5-mst-13.2.git] / x2p / util.c
index 79cba69..e8b666f 100644 (file)
@@ -1,6 +1,6 @@
 /* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $
  *
- *    Copyright (c) 1991, Larry Wall
+ *    Copyright (c) 1991-1997, 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.
@@ -8,33 +8,33 @@
  * $Log:       util.c,v $
  */
 
-#include <stdio.h>
-
-#include "handy.h"
 #include "EXTERN.h"
 #include "a2p.h"
 #include "INTERN.h"
 #include "util.h"
 
+#ifdef I_STDARG
+#  include <stdarg.h>
+#endif
 #define FLUSH
-#define MEM_SIZE unsigned int
 
 static char nomem[] = "Out of memory!\n";
 
 /* paranoid version of malloc */
 
 
-char *
+Malloc_t
 safemalloc(size)
 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 %d bytes\n",(unsigned long)ptr,
+               an++,size);
 #endif
     if (ptr != Nullch)
        return ptr;
@@ -47,19 +47,19 @@ MEM_SIZE size;
 
 /* paranoid version of realloc */
 
-char *
+Malloc_t
 saferealloc(where,size)
-char *where;
+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 %d bytes\n",(unsigned long)ptr,an++,size);
     }
 #endif
     if (ptr != Nullch)
@@ -73,12 +73,13 @@ MEM_SIZE size;
 
 /* safe version of free */
 
+Free_t
 safefree(where)
-char *where;
+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);
 }
@@ -191,76 +192,65 @@ int newlen;
     }
 }
 
+void
+#if defined(I_STDARG) && defined(HAS_VPRINTF)
+croak(char *pat,...)
+#else /* I_STDARG */
 /*VARARGS1*/
 croak(pat,a1,a2,a3,a4)
-char *pat;
+    char *pat;
+    int a1,a2,a3,a4;
+#endif /* I_STDARG */
 {
+#if defined(I_STDARG) && defined(HAS_VPRINTF)
+    va_list args;
+
+    va_start(args, pat);
+    vfprintf(stderr,pat,args);
+#else
     fprintf(stderr,pat,a1,a2,a3,a4);
+#endif
     exit(1);
 }
 
+void
+#if defined(I_STDARG) && defined(HAS_VPRINTF)
+fatal(char *pat,...)
+#else /* I_STDARG */
 /*VARARGS1*/
 fatal(pat,a1,a2,a3,a4)
-char *pat;
+    char *pat;
+    int a1,a2,a3,a4;
+#endif /* I_STDARG */
 {
+#if defined(I_STDARG) && defined(HAS_VPRINTF)
+    va_list args;
+
+    va_start(args, pat);
+    vfprintf(stderr,pat,args);
+#else
     fprintf(stderr,pat,a1,a2,a3,a4);
+#endif
     exit(1);
 }
 
+void
+#if defined(I_STDARG) && defined(HAS_VPRINTF)
+warn(char *pat,...)
+#else /* I_STDARG */
 /*VARARGS1*/
 warn(pat,a1,a2,a3,a4)
-char *pat;
+    char *pat;
+    int a1,a2,a3,a4;
+#endif /* I_STDARG */
 {
-    fprintf(stderr,pat,a1,a2,a3,a4);
-}
+#if defined(I_STDARG) && defined(HAS_VPRINTF)
+    va_list args;
 
-static bool firstsetenv = TRUE;
-extern char **environ;
-
-void
-setenv(nam,val)
-char *nam, *val;
-{
-    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*));
+    va_start(args, pat);
+    vfprintf(stderr,pat,args);
 #else
-           char **tmpenv = Null(char **);
-#endif /* lint */
-    
-           firstsetenv = FALSE;
-           for (j=0; j<i; j++)         /* copy environment */
-               tmpenv[j] = environ[j];
-           environ = tmpenv;           /* tell exec where it is now */
-       }
-#ifndef lint
-       else
-           environ = (char**) saferealloc((char*) environ,
-               (i+2) * sizeof(char*));
-                                       /* just expand it a bit */
-#endif /* lint */
-       environ[i+1] = Nullch;  /* make sure it's null terminated */
-    }
-    environ[i] = safemalloc(strlen(nam) + strlen(val) + 2);
-                                       /* this may or may not be in */
-                                       /* the old environ structure */
-    sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
+    fprintf(stderr,pat,a1,a2,a3,a4);
+#endif
 }
 
-int
-envix(nam)
-char *nam;
-{
-    register int i, len = strlen(nam);
-
-    for (i = 0; environ[i]; i++) {
-       if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
-           break;                      /* strnEQ must come first to avoid */
-    }                                  /* potential SEGV's */
-    return i;
-}