Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*gcc uebung05.c -Wall -ansi -o uebung05.out*/
- #include <sys/types.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- char* mystdin(){
- int strlen = 4; /*string lenght*/
- char *str = malloc(strlen * sizeof(char)); /*input string*/
- char c; /*new char*/
- int counter = 0; /*position neuer char*/
- while((c = getchar()) != '\n'){ /*zeile einlesen*/
- if(counter >= strlen) /*falls länge überschritten*/
- str = realloc(str, (strlen += 4) * sizeof(char)); /* mache platz für 4 neue Zeichen*/
- str[counter++] = c; /*füge neuen cahr hinzu*/
- }
- str = realloc(str, (counter++) * sizeof(char));
- str[counter] = '\0'; /*beende string*/
- return str;
- }
- int main(void)
- {
- pid_t pid, wpid;
- int status = 0;
- int newSTDIN = 0;
- int newSTDOUT = 0;
- char* input = NULL;
- char* output = NULL;
- char* cmd;
- char* params;
- char* lcmd=mystdin();
- char* temp=strchr(lcmd,'<');
- if(temp!=NULL){
- input=lcmd;
- *(temp-1)='\0';
- temp++;
- cmd=temp;
- }
- else
- cmd=lcmd;
- output=strchr(cmd,'>');
- if(output!=NULL){
- *output='\0';
- output++;
- }
- while(*output==' ')
- *output++;
- while(*cmd==' ')
- *output++;
- params=strchr(cmd,' ');
- if(params!=NULL){
- *params='\0';
- params++;
- }
- printf("input:\"%s\"\n", input);
- printf("output:\"%s\"\n", output);
- printf("cmd:\"%s\"\n", cmd);
- printf("params:\"%s\"\n", params);
- if((pid=fork()) < 0)
- perror("fork error");
- else if(pid == 0) {
- if(input != NULL){
- newSTDIN = open(input, O_RDONLY);
- dup2(newSTDIN, STDIN_FILENO);
- close(newSTDIN);
- }
- if(output != NULL){
- newSTDOUT = open(output, O_RDONLY);
- dup2(newSTDOUT, STDIN_FILENO);
- close(newSTDOUT);
- }
- return execlp(cmd,cmd,params,NULL);
- }else {
- wpid = wait(&status);
- printf("Exit status of %d was %d (%s)\n", (int)wpid, status, (status == 0)? "OK" : "ERROR");
- /*free?*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement