home *** CD-ROM | disk | FTP | other *** search
- /* $Header: subs.c,v 1.3 88/09/14 19:42:33 network Exp $
- *
- * Miscellaneous subroutines.
- *
- * $Log: subs.c,v $
- * Revision 1.3 88/09/14 19:42:33 network
- * Portability to System V and BSD.
- * General fixup.
- *
- * Revision 1.2 88/08/30 16:14:53 network
- * New module. Includes routines from main.c.
- * Also, new routine savestr().
- *
- */
-
- #include "deliver.h"
-
- /*----------------------------------------------------------------------
- * Allocate memory for an environment variable, and putenv() it.
- */
-
- alloc_env(name, value)
- char *name;
- char *value;
- {
- char *s;
-
- if (!name || !value)
- return;
-
- s = zalloc((unsigned) (strlen(name) + strlen(value) + 2));
- (void) sprintf(s, "%s=%s", name, value);
- if (putenv(s))
- nomem();
- }
-
- /*----------------------------------------------------------------------
- * Allocate and clear. If it fails, it takes the emergency exit.
- */
-
- char *
- zalloc(size)
- unsigned size;
- {
- char *p;
-
- if ((p = malloc(size)) == NULL)
- nomem();
-
- (void) Zero(p, size);
- return p;
- }
-
- /*----------------------------------------------------------------------
- * Reallocate to new size. If it fails, it takes the emergency exit.
- */
-
- char *
- srealloc(ptr, size)
- char *ptr;
- unsigned size;
- {
- char *p;
-
- if ((p = realloc(ptr, size)) == NULL)
- nomem();
-
- return p;
- }
-
- /*----------------------------------------------------------------------
- * Make an allocated copy of a string.
- */
-
- char *
- copystr(s)
- char *s;
- {
- char *p;
-
- if (s == NULL)
- return NULL;
-
- if ((p = malloc((unsigned) strlen(s) + 1)) == NULL)
- nomem();
-
- (void) strcpy(p, s);
- return p;
- }
-
- /*----------------------------------------------------------------------
- * Emergency exit for memory loss.
- */
-
- nomem()
- {
- message("%s: out of memory\n", progname);
- leave(1);
- }
-
- /*----------------------------------------------------------------------
- * Return the last component of the given pathname.
- */
-
- char *
- basename(name)
- char *name;
- {
- char *b;
-
- if ((b = strrchr(name, '/')) != NULL)
- ++b;
- else
- b = name;
-
- return (b);
- }
-