Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A01637064-homework-03.c #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions labs/01/hello.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* Hello World program */

#include <stdio.h>

int main(void) {
printf("Hello World\n");
printf("My name is Christopher Salvador Márquez Álvarez \n");
return 0;
}
/* Hello World program */
#include <stdio.h>
int main(void) {
printf("Hello World\n");
printf("My name is Edwin Iniguez Moncada \n");
return 0;
}
Empty file added labs/03/Readme.txt
Empty file.
72 changes: 72 additions & 0 deletions labs/03/my_terminal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <errno.h>
#include <sys/stat.h>

#define MAX_INPUT_SIZE 500

int gato(int argc, char *argv[]){
FILE archivo;
ssize_t rSize;
const int bufSize = 512;
char buffer[bufSize];

if ((archivo = syscall(SYS_open, argv[1], O_RDONLY)) < 0){
fprintf(stderr, "Error abriendo el archivo, el error es = %s\n", errno);
exit(1);
}

while ((rSize = syscall(SYS_read, archivo, &buffer, bufSize)) > 0){
if (syscall(SYS_write, STDOUT, &buffer, rSize) < 0){
fprintf(stderr, "Error escribiendo al stdout, error = %s\n", errno);
exit(1);
}
}
close(archivo);
return 0;
}

int main(){
char input[MAX_INPUT_SIZE];
while (1){
printf("Intento de Terminal > ");
fgets(input, MAX_INPUT_SIZE, stdin);

input[strcspn(input, "\n")] = '\0';

if (strcmp(input, "ls") == 0){
DIR *dir = opendir(".");
if (dir == NULL){
perror("opendir");
exit(1);
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL){
printf("%S\n", entry->d_name);
}
closedir(dir);
} else if (strcmp(input, "pwd") == 0){
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL){
printf("%s\n", cwd);
} else{
perror("getcwd");
exit(1);
}
} else if (strstr(input, "cat") == input){
char *archivo = input + 4;
char *argv[] = {"cat", archivo, NULL};
gato(2, argv);
}else if (strcmp(input, "clear") == 0){
system("clear");
} else if (strcmp(input, "exit") == 0){
break;
} else{
printf("Comando no reconocido: %s\n", input);
}
}
return 0;
}