diff options
author | Larry Wall <lwall@jpl-devvax.jpl.nasa.gov> | 1987-12-18 00:00:00 +0000 |
---|---|---|
committer | Larry Wall <lwall@jpl-devvax.jpl.nasa.gov> | 1987-12-18 00:00:00 +0000 |
commit | 8d063cd8450e59ea1c611a2f4f5a21059a2804f1 (patch) | |
tree | 9bba34a99f94e47746e40ffe1419151779d8a4fc /util.c | |
download | perl-8d063cd8450e59ea1c611a2f4f5a21059a2804f1.tar.gz |
a "replacement" for awk and sedperl-1.0
[ Perl is kind of designed to make awk and sed semi-obsolete. This posting
will include the first 10 patches after the main source. The following
description is lifted from Larry's manpage. --r$ ]
Perl is a interpreted language optimized for scanning arbitrary text
files, extracting information from those text files, and printing
reports based on that information. It's also a good language for many
system management tasks. The language is intended to be practical
(easy to use, efficient, complete) rather than beautiful (tiny,
elegant, minimal). It combines (in the author's opinion, anyway) some
of the best features of C, sed, awk, and sh, so people familiar with
those languages should have little difficulty with it. (Language
historians will also note some vestiges of csh, Pascal, and even
BASIC-PLUS.) Expression syntax corresponds quite closely to C
expression syntax. If you have a problem that would ordinarily use sed
or awk or sh, but it exceeds their capabilities or must run a little
faster, and you don't want to write the silly thing in C, then perl may
be for you. There are also translators to turn your sed and awk
scripts into perl scripts.
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 263 |
1 files changed, 263 insertions, 0 deletions
diff --git a/util.c b/util.c new file mode 100644 index 0000000000..b0b78f1926 --- /dev/null +++ b/util.c @@ -0,0 +1,263 @@ +/* $Header: util.c,v 1.0 87/12/18 13:06:30 root Exp $ + * + * $Log: util.c,v $ + * Revision 1.0 87/12/18 13:06:30 root + * Initial revision + * + */ + +#include <stdio.h> + +#include "handy.h" +#include "EXTERN.h" +#include "search.h" +#include "perl.h" +#include "INTERN.h" +#include "util.h" + +#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; +{ + char *ptr; + char *malloc(); + + ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */ +#ifdef DEBUGGING + if (debug & 128) + fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size); +#endif + if (ptr != Nullch) + return ptr; + else { + fputs(nomem,stdout) FLUSH; + exit(1); + } + /*NOTREACHED*/ +} + +/* paranoid version of realloc */ + +char * +saferealloc(where,size) +char *where; +MEM_SIZE size; +{ + char *ptr; + char *realloc(); + + ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */ +#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); + } +#endif + if (ptr != Nullch) + return ptr; + else { + fputs(nomem,stdout) FLUSH; + exit(1); + } + /*NOTREACHED*/ +} + +/* safe version of free */ + +safefree(where) +char *where; +{ +#ifdef DEBUGGING + if (debug & 128) + fprintf(stderr,"0x%x: (%05d) free\n",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; +} + +#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; +{ + for (; *from; from++,to++) { + if (*from == '\\' && from[1] == delim) + from++; + else if (*from == delim) + break; + *to = *from; + } + *to = '\0'; + return from; +} + +/* return ptr to little string in big string, NULL if not found */ + +char * +instr(big, little) +char *big, *little; + +{ + register char *t, *s, *x; + + for (t = big; *t; t++) { + for (x=t,s=little; *s; x++,s++) { + if (!*x) + return Nullch; + if (*s != *x) + break; + } + if (!*s) + return t; + } + return Nullch; +} + +/* copy a string to a safe spot */ + +char * +savestr(str) +char *str; +{ + register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1)); + + (void)strcpy(newaddr,str); + return newaddr; +} + +/* grow a static string to at least a certain length */ + +void +growstr(strptr,curlen,newlen) +char **strptr; +int *curlen; +int newlen; +{ + if (newlen > *curlen) { /* need more room? */ + if (*curlen) + *strptr = saferealloc(*strptr,(MEM_SIZE)newlen); + else + *strptr = safemalloc((MEM_SIZE)newlen); + *curlen = newlen; + } +} + +/*VARARGS1*/ +fatal(pat,a1,a2,a3,a4) +char *pat; +{ + extern FILE *e_fp; + extern char *e_tmpname; + + fprintf(stderr,pat,a1,a2,a3,a4); + if (e_fp) + UNLINK(e_tmpname); + exit(1); +} + +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*)); +#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 */ +} + +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; +} |