finally move buildroot-ng to trunk
[openwrt/staging/dedeckeh.git] / package / nvram / src / main.c
1 /*
2 * Frontend command-line utility for Linux NVRAM layer
3 *
4 * Copyright 2004, Broadcom Corporation
5 * All Rights Reserved.
6 *
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
11 *
12 * $Id$
13 */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include <typedefs.h>
20 #include <bcmnvram.h>
21
22 static void
23 usage(void)
24 {
25 fprintf(stderr, "usage: nvram [get name] [set name=value] [unset name] [show]\n");
26 exit(0);
27 }
28
29 /* hack for some PMON default nvram values which have '\r' appended */
30 void
31 puts_trim_cr(char *str)
32 {
33 int len= strlen(str);
34 if (len && (str[len-1] == '\r')) len--;
35 printf("%.*s\n", len, str);
36 }
37
38 /* NVRAM utility */
39 int
40 main(int argc, char **argv)
41 {
42 char *name, *value, buf[NVRAM_SPACE];
43 int size;
44
45 /* Skip program name */
46 --argc;
47 ++argv;
48
49 if (!*argv)
50 usage();
51
52 /* Process the remaining arguments. */
53 for (; *argv; argv++) {
54 if (!strncmp(*argv, "get", 3)) {
55 if (*++argv) {
56 if ((value = nvram_get(*argv))) {
57 puts_trim_cr(value);
58 }
59 }
60 }
61 else if (!strncmp(*argv, "set", 3)) {
62 if (*++argv) {
63 strncpy(value = buf, *argv, sizeof(buf));
64 name = strsep(&value, "=");
65 nvram_set(name, value);
66 }
67 }
68 else if (!strncmp(*argv, "unset", 5)) {
69 if (*++argv)
70 nvram_unset(*argv);
71 }
72 else if (!strncmp(*argv, "commit", 5)) {
73 nvram_commit();
74 }
75 else if (!strncmp(*argv, "show", 4) ||
76 !strncmp(*argv, "getall", 6)) {
77 nvram_getall(buf, sizeof(buf));
78 for (name = buf; *name; name += strlen(name) + 1)
79 puts_trim_cr(name);
80 size = sizeof(struct nvram_header) + (int) name - (int) buf;
81 fprintf(stderr, "size: %d bytes (%d left)\n", size, NVRAM_SPACE - size);
82 }
83 if (!*argv)
84 break;
85 }
86
87 return 0;
88 }