修改之前的myshell使之支持輸入輸出重定向

1.open函數
這里寫圖片描述
????這個函數是打開一個文件(文件名叫pathname),以 flag 權限打開,flag 包括了以下幾種 O_RDONLY(只讀), O_WRONLY(只寫), O_RDWR(讀寫),當文件打開成功時,函數返回所要打開的文件名, 當函數執行失敗時,函數返回 -1.
2.write函數
這里寫圖片描述
?????write函數是打開一個文件描述符為 fd 的文件,并將該文件的內容寫到 buf 中, 同時期望寫 count 個字節,當函數執行成功時返回往 buf 中所寫的字節數, 失敗時返回值為 -1
3.之前編寫的自主shell進行修改,使其支持輸入/輸出/追加重定向

#include<stdio.h>
#include<fcntl.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>int main()
{char buf[1024] = { 0 };while(1){printf("mysell#");fflush(stdout);size_t s = read( 0, buf, sizeof( buf ) );if(s > 0){buf[s - 1] = '\0';printf("%s\n", buf);}char* start = buf;char* _argv[32];char* argnext[32];_argv[0] = buf;int i = 1;while(*start){if(*start == ' '){*start = '\0';start ++;_argv[i++] = start;}else{start ++;}}_argv[i] = NULL;pid_t pid;pid = fork();if(pid < 0){perror("fork");exit(1);}if(pid == 0){int i = 0;int fd = 0;for(i = 0; _argv[i] != NULL; i++){if(strcmp(_argv[i], ">") == 0){char* file_name = _argv[i + 1];_argv[i] = NULL;close(1);fd = open(file_name, O_CREAT | O_WRONLY, 0664);break;}}execvp(_argv[0], _argv);close(fd);}else if(pid > 0){int st = 0;waitpid(pid, NULL, 0);}}return 0;
}

????上一次寫了一個簡單的shell, 這次給其加上輸入重定向
????????????????????????????這里寫圖片描述
這里寫圖片描述
????雖然加入了輸出重定向,但還沒有加入輸入重定向以及追加重定向,以及感到,在下一篇當中將會給其加入輸入重定向, 追加重定向以及管道。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384095.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384095.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384095.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

HDU - 6621 K-th Closest Distance——主席樹+二分

【題目描述】 HDU - 6621 K-th Closest Distance 【題目分析】 因為看到第kkk大的要求&#xff0c;剛開始的時候一直都在想怎么運用第kkk大來解決問題&#xff0c;但是后來看其他人的博客才發現并不需要用第k大&#xff0c;但是主席樹維護權值線段樹還是需要的&#xff0c;這…

鏈表相關的算法題大匯總 — 數據結構之鏈表奇思妙想

http://blog.csdn.net/lanxuezaipiao/article/details/22100021基本函數&#xff08;具體代碼實現見后面&#xff09; 1&#xff0c;構造節點 //定義節點類型 struct Node { int value; Node*next; }; 2&#xff0c;分配節點 //之所以要分配節點原因是需要在分配函數中…

CodeForces - 372CWatching Fireworks is Fun+DP+單調隊列優化

【題目描述】 CodeForces - 372CWatching Fireworks is Fun 題目的大概意思就是在一個編號為1…n的街道上現在按照時間順序放煙花&#xff0c;每個煙花獲得的幸福感為b?abs(a?x)b-abs(a-x)b?abs(a?x)&#xff0c;x為觀看煙花的位置&#xff0c;為了提升我們的幸福感&#x…

雙向鏈表的基本操作

1.雙向鏈表的數據結構 typedef char DLinkType;typedef struct DLinkNode { DLinkType data; struct DLinkNode* next; struct DLinkNode* prev; }DLinkNode; 雙向帶頭結點的鏈表有三個成員&#xff0c; 一個是數據&#xff0c; 一個是指針 next 指向當前結點的下一個結點&…

匿名管道

1.進程通信的目的 (1) 數據傳輸: 一個進程需要將它的數據傳輸給另一個進程 ????(2) 資源共享: 多個進程之間共享同樣的資源 ????(3) 通知事件: 一個進程需要向另一個或一組進程發送消息, 通知它們發生了什么事情 2.管道 管道是一種進程之間通信的一種方式, 我們把從…

Currency Exchange——最短路Bellman-Ford算法

【題目描述】 Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the sam…

C++實現String類

http://blog.csdn.net/randyjiawenjie/article/details/6709539 C實現String類&#xff0c;還沒有完成&#xff0c;待繼續。 有以下注意的點&#xff1a; &#xff08;1&#xff09;賦值操作符返回的是一個MyString&&#xff0c;而重載的返回的是一個MyString。其中的原因…

POJ 3370 Halloween treats——鴿巢原理+思維

【題目描述】 POJ 3370 Halloween treats Description Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a chi…

將信號量代碼生成靜態庫以及動態庫

1.信號量相關代碼生成靜態庫 2.信號量相關代碼生成動態庫

Wormholes——Bellman-Ford判斷負環

【題目描述】 While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of…

C++11 標準新特性:Defaulted 和 Deleted 函數

https://www.ibm.com/developerworks/cn/aix/library/1212_lufang_c11new/index.html Defaulted 函數 背景問題 C 的類有四類特殊成員函數&#xff0c;它們分別是&#xff1a;默認構造函數、析構函數、拷貝構造函數以及拷貝賦值運算符。這些類的特殊成員函數負責創建、初始化、…

順序表實現棧相關操作

1.棧的相關概念 棧是一種特殊的線性表, 其中只允許在固定的一端進行插入和刪除元素.進行數據插入和刪除的一端叫做棧頂, 另一端成為棧底. 不含任何元素的棧稱為空棧, 棧又稱為先進先出的線性表. 2. 順序棧的結構 3. 順序棧的具體操作 (1). 數據結構 typedef char SeqStackTyp…

MPI Maelstrom——Dijkstra

【題目描述】 BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee’s research advisor, Jack Swigert, has asked her to bench…

雙向帶環帶頭結點的鏈表實現棧

1. 數據結構 利用帶頭結點帶環的結點實現棧的相關操作.因此, 每一個結點包括了一個前驅, 一個后繼, 還有一個數據成員 typedef char DLinkStackType;typedef struct DLinkStack {DLinkStackType data;struct DLinkStack* next;struct DLinkStack* prev; }DLinkStack;2. 初始化…

Cow Contest——Floyed+連通性判斷

【題目描述】 N (1 ≤ N ≤ 100) cows, conveniently numbered 1…N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors. The contest …

C++11 標準新特性:委派構造函數

https://www.ibm.com/developerworks/cn/rational/1508_chenjing_c11/index.html陳 晶2015 年 8 月 11 日發布WeiboGoogle用電子郵件發送本頁面 1本文首先介紹了在委派構造函數提出之前類成員構造所面臨的問題&#xff0c;再結合實例介紹了委派構造函數的用法&#xff0c;并說明…

順序表實現隊列

一. 隊列相關概念 隊列是只允許在一段進行插入元素, 在另一端進行刪除元素的線性表,即只允許對隊列進行尾插,頭刪的操作.隊列具有先進先出, 后進后出的特性. ???????? 1.初始化 void SeqQueInit(SeqQue* q) {if(q NULL){return;//非法輸入}q -> head 0;q -> …

Arbitrage——判斷正環Bellman-Ford/SPFA

【題目描述】 Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French …

鏈表實現隊列

上篇博客是用順序表實現隊列, 現在用雙向帶頭結點帶環鏈表實現對隊列的出隊列, 入隊列, 取隊首元素, 以及銷毀隊列的相關操作 1.初始化鏈表 void DLinkQueInit(DLinkQue** q) {if(q NULL){return;//非法輸入}if(*q NULL){return;//非法輸入帶頭結點的鏈表至少有一個傀儡結點…

HDU - 1796——容斥原理+二進制枚舉

【題目描述】 Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N12, and M-integer set is {2,3}, so there is another set {2,3,4,…