-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6eb527c
commit 34b3a7a
Showing
7 changed files
with
306 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/wait.h> | ||
|
||
ssize_t readline(int fd, char* buf, int size); | ||
|
||
int main(int argc, char const *argv[]) { | ||
char* buffer = calloc(1024, sizeof(char)); | ||
char* args[1024]; | ||
size_t len = 0; | ||
size_t back_n = 0; | ||
while((len = readline(STDIN_FILENO, buffer, 1024)) > 0) { | ||
size_t i = 0; | ||
char* token; | ||
while((token = strtok_r(buffer, " ", &buffer))) { | ||
args[i++] = strdup(token); | ||
} | ||
int background = 0; | ||
if(strcmp(args[0], "exit") == 0) { | ||
break; | ||
} | ||
if(strcmp(args[i-1], "&") == 0) { | ||
background = 1; | ||
i--; | ||
} | ||
args[i] = NULL; | ||
|
||
int pid; | ||
if(!(pid = fork())) { | ||
execvp(args[0], args); | ||
} | ||
else { | ||
if(!background) waitpid(pid, NULL, 0); | ||
else back_n++; | ||
} | ||
} | ||
for(size_t i = 0; i < back_n; i++) wait(NULL); | ||
return 0; | ||
} | ||
|
||
ssize_t readline(int fd, char* buf, int size) { | ||
ssize_t bytesRead = read(fd, buf, size); | ||
if(!bytesRead) return 0; | ||
|
||
size_t len = strcspn(buf,"\n"); | ||
if(bytesRead < len) len = bytesRead; | ||
buf[len] = 0; | ||
|
||
lseek(fd, len - bytesRead, SEEK_CUR); | ||
return len; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
#include <string.h> | ||
#include <sys/wait.h> | ||
#include <stdlib.h> | ||
|
||
int main(int argc, char const *argv[]) { | ||
if(argc < 6) exit(1); | ||
|
||
int input_fd; | ||
int output_fd; | ||
|
||
if(strcmp(argv[1], "-i") == 0) { | ||
input_fd = open(argv[2], O_RDONLY); | ||
} else exit(1); | ||
|
||
if(strcmp(argv[3], "-o") == 0) { | ||
output_fd = open(argv[4], O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
} else exit(1); | ||
|
||
dup2(input_fd, STDIN_FILENO); | ||
dup2(output_fd, STDOUT_FILENO); | ||
|
||
close(input_fd); | ||
close(output_fd); | ||
|
||
argv[argc] = NULL; | ||
|
||
if(fork() == 0) { | ||
execvp(argv[5], argv + 5); | ||
_exit(1); | ||
} else wait(NULL); | ||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/wait.h> | ||
|
||
ssize_t readline(int fd, char* buf, int size); | ||
|
||
int main(int argc, char const *argv[]) { | ||
char* buffer = calloc(1024, sizeof(char)); | ||
char* args[1024]; | ||
size_t len = 0; | ||
size_t back_n = 0; | ||
|
||
int stdin_backup = dup(STDIN_FILENO); | ||
int stdout_backup = dup(STDOUT_FILENO); | ||
int stderr_backup = dup(STDERR_FILENO); | ||
|
||
while((len = readline(STDIN_FILENO, buffer, 1024)) > 0) { | ||
size_t i = 0; | ||
char* token; | ||
while((token = strtok_r(buffer, " ", &buffer))) { | ||
args[i] = strdup(token); | ||
if(i > 1) { | ||
if(strcmp(args[i-1],">") == 0) { | ||
int fd = open(args[i], O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
dup2(fd, STDOUT_FILENO); | ||
close(fd); | ||
i-=2; | ||
} | ||
else if(strcmp(args[i-1],">>") == 0) { | ||
int fd = open(args[i], O_WRONLY | O_CREAT | O_APPEND, 0644); | ||
dup2(fd, STDOUT_FILENO); | ||
close(fd); | ||
i-=2; | ||
} | ||
else if(strcmp(args[i-1],"<") == 0) { | ||
int fd = open(args[i], O_RDONLY); | ||
dup2(fd, STDIN_FILENO); | ||
close(fd); | ||
i-=2; | ||
} | ||
else if(strcmp(args[i-1],"2>") == 0) { | ||
int fd = open(args[i], O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
dup2(fd, STDERR_FILENO); | ||
close(fd); | ||
i-=2; | ||
} | ||
else if(strcmp(args[i-1],"2>>") == 0) { | ||
int fd = open(args[i], O_WRONLY | O_CREAT | O_APPEND, 0644); | ||
dup2(fd, STDERR_FILENO); | ||
close(fd); | ||
i-=2; | ||
} | ||
} | ||
i++; | ||
} | ||
int background = 0; | ||
if(strcmp(args[0], "exit") == 0) { | ||
break; | ||
} | ||
if(strcmp(args[i-1], "&") == 0) { | ||
background = 1; | ||
i--; | ||
} | ||
args[i] = NULL; | ||
|
||
int pid; | ||
if(!(pid = fork())) { | ||
execvp(args[0], args); | ||
_exit(1); | ||
} | ||
else { | ||
if(!background) waitpid(pid, NULL, 0); | ||
else back_n++; | ||
} | ||
|
||
dup2(stdin_backup, STDIN_FILENO); | ||
dup2(stdout_backup, STDOUT_FILENO); | ||
dup2(stderr_backup, STDERR_FILENO); | ||
} | ||
|
||
for(size_t i = 0; i < back_n; i++) wait(NULL); | ||
|
||
close(stdin_backup); | ||
close(stdout_backup); | ||
close(stderr_backup); | ||
|
||
return 0; | ||
} | ||
|
||
ssize_t readline(int fd, char* buf, int size) { | ||
ssize_t bytesRead = read(fd, buf, size); | ||
if(!bytesRead) return 0; | ||
|
||
size_t len = strcspn(buf,"\n"); | ||
if(bytesRead < len) len = bytesRead; | ||
buf[len] = 0; | ||
|
||
lseek(fd, len - bytesRead, SEEK_CUR); | ||
return len; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/wait.h> | ||
|
||
int main(int argc, char const *argv[]) { | ||
int pipe_fd[2]; | ||
if(pipe(pipe_fd) < 0) { | ||
perror("pipe"); | ||
exit(1); | ||
} | ||
|
||
if(fork() == 0) { | ||
close(pipe_fd[0]); | ||
dup2(pipe_fd[1], STDOUT_FILENO); | ||
close(pipe_fd[1]); | ||
execlp("ls", "ls", "/etc", NULL); | ||
_exit(1); | ||
} | ||
else if (fork() == 0) { | ||
close(pipe_fd[1]); | ||
dup2(pipe_fd[0], STDIN_FILENO); | ||
close(pipe_fd[0]); | ||
execlp("wc", "wc", "-l", NULL); | ||
_exit(1); | ||
} | ||
else { | ||
close(pipe_fd[0]); | ||
close(pipe_fd[1]); | ||
if (wait(NULL) == -1) puts("Error"); | ||
if (wait(NULL) == -1) puts("Error"); | ||
} | ||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/wait.h> | ||
|
||
int main(int argc, char const *argv[]) { | ||
int pipe_fd[2][2]; | ||
if(pipe(pipe_fd[0]) < 0) { | ||
perror("pipe"); | ||
exit(1); | ||
} | ||
|
||
if(fork() == 0) { | ||
close(pipe_fd[0][0]); | ||
dup2(pipe_fd[0][1], STDOUT_FILENO); | ||
close(pipe_fd[0][1]); | ||
execlp("grep", "grep", "-v", "^#", "/etc/passwd", NULL); | ||
_exit(1); | ||
} | ||
|
||
close(pipe_fd[0][1]); | ||
|
||
if(pipe(pipe_fd[1]) < 0) { | ||
perror("pipe"); | ||
exit(1); | ||
} | ||
|
||
if (fork() == 0) { | ||
close(pipe_fd[1][0]); | ||
|
||
dup2(pipe_fd[0][0], STDIN_FILENO); | ||
close(pipe_fd[0][0]); | ||
dup2(pipe_fd[1][1], STDOUT_FILENO); | ||
close(pipe_fd[1][1]); | ||
execlp("cut", "cut", "-f7", "-d:", NULL); | ||
_exit(1); | ||
} | ||
|
||
close(pipe_fd[0][0]); | ||
close(pipe_fd[1][1]); | ||
|
||
if(pipe(pipe_fd[2]) < 0) { | ||
perror("pipe"); | ||
exit(1); | ||
} | ||
|
||
if (fork() == 0) { | ||
close(pipe_fd[2][0]); | ||
|
||
dup2(pipe_fd[1][0], STDIN_FILENO); | ||
close(pipe_fd[1][0]); | ||
dup2(pipe_fd[2][1], STDOUT_FILENO); | ||
close(pipe_fd[2][1]); | ||
execlp("uniq", "uniq", NULL); | ||
_exit(1); | ||
} | ||
|
||
close(pipe_fd[1][0]); | ||
close(pipe_fd[2][1]); | ||
|
||
if (fork() == 0) { | ||
dup2(pipe_fd[2][0], STDIN_FILENO); | ||
close(pipe_fd[2][0]); | ||
execlp("wc", "wc", "-l", NULL); | ||
_exit(1); | ||
} | ||
|
||
close(pipe_fd[2][0]); | ||
|
||
if (wait(NULL) == -1) puts("Error"); | ||
if (wait(NULL) == -1) puts("Error"); | ||
if (wait(NULL) == -1) puts("Error"); | ||
if (wait(NULL) == -1) puts("Error"); | ||
|
||
return 0; | ||
} |