Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <errno.h>
- #include <ctype.h>
- #include "util.h"
- #include <stdlib.h>
- //#define max_prs 11
- typedef struct pr_list{
- char **pr_arr;
- pid_t pid;
- struct pr_list *next;
- }pr_list;
- typedef struct pipe_list{
- char *line;
- struct pipe_list *next;
- pr_list *prs;
- }pipe_list;
- void printf_prs(char *array[]){
- int i;
- for (i = 0; array[i] != NULL /*&& array[i] != NULL*/; i++){
- printf("%s ", array[i]);
- }
- }
- void add_pr(char *pr, char **array[]){
- int i, j, len;
- /* count "non-NULL" cells */
- for (j = 0; (*array)[j] != NULL; j++);
- j += 2;//j "non-NULL" + 1 "NULL" + 1 extra for a new argument
- (*array) = realloc((*array), j*sizeof(char*));//36
- /* MOVE PR INTO CELL */
- for (i = 0; ((*array)[i]) != NULL && (i < j) ; i++);
- len = strlen(pr);
- ((*array)[i]) = calloc(len + 1, sizeof(char));
- for (j = 0; j < len; j++){
- ((*array)[i][j]) = pr[j];
- }
- ((*array)[i][j]) = '\0';
- /* NEXT CELL = NULL */
- ((*array)[i+1]) = NULL;
- }//49
- /* MAIN */
- int main(int argc, char *argv[]){
- int i, j, k, len, word_begin, word_end;
- int status;
- pid_t pid;
- int not_empty, back_mode, pr_flag, error_flag;
- int flag = 1, next_pr = 0;
- pipe_list *c_pipe, *pipes, *p_pipe/*pointer_pipe*/;
- pr_list *c_pr, *prs, *p_pr;
- char *new_line, *cur_word;
- printf("Please input pipe\n");
- while ((new_line = getline_unlim()) != NULL){
- /* WAIT BACK PROCESSES */
- if( !strcmp(new_line, "exit") || !strcmp(new_line, "EXIT") ){
- printf("exit\n");
- break;
- }
- /* processing line, creating pipe */
- if (!flag){//if pipe won't be the first one
- c_pipe = c_pipe->next;
- }
- /* allocate memory for current pipe */
- c_pipe = calloc(1, sizeof(pipe_list));
- c_pipe->next = NULL;
- c_pipe->prs = NULL;
- if (flag){
- pipes = c_pipe;
- flag = 0;
- }
- /* allocate memory for first pr in current pipe */
- c_pr = calloc(1, sizeof(pr_list));
- c_pr->pr_arr = calloc(1, sizeof(char*));
- c_pr->next = NULL;
- if (!strcmp(new_line,"")) continue;
- len = strlen(new_line);
- /* copy new_line to current_pipe->line */
- c_pipe->line = calloc(len + 1, sizeof(char));
- for (i = 0; i < len + 1; i++){
- c_pipe->line[i] = new_line[i];
- }
- word_begin = 0; next_pr = 0; error_flag = 0;
- not_empty = 0, back_mode = 0, pr_flag = 1;
- for (i = 0; i < len; i++){
- if (pr_flag){
- c_pipe->prs = c_pr;
- pr_flag = 0;
- not_empty = 0;
- }
- if (next_pr){
- c_pr->next = calloc(1, sizeof(pr_list));
- c_pr = c_pr->next;
- c_pr->pr_arr = calloc(1, sizeof(char*));
- c_pr->next = NULL;
- next_pr = 0;
- not_empty = 0;
- }
- if ((!isalnum(new_line[i]) && new_line[i] != '-')){
- if (new_line[i] == '|'){
- if (!not_empty) error_flag = 1;
- next_pr = 1;
- not_empty = 0;
- }
- if (new_line[i] == '&'){
- back_mode = 1;
- }
- new_line[i] = '\0';
- word_end = i;
- k = word_end - word_begin;
- if (k != 0){
- cur_word = malloc(sizeof(char));
- cur_word = realloc(cur_word, k);
- for (j = 0; j < k; j++){
- cur_word[j] = new_line[word_begin + j];
- }
- word_begin = i + 1;
- }
- if (word_begin != word_end){
- if (strcmp(cur_word,"")){
- if (!not_empty) not_empty = 1;
- add_pr(cur_word, &(c_pr->pr_arr));
- free(cur_word);
- }
- }
- }
- }
- k = i - word_begin;
- if (k != 0){
- cur_word = malloc(sizeof(char));
- cur_word = realloc(cur_word, k);
- for (j = 0; j < k; j++){
- cur_word[j] = new_line[word_begin + j];
- }
- }
- if (word_begin != i){
- if (strcmp(cur_word,"")){
- if (!not_empty) not_empty = 1;
- add_pr(cur_word, &(c_pr->pr_arr));
- free(cur_word);
- }
- }
- /* if (c_pipe->prs){
- if (c_pipe->prs->pr_arr)Х{
- printf_prs(c_pipe->prs->pr_arr);
- printf("\n");
- }
- while (c_pipe->prs->next){
- c_pipe->prs = c_pipe->prs->next;
- printf_prs(c_pipe->prs->pr_arr);
- printf("\n");
- }
- }
- */
- /* executing pipe */
- if (error_flag){
- printf("pipe was written incorrectly\n");
- /* free(c_pipe->line);
- p_pr = c_pipe->prs;
- while (p_pr){
- for (i = 0; p_pr->pr_arr[i] != NULL; i++){
- free(p_pr->pr_arr[i]);
- }
- free(p_pr->pr_arr);
- c_pipe->prs = p_pr->next;
- p_pr->next = NULL;
- free(p_pr);
- p_pr = c_pipe->prs;
- }*/
- }else if (!back_mode){
- /* STANDART MODE */
- int cnt = 0;
- p_pr = c_pipe->prs;
- while (p_pr){
- cnt++;
- if (p_pr->next == NULL) break;
- else (p_pr = p_pr->next);
- }
- p_pr = c_pipe->prs;
- int fd[cnt-1][2];
- for (i = 0; i < cnt - 1; i++){
- pipe(fd[i]);
- }
- for (i = 0; p_pr && i < cnt; i++){
- //printf_prs(p_pr->pr_arr);
- if (p_pr->pr_arr[0] != NULL){
- if ((p_pr->pid = fork()) < 0){
- perror("fork ");
- exit(1);
- }else if (p_pr->pid == 0){
- if (i < cnt - 1){
- dup2(fd[i][1], 1);
- }
- if (i > 0){
- dup2(fd[i-1][0], 0);
- }
- for (j = 0; j < cnt - 1; j++){
- close(fd[j][0]);
- close(fd[j][1]);
- }
- execvp(p_pr->pr_arr[0], p_pr->pr_arr);
- perror("exec ");
- _exit(1);
- }
- p_pr = p_pr->next;
- if (p_pr == NULL) break;
- }else break;
- }
- for (i = 0; i < cnt-1; i++){
- close(fd[i][0]);
- close(fd[i][1]);
- }
- for (i = 0; i < cnt ; i++){
- pid = wait(&status);
- if (WIFEXITED(status)){
- p_pr = c_pipe->prs;
- while (p_pr){
- if (p_pr->pid == pid){
- printf("program \"%s\" has been completed with exit status: %d\n", p_pr->pr_arr[0], WEXITSTATUS(status));
- if (i == cnt - 1){
- printf("pipe \"%s\" has been completed with exit status: %d\n", c_pipe->line, WEXITSTATUS(status));
- }
- break;
- }else{
- p_pr = p_pr->next;
- }
- if (p_pr == NULL){
- printf("there aren't process with pid == %d\n", (int)pid);
- break;
- }
- }
- }else{
- p_pr = c_pipe->prs;
- while (p_pr){
- if (p_pr->pid == pid){
- printf("program \"%s\" hasn't been completed\n", p_pr->pr_arr[0]);
- if (i == cnt - 1){
- printf("pipe \"%s\" hasn't been completed\n", c_pipe->line);
- }
- break;
- }else{
- p_pr = p_pr->next;
- }
- if (p_pr == NULL){
- printf("there aren't process with pid == %d\n", (int)pid);
- break;
- }
- }
- }
- }
- /* DELETE USED MEMORY */
- p_pipe = pipes;
- while (p_pipe && p_pipe != c_pipe){
- p_pipe = p_pipe->next;
- }
- if (p_pipe == NULL){
- printf("Something was going wrong((\n");
- }else{
- p_pipe->next = c_pipe->next;
- free(c_pipe->line);
- p_pr = c_pipe->prs;
- while (p_pr){
- for (i = 0; p_pr->pr_arr[i] != NULL; i++){
- p_pr->pr_arr[i] = NULL;
- }
- free(p_pr->pr_arr);
- c_pipe->prs = p_pr->next;
- free(p_pr);
- p_pr = c_pipe->prs;
- }
- free(c_pipe);
- }
- ///////////////////////////////
- }else{
- /* BACKGROUND MODE */
- printf("backgroung mode\n");
- }
- printf("Please input pipe\n");
- }
- /* WAIT BACK PROCESSES AND FREE ALL PIPES */
- /* */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement