]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/server/common.c
r4262@bucket (orig r252): kcr | 2008-01-20 22:11:00 -0500
[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 (const char *sp)
29 {
30     char *ret;
31
32     ret = strdup(sp);
33     if (!ret) {
34         syslog(LOG_CRIT, "no mem strdup'ing");
35         abort();
36     }
37     return ret;
38 }
39
40 /* The "& 0x5f" provides case-insensitivity for ASCII. */
41
42 unsigned long
43 hash(const char *string)
44 {
45     unsigned long hval = 0;
46     char cp;
47
48     while (1) {
49         cp = *string++;
50         if (!cp)
51             break;
52         hval += cp & 0x5f;
53
54         cp = *string++;
55         if (!cp)
56             break;
57         hval += (cp & 0x5f) * (3 + (1 << 16));
58
59         cp = *string++;
60         if (!cp)
61             break;
62         hval += (cp & 0x5f) * (1 + (1 << 8));
63
64         cp = *string++;
65         if (!cp)
66             break;
67         hval += (cp & 0x5f) * (1 + (1 << 12));
68
69         cp = *string++;
70         if (!cp)
71             break;
72         hval += (cp & 0x5f) * (1 + (1 << 4));
73
74         hval += ((long) hval) >> 18;
75     }
76
77     hval &= 0x7fffffff;
78     return hval;
79 }
80
81 /* Output a name, replacing newlines with \n and single quotes with \q. */
82 void dump_quote(char *p, FILE *fp)
83 {
84     for (; *p; p++) {
85         if (*p == '\'') {
86             putc('\\', fp);
87             putc('q', fp);
88         } else if (*p == '\n') {
89             putc('\\', fp);
90             putc('n', fp);
91         } else {
92             putc(*p, fp);
93         }
94     }
95 }
96