簡體   English   中英

C Bash Shell重定向

[英]C Bash Shell Redirection

我是C語言編程的新手。我正在嘗試在程序中實現重定向功能。

我已經在這段代碼上工作了一段時間了,非常感謝一些新觀點。

正如我之前所說,我正在努力添加bash具有的重定向功能(例如:pwd> output.txt),該代碼能夠處理大多數基本功能,我評論了如何將其組合在一起。

我的問題:

  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
    }
}
}

是的,將其組合在一起的一種方法是使用strcmp()查找字符< 您可以使用strtok()strchr()分割字符串來獲取文件名。 然后將文件的內容作為參數傳遞給命令。

要重定向輸出:

打開文件描述符,然后使用dup2(fd, 1)進行寫操作以打開文件描述符。

或者,使用popen()執行命令,然后將輸出寫入文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM