www.pudn.com > SimpleShell.zip > utility.c


 
#include  
#include   
#include  
#include   
#include   
#include  
#include  
#include  
#include  
#include "myshell.h" 
 
void interDirectory(void);	 
void interPause(void);		 
void interEnvi(void);		 
void interEcho(void);		 
void interHelp(void);		 
void interMoveDic(void);	 
 
char *arg[MAX_ARG];		 
char *token;			 
int argv;				 
char firstToken[1000];	 
 
char check[1000];	 
extern char **environ;	 
int iterator;		 
 
 
int pid, cPid, status, tempPid;	 
int background = FALSE;		 
 
 
DIR *dp;				 
struct dirent *dirp; 
 
 
char helpLine[MAX]; 
FILE *helpFileFp; 
int lineCounter; 
 
 
FILE *fpForExternal; 
char readLine[MAX]; 
char *eToken; 
 
void excuteCommand(char *command, char *desti, char *dDesti, char *source) { 
 
	background = FALSE; 
 
	 
	token = (char*)strtok(command, DIVISION); 
 
	if(token == NULL) return;  
 
	else { 
		argv = 0; 
 
		strcpy(firstToken,token); 
 
		 
		while(token){ 
			arg[argv++] = token; 
			token = (char*)strtok(NULL, DIVISION); 
		} 
 
		 
		arg[argv] = NULL; 
	} 
 
	if( argv > 1 ) {						 
		if(!strcmp(arg[argv-1], "&")) {		 
 
			background = TRUE;				 
			argv--;							 
			arg[argv] = NULL;			 
		} 
 
		else {						 
 
			strcpy(check, arg[argv-1]);		 
			iterator = 0;					 
 
			while(check[iterator] != (char)NULL) { 
				if(check[iterator] == '&' && check[iterator+1] == (char)NULL) { 
					background = TRUE;		  
					check[iterator] = (char)NULL; 
					break; 
				} 
				iterator++; 
			} 
			strcpy(arg[argv-1], check);		 
 
		} 
	} 
 
	if (!strcmp(firstToken, "quit")) 
	{ 
		 
		if ( argv > 1 ) { 
			for ( iterator=0;iterator 3) { 
			printf(FIND_ERROR_MSG); 
		} 
		else { 
			interMoveDic();  
		} 
		return;  
	} 
 
	if((pid=fork()) == -1) perror("process fork() failed\n");  
 
	 
	else if(pid != 0) { 
 
		 
		if(background) 
			cPid = waitpid(pid, &status, WNOHANG); 
		else 
			cPid = waitpid(pid, &status, 0); 
 
	} 
 
	 
	else { 
 
		 
		if(!strcmp(firstToken, "dir")) { 
 
		 
			if( argv >= 3 || source[0] != (char)NULL) { 
				printf(ARGU_ERROR_MSG); 
				exit(1); 
			} 
			 
		else { 
				 
				if( desti[0] != (char)NULL){ 
					freopen (desti,"w",stdout);	 
					interDirectory();				 
					fclose (stdout);			 
					exit(0);					 
				} 
				 
				else if ( dDesti[0] != (char)NULL) 
				{ 
					freopen (dDesti,"a",stdout); 
					interDirectory();				 
					fclose (stdout);			 
					exit(0);					 
				} 
			} 
			 
			interDirectory(); 	 
			exit(0);  
		} 
	 
		if(!strcmp(firstToken, "help")) { 
 
		 
			if( argv > 1 || source[0] != (char)NULL) { 
				printf(FIND_ERROR_MSG);	 
				exit(1);				 
			} 
			else { 
			 
				if( desti[0] != (char)NULL ) { 
					freopen (desti,"w",stdout); 
					interHelp();				 
					fclose (stdout);		 
					exit(0);				 
				} 
				 
				else if (dDesti[0] != (char)NULL) { 
					freopen (dDesti,"a",stdout); 
					interHelp();				 
					fclose (stdout);		 
					exit(0);				 
				}				 
			} 
			interHelp(); 
			exit(0);  
		} 
		 
		if(!strcmp(firstToken, "pause")) { 
 
		 
			if( argv > 1) { 
				printf(FIND_ERROR_MSG); 
				exit(1);			 
			} 
			else { 
				printf(" < PAUSE :: Please ENTER to release >\n"); 
				interPause();		 
			} 
			exit(0);  
		} 
		 
		if(!strcmp(firstToken, "clr")){ 
 
			 
			if (argv > 1 ) { 
				printf(FIND_ERROR_MSG);  
				exit(1);				 
			} 
			else { 
				system("clear");		 
			} 
			exit(0); 
		} 
		 
		if(!strcmp(firstToken, "environ")){ 
 
		 
			if (argv > 1 || source[0] != (char)NULL) { 
				printf(FIND_ERROR_MSG);	 
				exit(1);				 
			} 
			else { 
			 
				if( desti[0] != (char)NULL) { 
					freopen (desti,"w",stdout);  
					interEnvi();			 
					fclose (stdout);		 
					exit(0);				 
				} 
				 
				else if( dDesti[0] != (char)NULL) { 
					freopen (dDesti,"a",stdout); 
					interEnvi();			 
					fclose (stdout);		 
					exit(0);				 
				} 
			} 
			interEnvi(); 
			exit(0);	 
		} 
	 
		if(!strcmp(firstToken, "echo")){ 
			 
		 
			if( source[0] != (char)NULL) { 
				printf(FIND_ERROR_MSG);	 
				exit(1);				 
			} 
			else { 
				 
				if ( desti[0] != (char)NULL) { 
					freopen (desti,"w",stdout);	 
					interEcho();				 
					fclose (stdout);		 
					exit(0);					 
				} 
				 
				else if (dDesti[0] != (char)NULL) { 
					freopen (dDesti,"a",stdout); 
					interEcho();				 
					fclose (stdout);			 
					exit(0);					 
				} 
			} 
			interEcho(); 
			exit(0);	 
		} 
		 
		if( source[0] != (char)NULL ) { 
			 
			fpForExternal = fopen(source, "r");			 
			 
			if (fpForExternal == NULL) { 
				perror(" myshell : ");	 
				exit(1);			 
			}			 
 
			else { 
				 
				 
				while(!feof(fpForExternal)){ 
 
					fgets(readLine,MAX,fpForExternal); 
 
					eToken = (char*)strtok(readLine, DIVISION); 
 
					while(eToken){ 
						arg[argv++] = eToken; 
						eToken = (char*)strtok(NULL, DIVISION); 
					} 
 
					arg[argv] == NULL; 
 
					fclose(fpForExternal);  
				} 
 
			} 
		} 
		if( desti[0] != (char)NULL ) { 
			freopen (desti,"w",stdout); 
		} 
 
		else if( dDesti[0] != (char)NULL ) { 
			freopen (dDesti,"a",stdout); 
		} 
		 
		if( execvp(firstToken, arg) == -1 ) { 
			printf(" myshell : command '%s' isn't the internal or the external command.\n", firstToken);			 
		} 
 
		if( (desti[0] != (char)NULL) || (dDesti[0] != (char)NULL) ) { 
			fclose (stdout); 
		}				 
		exit(1); 
	} 
} 
 
 
////////////////////////////////////////////////////////////////////////////// 
// Function Name     : interDirectory()                                     // 
// Brief Description : Display the contents of the inputed(or current)      // 
//                     directory to output stream                           // 
//                     (primarily standard output stream is a monitor)      // 
////////////////////////////////////////////////////////////////////////////// 
void interDirectory() { 
	 
	int count;	 
 
	 
	if( arg[1] == NULL ) { 
		dp=opendir("."); 
	} 
 
	else if( arg[1] != NULL ) { 
		if( !(dp=opendir(arg[1])) ){  
			printf(" myshell : Inputed directory is not exist.\n"); 
			exit(1);		 
		} 
	} 
 
	count = 0; 
	printf("\n --- Directory contents --- \n"); 
	while(dirp=readdir(dp)) {		 
		printf("%s\t", dirp->d_name); 
 
		 
		count++; 
		if( (count % 5) == 0) printf("\n"); 
	} 
	printf("\n --- This is all of item(s) in directory --- \n\n"); 
 
	 
	closedir(dp); 
} 
 
////////////////////////////////////////////////////////////////////////////// 
// Function Name     : interPause()                                         // 
// Brief Description : Display the environment variable strings             // 
//                     to output stream                                     // 
//                     (primarily standard output stream is a monitor)      // 
////////////////////////////////////////////////////////////////////////////// 
void interPause(void) { 
	 
	 
	char temp = 'X'; 
	struct termios original_t, new_t; 
 
	do { 
		 
		tcgetattr(fileno(stdin), &original_t); 
		new_t = original_t; 
 
		new_t.c_lflag &= ~(ICANON | ECHO); 
 
		 
		tcsetattr(fileno(stdin), TCSAFLUSH, &new_t); 
		fflush(stdin); 
		temp = getchar(); 
 
		 
		tcsetattr(fileno(stdin), TCSANOW, &original_t); 
 
	} while(temp != '\n'); 
} 
 
 
////////////////////////////////////////////////////////////////////////////// 
// Function Name     : interEnvi()                                          // 
// Brief Description : Display the environment variable strings             // 
//                     to output stream                                     // 
//                     (primarily standard output stream is a monitor)      // 
////////////////////////////////////////////////////////////////////////////// 
void interEnvi(void) { 
 
 
	for(iterator = 0; environ[iterator] != NULL; iterator++) 
		printf("%s\n", environ[iterator]); 
} 
 
 
////////////////////////////////////////////////////////////////////////////// 
// Function Name     : interEcho()                                          // 
// Brief Description : Display some contents to output stream               // 
//                     (primarily standard output stream is a monitor)      // 
////////////////////////////////////////////////////////////////////////////// 
void interEcho(void) { 
	iterator = 1; 
	 
	 
	while(arg[iterator]!= NULL) 
		printf("%s ", arg[iterator++]);  
 
	printf("\n"); 
} 
 
 
////////////////////////////////////////////////////////////////////////////// 
// Function Name     : interHelp()                                          // 
// Brief Description : Display "readme.txt" files to output stream          // 
//                     (primarily standard output stream is a monitor)      // 
////////////////////////////////////////////////////////////////////////////// 
void interHelp(void) { 
 
 
	helpFileFp = fopen("readme.txt", "r"); 
 
	 
	if (helpFileFp == NULL) { 
		perror(" myshell "); 
		exit(1);			 
	} 
 
	else { 
 
		lineCounter = 0; 
 
		while(!feof(helpFileFp)) { 
 
			fgets(helpLine,MAX,helpFileFp); 
 
		 
			printf("%s", helpLine); 
			 
			lineCounter++; 
 
		 
			if (lineCounter > 10) {	 
				interPause(); 
			} 
		} 
		fclose(helpFileFp);	 
	} 
} 
 
 
////////////////////////////////////////////////////////////////////////////// 
// Function Name     : interMoveDic()                                       // 
// Brief Description : Move current directory to inputed path               // 
////////////////////////////////////////////////////////////////////////////// 
void interMoveDic(void) { 
 
	 
	char curDic2[MAX];	 
	char *pwdForCh2;	 
	int count = 0; 
	 
 
	if(arg[1] == NULL) { 
 
		 
		getcwd(curDic2, MAX); 
		printf(" Current directory : %s \n", curDic2); 
	} 
 
	 
	else if( !(dp=opendir(arg[1])) ) { 
		perror(" myshell "); 
	} 
 
 
	else { 
		 
		chdir(arg[1]);			 
		getcwd(curDic2, MAX);	 
		 
		 
		pwdForCh2 = (char *)malloc( 4 + strlen(curDic2) + 2); 
		strcat(pwdForCh2, "PWD="); 
		strcat(pwdForCh2, curDic2); 
		putenv(pwdForCh2); 
	} 
}