-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpseudoterminal.c
140 lines (116 loc) · 3.79 KB
/
pseudoterminal.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// #include <stdio.h>
// #include <stdlib.h>
// #include <string.h>
// /* Maximum comando size + 1. */
// #define MAX_COMMAND_SZ 256
// int main(int argC, char *argV[]) {
// char *comando;
// do{
// /* Allocate memory and check if okay. */
// comando = malloc (MAX_COMMAND_SZ);
// if (comando == NULL) {
// printf ("No memory\n");
// return 1;
// }
// /* Ask user for comando. */
// printf("What is your command?>");
// /* Get the comando, with size limit. */
// fgets (comando, MAX_COMMAND_SZ, stdin);
// /* Remove trailing newline, if there. */
// if ((strlen(comando)>0) && (comando[strlen (comando) - 1] == '\n'))
// comando[strlen (comando) - 1] = '\0';
// if (strcasecmp (comando,"exit") != 0 ){
// return 0;
// }
// char * params, * params2;
// params = strtok (comando,"-");
// params = strtok (NULL, "-");
// char* strArray[10];
// int i=0;
// while (params != NULL)
// {
// strArray[i] = malloc(strlen(params) + 1);
// strcpy(strArray[i], params);
// printf ("Algo: %s\n",params);
// /*char * args;
// args = strdup(params);
// params2 = strtok (params,"-");
// args = strtok (args,"=");
// while (args != NULL)
// {
// printf ("args: %s\n",args);
// args = strtok (NULL, "=");
// }*/
// i++;
// params = strtok (NULL, "-");
// //strdup(variable)
// }//afda -asffgh=6654 -kjhg=764 -nvbbc=8548
// /* Say hello. */
// printf("El comando fué: %s \n", comando);
// i=0;
// for (i=0;i < 3;i++) {
// printf("Array en %d: %s\n",i,strArray[i]);
// }
// /* Free memory and exit. */
// }while(strcasecmp (comando,"exit") != 0 );
// free (comando);
// return 0;
// }
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Maximum comando size + 1. */
#define MAX_COMMAND_SZ 256
int main(int argC, char *argV[]) {
char *comando;
do{
/* Allocate memory and check if okay. */
comando = malloc (MAX_COMMAND_SZ);
if (comando == NULL) {
printf ("No memory\n");
return 1;
}
/* Ask user for comando. */
printf("What is your commad?>");
/* Get the comando, with size limit. */
fgets (comando, MAX_COMMAND_SZ, stdin);
/*Remove trailing newline, if there. */
if ((strlen(comando)>0) && (comando[strlen (comando) - 1] == '\n'))
comando[strlen (comando) - 1] = '\0';
/* EDIT */
char * tokens[50];
size_t n = 0;
for (char * p = strtok(comando, "-"); p; p = strtok(NULL, "-"))
{
if (n >= 50)
{
// maximum number of storable tokens exceeded
break;
}
tokens[n++] = p;
}
for (size_t i = 0; i != n; ++i)
{
printf("Token %zu is '%s'.\n", i, tokens[i]);
}
/* /EDIT */
char * params;
params = strtok (comando,"-");
params = strtok (NULL, "-");
// printf("Antes del if: %s \n", comando);
if( strcasecmp (comando,"exit") == 0 ){
// printf("dentro del if: %s \n", comando);
return 0;
}
while (params != NULL)
{
printf ("Algo: %s\n",params);
params = strtok (NULL, "-");
}
/* Say hello. */
printf("El comando fué: %s \n", comando);
/* Free memory and exit. */
}while(strcasecmp (comando,"exit") != 0 );
free (comando);
return 0;
}