Last active
October 7, 2018 13:52
-
-
Save 8q/7fa61bce2508a3b066fb6955d43f1a05 to your computer and use it in GitHub Desktop.
uniq < tmp.txt | wc -l
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#define MAX_READ 128 | |
int main(int argc, char *argv[]) { | |
// 読み込むファイルが指定されているか | |
if(argc <= 1) { | |
fprintf(stderr, "1つ以上のファイルを指定してください。\n"); | |
return 1; | |
} | |
// concat.txtを準備 | |
int fdDst = open("concat.txt", O_WRONLY | O_TRUNC | O_CREAT, 0666); | |
if(fdDst == -1) { | |
fprintf(stderr, "concat.txtの準備に失敗しました。\n"); | |
return 1; | |
} | |
// ファイルを一つずつ開いてconcat.txtに書き込む | |
for(int i = 0; i < argc - 1; i++) { | |
// 読み込みファイルを開く | |
int fd = open(argv[i + 1], O_RDONLY); | |
if(fd == -1) { | |
fprintf(stderr, "%d番目のファイルの読み込みに失敗しました。\n", i + 1); | |
fprintf(stderr, "ファイル名:%s\n", argv[i + 1]); | |
close(fdDst); | |
return 1; | |
} | |
//読み込みファイルからデータを読み込む | |
char buf[MAX_READ + 1]; | |
ssize_t numRead = read(fd, buf, MAX_READ); | |
if(numRead == -1) { | |
fprintf(stderr, "%d番目のファイルのデータ読み込み時にエラーが発生しました。\n", i + 1); | |
fprintf(stderr, "ファイル名:%s\n", argv[i + 1]); | |
close(fd); | |
close(fdDst); | |
return 1; | |
} | |
buf[numRead] = '\0'; | |
// 読み込んだデータを書き込む | |
ssize_t numWrite = write(fdDst, buf, strlen(buf)); | |
if(numWrite == -1) { | |
fprintf(stderr, "%d番目のファイルのデータのconcat.txtへの書き込み時にエラーが発生しました。\n", i + 1); | |
fprintf(stderr, "ファイル名:%s\n", argv[i + 1]); | |
close(fd); | |
close(fdDst); | |
return 1; | |
} | |
// 読み込みファイルを閉じる | |
close(fd); | |
} | |
// concat.txtを閉じる | |
fsync(fdDst); | |
close(fdDst); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/uio.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
int main() { | |
// tmp.txtを開く | |
int fdTmp = open("tmp.txt", O_RDONLY); | |
if(fdTmp == -1) { | |
perror("tmp.txtの読み込みに失敗しました。\n"); | |
return 1; | |
} | |
// パイプをつなぐ | |
int fd[2]; | |
if(pipe(fd) == -1) { | |
perror("パイプの生成に失敗しました。\n"); | |
return 1; | |
} | |
// プロセスをforkする | |
pid_t pid = fork(); | |
if(pid < 0) { | |
perror("forkに失敗しました。\n"); | |
return 1; | |
} | |
// 子プロセス | |
if(pid == 0) { | |
// 使わないファイルディスクリプタを閉じる | |
close(fd[1]); | |
close(fdTmp); | |
// stdinをパイプの出力に変更 | |
dup2(fd[0], STDIN_FILENO); | |
close(fd[0]); | |
// wcコマンドを実行 | |
execlp("wc", "wc", "-l", NULL); | |
} else { // 親プロセス | |
// 使わないファイルディスクリプタを閉じる | |
close(fd[0]); | |
// stdoutをパイプの入力に変更 | |
dup2(fd[1], STDOUT_FILENO); | |
close(fd[1]); | |
// stdinをtmp.txtに変更 | |
dup2(fdTmp, STDIN_FILENO); | |
close(fdTmp); | |
// uniqコマンドを実行 | |
execlp("uniq", "uniq", NULL); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment