Здравствуйте. Может кто-нибудь хоть как-то подтолкнёт меня. Сегодня весь день потратил, но так и не понял
Мне нужно написать shell под unix, который например получает команду
| Цитата | ls -l | cut -c 1-20 | cut -c 1-1
|
и её выполняет Вот такой код имею
| Код | //---------------------------------------------------------------------------
#pragma hdrstop
#include <fcntl.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <stdio.h> #include <netinet/in.h> #include <resolv.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h>
void parseA(char* input); void parseB(char* input); void parseC(char* input); void parseD(char* input);
char* cmdparams[20][20][20] = {NULL}; int block = 0; int cmd = 0; int param = 0;
void parseA(char* input) { while (input[0] != 0) { cmd = 0; const char* delim = strchr(input, '|'); if (delim) { int len = delim-input; char* subcmd = (char*)malloc(len); strncpy(subcmd, input, len); subcmd[len] = '\0';
parseB(subcmd); block++; strcpy(input, delim+1); } else { parseB(input); return; } } }
void parseB(char* input) { while (input[0] != 0) { param = 0; char* delim = strchr(input, '&'); if (delim) { int len = delim-input; char* subcmd = (char*)malloc(len); strncpy(subcmd, input, len); subcmd[len] = '\0'; parseC(subcmd); cmd++; strcpy(input, delim+1); } else { parseC(input); return; } } }
void parseC(char* input) { while (input[0] == ' ') strcpy(input, input+1); while (input[strlen(input)-1] == ' ') input[strlen(input)-1] = '\0';
while (input[0] != 0) { char* delim = strchr(input, ' '); if (delim) { int len = delim-input; char* subcmd = (char*)malloc(len); strncpy(subcmd, input, len); subcmd[len] = '\0'; parseD(subcmd); param++; strcpy(input, delim+1); } else { parseD(input); return; } } }
void parseD(char* input) { cmdparams[block][cmd][param] = input; } //---------------------------------------------------------------------------
void childproc(char* params[], int pipes[2]) { // Wait for event?
printf("%s %d %d\n", params[0], pipes[0], pipes[1]); if (pipes[0] != 0) { //printf("HERE1!!!!\n"); dup2(pipes[0], 0); close(pipes[0]); } if (pipes[1] != 0) { //printf("HERE2!!!!\n"); dup2(pipes[1], 1); close(pipes[1]); }
execvp(params[0], params);
perror("error executing child process"); exit(1); }
//---------------------------------------------------------------------------
int main(int argc, char* argv[]) { char input[] = "ls -l | cut -c 1-20 | cut -c 1-1 \0"; parseA(input); if (input[0] == 0) {perror("no params"); exit(1);}
int pipes[20][2]; for (int i = 0; i < block; i++) { if (pipe(pipes[i]) == -1) {perror("error creating pipe"); exit(1);} } //printf("%d", block); //printf("%d\n", pipes[0][0]); //printf("%d\n", pipes[0][1]); //printf("%d\n", pipes[1][0]); //printf("%d\n", pipes[1][1]);
//printf("%s, %s\n", pipes[1][0], pipes[1][1]);
int i = 0; int e = 0; int ids[20]; while (cmdparams[i][e][0] != NULL) {
while (cmdparams[i][e][0] != NULL) { if (cmdparams[i][e][0] != NULL) { if ((ids[i*e] = fork()) == -1) {perror("error creating child process"); exit(1);} int pipe[2]; if (i == 0) pipe[0] = 0; else pipe[0] = pipes[i-1][1]; if (cmdparams[i+1][e][0] == NULL) pipe[1] = 0; else pipe[1] = pipes[i][0];
if (ids[i*e] != 0) childproc(cmdparams[i][e], pipe); }
e++; } i++; e = 0; }
printf("from parent\n"); getchar(); return 0; } //---------------------------------------------------------------------------
|
Пробую выполнить но получаю
| Цитата | ls 0 3 from parent cut 4 5 cut: -: Bad file descriptor cut 6 0 cut: -: Bad file descriptor ls: write error: Bad file descriptor
|
проблема с пайпами, но так и немогу понять где |