OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / boottools / setbenv.c
1 /*      setbenv.c
2  *      OZH, 2001-2005
3  *      David Wu 2007 
4  */
5  
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <errno.h>
10 #ifdef CONFIG_LIB_LIBBSC
11 #include "bootstd.h"
12 #else
13 #include <asm/uCbootstrap.h>
14 _bsc1 (int, setbenv, char *, a)
15 #endif
16
17 #ifndef MAX_ENVNAME_SIZE
18 #define MAX_ENVNAME_SIZE        31
19 #endif
20 #ifndef MAX_ENVDATA_SIZE
21 #define MAX_ENVDATA_SIZE        1024
22 #endif
23
24 #define ENAME 2
25 #define EDATA 3
26
27 int main(int argc, char *argv[]) 
28 {
29     /* 
30      * Buffer length needs to support a 31 character variable name
31      * an equal sign and a 1024 character value and terminator. 
32      */
33     char buf[MAX_ENVNAME_SIZE+1+MAX_ENVDATA_SIZE+1];
34
35     if  (argc<2) {
36         printf("usage: %s varname value\n       %s varname\n",argv[0],argv[0]);
37         return 0; 
38     }
39
40     if  ( strchr(argv[1], '=') && argc > 2 ) {  /* variable name _MUST_NOT_ include '=' symbol  */
41         printf("%s: variable name must not include \"=\"\n", argv[0]);  
42         return -1;
43     }
44     if  (argc>2) {
45         if(strlen(argv[1]) > MAX_ENVNAME_SIZE) {
46             printf("%s: variable name is longer than %d Bytes\n", argv[0], MAX_ENVNAME_SIZE);
47             return ENAME;
48         }
49         if(strlen(argv[2]) > MAX_ENVDATA_SIZE) {
50             printf("%s: value is longer than %d Bytes\n", argv[0], MAX_ENVDATA_SIZE);
51             return EDATA;
52         }
53         strcpy(buf, argv[1]);
54         strcat(buf, "=");
55         strcat(buf, argv[2]); 
56     } else {
57         if(strlen(argv[1]) > sizeof(buf)) {
58             /* printf("%s: %s is too large\n", argv[0], argv[1]); */
59             return -1;
60         }
61         strcpy(buf, argv[1]);
62     }
63 #ifdef CONFIG_LIB_LIBBSC
64     return bsc_setenv(buf);
65 #else
66     setbenv(buf);
67     return (0); 
68 #endif
69 }