2012-10-22 21 views
1

我寫我自己的殼了家庭作業,和我遇到了這個問題:錯誤的文件描述符問題殼

每當我輸入一個命令的輸出重定向到一個文件(即LS -al>輸出.txt)我的shell應該處理命令並重定向輸出。不過,我不斷收到此消息顯示:ls: write error: Bad file descriptor

我在其他幾個論壇上,這可能是在參考超出的內存量看見了,但我不明白怎麼會是問題。這裏有一點我的代碼(如果你需要更多的透明度,隨時問):

loc[0] = argv[0];     //used for execution 

while(argv[count] != 0){     //loop through commands 
    if(strcmp(argv[count], "<") == 0) //and test for certain flags 
     inFlag = 1; 
    else if(strcmp(argv[count], ">") == 0){ 
     argv[count] = argv[count+1] = 0; 
     outFlag = 1; 
    } 
    else if(strcmp(argv[count], "&") == 0) 
     bgFlag == 1; 
    else if(strcmp(argv[count], "|") == 0){ 
     argv[count] = 0; 
     loc[pipes+1] = argv[count+1]; 
     pipes++; 
    } 
     count++; 
} 

for(k = 0; k <= pipes; k++){ 
    if(j < pipes){ 
     pipe(r_tube); 
     j++; 
    } 

    pid = fork(); 

    if(pid > 0){ 
     if(j > 0){ 
      close(l_tube[0]); 
      close(l_tube[1]); 
     } 
     l_tube[0] = r_tube[0]; 
     l_tube[1] = r_tube[1]; 
    } 
    else if(pid == 0){ 
     if((k == 0) && (inFlag == 1)){ 
      int n = open("input.txt", "r"); 
      close (0); 
      dup (n); 
      close (n); 
     } 
     else if((k > 0) && (k < pipes)){ 

     } 
     else if((k == pipes) && (outFlag == 1)){ //<-----issue 
      int out = open("output.txt", 0666); 
      close (1); 
      dup (out); 
      close (out); 
     } 
     else if(k == pipes){ 

     } 
     execvp(argv[loc[k]], &argv[loc[k]]); 

回答

-1

試試這個:

count = 0; 

loc[0] = argv[0];     //used for execution 

count++; 
pipes++; 

char* outFile = NULL; 
char* inFile = NULL; 

while(argv[count] != 0) 
{ 
    if(strcmp(argv[count], "<") == 0) 
    { 
     inFile = strdup(argv[count+1]); 
     inFlag = 1; 
     count++; 
    } 
    else if(strcmp(argv[count], ">") == 0) 
    { 
     outFile = strdup(argv[count+1]); 
     outFlag = 1; 
     count++; 
    } 
    else if(strcmp(argv[count], "&") == 0) 
    { 
     bgFlag == 1; 
    } 
    else if(strcmp(argv[count], "|") == 0) 
    { 
     argv[count] = 0; 
     loc[pipes] = argv[count+1]; 
     pipes++; 
    } 

    count++; 
} 

int current_out = STDOUT_FILENO; 
int current_in = STDIN_FILENO; 
int next_in = STDIN_FILENO; 

for(k = 0; k <= pipes; k++) 
{ 
    int cur_pipes[2]; 

    int last_out = current_out; 
    int last_in = current_in; 

    if(k < pipes) 
    { 
     pipe(cur_pipes); 
     current_out = cur_pipes[1]; 
     current_in = next_in; 
     next_in = cur_pipes[0]; 
    } 

    else if (k == pipes) 
    { 
     current_out = STDOUT_FILENO; 
     current_in = next_in; 
    } 

    pid = fork(); 

    if(pid > 0) 
    { 
     if(k > 0) 
     { 
      close(last_out); 
      close(last_in); 
     } 
    } 
    else if(pid == 0) 
    { 
     if((k == 0) && (inFlag == 1)) 
     { 
      int n = open(inFile, O_RDONLY); 
      if (n == -1) 
      { 
       printf("Could not open input file!\n"); 
       exit(1); 
      } 
      current_in = n; 
     } 

     if((k == pipes) && (outFlag == 1)) 
     { 
      int n = open(outFile, O_WRONLY | O_CREAT, 0666); 
      if (n == -1) { 
       printf("Could not open output file!\n"); 
       exit(1); 
      } 

      current_out = n; 
     }  

     if (current_in != STDIN_FILENO) 
     { 
      close (STDIN_FILENO); 
      dup (current_in); 
      close (current_in); 
     } 

     if(current_out != STDOUT_FILENO) 
     { 
      close(STDOUT_FILENO); 
      dup(current_out); 
      close(current_out); 
     } 

     execvp(argv[loc[k]], &argv[loc[k]]); 
    } 
} 
+0

所以更多...,事實證明,我居然無法打開文件(它返回printf語句)。有沒有辦法來解決這個問題?它可以在我的權限(0666)? – Baelix

+0

在我的目錄下,文件「output.txt中」已經存在(在同一目錄這個shell程序)。我將如何設置密碼寫入權限? – Baelix

+0

這些變化解決了這個問題,但現在正在引發另一個問題。如果您在我的代碼中注意到,在看到「>」存在之後,我將「>」和輸出文件名都設置爲NULL。我收到此錯誤,這我現在再次接收:LS:不能訪問>:沒有這樣的文件或目錄 – Baelix

0

INT開放(常量字符路徑,詮釋旗幟,.. 。/ mode_t mode * /);

你爲了需要的訪問模式,以決定你將與該文件做什麼。例如(O_WRONLY - 只或O_RDONLY寫 - 只讀)BTW有很多比這2

+0

我已經指出了這一點。他正在使用標準C開放標誌,而不是POSIX開放標誌。除此之外還有問題。 –

相關問題