?? envlib.c
字號:
/* envLib.c - environment variable library *//* Copyright 1984-1993 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01k,22jan93,jdi documentation cleanup for 5.1.01j,19jul92,smb added some ANSI documentation. made getenv parameter list ANSI.01i,26may92,rrr the tree shuffle01h,19nov91,rrr shut up some ansi warnings.01g,04oct91,rrr passed through the ansification filter -changed functions to ansi style -changed includes to have absolute path from h/ -changed VOID to void -changed copyright notice01f,10aug90,kdl added forward declaration for envDestroy.01e,01aug90,jcf cleanup.01d,30jul90,rdc ick, more lint.01c,18jul90,rdc lint.01b,15jul90,dnw fixed to coerce malloc() to (char*) where necessary01a,12jul90,rdc written.*//*This library provides a UNIX-compatible environment variable facility. Environment variables are created or modified with a call to putenv():.CS putenv ("variableName=value");.CEThe value of a variable may be retrieved with a call to getenv(), which returnsa pointer to the value string.Tasks may share a common set of environment variables, or they may optionallycreate their own private environments, either automatically when the taskcreate hook is installed, or by an explicit call to envPrivateCreate().The task must be spawned with the VX_PRIVATE_ENV option set to receive aprivate set of environment variables. Private environments created by thetask creation hook inherit the values of the environment of the task that calledtaskSpawn() (since task create hooks run in the context of the calling task).INCLUDE FILES: envLib.hSEE ALSO: UNIX BSD 4.3 manual entry for \f3environ(5V)\f1,* .I "American National Standard for Information Systems -"* .I "Programming Language - C, ANSI X3.159-1989: General Utilities (stdlib.h)"*//* LINTLIBRARY */#include "vxWorks.h"#include "taskLib.h"#include "taskHookLib.h"#include "string.h"#include "stdio.h"#include "stdlib.h"#include "logLib.h"#define ENV_NENTRIES_TO_ALLOC 50/* globals */char **ppGlobalEnviron; /* global envivonment variable table *//* locals */LOCAL int envTblSize; /* global table size */LOCAL int nEntries; /* number of entries in global table *//* forward static functions */static void envCreateHook (WIND_TCB *pNewTcb);static STATUS envDuplicate (char ** srcEnv, int srcEnvTblSize, int srcNEnvVarEntries, WIND_TCB *pTcb);static void envDestroy (char ** ppEnv, int nEnvVarEntries);static void envDeleteHook (WIND_TCB *pTcb);static char ** envFind (char *name, int nameLen);/*********************************************************************** envLibInit - initialize environment variable facility** If <installHooks> is TRUE, task create and delete hooks are installed that* will optionally create and destroy private environments for the task being* created or destroyed, depending on the state of VX_PRIVATE_ENV in the task* options word. If <installHooks> is FALSE and a task requires a private* environment, it is the application's responsibility to create and destroy* the private environment, using envPrivateCreate() and envPrivateDestroy().** RETURNS: OK, or ERROR if an environment cannot be allocated or the hooks* cannot be installed.*/STATUS envLibInit ( BOOL installHooks ) { /* allocate the global environ array */ ppGlobalEnviron = (char **) calloc (ENV_NENTRIES_TO_ALLOC, sizeof (char *)); if (ppGlobalEnviron == NULL) return (ERROR); envTblSize = ENV_NENTRIES_TO_ALLOC; nEntries = 0; /* optionally install task create/delete hooks */ if (installHooks) { if(taskCreateHookAdd ((FUNCPTR)envCreateHook) != OK) return (ERROR); if (taskDeleteHookAdd ((FUNCPTR)envDeleteHook) != OK) return (ERROR); } return (OK); }/*********************************************************************** envCreateHook - create a private environment for the new task** installed by envLibInit to optionally create a private environment* for tasks that have VX_PRIVATE_ENV set in their task options word.*/LOCAL void envCreateHook ( WIND_TCB *pNewTcb ) { char ** ppSrcEnviron; int srcEnvTblSize; int srcNEnvVarEntries; WIND_TCB * pMyTcb; /* if VX_PRIVATE_ENV is set, copy spawning task's environment to a private environment and stash a pointer to it in the new task's tcb */ if (pNewTcb->options & VX_PRIVATE_ENV) { pMyTcb = taskTcb (0); if ((ppSrcEnviron = pMyTcb->ppEnviron) == NULL) { ppSrcEnviron = ppGlobalEnviron; srcEnvTblSize = envTblSize; srcNEnvVarEntries = nEntries; } else { srcEnvTblSize = pMyTcb->envTblSize; srcNEnvVarEntries = pMyTcb->nEnvVarEntries; } /* allocate the private environ array - make it the same size * as the "current" environ array. */ if (envDuplicate (ppSrcEnviron, srcEnvTblSize, srcNEnvVarEntries, pNewTcb) == ERROR) { logMsg ("evnCreateHook: couldn't create private environment!\n", 0,0,0,0,0,0); /* turn VX_PRIVATE_ENV off in options word so we don't deallocate * an environment that doesn't exist when the task exits. */ taskOptionsSet ((int) pNewTcb, VX_PRIVATE_ENV, 0); return; } } else { /* set the environment pointer in the tcb to NULL so the global * environment will be used. */ pNewTcb->ppEnviron = NULL; } }/*********************************************************************** envDuplicate - duplicate the given environment** create a new environment and copy the information in the given* source environment.** RETURNS: OK, or ERROR if not enough memory*/LOCAL STATUS envDuplicate ( FAST char **srcEnv, /* environment table to duplicate */ int srcEnvTblSize, /* table size */ int srcNEnvVarEntries, /* N entries in use */ WIND_TCB *pTcb /* tcb of task to receive duplicate env */ ) { FAST char **privateEnv; FAST int i; privateEnv = (char **) calloc ((unsigned) srcEnvTblSize, sizeof (char *)); if (privateEnv == (char **) NULL) return (ERROR); pTcb->envTblSize = srcEnvTblSize; pTcb->nEnvVarEntries = 0; pTcb->ppEnviron = privateEnv; /* copy the source environment to the new environment */ for (i = 0; i < srcNEnvVarEntries; i++) { FAST char *destString; FAST char *srcString; srcString = srcEnv[i]; destString = (char *) malloc ((unsigned) strlen (srcString) + 1); if (destString == NULL) { envDestroy (privateEnv, pTcb->nEnvVarEntries); return (ERROR); } strcpy (destString, srcString); privateEnv[i] = destString; pTcb->nEnvVarEntries++; } return (OK); }/*********************************************************************** envDestroy - free the given environment** free all the strings and data structures associated with an* environment.*/LOCAL void envDestroy ( char **ppEnv, int nEnvVarEntries ) { FAST int i; for (i = 0; i < nEnvVarEntries; i++) { free (ppEnv[i]); } /* deallocate the env array */ free ((char *) ppEnv); }/*********************************************************************** envDeleteHook - delete a task's private environment** installed by envLibInit to optionally delete private environments* when a task exits.*/LOCAL void envDeleteHook ( WIND_TCB *pTcb /* pointer to deleted task's TCB */ ) { /* if VX_PRIVATE_ENV is set, destroy the environment */ if (pTcb->options & VX_PRIVATE_ENV) if (pTcb->ppEnviron != NULL) envDestroy (pTcb->ppEnviron, pTcb->nEnvVarEntries); }/*********************************************************************** envPrivateCreate - create a private environment** This routine creates a private set of environment variables for a specified* task, if the environment variable task create hook is not installed.** RETURNS: OK, or ERROR if memory is insufficient.** SEE ALSO: envLibInit(), envPrivateDestroy()*/STATUS envPrivateCreate
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -