diff -r 30d751ac6d49 -r 47438813ede2 ftp/ftpc.c --- a/ftp/ftpc.c Mon Jan 18 14:10:13 2010 +0530 +++ b/ftp/ftpc.c Mon Jan 18 17:21:39 2010 +0530 @@ -12,15 +12,15 @@ int get_file(char *host, char *name) { CLIENT *clnt; - int total_bytes = 0, write_bytes; + int bytes = 0, write_items; readfile_res *result; - request req; - FILE *file; + request req; + FILE *file; /* - * Initialize the request with the file name - */ - req.name = name; + * Initialize the request with the file name + */ + req.name = name; /* * Create client handle used for calling FTPPROG on @@ -40,7 +40,7 @@ /* * Open the file for writing on the client machine */ - file = fopen(name, "w"); + file = fopen(name, "wb"); /* * Call the remote procedure retrieve_file on the server. @@ -49,12 +49,12 @@ * by the server. The loop terminates when the data returned * from the server is less than 1024 bytes */ - while (1) { - /* - * Specifies the byte position where the next read should be - * started in the server. - */ - req.start = total_bytes; + while (1) { + /* + * Specifies the byte position where the next read should be + * started in the server. + */ + req.start = bytes; result = retrieve_file_1(&req, clnt); if (result == NULL) { @@ -82,17 +82,17 @@ /* * Successfully got a chunk of the file. * Write into our local file and update the - * total bytes of data read till now. + * total bytes of data read till now. */ - write_bytes = fwrite(result->readfile_res_u.chunk.data, 1, result->readfile_res_u.chunk.bytes, file); - total_bytes += result->readfile_res_u.chunk.bytes; - if (result->readfile_res_u.chunk.bytes < MAXLEN) - break; - } + write_items = fwrite(result->readfile_res_u.chunk.data, sizeof(int), result->readfile_res_u.chunk.items, file); + bytes += result->readfile_res_u.chunk.items * (sizeof(int)); + if (result->readfile_res_u.chunk.items < MAXLEN) + break; + } - fclose(file); + fclose(file); - return 0; + return 0; } /* @@ -102,11 +102,10 @@ int put_file(char *host, char *name) { CLIENT *clnt; - char data[1024]; - int read_bytes; int *result; - chunksend chunk; - FILE *file; + filechunk data; + chunksend chunk; + FILE *file; /* * Create client handle used for calling FTPPROG on @@ -124,27 +123,24 @@ } /* - * Open the file that should be stored on the server - */ - file = fopen(name, "r"); + * Open the file that should be stored on the server + */ + file = fopen(name, "rb"); /* - * Initialize the chunk to be sent with the name - * of the file - */ - chunk.name = name; + * Initialize the chunk to be sent with the name + * of the file + */ + chunk.name = name; /* * Call the remote procedure readdir on the server - * in a loop sending only 1024 bytes of data in each - * iteration. The loop terminates once the data less - * than 1024 bytes is sent. + * in a loop sending only 1024 bytes of data in each + * iteration. The loop terminates once the data less + * than 1024 bytes is sent. */ - while (1) { - read_bytes = fread(data, 1, MAXLEN, file); - - chunk.data = data; - chunk.bytes = read_bytes; + while (1) { + chunk.items = fread(chunk.data, sizeof(int), MAXLEN, file); result = send_file_1(&chunk, clnt); if (result == NULL) { @@ -173,13 +169,13 @@ * Successfully got a chunk of the file. * Write into our local file. */ - if (read_bytes < MAXLEN) - break; - } + if (chunk.items < MAXLEN) + break; + } - fclose(file); + fclose(file); - return 0; + return 0; } /* @@ -188,21 +184,21 @@ */ int read_command(char *host) { - char command[MAXLEN]; + char command[MAXLEN]; - printf("> "); - fflush(stdin); - gets(command); + printf("> "); + fflush(stdin); + gets(command); if (strncmp(command, "get", 3) == 0) { - return get_file(host, command+4); - } else if(strncmp(command, "put", 3) == 0){ - return put_file(host, command+4); - } else if(strncmp(command, "exit", 4) == 0){ - exit(0); + return get_file(host, command+4); + } else if(strncmp(command, "put", 3) == 0){ + return put_file(host, command+4); + } else if(strncmp(command, "exit", 4) == 0){ + exit(0); } else { - return -1; - } + return -1; + } } int main(int argc, char *argv[]) @@ -216,9 +212,9 @@ /* * Command handling loop - */ + */ while(TRUE) { - result = read_command(argv[1]); + result = read_command(argv[1]); } return 0;