Annotation of 43BSDReno/usr.bin/mail/vars.c, revision 1.1.1.1

1.1       root        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: (1) source distributions retain this entire copyright
                      7:  * notice and comment, and (2) distributions including binaries display
                      8:  * the following acknowledgement:  ``This product includes software
                      9:  * developed by the University of California, Berkeley and its contributors''
                     10:  * in the documentation or other materials provided with the distribution
                     11:  * and in all advertising materials mentioning features or use of this
                     12:  * software. Neither the name of the University nor the names of its
                     13:  * contributors may be used to endorse or promote products derived
                     14:  * from this software without specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: static char sccsid[] = "@(#)vars.c     5.6 (Berkeley) 6/1/90";
                     22: #endif /* not lint */
                     23: 
                     24: #include "rcv.h"
                     25: 
                     26: /*
                     27:  * Mail -- a mail program
                     28:  *
                     29:  * Variable handling stuff.
                     30:  */
                     31: 
                     32: /*
                     33:  * Assign a value to a variable.
                     34:  */
                     35: 
                     36: assign(name, value)
                     37:        char name[], value[];
                     38: {
                     39:        register struct var *vp;
                     40:        register int h;
                     41: 
                     42:        h = hash(name);
                     43:        vp = lookup(name);
                     44:        if (vp == NOVAR) {
                     45:                vp = (struct var *) calloc(sizeof *vp, 1);
                     46:                vp->v_name = vcopy(name);
                     47:                vp->v_link = variables[h];
                     48:                variables[h] = vp;
                     49:        }
                     50:        else
                     51:                vfree(vp->v_value);
                     52:        vp->v_value = vcopy(value);
                     53: }
                     54: 
                     55: /*
                     56:  * Free up a variable string.  We do not bother to allocate
                     57:  * strings whose value is "" since they are expected to be frequent.
                     58:  * Thus, we cannot free same!
                     59:  */
                     60: 
                     61: vfree(cp)
                     62:        char *cp;
                     63: {
                     64:        if (*cp)
                     65:                free(cp);
                     66: }
                     67: 
                     68: /*
                     69:  * Copy a variable value into permanent (ie, not collected after each
                     70:  * command) space.  Do not bother to alloc space for ""
                     71:  */
                     72: 
                     73: char *
                     74: vcopy(str)
                     75:        char str[];
                     76: {
                     77:        char *new;
                     78:        unsigned len;
                     79: 
                     80:        if (*str == '\0')
                     81:                return "";
                     82:        len = strlen(str) + 1;
                     83:        if ((new = malloc(len)) == NULL)
                     84:                panic("Out of memory");
                     85:        bcopy(str, new, (int) len);
                     86:        return new;
                     87: }
                     88: 
                     89: /*
                     90:  * Get the value of a variable and return it.
                     91:  * Look in the environment if its not available locally.
                     92:  */
                     93: 
                     94: char *
                     95: value(name)
                     96:        char name[];
                     97: {
                     98:        register struct var *vp;
                     99: 
                    100:        if ((vp = lookup(name)) == NOVAR)
                    101:                return(getenv(name));
                    102:        return(vp->v_value);
                    103: }
                    104: 
                    105: /*
                    106:  * Locate a variable and return its variable
                    107:  * node.
                    108:  */
                    109: 
                    110: struct var *
                    111: lookup(name)
                    112:        register char name[];
                    113: {
                    114:        register struct var *vp;
                    115: 
                    116:        for (vp = variables[hash(name)]; vp != NOVAR; vp = vp->v_link)
                    117:                if (*vp->v_name == *name && equal(vp->v_name, name))
                    118:                        return(vp);
                    119:        return(NOVAR);
                    120: }
                    121: 
                    122: /*
                    123:  * Locate a group name and return it.
                    124:  */
                    125: 
                    126: struct grouphead *
                    127: findgroup(name)
                    128:        register char name[];
                    129: {
                    130:        register struct grouphead *gh;
                    131: 
                    132:        for (gh = groups[hash(name)]; gh != NOGRP; gh = gh->g_link)
                    133:                if (*gh->g_name == *name && equal(gh->g_name, name))
                    134:                        return(gh);
                    135:        return(NOGRP);
                    136: }
                    137: 
                    138: /*
                    139:  * Print a group out on stdout
                    140:  */
                    141: 
                    142: printgroup(name)
                    143:        char name[];
                    144: {
                    145:        register struct grouphead *gh;
                    146:        register struct group *gp;
                    147: 
                    148:        if ((gh = findgroup(name)) == NOGRP) {
                    149:                printf("\"%s\": not a group\n", name);
                    150:                return;
                    151:        }
                    152:        printf("%s\t", gh->g_name);
                    153:        for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link)
                    154:                printf(" %s", gp->ge_name);
                    155:        putchar('\n');
                    156: }
                    157: 
                    158: /*
                    159:  * Hash the passed string and return an index into
                    160:  * the variable or group hash table.
                    161:  */
                    162: 
                    163: hash(name)
                    164:        register char *name;
                    165: {
                    166:        register h = 0;
                    167: 
                    168:        while (*name) {
                    169:                h <<= 2;
                    170:                h += *name++;
                    171:        }
                    172:        if (h < 0 && (h = -h) < 0)
                    173:                h = 0;
                    174:        return (h % HSHSIZE);
                    175: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.