home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume16 / deliver / part03 / subs.c < prev    next >
C/C++ Source or Header  |  1988-11-14  |  2KB  |  118 lines

  1. /* $Header: subs.c,v 1.3 88/09/14 19:42:33 network Exp $
  2.  *
  3.  * Miscellaneous subroutines.
  4.  *
  5.  * $Log:    subs.c,v $
  6.  * Revision 1.3  88/09/14  19:42:33  network
  7.  * Portability to System V and BSD.
  8.  * General fixup.
  9.  * 
  10.  * Revision 1.2  88/08/30  16:14:53  network
  11.  * New module.  Includes routines from main.c.
  12.  * Also, new routine savestr().
  13.  * 
  14.  */
  15.  
  16. #include "deliver.h"
  17.  
  18. /*----------------------------------------------------------------------
  19.  * Allocate memory for an environment variable, and putenv() it.
  20.  */
  21.  
  22. alloc_env(name, value)
  23. char    *name;
  24. char    *value;
  25. {
  26.     char    *s;
  27.  
  28.     if (!name || !value)
  29.         return;
  30.  
  31.     s = zalloc((unsigned) (strlen(name) + strlen(value) + 2));
  32.     (void) sprintf(s, "%s=%s", name, value);
  33.     if (putenv(s))
  34.         nomem();
  35. }
  36.  
  37. /*----------------------------------------------------------------------
  38.  * Allocate and clear.  If it fails, it takes the emergency exit.
  39.  */
  40.  
  41. char *
  42. zalloc(size)
  43. unsigned size;
  44. {
  45.     char    *p;
  46.  
  47.     if ((p = malloc(size)) == NULL)
  48.         nomem();
  49.  
  50.     (void) Zero(p, size);
  51.     return p;
  52. }
  53.  
  54. /*----------------------------------------------------------------------
  55.  * Reallocate to new size.  If it fails, it takes the emergency exit.
  56.  */
  57.  
  58. char *
  59. srealloc(ptr, size)
  60. char    *ptr;
  61. unsigned size;
  62. {
  63.     char    *p;
  64.  
  65.     if ((p = realloc(ptr, size)) == NULL)
  66.         nomem();
  67.  
  68.     return p;
  69. }
  70.  
  71. /*----------------------------------------------------------------------
  72.  * Make an allocated copy of a string.
  73.  */
  74.  
  75. char *
  76. copystr(s)
  77. char    *s;
  78. {
  79.     char    *p;
  80.  
  81.     if (s == NULL)
  82.         return NULL;
  83.  
  84.     if ((p = malloc((unsigned) strlen(s) + 1)) == NULL)
  85.         nomem();
  86.  
  87.     (void) strcpy(p, s);
  88.     return p;
  89. }
  90.  
  91. /*----------------------------------------------------------------------
  92.  * Emergency exit for memory loss.
  93.  */
  94.  
  95. nomem()
  96. {
  97.     message("%s: out of memory\n", progname);
  98.     leave(1);
  99. }
  100.  
  101. /*----------------------------------------------------------------------
  102.  * Return the last component of the given pathname.
  103.  */
  104.  
  105. char *
  106. basename(name)
  107. char    *name;
  108. {
  109.     char    *b;
  110.  
  111.     if ((b = strrchr(name, '/')) != NULL)
  112.         ++b;
  113.     else
  114.         b = name;
  115.  
  116.     return (b);
  117. }
  118.