home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / delivery / deliver.tz / deliver / subs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-07  |  5.4 KB  |  300 lines

  1. /* $Header: subs.c,v 2.6 90/03/06 12:21:17 chip Exp $
  2.  *
  3.  * Miscellaneous subroutines.
  4.  *
  5.  * $Log:    subs.c,v $
  6.  * Revision 2.6  90/03/06  12:21:17  chip
  7.  * Move logging into log.c and address parsing into addr.c.
  8.  * New: error delivery file for messages that fail.
  9.  * Major rearrangement of delivery file code.
  10.  * 
  11.  * Revision 2.5  90/02/23  14:16:56  chip
  12.  * Support "#!" in delivery files.
  13.  * Support "user|program" and "user?error" from delivery files.
  14.  * Improve debugging and error message formatting.
  15.  * Rearrange code for clarity.
  16.  * 
  17.  * Revision 2.4  90/02/06  11:56:46  chip
  18.  * Enforce MBX_MODE regardless of UMASK.
  19.  * Enforce ordered logging with a log lockfile.
  20.  * Revise log format.
  21.  * 
  22.  * Revision 2.3  89/11/01  10:37:58  network
  23.  * Add exists() function.
  24.  * 
  25.  * Revision 2.2  89/09/29  18:18:05  network
  26.  * Save message when delivery file produces no output,
  27.  * unless delivery file output the "DROP" string.
  28.  * Don't recopy temp files for sys and post-user delfiles.
  29.  * 
  30.  * Revision 2.1  89/06/09  12:25:39  network
  31.  * Update RCS revisions.
  32.  * 
  33.  * Revision 1.8  89/06/09  12:23:58  network
  34.  * Baseline for 2.0 release.
  35.  * 
  36.  */
  37.  
  38. #include "deliver.h"
  39. #include <sys/stat.h>
  40.  
  41. /*----------------------------------------------------------------------
  42.  * Report as to whether a file exists or not.
  43.  */
  44.  
  45. int
  46. exists(path)
  47. char    *path;
  48. {
  49.     struct stat st;
  50.  
  51.     return (stat(path, &st) == 0);
  52. }
  53.  
  54. /*----------------------------------------------------------------------
  55.  * Return a pointer to a temporary filename, or NULL if error.
  56.  */
  57.  
  58. char *
  59. tempfile()
  60. {
  61.     static char template[] = "/tmp/dl.XXXXXX";
  62.     char    *f;
  63.  
  64.     f = zalloc(32);
  65.     (void) strcpy(f, template);
  66.     if (mktemp(f) == NULL)
  67.     {
  68.         error("can't create temporary file");
  69.         return NULL;
  70.     }
  71.     return f;
  72. }
  73.  
  74. /*----------------------------------------------------------------------
  75.  * Create a file and return an fd, or complain if it doesn't work.
  76.  * The file is opened for read/write.
  77.  */
  78.  
  79. int
  80. tcreate(name)
  81. char    *name;
  82. {
  83.     int     fd;
  84.  
  85. #ifdef O_CREAT
  86.     fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0);
  87. #else
  88.     fd = creat(name, 0);
  89. #endif
  90.     if (fd == -1)
  91.     {
  92.         syserr("can't create %s", name);
  93.         return -1;
  94.     }
  95.  
  96. #ifndef O_CREAT
  97.     (void) close(fd);
  98.     if ((fd = open(name, 2)) == -1)
  99.     {
  100.         syserr("can't re-open %s", name);
  101.         return -1;
  102.     }
  103. #endif
  104.  
  105.     return fd;
  106. }
  107.  
  108. /*----------------------------------------------------------------------
  109.  * Create a file and return a FILE *, or complain if it doesn't work.
  110.  * The file is opened for read/write.
  111.  */
  112.  
  113. FILE *
  114. ftcreate(name)
  115. char    *name;
  116. {
  117.     FILE    *fp;
  118.     int    fd;
  119.  
  120.     if ((fd = tcreate(name)) == -1)
  121.         return NULL;
  122.  
  123.     if ((fp = fdopen(fd, "r+")) == NULL)
  124.         (void) close(fd);
  125.  
  126.     return fp;
  127. }
  128.  
  129. /*----------------------------------------------------------------------
  130.  * Allocate memory for an environment variable, and putenv() it.
  131.  */
  132.  
  133. alloc_env(name, value)
  134. char    *name;
  135. char    *value;
  136. {
  137.     char    *s;
  138.  
  139.     if (!name || !value)
  140.         return;
  141.  
  142.     /* If it's the same value it already has, don't bother. */
  143.  
  144.     if ((s = getenv(name)) != NULL && strcmp(s, value) == 0)
  145.         return;
  146.  
  147.     s = zalloc((unsigned) (strlen(name) + strlen(value) + 2));
  148.     (void) sprintf(s, "%s=%s", name, value);
  149.     if (putenv(s))
  150.         nomem();
  151. }
  152.  
  153. /*----------------------------------------------------------------------
  154.  * Remove an environment variable.
  155.  */
  156.  
  157. del_env(name)
  158. char    *name;
  159. {
  160.     int     len;
  161.     char    **e;
  162.  
  163.     if (!name)
  164.         return;
  165.  
  166.     len = strlen(name);
  167.  
  168.     for (e = environ; *e; ++e)
  169.     {
  170.         char    c;
  171.  
  172.         if (strncmp(*e, name, len) != 0)
  173.             continue;
  174.  
  175.         c = (*e)[len];
  176.         if (c == '=' || c == '\0')
  177.             break;
  178.     }
  179.  
  180.     for (; *e; ++e)
  181.         *e = *(e + 1);
  182. }
  183.  
  184. /*----------------------------------------------------------------------
  185.  * Allocate and clear.  If it fails, it takes the emergency exit.
  186.  */
  187.  
  188. char *
  189. zalloc(size)
  190. unsigned size;
  191. {
  192.     char    *p;
  193.  
  194.     if ((p = malloc(size)) == NULL)
  195.         nomem();
  196.  
  197.     Zero(p, size);
  198.     return p;
  199. }
  200.  
  201. /*----------------------------------------------------------------------
  202.  * Reallocate to new size.  If it fails, it takes the emergency exit.
  203.  */
  204.  
  205. char *
  206. srealloc(ptr, size)
  207. char    *ptr;
  208. unsigned size;
  209. {
  210.     char    *p;
  211.  
  212.     if ((p = realloc(ptr, size)) == NULL)
  213.         nomem();
  214.  
  215.     return p;
  216. }
  217.  
  218. /*----------------------------------------------------------------------
  219.  * Make an allocated copy of a string.
  220.  */
  221.  
  222. char *
  223. copystr(s)
  224. char    *s;
  225. {
  226.     char    *p;
  227.  
  228.     if (s == NULL)
  229.         return NULL;
  230.  
  231.     if ((p = malloc((unsigned) strlen(s) + 1)) == NULL)
  232.         nomem();
  233.  
  234.     (void) strcpy(p, s);
  235.     return p;
  236. }
  237.  
  238. /*----------------------------------------------------------------------
  239.  * Emergency exit for memory loss.
  240.  */
  241.  
  242. nomem()
  243. {
  244.     error("out of memory\n");
  245.     leave(1);
  246. }
  247.  
  248. /*----------------------------------------------------------------------
  249.  * Return the last component of the given pathname.
  250.  */
  251.  
  252. char *
  253. basename(name)
  254. char    *name;
  255. {
  256.     char    *b;
  257.  
  258.     if ((b = strrchr(name, '/')) != NULL)
  259.         ++b;
  260.     else
  261.         b = name;
  262.  
  263.     return (b);
  264. }
  265.  
  266. /*----------------------------------------------------------------------
  267.  * Return the given pathname relative to the given directory.
  268.  * No fancy checking for "." and ".." -- sorry.
  269.  */
  270.  
  271. char *
  272. relpath(dir, file)
  273. char    *dir, *file;
  274. {
  275.     static  char    *path;
  276.     static  unsigned pathsize;
  277.     unsigned n;
  278.  
  279.     if (file[0] == '/')
  280.         return file;
  281.  
  282.     n = strlen(dir) + strlen(file) + 2;
  283.     if (pathsize < n)
  284.     {
  285.         pathsize = n + 64;      /* gas */
  286.         if (path)
  287.             path = srealloc(path, pathsize);
  288.         else
  289.             path = zalloc(pathsize);
  290.     }
  291.  
  292.     (void) strcpy(path, dir);
  293.     n = strlen(path);
  294.     if (n == 0 || path[n - 1] != '/')
  295.         path[n++] = '/';
  296.     (void) strcpy(path + n, file);
  297.  
  298.     return path;
  299. }
  300.