Click to See Complete Forum and Search --> : C string question


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

Strike
12-10-2000, 11:37 PM
Fine, I got it myself,

Strike
12-11-2000, 12:31 AM
(for those interested)
In each instance, just change:

(char *)(&message + 4)

to

(message + 4)

Which makes total sense, of course, since the name of an array is a pointer to the base.

Stuka
12-11-2000, 01:27 AM
Strike-
Just read the topic first time today - cool that ya figured that out. Anyhoo, why not use strlen(message - 4) instead of (MAX_MSG_SIZE - 4) ... seems like it might save ya some data bein' sent over the network...I know, ya have another function call, but since when was the local machine the slowest part of the equation? http://www.linuxnewbie.org/ubb/smile.gif Anyway, that's my curiosity question...unless you want to allow whitespace in the filename? But isn't that a bit of a security problem? Or does your prof care?

Strike
12-11-2000, 01:47 AM
Well, I don't really process those filenames in this function, I just copy everything I've got to that buffer which I send to another function. Those functions check for the existence of the file, not this one.