-
Notifications
You must be signed in to change notification settings - Fork 1
/
prog5ftp.c
212 lines (153 loc) · 3.45 KB
/
prog5ftp.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
/********************************************************************
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 "prog5cli.h"
#include "util5cli.h"
#include "wrap5cli.h"
/*
* Name: main
*
* Description:
* FTP Client
*
*/
int main(int argc, char** argv) {
// buffer to hold user input
char buffer[MAXLINE];
// ip address
char ip_address[MAXLINE];
// socket fd
int socket_fd = -1;
// command id
cmd_id id;
// tokenized command
char command[MAXLINE];
// argument 1
char arg1[MAXLINE];
// argument 2
char arg2[MAXLINE];
// command line input, if ip address and port is provided while executing it will
// directly try to connect to server
if (argc > 3) {
fprintf(stderr,
"Invalid number of parameters: Usage: %s [IP address]\n",
argv[0]);
exit(1);
}
// only ip address provided
if (argc == 2) {
// copying ip address
strcpy(arg1, argv[1]);
id = OPEN_;
// goto open
goto s_cmd;
}
// ip address and port provided
if (argc == 3) {
// copying ip address and port
strcpy(arg1, argv[1]);
strcpy(arg2, argv[2]);
id = OPEN_;
// goto open
goto s_cmd;
}
// quit flag
bool quit = false;
// prompt flag
bool prompt = true;
// printing prompt
printf(PROMPT);
while (!quit) {
// initializing command
memset(command, 0, MAXLINE);
// initializing arg1
memset(arg1, 0, MAXLINE);
// initializing arg2
memset(arg2, 0, MAXLINE);
prompt = true;
// taking input for FTP
Fgets(buffer, MAXLINE, stdin);
// command id
id = get_args(buffer, command, arg1, arg2);
// goto statement
s_cmd:
switch (id) {
case NEG_: // invalid command
fprintf(stderr, INVALID_CMD);
break;
case QUIT_: // quit and bye command
case BYE_:
quit = true;
prompt = false;
break;
case HELP_: // help and ? command
case QM_:
// printing whole help
if (arg1[0] == BACKSLASH_ZERO) {
prnt_cmd_list();
} else {
// printing specific command
prnt_cmd_help(arg1);
}
break;
case LCD_: // lcd command
new_dir(arg1, id, socket_fd);
break;
case CD_: // cd command
not_connected();
break;
case OPEN_: // open command
// ip address and port not provided
if (arg1[0] == 0) {
printf(TO_MSG);
// taking ip address
Fgets(buffer, MAXLINE, stdin);
// parsing command
get_args(buffer, ip_address, arg2, NULL);
} else {
// ip address provided
strcpy(ip_address, arg1);
}
// if again ip address not provided
if (ip_address[0] == BACKSLASH_ZERO || ip_address[0] == BACKSLASH_N) {
printf(OPEN_MSG);
break;
}
// sending control to client
if (client(ip_address, arg2, true)) {
exit(0);
} else {
// setting prompt based on return value of client
prompt = true;
}
break;
case CLOSE_: // close and disconnect command in ftp
case DISCONNECT_:
// not connected in ftp
not_connected();
break;
case PWD_: // pwd command
// not connected in ftp
not_connected();
break;
case CDUP_: // cdup command
// not connected in ftp
not_connected();
break;
case ZERO_: // just enter is pressed again printing prompt
default:
break;
}
// printing prompt based on prompt flag
if (prompt) {
printf(PROMPT);
}
}
}