-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
79 lines (70 loc) · 1.84 KB
/
main.c
File metadata and controls
79 lines (70 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* @file main.c
* @author Alejandro C. Mosquera (AxzelBC)
* @code 2022499
* @brief Archivo principal de la shell.
* @version 0.1
* @date 2021-10-01
*/
/*
* Archivos de cabeceras (includes).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include "src/shell.h"
int main(void){
/* Variable de status para el hijo. */
int status;
do{
/**
* @brief Almacena le comando ingresado.
*/
char comando[MAX_COMANDO];
promtShell();
scanf(" %255[^\n]",comando);
/**
* @brief si el comando es 'exit' para la shell.
*/
if (!strcmp("exit",comando)){
printf("Hasta pronto. :D\n");
break;
exit(EXIT_SUCCESS);
}
/**
* @brief Creación de subproceso para los comandos, y verificación del PID.
*/
pid_t pid = fork();
assert(pid >= 0);
/* Creación del proceso hijo. */
if (!pid){
leerComando(comando);
}
/* Verificación de estado del proceso padre .*/
else if (pid > 0){
wait(&status);
/* Se verifica que el estado es de salida. */
if (WIFEXITED(status)){
if (!(WEXITSTATUS(status))){
/* Revisión del clear (estético) */
if(!strcmp("clear",comando)){
printf("");
} else{
printf("\n:)\n");
}
} else{
printf("\n:(\n");
}
} else{
return 1;
}
/* Error en PID */
} else{
perror("Error en la creación de pid\n");
exit(EXIT_FAILURE);
}
} while (1);
}