Exemple #1
0
static	int	getsock( char *dirc )
{
   FILE		*file;				/* points to .glock */
   int		r = 0;				/* return value */

   file = serveropen( dirc, 0 ); 		/* open server */
   if ( file == NULL ) return( GLOCK_ERROR_SERVERSTART );
   r = makeconnection( file );			/* make connection */
   if ( r < 0 ) {				/* error */
      file = serveropen( dirc, 1 );
      if ( file == NULL ) return( GLOCK_ERROR_SERVERSTART );
      r = makeconnection( file );
   }
   return( r );					/* we're done */
}
Exemple #2
0
int main(int argc, char *argv[]){
	//check for correct args
	if(argc < 2){
		printerror("Error to few arguments: ");
		exit(EXIT_FAILURE);
	}

	int controlfd = makeconnection(argv[1], PORT_NUMBER);
	char *command = (char*)calloc(MAX_COMMAND_SIZE, sizeof(char));
	char *tokenize = (char*)calloc(MAX_COMMAND_SIZE, sizeof(char));
	char *token = (char*)calloc(MAX_COMMAND_SIZE, sizeof(char));
	char *delims = " \n\t";

	while(1){
		printf("mftp>");
		fgets(command, sizeof(char) * MAX_COMMAND_SIZE, stdin);
		if(!hasargs(command)){
			continue;	
		}
		memcpy(tokenize, command, sizeof(char) * MAX_COMMAND_SIZE);
		token = strtok(tokenize, delims);

		if(!strcmp(token, "ls")){
			ls();
		}else if(!strcmp(token, "rls")){
			int portnum;
			int rlssocket;
			if((portnum = getportnum(controlfd)) < 0){	
				printerror("rls not executed due to error");
				continue;
			}
			if((rlssocket = makeconnection(argv[1], portnum)) < 0){
				printerror("rls not executed due to error");
				continue;
			}
			rls(rlssocket, controlfd);
			close(rlssocket);
		}else if(!strcmp(token, "cd")){
			token = strtok(NULL, delims);
			cd(token);
		}else if(!strcmp(token, "rcd")){
			token = strtok(NULL, delims);
			rcd(token, controlfd);
		}else if(!strcmp(token, "get")){
			int portnum;
			int getsocket;
			int filefd;
			token = strtok(NULL, delims);
			if(token == NULL){
				printerror("Error expected parameter to get");
				continue;
			}
			char *filenamedelims = "/";
			char *filename = strtok(token, filenamedelims);
			char *lastname = filename;
			while((filename = strtok(NULL, filenamedelims)) != NULL){
				lastname = filename;
			}
			if((filefd = open(lastname, O_CREAT | O_APPEND | O_EXCL | O_WRONLY, S_IRUSR 
										| S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)) < 0){
				perror("Error opening file");
				continue;
			}
			if((portnum = getportnum(controlfd)) < 0){
				printerror("get not executed due to error");
				continue;
			}
			if((getsocket = makeconnection(argv[1], portnum)) < 0){
				printerror("get not executed due to error");
				continue;
			}
			get(token, filefd, getsocket, controlfd);
			close(filefd);
			close(getsocket);
		}else if(!strcmp(token, "show")){
			int portnum;
			int showsocket;
			token = strtok(NULL, delims);
			if(token == NULL){
				printerror("Error expected parameter to show");
				continue;
			}
			if((portnum = getportnum(controlfd)) < 0){
				printerror("show not executed due to error");
				continue;
			}
			if((showsocket = makeconnection(argv[1], portnum)) < 0){
				printerror("show not executed due to error");
				continue;
			}
			show(token, showsocket, controlfd);
			close(showsocket);
		}else if(!strcmp(token, "put")){
			int portnum;
			int putsocket;
			int filefd;
			token = strtok(NULL, delims);
			if(token == NULL){
				printerror("Error expected parameter to put");
				continue;
			}
			char *filenamedelims = "/";
			char *filename = strtok(token, filenamedelims);
			char *lastname = filename;
			while((filename = strtok(NULL, filenamedelims)) != NULL){
				lastname = filename;
			}	
			if((filefd = open(token, O_RDONLY)) < 0){
				perror("Error opening file");
				continue;
			}
			if(isregular(token)){
				if((portnum = getportnum(controlfd)) < 0){
					printerror("put not executed due to error");
					continue;
				}
				if((putsocket = makeconnection(argv[1], portnum)) < 0){	
					printerror("put not executed due to error");
					continue;
				}
				put(lastname, filefd, putsocket, controlfd);
				close(filefd);
				close(putsocket);
			}else{
				close(filefd);
				printerror("File not regular put not executed");
			}
		}else if(!strcmp(token, "exit")){
			char *quit = "Q\n";
			char buf[100];
			write(controlfd, quit, sizeof(char) * 2);
			read(controlfd, buf, 100); 
			close(controlfd);
			exit(EXIT_SUCCESS);
		}else{
			printerror("Unknown command ignored");
		}
	}
}