]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/server/common.c
b668ef8e7052dd91ad7c672866a5b5e1dfe6f751
[1ts-debian.git] / zephyr / server / common.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains functions for general use within the Zephyr server.
3  *
4  *      Created by:     John T. Kohl
5  *
6  *      $Id$
7  *
8  *      Copyright (c) 1987 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #include <zephyr/mit-copyright.h>
14 #include "zserver.h"
15
16 #ifndef lint
17 #ifndef SABER
18 static const char rcsid_common_c[] =
19     "$Id$";
20 #endif /* SABER */
21 #endif /* lint */
22
23 /* common routines for the server */
24
25 /* copy a string into a newly allocated area */
26
27 char *
28 strsave (sp)
29     const char *sp;
30 {
31     char *ret;
32
33     ret = (char *) malloc(strlen(sp) + 1);
34     if (!ret) {
35         syslog(LOG_CRIT, "no mem strdup'ing");
36         abort();
37     }
38     strcpy(ret, sp);
39     return ret;
40 }
41
42 /* The "& 0x5f" provides case-insensitivity for ASCII. */
43
44 unsigned long
45 hash(string)
46     const char *string;
47 {
48     unsigned long hval = 0;
49     char cp;
50
51     while (1) {
52         cp = *string++;
53         if (!cp)
54             break;
55         hval += cp & 0x5f;
56
57         cp = *string++;
58         if (!cp)
59             break;
60         hval += (cp & 0x5f) * (3 + (1 << 16));
61
62         cp = *string++;
63         if (!cp)
64             break;
65         hval += (cp & 0x5f) * (1 + (1 << 8));
66
67         cp = *string++;
68         if (!cp)
69             break;
70         hval += (cp & 0x5f) * (1 + (1 << 12));
71
72         cp = *string++;
73         if (!cp)
74             break;
75         hval += (cp & 0x5f) * (1 + (1 << 4));
76
77         hval += ((long) hval) >> 18;
78     }
79
80     hval &= 0x7fffffff;
81     return hval;
82 }
83
84 /* Output a name, replacing newlines with \n and single quotes with \q. */
85 void dump_quote(p, fp)
86     char *p;
87     FILE *fp;
88 {
89     for (; *p; p++) {
90         if (*p == '\'') {
91             putc('\\', fp);
92             putc('q', fp);
93         } else if (*p == '\n') {
94             putc('\\', fp);
95             putc('n', fp);
96         } else {
97             putc(*p, fp);
98         }
99     }
100 }
101