#include #include #include #include #include #include #include "she.h" #include "rdir.h" #include "errors.h" /*************************************************************/ /* */ /* Funcion para extraer de la linea de comandos inform. */ /* relativa al redireccionamiento. */ /* devuelve codigo de error entradas: a_com: vector resultado de parsing de la linea de comandos correspondiente a un solo proceso. ( acaba en NULL ) salidas: el_proc: parte que correspondiente al proceso a ejecutar rdir: informacion sobre redirecciones */ /* */ /*************************************************************/ int rdir_parsing( char *l_com[MAX_LPROC], proc *el_proc) { int error = NO_ERR, fin = 0, inlproc = 1; int i = 0, j = 0; el_proc->rdir.in = 0; el_proc->rdir.out = 0; while ((!fin) && (error == NO_ERR)) { if (l_com[i] == NULL) { fin = 1; } else if (0 == strcmp(l_com[i], M_RDIRIN)) { inlproc = 0; if (el_proc->rdir.in != 0) error = SINTAX_ERR; else { el_proc->rdir.in = 1; el_proc->rdir.file_in = l_com[++i]; if (l_com[i] == NULL) error = FLIN_NEX; } } else if (0 == strcmp(l_com[i], M_RDIROUT)) { inlproc = 0; if (el_proc->rdir.out != 0) error = SINTAX_ERR; else { el_proc->rdir.out = 1; el_proc->rdir.file_out = l_com[++i]; if (l_com[i] == NULL) error = FLOUT_NEX; } } else if (0 == strcmp(l_com[i], M_RDIRAPP)) { inlproc = 0; if (el_proc->rdir.out != 0) error = SINTAX_ERR; else { el_proc->rdir.out = 2; el_proc->rdir.file_out = l_com[++i]; if (l_com[i] == NULL) error = FLOUT_NEX; } } else { /* default */ if (inlproc != 1) error = SINTAX_ERR; else el_proc->lproc[j++] = l_com[i]; } i++; }/* de while */ el_proc->lproc[j] = NULL; if ((j == 0) && (error == NO_ERR)) error = EMPTY; return(error); }/* de la funcion rdir_parsing */ /*************************************************************/ /* */ /* Funcion que separa los procesos independientes de la linea de comandos ( acabada en NULL ) Entrada: char *lcom[], linea de comandos troceada Salidas: aProcesar *procesos, procesos separados */ /* */ /*************************************************************/ void lcom_parsing( char *lcom[MAXTERM], aProcesar *procesos) { int i = 0, j = 0, start, fin = 0; procesos->error = NO_ERR ; while ((procesos->error == NO_ERR )&&(fin==0)) { start=j; while ((lcom[j] != NULL) && (0 != strcmp(lcom[j], M_PIPELINE))) { j++; } if (j == start )/* empty pipe */ { procesos->error = SINTAX_ERR; } else { /* otro proceso */ if ( lcom[j]==NULL ) fin=1; else lcom[j]=NULL; procesos->error = rdir_parsing( &lcom[start], &procesos->elProc[i]); i++; j++; } }/* while */ procesos->elProc[i].lproc[0]=NULL; } /*************************************************************/ int rdir_in( char *nombre) { int fd; if ((fd = open(nombre, O_RDONLY)) == -1) if (errno == EACCES) { DoError(SYS_ERR,INFO_ERR,"%s no existe", nombre); return(FLIN_NEX); } else DoError(SYS_ERR,ABORT_ERR,"can't open %s", nombre); close(fd); close(0); fd = open(nombre, O_RDONLY); if (fd != 0) { DoError(SYS_ERR,ABORT_ERR,"open %s, fd!=0", nombre); } return(NO_ERR); } /*************************************************************/ int rdir_out( char *nombre ) { int fd; if ((fd = creat(nombre, 0640)) == -1) if (errno == EACCES) { DoError(SYS_ERR,INFO_ERR,"%s existe y no se deja", nombre); return(ERR_OUT); } else DoError(SYS_ERR,ABORT_ERR,"can't create %s", nombre); close(fd); close(1); fd = open(nombre, O_WRONLY); if (fd != 1) { DoError(SYS_ERR,ABORT_ERR,"creat %s, fd!=1", nombre); } return(NO_ERR); } /*************************************************************/ int rdir_app( char *nombre) { int fd; if ((fd = open(nombre, O_RDWR | O_APPEND | O_CREAT)) == -1) if (errno == EACCES) { DoError(SYS_ERR,INFO_ERR,"%s existe y no se deja", nombre); return(ERR_OUT); } else DoError(SYS_ERR,ABORT_ERR,"can't open %s", nombre); fchmod(fd, 00600); close(fd); close(1); fd = open(nombre, O_WRONLY | O_APPEND); if (fd != 1) { DoError(SYS_ERR,ABORT_ERR,"open %s, fd!=1", nombre); } return(NO_ERR); }