-
Notifications
You must be signed in to change notification settings - Fork 0
/
nivel2.c
117 lines (103 loc) · 2.48 KB
/
nivel2.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* nivel2.c - Parte 2 de Aventura 2 - Gian Lucas Martín Chamorro,
Tomás Bordoy García-Carpintero y Jordi Antoni Sastre Moll*/
#include "nivel2.h"
int main() {
char line[COMMAND_LINE_SIZE];
while(read_line(line)){
execute_line(line);
}
return 0;
}
char * read_line(char * line) {
imprimir_prompt();
if(fgets(line,COMMAND_LINE_SIZE,stdin)){
return line;
}else{
return NULL;
}
}
int imprimir_prompt() {
printf(BOLD RED "%s",getenv("USER"));
printf(RESET ":");
printf(BOLD BLU "~%s",getenv("PWD"));
printf(RESET "$ ");
return 0;
}
int execute_line(char *line){
char *args[ARGS_SIZE];
parse_args(args,line);
check_internal(args);
return 0;
}
int parse_args(char **args,char *line){
const char delim[5] = " \t\n\r";
char *token;
int n=0;
token=strtok(line,delim);
while(token){
if(token[0]!='#'){
args[n]=token;
n++;
}
token=strtok(NULL,delim);
}
args[n]=token;
n++;
for(int i=0;i<n;i++){
printf("%d, %s\n",i,args[i]);
}
return(n-1);
}
int check_internal(char **args){
if (strcmp(args[0], "cd") == 0){
internal_cd(args);
return 1;
} else if(strcmp(args[0], "export") == 0){
internal_export(args);
return 1;
} else if (strcmp(args[0], "source") == 0){
internal_source(args);
return 1;
} else if (strcmp(args[0], "jobs") == 0){
internal_jobs(args);
return 1;
} else if (strcmp(args[0], "exit") == 0){
exit(0);
return 1;
} else {
return 0;
}
}
int internal_cd(char **args){
char cwd[256];
if(!args[1]){
chdir("/home");
}else if(chdir(args[1])<0){
fprintf(stderr,"Error %d: %s\n",errno,strerror(errno));
return -1;
}
printf("%s\n",getcwd(cwd,sizeof(cwd)));
setenv("PWD",cwd,1);
return 0;
}
int internal_export(char **args){
const char delim[2] = "=";
char *evar; //Environment variable
char *value; //New environment variable value
if(!args[1]||!(evar=strtok(args[1],delim))||!(value=strtok(NULL,"\n"))){
fprintf(stderr, "Error de sintaxis. Uso: export Nombre=Valor\n");
}
printf("[%s=%s]\n",evar,getenv(evar));
setenv(evar, value, 1);
printf("[%s=%s]\n",evar,getenv(evar));
return 0;
}
int internal_source(char **args){
printf("El comando source sirve para cargar cualquier archivo de funciones"
" en el shell o en un indicador de comandos.\n");
return 0;
}
int internal_jobs(char **args){
printf("El comando jobs lista el estatus de todos los trabajos activos.\n");
return 0;
}