Untitled
unknown
plain_text
a year ago
839 B
4
Indexable
Never
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> char **parce(char *str1) { char **array = NULL; int i; char *str; str = strdup(str1); i = 1; str = strtok(str, " \t\n"); while (str != NULL) { array = realloc(array, (i + 1) * sizeof(char *)); array[i - 1] = malloc(sizeof(char) * strlen(str) + 1); strcpy(array[i - 1], str); i++; str = strtok(NULL, " \t\n"); } array[i - 1] = NULL; free(str); return (array); } int main() { char **array; char *buf = NULL; size_t buf_size = 0; int num_char = 0; num_char = getline(&buf, &buf_size, stdin); if (num_char == -1) { perror("getline"); return (1); } array = parce(buf); execve(array[0], array, NULL); for (int i = 0; array[i] != NULL; i++) { free(array[i]); } free(array); return (0); }