annotate toys/printenv.c @ 517:a191ea9bc5df

Implement printenv command.
author Georgi Chorbadzhiyski <georgi@unixsol.org>
date Sat, 03 Mar 2012 23:55:27 -0600
parents
children e0eed40f4ab1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
517
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
2 *
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
3 * printenv.c - Print environment variables.
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
4 *
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
5 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org>
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
6 *
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
7
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
8 USE_PRINTENV(NEWTOY(printenv, "0", TOYFLAG_USR|TOYFLAG_BIN))
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
9
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
10 config PRINTENV
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
11 bool "printenv"
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
12 default y
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
13 help
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
14 usage: printenv [-0] [env_var...]
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
15 Print enviroment variables.
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
16
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
17 -0 Use \0 as environment delimiter instead of \n
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
18 */
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
19
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
20 #include "toys.h"
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
21
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
22 extern char **environ;
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
23
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
24 void printenv_main(void)
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
25 {
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
26 char **env;
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
27 char delim = '\n';
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
28
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
29 if (toys.optflags)
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
30 delim = '\0';
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
31
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
32 if (!toys.optargs[0]) {
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
33 for (env = environ; *env; env++)
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
34 xprintf("%s%c", *env, delim);
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
35 } else {
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
36 char **var = toys.optargs;
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
37 for (var = toys.optargs; *var; var++) {
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
38 int len = strlen(*var);
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
39 for (env = environ; *env; env++) {
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
40 if (strncmp(*env, *var, len) == 0)
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
41 xprintf("%s%c", *env + len + 1, delim);
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
42 }
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
43 }
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
44 }
a191ea9bc5df Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
45 }