Mercurial > hg > toybox
annotate toys/printenv.c @ 543:60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 12 Mar 2012 23:00:28 -0500 |
parents | e0eed40f4ab1 |
children |
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 |
518
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
8 USE_PRINTENV(NEWTOY(printenv, "0(null)", TOYFLAG_USR|TOYFLAG_BIN)) |
517
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 |
518
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
16 Print environment variables. |
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
17 |
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
18 -0 Use \0 as delimiter instead of \n |
517
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 |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
21 #include "toys.h" |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
22 |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
23 extern char **environ; |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
24 |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
25 void printenv_main(void) |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
26 { |
518
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
27 char **env, **var = toys.optargs; |
517
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
28 char delim = '\n'; |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
29 |
518
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
30 if (toys.optflags) delim = 0; |
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
31 |
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
32 do { |
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
33 int catch = 0, len = *var ? strlen(*var) : 0; |
517
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
34 |
518
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
35 for (env = environ; *env; env++) { |
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
36 char *out = *env; |
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
37 if (*var) { |
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
38 if (!strncmp(out, *var, len) && out[len] == '=') |
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
39 out += len +1; |
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
40 else continue; |
517
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
41 } |
518
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
42 xprintf("%s%c", out, delim); |
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
43 catch++; |
517
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
44 } |
518
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
45 if (*var && !catch) toys.exitval = 1; |
e0eed40f4ab1
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Rob Landley <rob@landley.net>
parents:
517
diff
changeset
|
46 } while (*var && *(++var)); |
517
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
47 } |