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