/*****************************************************************
** File Name:client.c
* Author:Maxiao
* Date:2003.5.23
*****************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <dirent.h>
#define MAX_INPUT_SIZE 200
#define SERVICE_PORT 3000
#define FILEBUF_SIZE 1000
unsigned int get(char * filename, char * server);
unsigned int put(char * filename, char * server);
unsigned int pwd(char * server);
unsigned int rls(char * server);
unsigned int rcd(char * filename, char * server);
void help();
void lpwd();
void ldir();
void lcd(char * filename);
unsigned int mdk(char * pathname, char * server);
unsigned int rmd(char * pathname, char * server);
unsigned int dlf(char * filename, char * server);
int main(int argc, char *argv[]) {
char line_in[MAX_INPUT_SIZE+1];
unsigned int bytes;
line_in[0] = '\0';
/* Enforce the correct usage */
if (argc != 2) {
printf("\nUsage: \"./client ServerName( or IpAddress)\"\n");
exit(1);
}
/* Print a welcome and instructions */
help();
printf("\n> "); /* First prompt */
while (strncmp("quit", line_in, 4) != 0) { /* As long as "quit" wasn't entered */
fgets(line_in, MAX_INPUT_SIZE, stdin); /* Get the input line */
line_in[strlen(line_in)-1] = '\0'; /* Replace the newline that gets copied */
if (strncmp("quit", line_in, 4) != 0) { /* If it's not "quit" */
if (strncmp("put", line_in, 3) == 0) {
/* PUT command... */
if ((bytes=put(&(line_in[4]), argv[1]))) {
printf("put successful: %d bytes sent to server", bytes);
} else {
printf("put unsuccessful...");
}
} else if (strncmp("get", line_in, 3) == 0) {
/* GET command... */
if ((bytes=get(&(line_in[4]), argv[1]))) {
printf("get successful: %d bytes retrieved from server", bytes);
} else {
printf("get unsuccessful... file probably does not exist");
}
} else if (strncmp("rls", line_in, 3) == 0) {
/* dir command... (remote) */
if (rls(argv[1])==0) {
printf("Unable to retrieve remote directory listing!");
}
} else if (strncmp("ldir", line_in, 4) == 0) {
/* ldir command... (local) */
ldir();
} else if (strncmp("lpwd", line_in, 3) == 0) {
/* lpwd command... (local) */
lpwd();
} else if (strncmp("help", line_in, 1) == 0) {
/* ? command... (local) */
help();
} else if (strncmp("pwd", line_in, 3) == 0) {
/* pwd command... (remote) */
if (pwd(argv[1])==0) {
printf("Unable to retrieve remote directory listing!");
}
} else if (strncmp("rcd", line_in, 3) == 0) {
/* rcd command... (remote)*/
if ((bytes=rcd(&(line_in[4]), argv[1]))) {
printf("get unsuccessful.", bytes);
} else {
printf(&(line_in[4]));
}
} else if (strncmp("lcd", line_in, 3) == 0) {
/* lcd command... (local)*/
lcd(&(line_in[4])) ;
}else if (strncmp("mkd", line_in, 3) == 0) {
/* lcd command... (local)*/
if((bytes= mkd(&(line_in[4]),argv[1]))){
printf("mkd successful.",bytes);
}else{
printf("mkd success",&(line_in[4]));
}
} else if (strncmp("rmd", line_in, 3) == 0) {
/* GET command... */
if ((bytes=rmd(&(line_in[4]), argv[1]))) {
printf("rmd unsuccessful... director probably does not exist");
} else {
printf("rmd successful");
}
}else if (strncmp("dlf", line_in, 3) == 0) {
/* GET command... */
if ((bytes=dlf(&(line_in[4]), argv[1]))) {
printf("dlf successful.");
} else {
printf("dlf unsuccessful... file probably does not exist");
}
}
else {
printf("Unrecognized command!");
}
printf("\n> "); /* Next prompt */
}
}
exit(0);
}
/*show the help info*/
void help() {
printf("\nWelcome to FTP program client!\n");
printf("Valid commands are (case sensitive):\n");
printf("\tget\t\tPlaces a remote file in the local directory\n");
printf("\tput\t\tPlaces a local file in the remote directory\n");
printf("\tpwd\t\tLists a remote current directory\n");
printf("\trls\t\tLists the contents of the remote directory\n");
printf("\trcd\t\tChange a remote current directory\n");
printf("\thelp\t\tFor help\n");
printf("\tquit\t\tExits the program\n");
printf("\tlpwd\t\tLists a local current directory\n");
printf("\tldir\t\tLists the contents of the local directory\n");
printf("\tlcd\t\tChange client current directory\n");
printf("\tmkd\t\tCreat directory in the remonte directory\n");
printf("\trmd\t\tDelete directory in the remote directory\n");
}
/*Places a local file in the remote directory*/
unsigned int put(char * filename, char * server) {
int sck;
struct sockaddr_in serv_adr;
struct hostent *host;
FILE *infile;
unsigned char databuf[FILEBUF_SIZE];
int bytes = 0, bytesread = 0;
/* Open the file locally */
if ((infile = fopen(filename, "r")) == 0) {
perror("fopen failed to open file");
return 0;
}
/* Attempt to get the IP for the server by hostname */
host = gethostbyname(server);
if (host == (struct hostent *) NULL) {
perror("gethostbyname failed");
return 0;
}
/* Setup the port for the connection */
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
memcpy(&serv_adr.sin_addr, host->h_addr, host->h_length);
serv_adr.sin_port = htons(SERVICE_PORT);
/* Get the socket */
if ((sck = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("error on socket()");
return 0;
}
/* Connect to the server */
if (connect(sck, (struct sockaddr *)&serv_adr, sizeof(serv_adr)) < 0) {
perror("error on connect()");
return 0;
}
/* Write the command & filename, followed by two newlines */
write(sck, "PUT ", 4);
write(sck, filename, strlen(filename));
write(sck, "\n\n", 2);
/* Read from the file and write to the socket */
while ((bytes = read(fileno(infile), databuf, FILEBUF_SIZE)) > 0) {
write(sck, databuf, bytes);
bytesread += bytes;
}
/* Close the file */
fclose(infile);
if ((bytes = read(sck, databuf, 2)) > 0) {
/* Read the status bytes from the server, print and return accordingly */
if ((databuf[0] == 'E') && (databuf[1] == 'R')) {
printf("Server error on PUT!\n");
close(sck);
return 0;
} else if ((databuf[0] == 'N') && (databuf[1] == 'W')) {
printf("File created on server\n");
close(sck);
return bytesread;
} else if ((databuf[0] == 'U') && (databuf[1] == 'P')) {
printf("File updated on server\n");
close(sck);
return bytesread;
}
} else return 0;
}
/*Places a remote file in the local directory*/
unsigned int get(char * filename, char * server) {
int sck;
struct sockaddr_in serv_adr;
struct hostent *host;
FILE *outfile;
short file_open=0;
unsigned char databuf[FILEBUF_SIZE];
int bytes = 0, bytesread = 0;
/* Attempt to get the IP for the server by hostname */
host = gethostbyname(server);
if (host == (struct hostent *) NULL) {
perror("gethostbyname failed");
return 0;
}
/* Setup the port for the connection */
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
memcpy(&serv_adr.sin_addr, host-