📄 envlib.c
字号:
( int taskId, /* task to have private environment */ int envSource /* -1 = make an empty private environment */ /* 0 = copy global env to new private env */ /* taskId = copy the specified task's env */ ) { char **ppPrivateEnv; FAST WIND_TCB *pTcb; /* create specified environment and set VX_PRIVATE_ENV in tcb */ switch (envSource) { case (-1): ppPrivateEnv = (char **) calloc (ENV_NENTRIES_TO_ALLOC, sizeof (char *)); if (ppPrivateEnv == NULL) return (ERROR); pTcb = taskTcb (taskId); if (pTcb == NULL) return (ERROR); pTcb->envTblSize = ENV_NENTRIES_TO_ALLOC; pTcb->nEnvVarEntries = 0; pTcb->ppEnviron = ppPrivateEnv; break; case (0): /* duplicate the global environment */ if (envDuplicate (ppGlobalEnviron, envTblSize, nEntries, (WIND_TCB *) taskIdSelf ()) == ERROR) return (ERROR); break; default: /* duplicate the given task's environment */ if ((pTcb = taskTcb (taskId)) == NULL) return (ERROR); if (envDuplicate (pTcb->ppEnviron, pTcb->envTblSize, pTcb->nEnvVarEntries, taskTcb (0)) == ERROR) return (ERROR); } taskOptionsSet (taskId, VX_PRIVATE_ENV, VX_PRIVATE_ENV); return (OK); }/*********************************************************************** envPrivateDestroy - destroy a private environment** This routine destroys a private set of environment variables that were* created with envPrivateCreate(). Calling this routine is unnecessary if* the environment variable task create hook is installed and the task* was spawned with VX_PRIVATE_ENV.* * RETURNS: OK, or ERROR if the task does not exist.** SEE ALSO: envPrivateCreate()*/STATUS envPrivateDestroy ( int taskId /* task with private env to destroy */ ) { FAST WIND_TCB *pTcb; if ((pTcb = taskTcb (taskId)) == NULL) return (ERROR); envDestroy (pTcb->ppEnviron, pTcb->nEnvVarEntries); pTcb->ppEnviron = NULL; taskOptionsSet (taskId, VX_PRIVATE_ENV, 0); return (OK); }/*********************************************************************** putenv - set an environment variable** This routine sets an environment variable to a value by altering an* existing variable or creating a new one. The parameter points to a string* of the form "variableName=value". Unlike the UNIX implementation, the string* passed as a parameter is copied to a private buffer.** RETURNS: OK, or ERROR if space cannot be malloc'd.** SEE ALSO: envLibInit(), getenv()*/STATUS putenv ( char *pEnvString /* string to add to env */ ) { FAST char **ppEnvLine; char **ppThisEnviron; int thisEnvironTblSize; int thisEnvironNEntries; FAST char *pChar = pEnvString; WIND_TCB *pTcb = taskTcb (0); /* find end of environment variable name */ while ((*pChar != ' ') && (*pChar != '\t') && (*pChar != '=') && (*pChar != NULL)) pChar++; ppEnvLine = envFind (pEnvString, pChar - pEnvString); /* new environment variable? */ if (ppEnvLine == NULL) { if (pTcb->ppEnviron == NULL) { /* use global environment */ ppThisEnviron = ppGlobalEnviron; thisEnvironTblSize = envTblSize; thisEnvironNEntries = nEntries; ppEnvLine = &ppGlobalEnviron[nEntries++]; } else { /* use private environment */ ppThisEnviron = pTcb->ppEnviron; thisEnvironTblSize = pTcb->envTblSize; thisEnvironNEntries = pTcb->nEnvVarEntries; ppEnvLine = &pTcb->ppEnviron[pTcb->nEnvVarEntries++]; } /* make more room in environ if necessary */ if (thisEnvironTblSize == thisEnvironNEntries) { FAST char **environBuf; environBuf = (char **) realloc ((char *) ppThisEnviron, (unsigned ) (thisEnvironTblSize + ENV_NENTRIES_TO_ALLOC) * sizeof (char *)); if (environBuf == NULL) return (ERROR); /* zero out the new area */ bzero ((char *) &(environBuf[thisEnvironTblSize]), sizeof (char *) * ENV_NENTRIES_TO_ALLOC); if (pTcb->ppEnviron == NULL) { ppGlobalEnviron = environBuf; envTblSize += ENV_NENTRIES_TO_ALLOC; } else { pTcb->ppEnviron = environBuf; pTcb->envTblSize += ENV_NENTRIES_TO_ALLOC; } ppEnvLine = &environBuf[thisEnvironNEntries]; } } else { /* free the string for the given environment variable */ free (*ppEnvLine); } /* allocate memory to hold a copy of the string */ *ppEnvLine = (char *) malloc ((unsigned) strlen (pEnvString) + 1); strcpy (*ppEnvLine, pEnvString); return (OK); }/*********************************************************************** getenv - get an environment variable (ANSI)** This routine searches the environment list (see the UNIX BSD 4.3 manual* entry for \f3environ(5V)\f1) for a string of the form "name=value" and* returns the value portion of the string, if the string is present;* otherwise it returns a NULL pointer.** RETURNS: A pointer to the string value, or a NULL pointer.** SEE ALSO: envLibInit(), putenv(), 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)"*/char *getenv ( FAST const char *name /* env variable to get value for */ ) { FAST char **pEnvLine = envFind (CHAR_FROM_CONST (name), strlen (name)); FAST char *pValue; if (pEnvLine == NULL) return (NULL); /* advance past the '=' and any white space */ pValue = *pEnvLine + strlen (name); while ((*pValue == ' ') || (*pValue == '\t') || (*pValue == '=')) pValue++; return (pValue); }/*********************************************************************** envFind - find an environment variable**/LOCAL char **envFind ( FAST char *name, FAST int nameLen ) { FAST char **envVar; WIND_TCB *pTcb = taskTcb (0); FAST int i; FAST int nEnvEntries; FAST char endChar; if (pTcb->ppEnviron == NULL) { envVar = ppGlobalEnviron; nEnvEntries = nEntries; } else { envVar = pTcb->ppEnviron; nEnvEntries = pTcb->nEnvVarEntries; } for (i = 0; i < nEnvEntries; envVar++, i++) if (strncmp (name, *envVar, nameLen) == 0) { /* make sure it's not just a substring */ endChar = (*envVar)[nameLen]; if ((endChar == ' ') || (endChar == '\t') || (endChar == '=') || (endChar == NULL)) return (envVar); } return (NULL); /* not found */ }/*********************************************************************** envShow - display the environment for a task** This routine prints to standard output all the environment variables for a* specified task. If <taskId> is NULL, then the calling task's environment* is displayed.** RETURNS: N/A*/void envShow ( int taskId /* task for which environment is printed */ ) { FAST char **ppEnvVar; FAST int i; FAST int nEnvEntries; WIND_TCB *pTcb = taskTcb (taskId); if (pTcb->ppEnviron == NULL) { printf ("(global environment)\n"); ppEnvVar = ppGlobalEnviron; nEnvEntries = nEntries; } else { printf ("(private environment)\n"); ppEnvVar = pTcb->ppEnviron; nEnvEntries = pTcb->nEnvVarEntries; } for (i = 0; i < nEnvEntries; ppEnvVar++, i++) printf ("%d: %s\n", i, *ppEnvVar); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -