home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / tahoe / vars.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-18  |  3.0 KB  |  169 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #ifdef notdef
  14. static char sccsid[] = "@(#)vars.c    5.4 (Berkeley) 2/18/88";
  15. #endif /* notdef */
  16.  
  17. #include "rcv.h"
  18.  
  19. /*
  20.  * Mail -- a mail program
  21.  *
  22.  * Variable handling stuff.
  23.  */
  24.  
  25. /*
  26.  * Assign a value to a variable.
  27.  */
  28.  
  29. assign(name, value)
  30.     char name[], value[];
  31. {
  32.     register struct var *vp;
  33.     register int h;
  34.  
  35.     h = hash(name);
  36.     vp = lookup(name);
  37.     if (vp == NOVAR) {
  38.         vp = (struct var *) calloc(sizeof *vp, 1);
  39.         vp->v_name = vcopy(name);
  40.         vp->v_link = variables[h];
  41.         variables[h] = vp;
  42.     }
  43.     else
  44.         vfree(vp->v_value);
  45.     vp->v_value = vcopy(value);
  46. }
  47.  
  48. /*
  49.  * Free up a variable string.  We do not bother to allocate
  50.  * strings whose value is "" since they are expected to be frequent.
  51.  * Thus, we cannot free same!
  52.  */
  53.  
  54. vfree(cp)
  55.     char *cp;
  56. {
  57.     if (*cp)
  58.         free(cp);
  59. }
  60.  
  61. /*
  62.  * Copy a variable value into permanent (ie, not collected after each
  63.  * command) space.  Do not bother to alloc space for ""
  64.  */
  65.  
  66. char *
  67. vcopy(str)
  68.     char str[];
  69. {
  70.     char *new;
  71.     unsigned len;
  72.  
  73.     if (*str == '\0')
  74.         return "";
  75.     len = strlen(str) + 1;
  76.     if ((new = malloc(len)) == NULL)
  77.         panic("Out of memory");
  78.     bcopy(str, new, (int) len);
  79.     return new;
  80. }
  81.  
  82. /*
  83.  * Get the value of a variable and return it.
  84.  * Look in the environment if its not available locally.
  85.  */
  86.  
  87. char *
  88. value(name)
  89.     char name[];
  90. {
  91.     register struct var *vp;
  92.  
  93.     if ((vp = lookup(name)) == NOVAR)
  94.         return(getenv(name));
  95.     return(vp->v_value);
  96. }
  97.  
  98. /*
  99.  * Locate a variable and return its variable
  100.  * node.
  101.  */
  102.  
  103. struct var *
  104. lookup(name)
  105.     register char name[];
  106. {
  107.     register struct var *vp;
  108.  
  109.     for (vp = variables[hash(name)]; vp != NOVAR; vp = vp->v_link)
  110.         if (*vp->v_name == *name && equal(vp->v_name, name))
  111.             return(vp);
  112.     return(NOVAR);
  113. }
  114.  
  115. /*
  116.  * Locate a group name and return it.
  117.  */
  118.  
  119. struct grouphead *
  120. findgroup(name)
  121.     register char name[];
  122. {
  123.     register struct grouphead *gh;
  124.  
  125.     for (gh = groups[hash(name)]; gh != NOGRP; gh = gh->g_link)
  126.         if (*gh->g_name == *name && equal(gh->g_name, name))
  127.             return(gh);
  128.     return(NOGRP);
  129. }
  130.  
  131. /*
  132.  * Print a group out on stdout
  133.  */
  134.  
  135. printgroup(name)
  136.     char name[];
  137. {
  138.     register struct grouphead *gh;
  139.     register struct group *gp;
  140.  
  141.     if ((gh = findgroup(name)) == NOGRP) {
  142.         printf("\"%s\": not a group\n", name);
  143.         return;
  144.     }
  145.     printf("%s\t", gh->g_name);
  146.     for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link)
  147.         printf(" %s", gp->ge_name);
  148.     putchar('\n');
  149. }
  150.  
  151. /*
  152.  * Hash the passed string and return an index into
  153.  * the variable or group hash table.
  154.  */
  155.  
  156. hash(name)
  157.     register char *name;
  158. {
  159.     register h = 0;
  160.  
  161.     while (*name) {
  162.         h <<= 2;
  163.         h += *name++;
  164.     }
  165.     if (h < 0 && (h = -h) < 0)
  166.         h = 0;
  167.     return (h % HSHSIZE);
  168. }
  169.