Skip to content

Commit

Permalink
Added more exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
RisingFisan committed May 10, 2020
1 parent 6eb527c commit 34b3a7a
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Guião 1/mycp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int main(int argc, char const *argv[]) {
int source = open(argv[1], O_RDONLY);
if(source == -1) {
printf("ERROR - %s - No such file or directory\n", argv[1]);
return 1;
exit(1);
}

int dest = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
Expand Down
13 changes: 5 additions & 8 deletions Guião 3/Ex4.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@

int main(void) {
char buffer[1024];
puts("Number of elements:");
fgets(buffer, 1024, stdin);
long N = strtod(buffer, NULL);
char* args[N+2];
char* args[1024];
args[0] = "./Ex3";
for(size_t i = 1; i <= N; i++) {
fgets(buffer, 1024, stdin);
size_t i = 1;
while(fgets(buffer, 1024, stdin)) {
buffer[strcspn(buffer, "\n\r")] = 0;
args[i] = strdup(buffer);
args[i++] = strdup(buffer);
}
args[N+1] = NULL;
args[i] = NULL;
execv("./Ex3", args);
return 0;
}
53 changes: 53 additions & 0 deletions Guião 3/Ex7.c
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;
}
34 changes: 34 additions & 0 deletions Guião 4/Ex4.c
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;
}
103 changes: 103 additions & 0 deletions Guião 4/Ex5.c
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;
}
34 changes: 34 additions & 0 deletions Guião 5/Ex4.c
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;
}
76 changes: 76 additions & 0 deletions Guião 5/Ex5.c
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;
}

0 comments on commit 34b3a7a

Please sign in to comment.