home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / vcron.t.Z / vcron.t / VCRON / env.c < prev    next >
Text File  |  1988-11-15  |  4KB  |  166 lines

  1. /* $Source: /usr/src/local/vix/cron/env.c,v $
  2.  * $Revision: 1.3 $
  3.  * $Log:    env.c,v $
  4.  * Revision 1.3  87/05/02  17:34:00  paul
  5.  * baseline for mod.sources release
  6.  * 
  7.  * Revision 1.2  87/03/19  12:48:19  paul
  8.  * suggestions from rs@mirror (Rich $alz):
  9.  *    allow quotes around value string
  10.  * 
  11.  * Revision 1.1  87/02/13  00:26:58  paul
  12.  * Initial revision
  13.  * 
  14.  */
  15.  
  16. /* Copyright 1987 by Vixie Enterprises
  17.  * All rights reserved
  18.  *
  19.  * Distribute freely, except: don't sell it, don't remove my name from the
  20.  * source or documentation (don't take credit for my work), mark your changes
  21.  * (don't get me blamed for your possible bugs), don't alter or remove this
  22.  * notice.  Commercial redistribution is negotiable; contact me for details.
  23.  *
  24.  * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
  25.  * I'll try to keep a version up to date.  I can be reached as follows:
  26.  * Paul Vixie, Vixie Enterprises, 329 Noe Street, San Francisco, CA, 94114,
  27.  * (415) 864-7013, {ucbvax!dual,ames,ucsfmis,lll-crg,sun}!ptsfa!vixie!paul.
  28.  */
  29.  
  30.  
  31. #include "cron.h"
  32.  
  33.  
  34. char **
  35. env_init()
  36. {
  37.     extern char    *malloc();
  38.     REG char    **p = (char **) malloc(sizeof(char **));
  39.  
  40.     p[0] = NULL;
  41.     return p;
  42. }
  43.  
  44.  
  45. char **
  46. env_set(envp, envstr)
  47.     char    **envp;
  48.     char    *envstr;
  49. {
  50.     extern char    *realloc(), *savestr();
  51.     REG int    count, found;
  52.     REG char    **p;
  53.  
  54.     /*
  55.      * count the number of elements, including the null pointer;
  56.      * also set 'found' to -1 or index of entry if already in here.
  57.      */
  58.     found = -1;
  59.     for (count = 0;  envp[count] != NULL;  count++)
  60.     {
  61.         if (!strcmp_until(envp[count], envstr, '='))
  62.             found = count;
  63.     }
  64.     count++;        /* for the null pointer
  65.                  */
  66.  
  67.     if (found != -1)
  68.     {
  69.         /*
  70.          * it exists already, so just free the existing setting,
  71.          * save our new one there, and return the existing array.
  72.          */
  73.         free(envp[found]);
  74.         envp[found] = savestr(envstr);
  75.         return envp;
  76.     }
  77.  
  78.     /*
  79.      * it doesn't exist yet, so resize the array, move null pointer over
  80.      * one, save our string over the old null pointer, and return resized
  81.      * array.
  82.      */
  83.     p = (char **) realloc(
  84.             (char *)   envp,
  85.             (unsigned) ((count+1) * sizeof(char **))
  86.     );
  87.     p[count] = p[count-1];
  88.     p[count-1] = savestr(envstr);
  89.     return p;
  90. }
  91.  
  92.  
  93. int
  94. load_env(envstr, f)
  95.     char    *envstr;
  96.     FILE    *f;
  97. {
  98.     /* return    ERR = end of file
  99.      *        FALSE = not an env setting (file was repositioned)
  100.      *        TRUE = was an env setting
  101.      */
  102.     char    *strcpy(), *sprintf();
  103.     long    filepos = ftell(f);
  104.     char    name[MAX_TEMPSTR], val[MAX_TEMPSTR];
  105.     int    fields, strdtb();
  106.     void    skip_comments();
  107.  
  108.     skip_comments(f);
  109.     if (EOF == get_string(envstr, MAX_TEMPSTR, f, "\n"))
  110.         return ERR;
  111.  
  112. #if DEBUGGING
  113.     Debug(DPARS, "load_env, read <%s>\n", envstr);
  114. #endif
  115.  
  116.     name[0] = val[0] = '\0';
  117.     fields = sscanf(envstr, "%[^ =] = %[^\n#]", name, val);
  118.     if (fields != 2)
  119.     {
  120. #if DEBUGGING
  121.         Debug(DPARS, "load_env, not 2 fields (%d)\n", fields);
  122. #endif
  123.         fseek(f, filepos, 0);
  124.         Set_LineNum(LINE_NUMBER - 1)        /* kludge */
  125.         return FALSE;
  126.     }
  127.  
  128.     /* 2 fields from scanf; looks like an env setting
  129.      */
  130.  
  131.     /*
  132.      * process value string
  133.      */
  134.     {
  135.         int    len = strdtb(val);
  136.  
  137.         if (len >= 2)
  138.             if (val[0] == '\'' || val[0] == '"')
  139.                 if (val[len-1] == val[0])
  140.                 {
  141.                     val[len-1] = '\0';
  142.                     (void) strcpy(val, val+1);
  143.                 }
  144.     }
  145.  
  146.     (void) sprintf(envstr, "%s=%s", name, val);
  147. #if DEBUGGING
  148.     Debug(DPARS, "load_env, <%s> <%s> -> <%s>\n", name, val, envstr);
  149. #endif
  150.     return TRUE;
  151. }
  152.  
  153.  
  154. char *
  155. env_get(name, envp)
  156. REG    char    *name;
  157. REG    char    **envp;
  158. {
  159.     char    *index();
  160.  
  161.     for (;  *envp;  envp++)
  162.         if (!strcmp_until(*envp, name, '='))
  163.             return index(*envp, '=') + 1;  /* we know it's there */
  164.     return NULL;
  165. }
  166.