Strike
12-09-2000, 05:20 PM
Okay, this is for my final project in a class and I just need to have something verified (I don't really have the time to code a separate test program to verify it myself). The program is pretty much a very slim FTP server and a client that can use this server. On the server end, I need to parse three different requests - "get <filename", "put <filename>", and "list". Everything else is an invalid command (and I tell them so). But, I can't remember some simple string manipulation stuff. Tell me if this does what I think it does (in the comments):
void parse_message (char message[]) {
char tempbuf[MAX_MSG_SIZE-4];
/* This is essentially a big switch statement
* but with a string isntead of an int
* being checked. */
if ((strncmp("get ", message, 4)) == 0) {
/* Cut out the "get " part of the message and
* send that filename (well, what is _supposed_ to
* be a filename) to the send_to_client
* function. */
strncpy(tempbuf, (char *)(&message + 4), MAX_MSG_SIZE-4);
send_to_client(tempbuf);
} else if ((strncmp("put ", message, 4)) == 0) {
/* Like above, cut out the "put " part and send
* the supposed filename to the get_from_client
* function. */
strncpy(tempbuf, (char *)(&message + 4), MAX_MSG_SIZE-4);
get_from_client(tempbuf);
} else if ((strcmp("list", message)) == 0) {
/* No cutting necessary, we need an exact match
* to get here. */
list_files();
} else {
send_error("Invalid command");
}
}
Note - it compiles, at least http://www.linuxnewbie.org/ubb/smile.gif
void parse_message (char message[]) {
char tempbuf[MAX_MSG_SIZE-4];
/* This is essentially a big switch statement
* but with a string isntead of an int
* being checked. */
if ((strncmp("get ", message, 4)) == 0) {
/* Cut out the "get " part of the message and
* send that filename (well, what is _supposed_ to
* be a filename) to the send_to_client
* function. */
strncpy(tempbuf, (char *)(&message + 4), MAX_MSG_SIZE-4);
send_to_client(tempbuf);
} else if ((strncmp("put ", message, 4)) == 0) {
/* Like above, cut out the "put " part and send
* the supposed filename to the get_from_client
* function. */
strncpy(tempbuf, (char *)(&message + 4), MAX_MSG_SIZE-4);
get_from_client(tempbuf);
} else if ((strcmp("list", message)) == 0) {
/* No cutting necessary, we need an exact match
* to get here. */
list_files();
} else {
send_error("Invalid command");
}
}
Note - it compiles, at least http://www.linuxnewbie.org/ubb/smile.gif