Commit | Line | Data |
---|---|---|
800ffe89 NW |
1 | /*- |
2 | * Copyright (c) 1993 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Christoph Robitschko. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. All advertising materials mentioning features or use of this software | |
17 | * must display the following acknowledgement: | |
18 | * This product includes software developed by the University of | |
19 | * California, Berkeley and its contributors. | |
20 | * 4. Neither the name of the University nor the names of its contributors | |
21 | * may be used to endorse or promote products derived from this software | |
22 | * without specific prior written permission. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
34 | * SUCH DAMAGE. | |
35 | */ | |
36 | ||
37 | /* | |
38 | * utils.c | |
39 | * Misc. utility functions | |
40 | */ | |
41 | ||
42 | #include <unistd.h> | |
43 | #include <stdlib.h> | |
44 | #include <ctype.h> | |
45 | #include <string.h> | |
46 | #include <stdio.h> | |
47 | #include <stdarg.h> | |
48 | #include <syslog.h> | |
49 | #include <sys/types.h> | |
50 | #include <sys/signal.h> | |
51 | #include "init.h" | |
52 | #include "prototypes.h" | |
53 | ||
54 | ||
55 | /* global variable */ | |
56 | char **ienviron; | |
57 | ||
58 | ||
59 | /* | |
60 | * IPUTENV | |
61 | * put an environment variable in a separate space | |
62 | */ | |
63 | void | |
64 | iputenv(var, val) | |
65 | const char *var, *val; | |
66 | { | |
67 | static int ienvalloc = 0; | |
68 | char *plaza; | |
69 | char **ienv; | |
70 | int ienvcount; | |
71 | int varlen; | |
72 | ||
73 | ||
74 | varlen = strlen(var) + 1; /* VAR= */ | |
75 | plaza = malloc(varlen + strlen(val) + 1); | |
76 | if (!plaza) | |
77 | return; /* fail miserably */ | |
78 | sprintf(plaza, "%s=%s", var, val); | |
79 | ||
80 | ||
81 | if (!ienvalloc) { | |
82 | ienviron = malloc(4*sizeof(char *)); | |
83 | if (!ienviron) return; | |
84 | ienvalloc = 4; | |
85 | *ienviron = (char *)0; | |
86 | } | |
87 | ||
88 | /* Search ienviron if variable is already defined */ | |
89 | for (ienvcount = 0, ienv = ienviron; *ienv; ienv++, ienvcount++) | |
90 | if (!strncmp(*ienv, plaza, varlen)) { | |
91 | free(*ienv); | |
92 | *ienv = plaza; | |
93 | return; | |
94 | } | |
95 | ||
96 | /* Not found, create new environ entry */ | |
97 | if (ienvcount >= ienvalloc) | |
98 | if (( ienviron = realloc (ienviron, (ienvalloc + 4)* sizeof (char *)))) | |
99 | ienvalloc += 4; | |
100 | else | |
101 | return; /* kaaplotz */ | |
102 | ||
103 | for (ienv = ienviron; *ienv; ienv++); | |
104 | *ienv = plaza; | |
105 | *(++ienv) = (char *)0; | |
106 | return; | |
107 | } | |
108 | ||
109 | ||
110 | ||
111 | /* | |
112 | * dEBUG | |
113 | * print a message if current debug level > log level | |
114 | */ | |
115 | void | |
116 | Debug(int level, const char *format, ...) | |
117 | { | |
118 | #ifdef DEBUG | |
119 | va_list args; | |
120 | ||
121 | ||
122 | if (level > debug) | |
123 | return; | |
124 | va_start(args, format); | |
125 | vsyslog(LOG_DEBUG, format, args); | |
126 | va_end(args); | |
127 | #endif | |
128 | } | |
129 | ||
130 | ||
131 | #undef toupper | |
132 | /* | |
133 | * STRcCMP | |
134 | * see if s2 is the beginning of s1, ignoring case | |
135 | */ | |
136 | int | |
137 | strCcmp(s1, s2) /* strncasecmp(s1, s2, strlen(s1)) */ | |
138 | char *s1, *s2; | |
139 | { | |
140 | ||
141 | ||
142 | for (;*s1; s1++, s2++) | |
143 | if (toupper(*s1) != toupper(*s2)) | |
144 | return(1); | |
145 | return(0); | |
146 | } | |
147 | ||
148 | ||
149 | ||
150 | /* | |
151 | * NEWSTRING | |
152 | * allocate memory for a string and copy it. | |
153 | */ | |
154 | char * | |
155 | newstring(string) | |
156 | const char *string; | |
157 | { | |
158 | char *s; | |
159 | ||
160 | s = (char *)malloc(strlen(string)+1); | |
161 | if (s) | |
162 | strcpy(s, string); | |
163 | return(s); | |
164 | } | |
165 | ||
166 | ||
167 | ||
168 | /* | |
169 | * STR2U | |
170 | * atoi with error handling. errors are signalled with a negative return value | |
171 | */ | |
172 | long | |
173 | str2u(str) | |
174 | const char *str; | |
175 | { | |
176 | char *s; | |
177 | long res; | |
178 | ||
179 | res = strtol(str, &s, 0); | |
180 | if ((s != str) && !*s) | |
181 | return(res); | |
182 | return(-1); | |
183 | } |