簡單shell 命令重新導向

來源:互聯網
上載者:User

今天晚上花了一會寫的,開始用的是 argv 是個二位元組, 調程式的時候總是顯示不出,後來發現execvp貌似只支援 pointer array =。= 好吧。。。。
程式思路:開子進程以後把 子進程的標準輸出 關閉, 然後在子進程中建立一個檔案,此時檔案會選擇 自身進程中最小的檔案標識符 作為該檔案的檔案標識符,也就是fd

所以,以後子進程的所有標準輸出都流向了檔案, 這個應該是簡單的思路了吧,感覺GNU的源碼應該是用無名管道,甚至是有名管道(網路傳輸) 實現的,好吧我就不意淫了。。。 有興趣的朋友可以去官網上找找 GNU/Linux 的shell 原始碼

/*  1.c *  author:wangshiyang *  date: 2013/5/13 */#include <stdio.h>#include <unistd.h>#include <string.h>//------------------------------------------------------/* for the convience I'v set a lot of global-variables | * the formal shell program may not dit like this   | * */// warning: use argv[][] is wrong !!!  |char input[100];// the command you input               |char *argv[30];//the execvp must use point array   |char file[100];//you can input a filename              |//------------------------------------------------------char *newstr( char *s, int l ){char * rv = (char *)malloc( l+1 );rv[l] = '\0';strncpy( rv, s, l);return rv;}int deal()// deal the input command{char temp[100];int len, i, a, turn;fgets(input, 100, stdin);len = strlen(input);turn = 0; a = 0; for( i = 0; i < len; i++ ){if( input[i] == '\n' ){argv[turn++] = newstr( temp, strlen(temp) );break;}if( input[i] != ' ' ){temp[a++] = input[i];temp[a] = '\0';}else{argv[turn++] = newstr( temp, strlen(temp) );a = 0;}}for( i = 0; i < turn; i++ )if( !strcmp( argv[i], ">" )){strcpy( file, argv[i+1]);//copy the filename to file arrayreturn i;}return -1;};void execute( int flag )// run the command {intpid, fd;argv[flag] = NULL;if( (pid = fork() ) == -1 ){perror( "fork" ); exit(1);}if ( pid == 0 ){/* 0 for stdin, 1 for stdout, 2 for stderr */close(1);// close the stdout fd = creat( file, 0777 );// then open and fd connect to 1execvp( argv[0], argv );// and runperror( "execvp" );exit(1);}if ( pid != 0 ){wait(NULL);printf( "Father Done\n" );}};void free_all( )// free all the pointers{int i;for( i = 0; i < 30; i++ )free(argv[i]);printf( "All the pointer have been free.." )};int main(){int flag;printf( "Please input a command,please using the '>'\n" );printf( "example: ls -ail > 123 \n" );flag = deal();if( flag == -1 ){fprintf( stderr, "input wrong! do not include '>'\n");exit(1);}execute( flag );free_all();return 0;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.