/* $OpenBSD: env.c,v 1.13 2002/07/11 20:15:40 millert Exp $ */ /* Copyright 1988,1990,1993,1994 by Paul Vixie * All rights reserved */ /* * Copyright (c) 1997,2000 by Internet Software Consortium, Inc. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES * 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) static char const rcsid[] = "$OpenBSD: env.c,v 1.13 2002/07/11 20:15:40 millert Exp $"; #endif #include "cron.h" char ** env_init(void) { char **p = (char **) malloc(sizeof(char **)); if (p != NULL) p[0] = NULL; return (p); } void env_free(char **envp) { char **p; for (p = envp; *p != NULL; p++) free(*p); free(envp); } char ** env_copy(char **envp) { int count, i, save_errno; char **p; for (count = 0; envp[count] != NULL; count++) continue; p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */ if (p != NULL) { for (i = 0; i < count; i++) if ((p[i] = strdup(envp[i])) == NULL) { save_errno = errno; while (--i >= 0) free(p[i]); free(p); errno = save_errno; return (NULL); } p[count] = NULL; } return (p); } char ** env_set(char **envp, char *envstr) { int count, found; char **p, *envtmp; /* * count the number of elements, including the null pointer; * also set 'found' to -1 or index of entry if already in here. */ found = -1; for (count = 0; envp[count] != NULL; count++) { if (!strcmp_until(envp[count], envstr, '=')) found = count; } count++; /* for the NULL */ if (found != -1) { /* * it exists already, so just free the existing setting, * save our new one there, and return the existing array. */ if ((envtmp = strdup(envstr)) == NULL) return (NULL); free(envp[found]); envp[found] = envtmp; return (envp); } /* * it doesn't exist yet, so resize the array, move null pointer over * one, save our string over the old null pointer, and return resized * array. */ if ((envtmp = strdup(envstr)) == NULL) return (NULL); p = (char **) realloc((void *) envp, (size_t) ((count+1) * sizeof(char **))); if (p == NULL) { free(envtmp); return (NULL); } p[count] = p[count-1]; p[count-1] = envtmp; return (p); } /* return ERR = end of file * FALSE = not an env setting (file was repositioned) * TRUE = was an env setting */ int load_env(char *envstr, FILE *f) { long filepos; int fileline; char name[MAX_ENVSTR], val[MAX_ENVSTR]; int fields; filepos = ftell(f); fileline = LineNumber; skip_comments(f); if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n")) return (ERR); Debug(DPARS, ("load_env, read <%s>\n", envstr)) name[0] = val[0] = '\0'; fields = sscanf(envstr, "%[^ =] = %[^\n#]", name, val); if (fields != 2) { Debug(DPARS, ("load_env, not 2 fields (%d)\n", fields)) fseek(f, filepos, 0); Set_LineNum(fileline); return (FALSE); } /* * 2 fields from scanf; looks like an env setting. */ /* * process value string */ /*local*/{ int len = strdtb(val); if (len >= 2) { if (val[0] == '\'' || val[0] == '"') { if (val[len-1] == val[0]) { val[len-1] = '\0'; memmove(val, val+1, len); } } } } /* * This can't overflow because get_string() limited the size of the * name and val fields. Still, it doesn't hurt... */ (void) snprintf(envstr, MAX_ENVSTR, "%s=%s", name, val); Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr)) return (TRUE); } char * env_get(char *name, char **envp) { int len = strlen(name); char *p, *q; while ((p = *envp++) != NULL) { if (!(q = strchr(p, '='))) continue; if ((q - p) == len && !strncmp(p, name, len)) return (q+1); } return (NULL); }