int ft_unsetenv(t_cmd *cmd, t_data *data) { t_cmd *root; root = cmd; cmd = cmd->next->next; if (cmd == root) { ft_printf("unsetenv: Too few arguments\n"); return (9); } if (ft_strcmp(cmd->word, "*") == 1) { if ((option_star(data)) == -1) return (-1); } else { while (cmd != root) { if (remove_var(cmd->word, data->env) == -1) return (-1); cmd = cmd->next; } } return (1); }
static int exec_unsetenv(char *arg, t_env **env) { t_env *tmp; tmp = *env; while (tmp) { if (!strncmp(arg, tmp->name, strlen(tmp->name))) { if (!tmp->prev) { free(tmp->name); if (tmp->value) free(tmp->value); *env = tmp->next; free(tmp); tmp = *env; if (*env) (*env)->prev = NULL; } else remove_var(&tmp); return (EXIT_SUCCESS); } tmp = tmp->next; } return (EXIT_FAILURE); }
static int exec_unsetenv(char *arg, t_env **env) { t_env *tmp; tmp = *env; while (tmp) { if (!strncmp(arg, tmp->name, strlen(tmp->name))) { if (!tmp->prev) { free(tmp->name); if (tmp->value) free(tmp->value); *env = tmp->next; free(tmp); tmp = *env; } else remove_var(&tmp); printf("Variable %s has removed successfully.\n", arg); return (EXIT_SUCCESS); } tmp = tmp->next; } return (EXIT_FAILURE); }
int option_star(t_data *data) { t_env *elem; char *str_cpy; int i; elem = data->env->next; while (elem != data->env) { i = 0; while (elem->var[i] && elem->var[i] != '=') ++i; if ((str_cpy = ft_strndup(elem->var, i)) == NULL) return (-1); elem = elem->next; if (remove_var(str_cpy, data->env) == -1) return (-1); free(str_cpy); } return (0); }
int main(int argc, char **argv) { int opt, dump, fd, res, i, size; uint8_t buf[NVRAM_SIZE], *cp, *common; struct deletelist *dl; dump = 0; dl = NULL; while((opt = getopt(argc, argv, "d:p")) != -1) { switch(opt) { case 'p': dump = 1; break; case 'd': if (dl == NULL) { dl = malloc(sizeof(*dl)); if (dl == NULL) err(1, "malloc"); bzero(dl, sizeof(*dl)); dl->last = dl; } else { dl->last->next = malloc(sizeof(*dl)); if (dl->last->next == NULL) err(1, "malloc"); dl->last = dl->last->next; bzero(dl->last, sizeof(*dl)); } dl->last->name = optarg; break; default: usage(); /* Not reached */ } } argc -= optind; argv += optind; if (argc == 0 && dump == 0 && dl == NULL) { usage(); /* Not reached */ } fd = open(DEVICE_NAME, O_RDWR); if (fd == -1) err(1, DEVICE_NAME); for (i = 0; i < (int)sizeof(buf);) { res = read(fd, buf + i, sizeof(buf) - i); if (res == -1 && errno != EINTR) err(1, DEVICE_NAME); if (res == 0) break; if (res > 0) i += res; } if (i != sizeof(buf)) errx(1, "%s: short read", DEVICE_NAME); /* Locate common block */ size = 0; for (cp = buf; cp < buf + sizeof(buf); cp += size) { memcpy(conv.buf, cp, sizeof(struct chrp_header)); size = conv.header.length * 0x10; if (strncmp(conv.header.name, "common", 7) == 0) break; } if (cp >= buf + sizeof(buf) || size <= (int)sizeof(struct chrp_header)) errx(1, "%s: no common block", DEVICE_NAME); common = cp + sizeof(struct chrp_header); size -= sizeof(struct chrp_header); if (dump != 0) { while (size > 0) { i = strlen(common) + 1; if (i == 1) break; printf("%s\n", common); size -= i; common += i; } exit(0); } for (;dl != NULL; dl = dl->next) { if (remove_var(common, size, dl->name) == 0) warnx("%s: no such variable", dl->name); } for (; argc > 0; argc--, argv++) { cp = strchr(*argv, '='); if (cp == NULL) errx(1, "%s: invalid argument", *argv); cp[0] = '\0'; cp++; remove_var(common, size, *argv); if (append_var(common, size, *argv, cp) == -1) errx(1, "%s: error setting variable", *argv); } for (i = 0; i < (int)sizeof(buf);) { res = write(fd, buf + i, sizeof(buf) - i); if (res == -1 && errno != EINTR) err(1, DEVICE_NAME); if (res == 0) break; if (res > 0) i += res; } if (i != sizeof(buf)) errx(1, "%s: short write", DEVICE_NAME); if (close(fd) == -1) err(1, DEVICE_NAME); exit(0); }