mirror of
https://github.com/AsahiLinux/u-boot
synced 2024-11-13 08:27:23 +00:00
tools/env: reuse fw_getenv in fw_printenv function
Try to avoid adhoc iteration of the environment. Reuse fw_getenv to find the variables that should be printed. Only use open-coded iteration when printing all variables. For backwards compatibility, keep emitting a newline when printing with value_only. Signed-off-by: Andreas Fenkart <andreas.fenkart@digitalstrom.com>
This commit is contained in:
parent
1b7427cd2a
commit
c5c41c45b1
1 changed files with 17 additions and 27 deletions
44
tools/env/fw_env.c
vendored
44
tools/env/fw_env.c
vendored
|
@ -250,9 +250,14 @@ int parse_aes_key(char *key, uint8_t *bin_key)
|
||||||
*/
|
*/
|
||||||
int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
|
int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
|
||||||
{
|
{
|
||||||
char *env, *nxt;
|
|
||||||
int i, rc = 0;
|
int i, rc = 0;
|
||||||
|
|
||||||
|
if (value_only && argc != 1) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"## Error: `-n' option requires exactly one argument\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (!opts)
|
if (!opts)
|
||||||
opts = &default_opts;
|
opts = &default_opts;
|
||||||
|
|
||||||
|
@ -260,6 +265,7 @@ int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (argc == 0) { /* Print all env variables */
|
if (argc == 0) { /* Print all env variables */
|
||||||
|
char *env, *nxt;
|
||||||
for (env = environment.data; *env; env = nxt + 1) {
|
for (env = environment.data; *env; env = nxt + 1) {
|
||||||
for (nxt = env; *nxt; ++nxt) {
|
for (nxt = env; *nxt; ++nxt) {
|
||||||
if (nxt >= &environment.data[ENV_SIZE]) {
|
if (nxt >= &environment.data[ENV_SIZE]) {
|
||||||
|
@ -274,39 +280,23 @@ int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value_only && argc != 1) {
|
for (i = 0; i < argc; ++i) { /* print a subset of env variables */
|
||||||
fprintf(stderr,
|
|
||||||
"## Error: `-n' option requires exactly one argument\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < argc; ++i) { /* print single env variables */
|
|
||||||
char *name = argv[i];
|
char *name = argv[i];
|
||||||
char *val = NULL;
|
char *val = NULL;
|
||||||
|
|
||||||
for (env = environment.data; *env; env = nxt + 1) {
|
val = fw_getenv(name);
|
||||||
|
|
||||||
for (nxt = env; *nxt; ++nxt) {
|
|
||||||
if (nxt >= &environment.data[ENV_SIZE]) {
|
|
||||||
fprintf (stderr, "## Error: "
|
|
||||||
"environment not terminated\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val = envmatch (name, env);
|
|
||||||
if (val) {
|
|
||||||
if (!value_only) {
|
|
||||||
fputs (name, stdout);
|
|
||||||
putc ('=', stdout);
|
|
||||||
}
|
|
||||||
puts (val);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!val) {
|
if (!val) {
|
||||||
fprintf (stderr, "## Error: \"%s\" not defined\n", name);
|
fprintf (stderr, "## Error: \"%s\" not defined\n", name);
|
||||||
rc = -1;
|
rc = -1;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (value_only) {
|
||||||
|
puts(val);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s=%s\n", name, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
|
|
Loading…
Reference in a new issue