[1271] | 1 | /* $Id: common-env-and-cwd-opt.c 3332 2020-04-19 23:08:16Z bird $ */
|
---|
| 2 | /** @file
|
---|
[2912] | 3 | * kMk Builtin command - Commmon environment and CWD option handling code.
|
---|
[1527] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[2812] | 7 | * Copyright (c) 2007-2016 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
|
---|
[1271] | 8 | *
|
---|
| 9 | * This file is part of kBuild.
|
---|
| 10 | *
|
---|
| 11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
| 12 | * it under the terms of the GNU General Public License as published by
|
---|
[2019] | 13 | * the Free Software Foundation; either version 3 of the License, or
|
---|
[1271] | 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * kBuild is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
[2019] | 22 | * along with kBuild. If not, see <http://www.gnu.org/licenses/>
|
---|
[1271] | 23 | *
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | /*******************************************************************************
|
---|
| 27 | * Header Files *
|
---|
| 28 | *******************************************************************************/
|
---|
[2912] | 29 | #include "config.h"
|
---|
[1271] | 30 | #include <stdio.h>
|
---|
| 31 | #include <stdlib.h>
|
---|
| 32 | #include <string.h>
|
---|
[3173] | 33 | #include <assert.h>
|
---|
[1271] | 34 |
|
---|
[2840] | 35 | #include "kmkbuiltin.h"
|
---|
| 36 | #include "err.h"
|
---|
| 37 |
|
---|
[1271] | 38 |
|
---|
[2877] | 39 | /** The environment variable compare function.
|
---|
| 40 | * We must use case insensitive compare on windows (Path vs PATH). */
|
---|
| 41 | #ifdef KBUILD_OS_WINDOWS
|
---|
| 42 | # define KSUBMIT_ENV_NCMP _strnicmp
|
---|
| 43 | #else
|
---|
| 44 | # define KSUBMIT_ENV_NCMP strncmp
|
---|
| 45 | #endif
|
---|
[2843] | 46 |
|
---|
[2877] | 47 |
|
---|
[2843] | 48 | /**
|
---|
[3173] | 49 | * Duplicates a read-only enviornment vector.
|
---|
| 50 | *
|
---|
| 51 | * @returns The duplicate enviornment.
|
---|
[3192] | 52 | * @param pCtx The built-in command context.
|
---|
[3173] | 53 | * @param papszEnv The read-only vector.
|
---|
| 54 | * @param cEnvVars The number of variables.
|
---|
| 55 | * @param pcAllocatedEnvVars The allocated papszEnv size. This is zero on
|
---|
| 56 | * input and non-zero on successful return.
|
---|
| 57 | * @param cVerbosity The verbosity level.
|
---|
| 58 | */
|
---|
[3192] | 59 | static char **kBuiltinOptEnvDuplicate(PKMKBUILTINCTX pCtx, char **papszEnv, unsigned cEnvVars, unsigned *pcAllocatedEnvVars,
|
---|
| 60 | int cVerbosity)
|
---|
[3173] | 61 | {
|
---|
[3177] | 62 | unsigned cAllocatedEnvVars = (cEnvVars + 2 + 0xf) & ~(unsigned)0xf;
|
---|
[3173] | 63 | char **papszEnvNew = malloc(cAllocatedEnvVars * sizeof(papszEnvNew[0]));
|
---|
| 64 | assert(*pcAllocatedEnvVars == 0);
|
---|
| 65 | if (papszEnvNew)
|
---|
| 66 | {
|
---|
| 67 | unsigned i;
|
---|
| 68 | for (i = 0; i < cEnvVars; i++)
|
---|
| 69 | {
|
---|
| 70 | papszEnvNew[i] = strdup(papszEnv[i]);
|
---|
| 71 | if (!papszEnvNew)
|
---|
| 72 | {
|
---|
| 73 | while (i-- > 0)
|
---|
| 74 | free(papszEnvNew[i]);
|
---|
| 75 | free(papszEnvNew);
|
---|
[3192] | 76 | errx(pCtx, 1, "out of memory for duplicating environment variables!", i);
|
---|
[3173] | 77 | return NULL;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | papszEnvNew[i] = NULL;
|
---|
| 81 | *pcAllocatedEnvVars = cAllocatedEnvVars;
|
---|
| 82 | }
|
---|
| 83 | else
|
---|
[3192] | 84 | errx(pCtx, 1, "out of memory for duplicating environment vector!");
|
---|
[3173] | 85 | return papszEnvNew;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
[3039] | 90 | * Common worker for kBuiltinOptEnvSet and kBuiltinOptEnvAppendPrepend that adds
|
---|
| 91 | * a new variable to the environment.
|
---|
[2841] | 92 | *
|
---|
| 93 | * @returns 0 on success, non-zero exit code on error.
|
---|
[3192] | 94 | * @param pCtx The built-in command context.
|
---|
[2841] | 95 | * @param papszEnv The environment vector.
|
---|
| 96 | * @param pcEnvVars Pointer to the variable holding the number of
|
---|
| 97 | * environment variables held by @a papszEnv.
|
---|
| 98 | * @param pcAllocatedEnvVars Pointer to the variable holding max size of the
|
---|
| 99 | * environment vector.
|
---|
| 100 | * @param cVerbosity The verbosity level.
|
---|
| 101 | * @param pszValue The var=value string to apply.
|
---|
| 102 | */
|
---|
[3192] | 103 | static int kBuiltinOptEnvAddVar(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,
|
---|
[3039] | 104 | int cVerbosity, const char *pszValue)
|
---|
| 105 | {
|
---|
| 106 | /* Append new variable. We probably need to resize the vector. */
|
---|
| 107 | char **papszEnv = *ppapszEnv;
|
---|
| 108 | unsigned cEnvVars = *pcEnvVars;
|
---|
| 109 | if ((cEnvVars + 2) > *pcAllocatedEnvVars)
|
---|
| 110 | {
|
---|
| 111 | *pcAllocatedEnvVars = (cEnvVars + 2 + 0xf) & ~(unsigned)0xf;
|
---|
| 112 | papszEnv = (char **)realloc(papszEnv, *pcAllocatedEnvVars * sizeof(papszEnv[0]));
|
---|
| 113 | if (!papszEnv)
|
---|
[3192] | 114 | return errx(pCtx, 1, "out of memory growing environment vector!");
|
---|
[3039] | 115 | *ppapszEnv = papszEnv;
|
---|
| 116 | }
|
---|
| 117 | papszEnv[cEnvVars] = strdup(pszValue);
|
---|
| 118 | if (!papszEnv[cEnvVars])
|
---|
[3192] | 119 | return errx(pCtx, 1, "out of memory adding environment variable!");
|
---|
[3039] | 120 | papszEnv[++cEnvVars] = NULL;
|
---|
| 121 | *pcEnvVars = cEnvVars;
|
---|
| 122 | if (cVerbosity > 0)
|
---|
[3192] | 123 | warnx(pCtx, "added '%s'", papszEnv[cEnvVars - 1]);
|
---|
[3039] | 124 | return 0;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
| 129 | * Common worker for kBuiltinOptEnvSet and kBuiltinOptEnvAppendPrepend that
|
---|
| 130 | * remove duplicates.
|
---|
| 131 | *
|
---|
| 132 | * @returns 0 on success, non-zero exit code on error.
|
---|
[3192] | 133 | * @param pCtx The built-in command context.
|
---|
[3039] | 134 | * @param papszEnv The environment vector.
|
---|
| 135 | * @param cEnvVars Number of environment variables.
|
---|
| 136 | * @param cVerbosity The verbosity level.
|
---|
| 137 | * @param pszValue The var=value string to apply.
|
---|
| 138 | * @param cchVar The length of the variable part of @a pszValue.
|
---|
| 139 | * @param iEnvVar Where to start searching after.
|
---|
| 140 | */
|
---|
[3192] | 141 | static int kBuiltinOptEnvRemoveDuplicates(PKMKBUILTINCTX pCtx, char **papszEnv, unsigned cEnvVars, int cVerbosity,
|
---|
[3039] | 142 | const char *pszValue, size_t cchVar, unsigned iEnvVar)
|
---|
| 143 | {
|
---|
| 144 | for (iEnvVar++; iEnvVar < cEnvVars; iEnvVar++)
|
---|
| 145 | if ( KSUBMIT_ENV_NCMP(papszEnv[iEnvVar], pszValue, cchVar) == 0
|
---|
| 146 | && papszEnv[iEnvVar][cchVar] == '=')
|
---|
| 147 | {
|
---|
| 148 | if (cVerbosity > 0)
|
---|
[3192] | 149 | warnx(pCtx, "removing duplicate '%s'", papszEnv[iEnvVar]);
|
---|
[3039] | 150 | free(papszEnv[iEnvVar]);
|
---|
| 151 | cEnvVars--;
|
---|
| 152 | if (iEnvVar != cEnvVars)
|
---|
| 153 | papszEnv[iEnvVar] = papszEnv[cEnvVars];
|
---|
| 154 | papszEnv[cEnvVars] = NULL;
|
---|
| 155 | iEnvVar--;
|
---|
| 156 | }
|
---|
| 157 | return 0;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
| 162 | * Handles the --set var=value option.
|
---|
| 163 | *
|
---|
| 164 | * @returns 0 on success, non-zero exit code on error.
|
---|
[3192] | 165 | * @param pCtx The built-in command context.
|
---|
[3039] | 166 | * @param ppapszEnv The environment vector pointer.
|
---|
| 167 | * @param pcEnvVars Pointer to the variable holding the number of
|
---|
| 168 | * environment variables held by @a papszEnv.
|
---|
| 169 | * @param pcAllocatedEnvVars Pointer to the variable holding max size of the
|
---|
| 170 | * environment vector.
|
---|
| 171 | * @param cVerbosity The verbosity level.
|
---|
| 172 | * @param pszValue The var=value string to apply.
|
---|
| 173 | */
|
---|
[3192] | 174 | int kBuiltinOptEnvSet(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,
|
---|
| 175 | int cVerbosity, const char *pszValue)
|
---|
[2841] | 176 | {
|
---|
| 177 | const char *pszEqual = strchr(pszValue, '=');
|
---|
| 178 | if (pszEqual)
|
---|
| 179 | {
|
---|
[2875] | 180 | char **papszEnv = *ppapszEnv;
|
---|
[2841] | 181 | unsigned iEnvVar;
|
---|
| 182 | unsigned cEnvVars = *pcEnvVars;
|
---|
[2877] | 183 | size_t const cchVar = pszEqual - pszValue;
|
---|
[3173] | 184 |
|
---|
| 185 | if (!*pcAllocatedEnvVars)
|
---|
| 186 | {
|
---|
[3192] | 187 | papszEnv = kBuiltinOptEnvDuplicate(pCtx, papszEnv, cEnvVars, pcAllocatedEnvVars, cVerbosity);
|
---|
[3173] | 188 | if (!papszEnv)
|
---|
[3192] | 189 | return errx(pCtx, 1, "out of memory duplicating enviornment (setenv)!");
|
---|
[3173] | 190 | *ppapszEnv = papszEnv;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[2841] | 193 | for (iEnvVar = 0; iEnvVar < cEnvVars; iEnvVar++)
|
---|
[2877] | 194 | {
|
---|
| 195 | char *pszCur = papszEnv[iEnvVar];
|
---|
| 196 | if ( KSUBMIT_ENV_NCMP(pszCur, pszValue, cchVar) == 0
|
---|
| 197 | && pszCur[cchVar] == '=')
|
---|
[2841] | 198 | {
|
---|
| 199 | if (cVerbosity > 0)
|
---|
[3192] | 200 | warnx(pCtx, "replacing '%s' with '%s'", papszEnv[iEnvVar], pszValue);
|
---|
[2841] | 201 | free(papszEnv[iEnvVar]);
|
---|
[2912] | 202 | papszEnv[iEnvVar] = strdup(pszValue);
|
---|
| 203 | if (!papszEnv[iEnvVar])
|
---|
[3192] | 204 | return errx(pCtx, 1, "out of memory for modified environment variable!");
|
---|
[3039] | 205 |
|
---|
[3192] | 206 | return kBuiltinOptEnvRemoveDuplicates(pCtx, papszEnv, cEnvVars, cVerbosity, pszValue, cchVar, iEnvVar);
|
---|
[2841] | 207 | }
|
---|
[2877] | 208 | }
|
---|
[3192] | 209 | return kBuiltinOptEnvAddVar(pCtx, ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue);
|
---|
[3039] | 210 | }
|
---|
[3192] | 211 | return errx(pCtx, 1, "Missing '=': -E %s", pszValue);
|
---|
[3039] | 212 | }
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 | /**
|
---|
| 216 | * Common worker for kBuiltinOptEnvAppend and kBuiltinOptEnvPrepend.
|
---|
| 217 | *
|
---|
| 218 | * @returns 0 on success, non-zero exit code on error.
|
---|
[3192] | 219 | * @param pCtx The built-in command context.
|
---|
[3039] | 220 | * @param ppapszEnv The environment vector pointer.
|
---|
| 221 | * @param pcEnvVars Pointer to the variable holding the number of
|
---|
| 222 | * environment variables held by @a papszEnv.
|
---|
| 223 | * @param pcAllocatedEnvVars Pointer to the variable holding max size of the
|
---|
| 224 | * environment vector.
|
---|
| 225 | * @param cVerbosity The verbosity level.
|
---|
| 226 | * @param pszValue The var=value string to apply.
|
---|
| 227 | */
|
---|
[3192] | 228 | static int kBuiltinOptEnvAppendPrepend(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,
|
---|
[3039] | 229 | int cVerbosity, const char *pszValue, int fAppend)
|
---|
| 230 | {
|
---|
| 231 | const char *pszEqual = strchr(pszValue, '=');
|
---|
| 232 | if (pszEqual)
|
---|
| 233 | {
|
---|
| 234 | char **papszEnv = *ppapszEnv;
|
---|
| 235 | unsigned iEnvVar;
|
---|
| 236 | unsigned cEnvVars = *pcEnvVars;
|
---|
| 237 | size_t const cchVar = pszEqual - pszValue;
|
---|
[3173] | 238 |
|
---|
| 239 | if (!*pcAllocatedEnvVars)
|
---|
| 240 | {
|
---|
[3192] | 241 | papszEnv = kBuiltinOptEnvDuplicate(pCtx, papszEnv, cEnvVars, pcAllocatedEnvVars, cVerbosity);
|
---|
[3173] | 242 | if (!papszEnv)
|
---|
[3192] | 243 | return errx(pCtx, 1, "out of memory duplicating environment (append)!");
|
---|
[3173] | 244 | *ppapszEnv = papszEnv;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[3039] | 247 | for (iEnvVar = 0; iEnvVar < cEnvVars; iEnvVar++)
|
---|
[2841] | 248 | {
|
---|
[3039] | 249 | char *pszCur = papszEnv[iEnvVar];
|
---|
| 250 | if ( KSUBMIT_ENV_NCMP(pszCur, pszValue, cchVar) == 0
|
---|
| 251 | && pszCur[cchVar] == '=')
|
---|
[2841] | 252 | {
|
---|
[3133] | 253 | size_t cchOldValue = strlen(pszCur) - cchVar - 1;
|
---|
| 254 | size_t cchNewValue = strlen(pszValue) - cchVar - 1;
|
---|
| 255 | char *pszNew = malloc(cchVar + 1 + cchOldValue + cchNewValue + 1);
|
---|
| 256 | if (!pszNew)
|
---|
[3192] | 257 | return errx(pCtx, 1, "out of memory appending to environment variable!");
|
---|
[3039] | 258 | if (fAppend)
|
---|
[2841] | 259 | {
|
---|
[3133] | 260 | memcpy(pszNew, pszCur, cchVar + 1 + cchOldValue);
|
---|
[3039] | 261 | memcpy(&pszNew[cchVar + 1 + cchOldValue], &pszValue[cchVar + 1], cchNewValue + 1);
|
---|
[2841] | 262 | }
|
---|
[3039] | 263 | else
|
---|
| 264 | {
|
---|
[3133] | 265 | memcpy(pszNew, pszCur, cchVar + 1); /* preserve variable name case */
|
---|
[3039] | 266 | memcpy(&pszNew[cchVar + 1], &pszValue[cchVar + 1], cchNewValue);
|
---|
[3133] | 267 | memcpy(&pszNew[cchVar + 1 + cchNewValue], &pszCur[cchVar + 1], cchOldValue + 1);
|
---|
[3039] | 268 | }
|
---|
| 269 |
|
---|
| 270 | if (cVerbosity > 0)
|
---|
[3192] | 271 | warnx(pCtx, "replacing '%s' with '%s'", pszCur, pszNew);
|
---|
[3133] | 272 | free(pszCur);
|
---|
[3039] | 273 | papszEnv[iEnvVar] = pszNew;
|
---|
| 274 |
|
---|
[3192] | 275 | return kBuiltinOptEnvRemoveDuplicates(pCtx, papszEnv, cEnvVars, cVerbosity, pszValue, cchVar, iEnvVar);
|
---|
[3039] | 276 | }
|
---|
[2841] | 277 | }
|
---|
[3192] | 278 | return kBuiltinOptEnvAddVar(pCtx, ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue);
|
---|
[2841] | 279 | }
|
---|
[3332] | 280 | return errx(pCtx, 1, "Missing '=': -%c %s", fAppend ? 'A' : 'D', pszValue);
|
---|
[3039] | 281 | }
|
---|
[2841] | 282 |
|
---|
[3039] | 283 |
|
---|
| 284 | /**
|
---|
| 285 | * Handles the --append var=value option.
|
---|
| 286 | *
|
---|
| 287 | * @returns 0 on success, non-zero exit code on error.
|
---|
[3192] | 288 | * @param pCtx The built-in command context.
|
---|
[3039] | 289 | * @param ppapszEnv The environment vector pointer.
|
---|
| 290 | * @param pcEnvVars Pointer to the variable holding the number of
|
---|
| 291 | * environment variables held by @a papszEnv.
|
---|
| 292 | * @param pcAllocatedEnvVars Pointer to the variable holding max size of the
|
---|
| 293 | * environment vector.
|
---|
| 294 | * @param cVerbosity The verbosity level.
|
---|
| 295 | * @param pszValue The var=value string to apply.
|
---|
| 296 | */
|
---|
[3192] | 297 | int kBuiltinOptEnvAppend(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,
|
---|
| 298 | int cVerbosity, const char *pszValue)
|
---|
[3039] | 299 | {
|
---|
[3192] | 300 | return kBuiltinOptEnvAppendPrepend(pCtx, ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue, 1 /*fAppend*/);
|
---|
[2841] | 301 | }
|
---|
| 302 |
|
---|
| 303 |
|
---|
| 304 | /**
|
---|
[3039] | 305 | * Handles the --prepend var=value option.
|
---|
| 306 | *
|
---|
| 307 | * @returns 0 on success, non-zero exit code on error.
|
---|
[3192] | 308 | * @param pCtx The built-in command context.
|
---|
[3039] | 309 | * @param ppapszEnv The environment vector pointer.
|
---|
| 310 | * @param pcEnvVars Pointer to the variable holding the number of
|
---|
| 311 | * environment variables held by @a papszEnv.
|
---|
| 312 | * @param pcAllocatedEnvVars Pointer to the variable holding max size of the
|
---|
| 313 | * environment vector.
|
---|
| 314 | * @param cVerbosity The verbosity level.
|
---|
| 315 | * @param pszValue The var=value string to apply.
|
---|
| 316 | */
|
---|
[3192] | 317 | int kBuiltinOptEnvPrepend(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,
|
---|
| 318 | int cVerbosity, const char *pszValue)
|
---|
[3039] | 319 | {
|
---|
[3192] | 320 | return kBuiltinOptEnvAppendPrepend(pCtx, ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue, 0 /*fAppend*/);
|
---|
[3039] | 321 | }
|
---|
| 322 |
|
---|
| 323 |
|
---|
| 324 | /**
|
---|
[2841] | 325 | * Handles the --unset var option.
|
---|
| 326 | *
|
---|
| 327 | * @returns 0 on success, non-zero exit code on error.
|
---|
[3192] | 328 | * @param pCtx The built-in command context.
|
---|
[3173] | 329 | * @param ppapszEnv The environment vector pointer.
|
---|
[2841] | 330 | * @param pcEnvVars Pointer to the variable holding the number of
|
---|
| 331 | * environment variables held by @a papszEnv.
|
---|
[3173] | 332 | * @param pcAllocatedEnvVars Pointer to the size of the vector allocation.
|
---|
| 333 | * The size is zero when read-only (CRT, GNU make)
|
---|
| 334 | * environment.
|
---|
[2841] | 335 | * @param cVerbosity The verbosity level.
|
---|
| 336 | * @param pszVarToRemove The name of the variable to remove.
|
---|
| 337 | */
|
---|
[3192] | 338 | int kBuiltinOptEnvUnset(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,
|
---|
| 339 | int cVerbosity, const char *pszVarToRemove)
|
---|
[2841] | 340 | {
|
---|
| 341 | if (strchr(pszVarToRemove, '=') == NULL)
|
---|
| 342 | {
|
---|
[3173] | 343 | char **papszEnv = *ppapszEnv;
|
---|
[2841] | 344 | unsigned cRemoved = 0;
|
---|
| 345 | size_t const cchVar = strlen(pszVarToRemove);
|
---|
| 346 | unsigned cEnvVars = *pcEnvVars;
|
---|
| 347 | unsigned iEnvVar;
|
---|
| 348 |
|
---|
| 349 | for (iEnvVar = 0; iEnvVar < cEnvVars; iEnvVar++)
|
---|
[2877] | 350 | if ( KSUBMIT_ENV_NCMP(papszEnv[iEnvVar], pszVarToRemove, cchVar) == 0
|
---|
[2841] | 351 | && papszEnv[iEnvVar][cchVar] == '=')
|
---|
| 352 | {
|
---|
| 353 | if (cVerbosity > 0)
|
---|
[3192] | 354 | warnx(pCtx, !cRemoved ? "removing '%s'" : "removing duplicate '%s'", papszEnv[iEnvVar]);
|
---|
[3173] | 355 |
|
---|
| 356 | if (!*pcAllocatedEnvVars)
|
---|
| 357 | {
|
---|
[3192] | 358 | papszEnv = kBuiltinOptEnvDuplicate(pCtx, papszEnv, cEnvVars, pcAllocatedEnvVars, cVerbosity);
|
---|
[3173] | 359 | if (!papszEnv)
|
---|
[3192] | 360 | return errx(pCtx, 1, "out of memory duplicating environment (unset)!");
|
---|
[3173] | 361 | *ppapszEnv = papszEnv;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[2841] | 364 | free(papszEnv[iEnvVar]);
|
---|
| 365 | cEnvVars--;
|
---|
| 366 | if (iEnvVar != cEnvVars)
|
---|
| 367 | papszEnv[iEnvVar] = papszEnv[cEnvVars];
|
---|
| 368 | papszEnv[cEnvVars] = NULL;
|
---|
| 369 | cRemoved++;
|
---|
| 370 | iEnvVar--;
|
---|
| 371 | }
|
---|
| 372 | *pcEnvVars = cEnvVars;
|
---|
| 373 |
|
---|
| 374 | if (cVerbosity > 0 && !cRemoved)
|
---|
[3192] | 375 | warnx(pCtx, "not found '%s'", pszVarToRemove);
|
---|
[2841] | 376 | }
|
---|
| 377 | else
|
---|
[3192] | 378 | return errx(pCtx, 1, "Found invalid variable name character '=' in: -U %s", pszVarToRemove);
|
---|
[2841] | 379 | return 0;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 |
|
---|
[3173] | 383 | /**
|
---|
| 384 | * Handles the --zap-env & --ignore-environment options.
|
---|
| 385 | *
|
---|
| 386 | * @returns 0 on success, non-zero exit code on error.
|
---|
[3192] | 387 | * @param pCtx The built-in command context.
|
---|
[3173] | 388 | * @param ppapszEnv The environment vector pointer.
|
---|
| 389 | * @param pcEnvVars Pointer to the variable holding the number of
|
---|
| 390 | * environment variables held by @a papszEnv.
|
---|
| 391 | * @param pcAllocatedEnvVars Pointer to the size of the vector allocation.
|
---|
| 392 | * The size is zero when read-only (CRT, GNU make)
|
---|
| 393 | * environment.
|
---|
| 394 | * @param cVerbosity The verbosity level.
|
---|
| 395 | */
|
---|
[3192] | 396 | int kBuiltinOptEnvZap(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, int cVerbosity)
|
---|
[3173] | 397 | {
|
---|
| 398 | if (*pcAllocatedEnvVars > 0)
|
---|
| 399 | {
|
---|
| 400 | char **papszEnv = *ppapszEnv;
|
---|
| 401 | unsigned i = *pcEnvVars;
|
---|
| 402 | while (i-- > 0)
|
---|
| 403 | {
|
---|
| 404 | free(papszEnv[i]);
|
---|
| 405 | papszEnv[i] = NULL;
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
| 408 | else
|
---|
| 409 | {
|
---|
| 410 | char **papszEnv = calloc(4, sizeof(char *));
|
---|
| 411 | if (!papszEnv)
|
---|
[3192] | 412 | return err(pCtx, 1, "out of memory!");
|
---|
[3173] | 413 | *ppapszEnv = papszEnv;
|
---|
| 414 | *pcAllocatedEnvVars = 4;
|
---|
| 415 | }
|
---|
| 416 | *pcEnvVars = 0;
|
---|
| 417 | return 0;
|
---|
| 418 | }
|
---|
[2841] | 419 |
|
---|
[3173] | 420 |
|
---|
[2841] | 421 | /**
|
---|
[3173] | 422 | * Cleans up afterwards, if necessary.
|
---|
| 423 | *
|
---|
| 424 | * @param ppapszEnv The environment vector pointer.
|
---|
| 425 | * @param cEnvVars The number of variables in the vector.
|
---|
| 426 | * @param pcAllocatedEnvVars Pointer to the size of the vector allocation.
|
---|
| 427 | * The size is zero when read-only (CRT, GNU make)
|
---|
| 428 | * environment.
|
---|
| 429 | */
|
---|
| 430 | void kBuiltinOptEnvCleanup(char ***ppapszEnv, unsigned cEnvVars, unsigned *pcAllocatedEnvVars)
|
---|
| 431 | {
|
---|
| 432 | char **papszEnv = *ppapszEnv;
|
---|
| 433 | *ppapszEnv = NULL;
|
---|
| 434 | if (*pcAllocatedEnvVars > 0)
|
---|
| 435 | {
|
---|
| 436 | *pcAllocatedEnvVars = 0;
|
---|
| 437 | while (cEnvVars-- > 0)
|
---|
| 438 | {
|
---|
| 439 | free(papszEnv[cEnvVars]);
|
---|
| 440 | papszEnv[cEnvVars] = NULL;
|
---|
| 441 | }
|
---|
| 442 | free(papszEnv);
|
---|
| 443 | }
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 |
|
---|
| 447 | /**
|
---|
[2841] | 448 | * Handles the --chdir dir option.
|
---|
| 449 | *
|
---|
| 450 | * @returns 0 on success, non-zero exit code on error.
|
---|
[3192] | 451 | * @param pCtx The built-in command context.
|
---|
[2841] | 452 | * @param pszCwd The CWD buffer. Contains current CWD on input,
|
---|
| 453 | * modified by @a pszValue on output.
|
---|
| 454 | * @param cbCwdBuf The size of the CWD buffer.
|
---|
| 455 | * @param pszValue The --chdir value to apply.
|
---|
| 456 | */
|
---|
[3192] | 457 | int kBuiltinOptChDir(PKMKBUILTINCTX pCtx, char *pszCwd, size_t cbCwdBuf, const char *pszValue)
|
---|
[2841] | 458 | {
|
---|
| 459 | size_t cchNewCwd = strlen(pszValue);
|
---|
| 460 | size_t offDst;
|
---|
| 461 | if (cchNewCwd)
|
---|
| 462 | {
|
---|
| 463 | #ifdef HAVE_DOS_PATHS
|
---|
| 464 | if (*pszValue == '/' || *pszValue == '\\')
|
---|
| 465 | {
|
---|
| 466 | if (pszValue[1] == '/' || pszValue[1] == '\\')
|
---|
| 467 | offDst = 0; /* UNC */
|
---|
| 468 | else if (pszCwd[1] == ':' && isalpha(pszCwd[0]))
|
---|
| 469 | offDst = 2; /* Take drive letter from CWD. */
|
---|
| 470 | else
|
---|
[3192] | 471 | return errx(pCtx, 1, "UNC relative CWD not implemented: cur='%s' new='%s'", pszCwd, pszValue);
|
---|
[2841] | 472 | }
|
---|
| 473 | else if ( pszValue[1] == ':'
|
---|
| 474 | && isalpha(pszValue[0]))
|
---|
| 475 | {
|
---|
| 476 | if (pszValue[2] == '/'|| pszValue[2] == '\\')
|
---|
| 477 | offDst = 0; /* DOS style absolute path. */
|
---|
| 478 | else if ( pszCwd[1] == ':'
|
---|
| 479 | && tolower(pszCwd[0]) == tolower(pszValue[0]) )
|
---|
| 480 | {
|
---|
| 481 | pszValue += 2; /* Same drive as CWD, append drive relative path from value. */
|
---|
| 482 | cchNewCwd -= 2;
|
---|
| 483 | offDst = strlen(pszCwd);
|
---|
| 484 | }
|
---|
| 485 | else
|
---|
| 486 | {
|
---|
| 487 | /* Get current CWD on the specified drive and append value. */
|
---|
| 488 | int iDrive = tolower(pszValue[0]) - 'a' + 1;
|
---|
| 489 | if (!_getdcwd(iDrive, pszCwd, cbCwdBuf))
|
---|
[3192] | 490 | return err(pCtx, 1, "_getdcwd(%d,,) failed", iDrive);
|
---|
[2841] | 491 | pszValue += 2;
|
---|
| 492 | cchNewCwd -= 2;
|
---|
| 493 | }
|
---|
| 494 | }
|
---|
| 495 | #else
|
---|
| 496 | if (*pszValue == '/')
|
---|
| 497 | offDst = 0;
|
---|
| 498 | #endif
|
---|
| 499 | else
|
---|
| 500 | offDst = strlen(pszCwd); /* Relative path, append to the existing CWD value. */
|
---|
| 501 |
|
---|
| 502 | /* Do the copying. */
|
---|
| 503 | #ifdef HAVE_DOS_PATHS
|
---|
| 504 | if (offDst > 0 && pszCwd[offDst - 1] != '/' && pszCwd[offDst - 1] != '\\')
|
---|
| 505 | #else
|
---|
| 506 | if (offDst > 0 && pszCwd[offDst - 1] != '/')
|
---|
| 507 | #endif
|
---|
| 508 | pszCwd[offDst++] = '/';
|
---|
| 509 | if (offDst + cchNewCwd >= cbCwdBuf)
|
---|
[3192] | 510 | return errx(pCtx, 1, "Too long CWD: %*.*s%s", offDst, offDst, pszCwd, pszValue);
|
---|
[2841] | 511 | memcpy(&pszCwd[offDst], pszValue, cchNewCwd + 1);
|
---|
| 512 | }
|
---|
| 513 | /* else: relative, no change - quitely ignore. */
|
---|
| 514 | return 0;
|
---|
| 515 | }
|
---|
| 516 |
|
---|