Question
Hello. I'm making my shell in c and I want to use linux command as external command. Below is my code and it work well.
Hello.
I'm making my shell in c and I want to use linux command as external command.
Below is my code and it work well. If I put "cat file.txt" it work. but if I put "cat -n file.txt", it doesn't work.
Please give help, tell me specifically
#include
#define BUF_SIZE 1024 #define READ 0 #define WRITE 1
char** eliminateToken(char **token, int size, int pos1, int pos2);
int main(int argc, char **argv) { char *buffer = malloc(sizeof(char) * BUF_SIZE); char **token = malloc(sizeof(char*) * BUF_SIZE); int c, status, position, fd; pid_t pid;
do { position = 0; printf("$ "); while (true) { c = getchar();
if (c == EOF || c == ' ') { buffer[position] = '\0'; break; } else { buffer[position] = c; } position++; } token[0] = strtok(buffer, " "); for(position = 1; position < BUF_SIZE; position++) { token[position] = strtok(NULL, " "); if (token[position] == NULL) { break; } } token = realloc(token, sizeof(char*) * position);
pid = fork(); if (pid < 0) { perror("fork error"); exit(EXIT_FAILURE); } else if (pid == 0) { for (int i = 0; i < position; i++) { if (!strncmp(token[i], ">>", 2)) { if ((fd = open(token[i+1], O_RDWR | O_APPEND | O_CREAT, 0666)) < 0) { perror("open error"); close(fd); exit(EXIT_FAILURE); } dup2(fd, WRITE); token = eliminateToken(token, position, i, i+1); break; } else if (!strncmp(token[i], ">", 1)) { if ((fd = open(token[i+1], O_RDWR | O_TRUNC | O_CREAT, 0666)) < 0) { perror("open error"); close(fd); exit(EXIT_FAILURE); } dup2(fd, WRITE); token = eliminateToken(token, position, i, i+1); break; } } if (execvp(token[0], token) == -1) { perror("exec error"); exit(EXIT_FAILURE); } close(fd); } else { waitpid(pid, &status, WUNTRACED); }
puts(""); token = calloc(position, sizeof(char*) * BUF_SIZE); } while (!status);
free(buffer); free(token); return EXIT_SUCCESS; }
char** eliminateToken(char **token, int size, int pos1, int pos2) { char **eliminate_buffer = malloc(sizeof(char*) * BUF_SIZE); int position = 0;
for (int i = 0; i < size; i++) { if (i != pos1 && i != pos2) { eliminate_buffer[position] = token[i]; position++; } }
return eliminate_buffer; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started