Mercurial > hg > toybox
annotate toys/other/printenv.c @ 656:6df4ccc0acbe
Regularize command headers, update links to standards documents.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 25 Aug 2012 18:08:51 -0500 |
parents | 2986aa63a021 |
children | 786841fdb1e0 |
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 |
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
|
7 USE_PRINTENV(NEWTOY(printenv, "0(null)", TOYFLAG_USR|TOYFLAG_BIN)) |
517
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
8 |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
9 config PRINTENV |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
10 bool "printenv" |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
11 default y |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
12 help |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
13 usage: printenv [-0] [env_var...] |
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
14 |
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
|
15 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
|
16 |
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 -0 Use \0 as delimiter instead of \n |
517
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 { |
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
|
26 char **env, **var = toys.optargs; |
517
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 |
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
|
29 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
|
30 |
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 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
|
32 int catch = 0, len = *var ? strlen(*var) : 0; |
517
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
33 |
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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 else continue; |
517
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
40 } |
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
|
41 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
|
42 catch++; |
517
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
43 } |
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
|
44 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
|
45 } while (*var && *(++var)); |
517
a191ea9bc5df
Implement printenv command.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff
changeset
|
46 } |