void set_env(t_all *all, char *var, char *value) { char *newenv; char **env2del; env2del = NULL; ASSERT_F(var, NULL, NO_ENV2SET); newenv = ft_kebab(var, "=", value, NULL); if ((env2del = get_env_addr(all->env.env, newenv)) != NULL) modify_env(all, env2del, newenv); else { if ((all->env.env = ft_2dchar_add(all->env.env, newenv)) == NULL) ft_error(all, SYSCALL_FAIL); } }
char *getenv (bd_t * bd, uchar *name) { int i, nxt; for (i=0; get_env_char(bd, i) != '\0'; i=nxt+1) { int val; for (nxt=i; get_env_char(bd, nxt) != '\0'; ++nxt) { if (nxt >= sizeof(bd->bi_env_data)) { return (NULL); } } if ((val=envmatch(bd, name, i)) < 0) continue; return (get_env_addr(bd, val)); } return (NULL); }
void unset_env(t_all *all, char *var) { char **env2del; int i; i = 1; env2del = NULL; ASSERT_F(var, NULL, NO_ENV2UNSET); if ((env2del = get_env_addr(all->env.env, var)) != NULL) { free(*env2del); while (env2del[i]) { env2del[i - 1] = env2del[i]; i++; } env2del[i - 1] = 0; if (ft_strequ(var, "PATH")) ft_2dchardel(&all->env.path_dir); } else error_no_quit(NO_ENV2UNSET, all); }
int _do_setenv (bd_t *bd, int flag, int argc, char *argv[]) { int i, len, oldval; uchar *env, *nxt = 0; uchar *name; /* need writable copy in RAM */ if (!bd->bi_env_data) return 1; name = argv[1]; /* * search if variable with this name already exists */ oldval = -1; for (env = bd->bi_env_data; *env; env = nxt+1) { for (nxt = env; *nxt; ++nxt) ; if ((oldval = envmatch(bd, name, (ulong)env - (ulong)bd->bi_env_data)) >= 0) break; } /* * Delete any existing definition */ if (oldval >= 0) { #ifndef CONFIG_ENV_OVERWRITE /* * Ethernet Address and serial# can be set only once */ if ( (strcmp (name, "serial#") == 0) || ((strcmp (name, "ethaddr") == 0) # if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR) && (strcmp (get_env_addr(bd, oldval),MK_STR(CONFIG_ETHADDR)) != 0) # endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */ ) ) { printf ("Can't overwrite \"%s\"\n", name); return 1; } #endif /* * Switch to new baudrate if new baudrate is supported */ if (strcmp(argv[1],"baudrate") == 0) { int baudrate = simple_strtoul(argv[2], NULL, 10); int i; for (i=0; i<N_BAUDRATES; ++i) { if (baudrate == baudrate_table[i]) break; } if (i == N_BAUDRATES) { printf ("## Baudrate %d bps not supported\n", baudrate); return 1; } printf ("## Switch baudrate to %d bps and press ENTER ...\n", baudrate); udelay(50000); serial_setbrg (bd, baudrate); udelay(50000); for (;;) { if (getc() == '\r') break; } bd->bi_baudrate = baudrate; } if (*++nxt == '\0') { if ((ulong)env > (ulong)bd->bi_env_data) { env--; } else { *env = '\0'; } } else { for (;;) { *env = *nxt++; if ((*env == '\0') && (*nxt == '\0')) break; ++env; } } *++env = '\0'; } /* Delete only ? */ if ((argc < 3) || argv[2] == NULL) { /* Update CRC */ bd->bi_env_crc = crc32(0, bd->bi_env_data, sizeof(bd->bi_env_data)); return 0; } /* * Append new definition at the end */ for (env = bd->bi_env_data; *env || *(env+1); ++env) ; if ((ulong)env > (ulong)bd->bi_env_data) ++env; /* * Overflow when: * "name" + "=" + "val" +"\0\0" > * sizeof(bd->bi_env_data) - (env-bd->bi_env_data) */ len = strlen(name) + 2; /* add '=' for first arg, ' ' for all others */ for (i=2; i<argc; ++i) { len += strlen(argv[i]) + 1; } if (len > sizeof(bd->bi_env_data)) { printf ("## Error: environment overflow, \"%s\" deleted\n", name); return 1; } while ((*env = *name++) != '\0') env++; for (i=2; i<argc; ++i) { char *val = argv[i]; *env = (i==2) ? '=' : ' '; while ((*++env = *val++) != '\0') ; } /* end is marked with double '\0' */ *++env = '\0'; /* Update CRC */ bd->bi_env_crc = crc32(0, bd->bi_env_data, sizeof(bd->bi_env_data)); /* * Some variables should be updated when the corresponding * entry in the enviornment is changed */ if (strcmp(argv[1],"ethaddr") == 0) { char *s = argv[2]; /* always use only one arg */ char *e; for (i=0; i<6; ++i) { bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0; if (s) s = (*e) ? e+1 : e; } return 0; } #if (CONFIG_COMMANDS & CFG_CMD_NET) if (strcmp(argv[1],"ipaddr") == 0) { char *s = argv[2]; /* always use only one arg */ bd->bi_ip_addr = string_to_ip(s); return 0; } #endif if (strcmp(argv[1],"loadaddr") == 0) { load_addr = simple_strtoul(argv[2], NULL, 16); return 0; } if (strcmp(argv[1],"pagelength") == 0) { pagelength = simple_strtoul(argv[2], NULL, 10); return 0; } #if (CONFIG_COMMANDS & CFG_CMD_NET) if (strcmp(argv[1],"bootfile") == 0) { copy_filename (BootFile, argv[2], sizeof(BootFile)); return 0; } #endif /* CFG_CMD_NET */ #ifdef CONFIG_KEYBOARD if (strcmp(argv[1],"keymap") == 0) { kbd_mapping (argv[2]); return 0; } #endif /* CONFIG_KEYBOARD */ #ifdef CONFIG_ADJUST_LCD if (strcmp(argv[1],"contrast") == 0) { lcd_contrast(simple_strtoul(argv[2], NULL, 10)); return 0; } if (strcmp(argv[1],"brightness") == 0) { lcd_brightness(simple_strtoul(argv[2], NULL, 10)); return 0; } #endif /* CONFIG_ADJUST_LCD */ return 0; }