-
Notifications
You must be signed in to change notification settings - Fork 1
/
util5cli.c
318 lines (256 loc) · 6.23 KB
/
util5cli.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/********************************************************************
prog5ftp.c
Class: CSCI 631 Network Applications Programming
Program: Assignment 5
Author: Vishrant K Gupta
Z-number: z1815637
Date Due: 04/28/17
Purpose: FTP
Execution: Make execute N=5 T=1
*********************************************************************/
#include "util5cli.h"
#include "prog5cli.h"
#include "wrap5cli.h"
// usage of commands
char* usage[] =
{
"The client host and an optional port number with which ftp is to communicate may be specified on the command line. If this is done, ftp will immediately attempt to establish a connection to an FTP server on that host; otherwise, ftp will enter its command interpreter and await instructions from the user. When ftp is awaiting commands from the user the prompt 'ftp>' is provided to the user. The following commands are recognized by ftp:",
"A synonym for help.",
"Terminate the FTP session with the remote server and exit FTP. An EOF will also terminate the session and exit.",
"Change the working directory on the remote machine to remote-directory",
"Change the remote machine working directory to the parent of the current remote machine working directory",
"Terminate the FTP session with the remote server, and return to the command interpreter.",
"A synonym for close",
"Print an informative message about the meaning of command. If no argument is given, FTP prints a list of the known commands.",
"Change the working directory on the local machine. If no directory is specified, the user’s home directory is used.",
"Establish a connection to the specified host FTP server. An optional port number may be supplied, in which case, FTP will attempt to contact an FTP server at that port.",
"Print the name of the current working directory on the remote machine.",
"A synonym for bye." };
// command list
const char* cmds[] = { "", "?", "bye", "cd", "cdup", "close", "disconnect",
"help", "lcd", "open", "pwd", "quit" };
// for handling ~ tilda
wordexp_t exp_result;
/*
* Name: find_id
*
* Prototype:
* cmd_id find_id(char* cmd)
*
* Description:
* get command id
*
* Header files:
* util5cli.c
*
*/
cmd_id find_id(char* cmd) {
// local copy of cd
char* l_cmd = cmd;
// if cd is null return -1
if (l_cmd == NULL) {
return NEG_;
}
// size of cmd
int size = sizeof(cmds) / sizeof(cmds[0]);
// compare and return index
for (int i = 0; i < size; ++i) {
trim(l_cmd);
int cmp = strcmp(l_cmd, cmds[i]);
// return index
if (cmp == 0) {
return i;
}
}
// not found return -1
return NEG_;
}
/*
* Name: trim
*
* Prototype:
* void trim(char* str)
*
* Description:
* trim string
*
* Header files:
* util5cli.c
*
*/
void trim(char* str) {
// size of str
size_t ln = strlen(str) - 1;
// replace last with \0
if (*str && str[ln] == BACKSLASH_N)
str[ln] = BACKSLASH_ZERO;
}
/*
* Name: prnt_cmd_list
*
* Prototype:
* void prnt_cmd_list()
*
* Description:
* print all list of help
*
* Header files:
* util5cli.c
*
*/
void prnt_cmd_list() {
int size = (sizeof(usage) / sizeof(usage[0]));
// iterate and print all commands
for (int var = 0; var < size; ++var) {
// command and usage index is same
printf("%s\n", cmds[var]);
printf("\t%s\n\n", usage[var]);
}
}
/*
* Name: prnt_cmd_help
*
* Prototype:
* void prnt_cmd_help()
*
* Description:
* print perticular cmd help
*
* Header files:
* util5cli.c
*
*/
void prnt_cmd_help(char* cmd) {
// get index
cmd_id cid = find_id(cmd);
// print invlaid command
if (cid == NEG_) {
printf(HELP_INVALID, cmd);
return;
}
// print usage
printf(HELP_CMD, cmd, usage[cid]);
}
/*
* Name: get_args
*
* Prototype:
* cmd_id get_args(char* buf, char* cmd, char* arg1, char* arg2)
*
* Description:
* get args
*
* Header files:
* util5cli.c
*
*/
cmd_id get_args(char* buf, char* cmd, char* arg1, char* arg2) {
// to tokenize
char* temp = strtok(buf, SPACE);
int count = 0;
while (temp != NULL) {
// remove extra chars
trim(temp);
// command
if (count == CMD) {
strcpy(cmd, temp);
}
// argument 1
if (count == ARG1) {
strcpy(arg1, temp);
}
// argument 2
if (count == ARG2) {
strcpy(arg2, temp);
}
temp = strtok(NULL, SPACE);
count++;
}
// get index
cmd_id cid = find_id(cmd);
// return index
return cid;
}
/*
* Name: new_dir
*
* Prototype:
* bool new_dir(char* dir, cmd_id id, int socket_fd)
*
* Description:
* handle cd , lcd cdup command
*
* Header files:
* util5cli.c
*
*/
bool new_dir(char* dir, cmd_id id, int socket_fd) {
// lcd command
if (id == LCD_) {
// take home directory ~
if (dir[0] == BACKSLASH_ZERO) {
dir = USER_HOME;
}
// trim
trim(dir);
// expand path
wordexp(dir, &exp_result, 0);
// change dir
Chdir(exp_result.we_wordv[0]);
} else if (id == CD_) { // cd command
// cd input
char cd_input[MAXLINE];
// directory name
char cd_dir[MAXLINE];
// directory not provided
if (dir[0] == BACKSLASH_ZERO) {
printf(CD_MSG);
// taking ip address
Fgets(cd_input, MAXLINE, stdin);
// parsing command
get_args(cd_input, cd_dir, NULL, NULL);
} else {
strcpy(cd_dir, dir);
}
// removing not required char
trim(cd_dir);
// if again ip address not provided
if (cd_dir[0] == BACKSLASH_ZERO || cd_dir[0] == BACKSLASH_N) {
printf(CD_USAGE);
return false;
}
// size of array
int size = sizeof(cmds[CD_]) + sizeof(cd_dir) + 1;
// server command
char* s_command = (char*) malloc(size);
// appending server command
strcpy(s_command, cmds[CD_]);
strcat(s_command, SPACE);
strcat(s_command, cd_dir);
// sending to server
str_echo(socket_fd, s_command);
} else if (id == CDUP_) { // cdup command
// appendng command
char cmd[MAXLINE];
strcpy(cmd, cmds[CDUP_]);
// sending to server
str_echo(socket_fd, cmd);
}
return true;
}
/*
* Name: not_connected
*
* Prototype:
* void not_connected()
*
* Description:
* for printing not connected message
*
* Header files:
* util5cli.c
*
*/
void not_connected() {
printf(NOT_CONNECTED);
}