diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2009-04-27 00:33:43 +0000 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2009-04-27 00:33:43 +0000 |
commit | 61d6197be8a5fab5c2aec3ec15ae57436a7235c6 (patch) | |
tree | d825f983e75499da1819c04148b8102ff4666a68 /contrib/userspace-nvram/cli.c | |
parent | 40dfa066abfff16f14db2c1c42fe746f1895de39 (diff) |
contrib: remove userspace-nvram, went upstream
Diffstat (limited to 'contrib/userspace-nvram/cli.c')
-rw-r--r-- | contrib/userspace-nvram/cli.c | 178 |
1 files changed, 0 insertions, 178 deletions
diff --git a/contrib/userspace-nvram/cli.c b/contrib/userspace-nvram/cli.c deleted file mode 100644 index e3cd826d77..0000000000 --- a/contrib/userspace-nvram/cli.c +++ /dev/null @@ -1,178 +0,0 @@ -#include <string.h> -#include <stdio.h> -#include <stdlib.h> -#include <errno.h> -#include <sys/mman.h> - -#include "nvram.h" - - -static nvram_handle_t * nvram_open_rdonly(void) -{ - const char *file = nvram_find_staging(); - - if( file == NULL ) - file = nvram_find_mtd(); - - if( file != NULL ) - return nvram_open(file, NVRAM_RO); - - return NULL; -} - -static nvram_handle_t * nvram_open_staging(void) -{ - if( nvram_find_staging() != NULL || nvram_to_staging() == 0 ) - return nvram_open(NVRAM_STAGING, NVRAM_RW); - - return NULL; -} - -static int do_show(nvram_handle_t *nvram) -{ - nvram_tuple_t *t; - int stat = 1; - - if( (t = nvram_getall(nvram)) != NULL ) - { - while( t ) - { - printf("%s=%s\n", t->name, t->value); - t = t->next; - } - - stat = 0; - } - - return stat; -} - -static int do_get(nvram_handle_t *nvram, const char *var) -{ - const char *val; - int stat = 1; - - if( (val = nvram_get(nvram, var)) != NULL ) - { - printf("%s\n", val); - stat = 0; - } - - return stat; -} - -static int do_unset(nvram_handle_t *nvram, const char *var) -{ - return nvram_unset(nvram, var); -} - -static int do_set(nvram_handle_t *nvram, const char *pair) -{ - char *val = strstr(pair, "="); - char var[strlen(pair)]; - int stat = 1; - - if( val != NULL ) - { - memset(var, 0, sizeof(var)); - strncpy(var, pair, (int)(val-pair)); - stat = nvram_set(nvram, var, (char *)(val + 1)); - } - - return stat; -} - - -int main( int argc, const char *argv[] ) -{ - nvram_handle_t *nvram; - int commit = 0; - int write = 0; - int stat = 1; - int done = 0; - int i; - - /* Ugly... iterate over arguments to see whether we can expect a write */ - for( i = 1; i < argc; i++ ) - if( ( !strcmp(argv[i], "set") && ++i < argc ) || - ( !strcmp(argv[i], "unset") && ++i < argc ) || - !strcmp(argv[i], "commit") ) - { - write = 1; - break; - } - - - if( (nvram = write ? nvram_open_staging() : nvram_open_rdonly()) != NULL && argc > 1 ) - { - for( i = 1; i < argc; i++ ) - { - if( !strcmp(argv[i], "show") ) - { - stat = do_show(nvram); - done++; - } - else if( !strcmp(argv[i], "get") && ++i < argc ) - { - stat = do_get(nvram, argv[i]); - done++; - } - else if( !strcmp(argv[i], "unset") && ++i < argc ) - { - stat = do_unset(nvram, argv[i]); - done++; - } - else if( !strcmp(argv[i], "set") && ++i < argc ) - { - stat = do_set(nvram, argv[i]); - done++; - } - else if( !strcmp(argv[i], "commit") ) - { - commit = 1; - done++; - } - else - { - done = 0; - break; - } - } - - if( write ) - stat = nvram_commit(nvram); - - nvram_close(nvram); - - if( commit ) - stat = staging_to_nvram(); - } - - if( !nvram ) - { - fprintf(stderr, - "Could not open nvram! Possible reasons are:\n" - " - No device found (/proc not mounted or no nvram present)\n" - " - Insufficient permissions to open mtd device\n" - " - Insufficient memory to complete operation\n" - " - Memory mapping failed or not supported\n" - ); - - stat = 1; - } - else if( !done ) - { - fprintf(stderr, - "Usage:\n" - " nvram show\n" - " nvram get variable\n" - " nvram set variable=value [set ...]\n" - " nvram unset variable [unset ...]\n" - " nvram commit\n" - ); - - stat = 1; - } - - return stat; -} |