Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/cron/lib/env.c
/* Copyright 1988,1990,1993,1994 by Paul Vixie | /* Copyright 1988,1990,1993,1994 by Paul Vixie | ||||
* All rights reserved | * All rights reserved | ||||
*/ | |||||
/* | |||||
* Copyright (c) 1997 by Internet Software Consortium | |||||
* | * | ||||
* Distribute freely, except: don't remove my name from the source or | * Permission to use, copy, modify, and distribute this software for any | ||||
* documentation (don't take credit for my work), mark your changes (don't | * purpose with or without fee is hereby granted, provided that the above | ||||
* get me blamed for your possible bugs), don't alter or remove this | * copyright notice and this permission notice appear in all copies. | ||||
* notice. May be sold if buildable source is provided to buyer. No | |||||
* warrantee of any kind, express or implied, is included with this | |||||
* software; use at your own risk, responsibility for damages (if any) to | |||||
* anyone resulting from the use of this software rests entirely with the | |||||
* user. | |||||
* | * | ||||
* Send bug reports, bug fixes, enhancements, requests, flames, etc., and | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||||
* I'll try to keep a version up to date. I can be reached as follows: | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||||
* Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||||
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |||||
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |||||
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |||||
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |||||
* SOFTWARE. | |||||
*/ | */ | ||||
#if !defined(lint) && !defined(LINT) | #if !defined(lint) && !defined(LINT) | ||||
static const char rcsid[] = | static const char rcsid[] = "$Id: env.c,v 1.3 1998/08/14 00:32:39 vixie Exp $"; | ||||
"$FreeBSD$"; | |||||
#endif | #endif | ||||
#include "cron.h" | #include "cron.h" | ||||
char ** | char ** | ||||
env_init(void) | env_init(void) | ||||
{ | { | ||||
register char **p = (char **) malloc(sizeof(char *)); | char **p = (char **) malloc(sizeof(char **)); | ||||
if (p) | if (p) | ||||
p[0] = NULL; | p[0] = NULL; | ||||
return (p); | return (p); | ||||
} | } | ||||
void | void | ||||
env_free(char **envp) | env_free(char **envp) | ||||
{ | { | ||||
char **p; | char **p; | ||||
if ((p = envp)) | if ((p = envp)) | ||||
for (; *p; p++) | for (; *p; p++) | ||||
free(*p); | free(*p); | ||||
free(envp); | free(envp); | ||||
} | } | ||||
char ** | char ** | ||||
env_copy(char **envp) | env_copy(char **envp) | ||||
{ | { | ||||
register int count, i; | int count, i; | ||||
register char **p; | char **p; | ||||
for (count = 0; envp[count] != NULL; count++) | for (count = 0; envp[count] != NULL; count++) | ||||
; | ; | ||||
p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */ | p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */ | ||||
if (p == NULL) { | if (p == NULL) { | ||||
errno = ENOMEM; | errno = ENOMEM; | ||||
return NULL; | return NULL; | ||||
} | } | ||||
for (i = 0; i < count; i++) | for (i = 0; i < count; i++) | ||||
if ((p[i] = strdup(envp[i])) == NULL) { | if ((p[i] = strdup(envp[i])) == NULL) { | ||||
while (--i >= 0) | while (--i >= 0) | ||||
(void) free(p[i]); | (void) free(p[i]); | ||||
free(p); | free(p); | ||||
errno = ENOMEM; | errno = ENOMEM; | ||||
return NULL; | return NULL; | ||||
} | } | ||||
p[count] = NULL; | p[count] = NULL; | ||||
return (p); | return (p); | ||||
} | } | ||||
char ** | char ** | ||||
env_set(char **envp, char *envstr) | env_set(char **envp, char *envstr) | ||||
{ | { | ||||
register int count, found; | int count, found; | ||||
register char **p; | char **p; | ||||
char *q; | char *q; | ||||
/* | /* | ||||
* count the number of elements, including the null pointer; | * count the number of elements, including the null pointer; | ||||
* also set 'found' to -1 or index of entry if already in here. | * also set 'found' to -1 or index of entry if already in here. | ||||
*/ | */ | ||||
found = -1; | found = -1; | ||||
for (count = 0; envp[count] != NULL; count++) { | for (count = 0; envp[count] != NULL; count++) { | ||||
if (!strcmp_until(envp[count], envstr, '=')) | if (!strcmp_until(envp[count], envstr, '=')) | ||||
▲ Show 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | if (state == VALUE) { | ||||
/* End of unquoted value: trim trailing whitespace */ | /* End of unquoted value: trim trailing whitespace */ | ||||
c = val + strlen (val); | c = val + strlen (val); | ||||
while (c > val && isspace (*(c - 1))) | while (c > val && isspace (*(c - 1))) | ||||
*(--c) = '\0'; | *(--c) = '\0'; | ||||
} | } | ||||
/* 2 fields from parser; looks like an env setting */ | /* 2 fields from parser; looks like an env setting */ | ||||
if (strlen(name) + 1 + strlen(val) >= MAX_ENVSTR-1) | if (snprintf(envstr, MAX_ENVSTR, "%s=%s", name, val) >= MAX_ENVSTR) | ||||
return (FALSE); | return (FALSE); | ||||
(void) sprintf(envstr, "%s=%s", name, val); | |||||
Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr)) | Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr)) | ||||
return (TRUE); | return (TRUE); | ||||
} | } | ||||
char * | char * | ||||
env_get(char *name, char **envp) | env_get(char *name, char **envp) | ||||
{ | { | ||||
register int len = strlen(name); | int len = strlen(name); | ||||
register char *p, *q; | char *p, *q; | ||||
while ((p = *envp++)) { | while ((p = *envp++) != NULL) { | ||||
if (!(q = strchr(p, '='))) | if (!(q = strchr(p, '='))) | ||||
continue; | continue; | ||||
if ((q - p) == len && !strncmp(p, name, len)) | if ((q - p) == len && !strncmp(p, name, len)) | ||||
return (q+1); | return (q+1); | ||||
} | } | ||||
return (NULL); | return (NULL); | ||||
} | } |