2016-09-25 67 views
0

我是C编程的新手,我试图在我的程序中实现重定向功能。C Bash Shell重定向

我一直在研究这段代码一段时间,真的会欣赏一些新的观点。

正如我前面所说,我正在努力添加bash所具有的重定向功能(例如:pwd> output.txt),代码能够处理大部分基本功能,我已经评论过我如何将它们放在一起。

我的问题(S):

  1. 将创建一个函数,用户命令重定向是一些聪明写?我的代码很肮脏(我会很感激任何关于清理的提示)
  2. 我假设我将再次使用strcmp,在一个控件中搜索'<',然后在另一个控件中搜索'>',但我是不确定我应该把搜索放在哪里(如果我留在主体中,而不创建新的功能)。

我目前正在打砖墙,也许是因为我一直在这一整天,这里是我的外壳看起来像现在, 谢谢大家。

#include <string.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h> 

/* C based bash shell, working on creating redirects..*/ 
int main(int argc, char* argv[]){ 
    char cmnd[1024]; 
    char* str1[100];   
    const char* str2 = "exit"; //string to compare for exit 
    char* fullpath= "/bin/";//path set to bin 
    char progpath[100];//complete file path 
    const char* home = getenv("HOME"); //get home enviornment 
    char directory[1024]; 
    char fileD[100]; 
    char* str3[100]; 
if(argc == 1) 
    { 
while(1){ 

    printf("dollar sign: "); 

    if(!fgets(cmnd, 1024, stdin)) 
     break; 
    size_t length = strlen(cmnd); 
    if (cmnd[length - 1] == '\n') 
     cmnd[length - 1] = '\0'; 

    if(strcmp(cmnd, str2)==0){ //check if cmnd is exit 
     break; 
    } 

    char *tkn; //split cmnd into separate strings 
    tkn = strtok(cmnd," "); 
    int i=0; 
    while(tkn!=NULL){//until null is reached, break into tkns 
     str1[i]=tkn;  
     tkn = strtok(NULL," "); 
     i++; 
    } 
    str1[i]=NULL; //last set to Null (EXECVP req) 
    strcpy(progpath, fullpath); //copy string /bin/ to file path, for running 
    if(strcmp(cmnd, "help")==0)//help line 
    { 
     printf("Enter bash cmnds, or 'exit' to exit\n");//if help is issued, ignore the rest 
    } else if(strcmp(cmnd, "today")==0){//today doesn't exist, but "date" does. Replace today with date. 
     strcat(progpath, "date"); 
    }else{ 
    strcat(progpath, str1[0]);//add program to path 
    } 

    for(i=0; i<strlen(progpath); i++){//delete newcmnd 
     if(progpath[i]=='\n'){  
      progpath[i]='\0'; 
     } 
    }//start fork 
    int pid= fork(); 

    if(pid==0){//if child 
    if(strcmp(str1[0],"cd") == 0) 
    { 
     int tempCmnd; 
     if(!str1[1]) 
     { 

      tempCmnd = chdir(getenv("HOME"));//for moving home 
      if(tempCmnd == -1) 
      { 
       fprintf(stderr, "CD err.\n"); 
      }else{   
      getcwd(directory, sizeof(directory)); 
      printf("Currently in %s\n", directory); 
      } 
     }else{ 

      tempCmnd = chdir(str1[1]); 
      if(tempCmnd == -1) 
      { 
       fprintf(stderr, "CD err.\n"); 
      }else{   
       getcwd(directory, sizeof(directory)); 
       printf("Move Success : %s\n", directory);//pwd 
      } 
      } 
     break;//(end child proc) 
    }else{ 
     execvp(progpath,str1); 
     if(strcmp(cmnd, "help")==0)//ignore the error if help is issued, otherwise standard error 
     { 
      //printf("Test") 
     } 
     else{fprintf(stderr, "Child process could not do execvp\n");} 
    break;} 
    } 
    else{//Parent control 
     wait(NULL); 
    } 
    } 
}else{ 

    FILE * pfile; 
    pfile = fopen(argv[1], "r"); 
    if (pfile == 0) 
    { 
     printf("Opening file failed\n"); 
    }else{ 
     fgets(fileD, 100, pfile);//fget file into string 
     fclose(pfile);//close 

     char *tkn; //split pCommand by strtok 
     tkn = strtok(fileD," "); 
     int i=0; 
     while(tkn!=NULL){//until null is reached, break into tkns 
     str1[i]=tkn;  
     tkn = strtok(NULL," "); 
     i++; 
     } 
     str1[i]=NULL; //end set to null 
     strcpy(progpath, fullpath); //copy full path to program path 
     strcat(progpath, str1[0]);//add cmnd to the path 
     for(i=0; i<strlen(progpath); i++){// iterate to delete newcmnd 
     if(progpath[i]=='\n'){  
      progpath[i]='\0'; 
     } 
     } 
     execvp(progpath,str1); //execvp with new string 
    } 
} 
} 

回答

0

是的,有办法把它在一起是通过使用strcmp()找到字符<。您可以使用strtok()strchr()分割字符串来获取文件名。然后将文件的内容作为参数传递给命令。

输出重定向:

打开文件描述符,并使用dup2(fd, 1)写打开文件描述符。

或者,使用popen()来执行该命令,并将输出写入文件。

+0

我应该在哪里放置它?正如你可以看到我已经使用strcmp和strtok我也看到了execvp(也在我的代码中)。感谢您的回复,虽然 –

+0

我改进了我的答案。我想如果你google'dup2'或'popen',你会更好地理解它。 –

+0

我给它一个镜头,非常感谢你的回复。 –