Exemplo n.º 1
0
int main(int argc, char *argv[], char *envp[])
{
	int write_bytes;
	char exec_file_name[STRING_MAX];
	char path_str[STRING_MAX];
	int flag_exit;
	int exec_status;
	int index;
	int fd;
	pid_t pid;
	signal(SIGINT, SIG_IGN);
	signal(SIGINT, handle_signal);
	read_bytes = STRING_MAX;
	fflush(stdout);
	get_path_string(envp, path_str);
	insert_path_str_to_search(path_str);
	do {
		memset(exec_file_name, 0, read_bytes);
		write_bytes = write(1, MESS_BASH_WAIT_COMMAND,
				sizeof(MESS_BASH_WAIT_COMMAND));
		read_bytes = 1;
		read_bytes = read(0, exec_file_name, STRING_MAX);
		if (read_bytes != 1) {
			exec_file_name[read_bytes-1] = 0;
			flag_exit = strcmp(exec_file_name, COMMAND_BASH_CLOSE);
			if (flag_exit)
				switch (pid = fork()) {
				case -1:
					perror("fork");
					exit(1);
				case 0:
					fill_argv(exec_file_name);
					if (argv_child[0][0] != '/') {
						fd = open(argv_child[0], O_RDONLY);
						if (fd > 0) {
							close(fd);
						} else if (attach_path() != 0) {
							printf("0 %s: %s\n",
								argv_child[0],
								MESS_BASH_COMMAND_MISS);
							exit(1);
						}
					} else {
						fd = open(argv_child[0], O_RDONLY);
						if (fd > 0) {
							close(fd);
						} else {
							printf("1 %s: %s\n",
								argv_child[0],
								MESS_BASH_COMMAND_MISS);
							exit(1);
						}
					}
					exec_status = execve(argv_child[0],
						argv_child, envp);
					if (exec_status < 0) {
						printf("2 %s: %s\n",
							argv_child[0],
							MESS_BASH_COMMAND_MISS);
						exit(1);
					}
					exit(0);
				default:
					wait(NULL);
				}
		}
	} while (flag_exit);
	for (index = 0; argv_child[index] != NULL; index++)
		free(argv_child[index]);
	for (index = 0; search_path[index] != NULL; index++)
		free(search_path[index]);
	write_bytes = write(1, MESS_BASH_NL, sizeof(MESS_BASH_NL));
	return 0;
}
Exemplo n.º 2
0
int main(int argc, char *argv[], char *envp[]) 
{
	int fd, i;
	char c;
    char *inputs = calloc(BUFFERSIZE, sizeof(char));
    char *cmd = calloc(BUFFERSIZE, sizeof(char));
    char *path_str = calloc(BUFFERSIZE, sizeof(char));

    // use ctrl+c to interrupt whatever the shell is doing
    signal(SIGINT, SIG_IGN);
	signal(SIGINT, sig_hdlr);

	get_paths(envp);

	if(fork() == 0) {
		execve("/usr/bin/clear", argv, envp);
		exit(1);
	} else {
		wait(NULL);
	}

	// Print the prompt string for the first time
    printf("$ ");
	fflush(stdout);

	// Main loop
    while(c != EOF) {
		c = getchar();
		switch(c) {
			case '\n':
				if(inputs[0] == NULL_SYMBOL) {
					printf("$ ");
				} else {
					// Parse the command line
					argv_parser(inputs);
					strncpy(cmd, parsed_argv[0], strlen(parsed_argv[0]));
					strncat(cmd, "\0", 1);
					if(strchr(cmd, '/') == NULL) {
						// Find the full pathname for the file
						if(attach_path(cmd) == 0) {
							// Create a process to execute the command
							run_cmd(cmd, envp);
						} else {
							printf("%s: command not found\n", cmd);
						}
					} else {
						if((fd = open(cmd, O_RDONLY)) == -1) {
							close(fd);
							run_cmd(cmd, envp);
						} else {
							printf("%s: command not found\n", cmd);
						}
					}
					// Parent waits until child finishes executing command
					// ????????????

					argv_reset();
					// Print the prompt string
					printf("$ ");
					memset(cmd, 0, BUFFERSIZE);
				}
				memset(inputs, 0, BUFFERSIZE);
				break;
			default:
				// Read the command line
				strncat(inputs, &c, 1);
				// printf("%s\n", inputs);
				break;
		}
	}
	free(inputs);
	free(path_str);

	for(i=0; i < BUFFERSIZE; i++)
		free(paths[i]);

	printf("\n");
	return 0;
}