toybox/toys/other/printenv.c
<<
>>
Prefs
   1/* printenv.c - Print environment variables.
   2 *
   3 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org>
   4
   5USE_PRINTENV(NEWTOY(printenv, "0(null)", TOYFLAG_BIN))
   6
   7config PRINTENV
   8  bool "printenv"
   9  default y
  10  help
  11    usage: printenv [-0] [env_var...]
  12
  13    Print environment variables.
  14
  15    -0  Use \0 as delimiter instead of \n
  16*/
  17
  18#include "toys.h"
  19
  20void printenv_main(void)
  21{
  22  char **env, **var = toys.optargs;
  23  char delim = '\n';
  24
  25  if (toys.optflags) delim = 0;
  26
  27  do {
  28    int catch = 0, len = *var ? strlen(*var) : 0;
  29
  30    for (env = environ; *env; env++) {
  31      char *out = *env;
  32      if (*var) {
  33        if (!strncmp(out, *var, len) && out[len] == '=') out += len +1;
  34        else continue;
  35      }
  36      xprintf("%s%c", out, delim);
  37      catch++;
  38    }
  39    if (*var && !catch) toys.exitval = 1;
  40  } while (*var && *(++var));
  41}
  42