Exemple #1
0
int shell (int argc, char *argv[]) {
    char *s = malloc(INPUT_STRING_SIZE+1);			/* user input string */
    tok_t *t;			/* tokens parsed from input */
    int lineNum = 0;
    int fundex = -1;
    pid_t pid = getpid();		/* get current processes PID */
    pid_t ppid = getppid();	/* get parents PID */
    pid_t cpid, tcpid, cpgid;

    init_shell();

    printf("%s running as PID %d under %d\n",argv[0],pid,ppid);

    lineNum=0;
    char cwd[5000];
    fprintf(stdout, "%d: %s:", lineNum, getcwd(cwd,sizeof(cwd)));
    while ((s = freadln(stdin))) {
        t = getToks(s); /* break the line into tokens */
        fundex = lookup(t[0]); /* Is first token a shell literal */
        if(fundex >= 0) cmd_table[fundex].fun(&t[1]);
        else {
            pid = fork();

            if( pid == 0 ) { // child process
                // printf("%d\n", strlen(strstr(s,">")));
                int i;
                for(i=0; i<MAXTOKS && t[i]; i++) {
                    if (strcmp( t[i], ">") == 0) {
                        t[i]=NULL;
                        //printf("hello");
                        part4(t,t[i+1],">");
                    }
                    if (strcmp( t[i], "<") == 0) {
                        t[i]=NULL;
                        //printf("hello");
                        part4(t,t[i+1],"<");
                    }
                }
                part3(t);
                part2(t);

            } else if(pid<0) {
                perror( "Fork failed" );
                exit( EXIT_FAILURE );
            }
        }
        lineNum++;
        wait(NULL);

        fprintf(stdout, "%d: %s :", lineNum, getcwd(cwd,sizeof(cwd)));
    }
    return 0;
}
Exemple #2
0
int main()
{
	Company NetEase("NetEasy");
	Department part1("在线游戏事业部");
	Department part2("盘古游戏部");
	Department part3("雷火游戏部");
	Department part4("大话&梦幻游戏部");
	Company littleCompany("美术子公司");
	Department part5("人物原画部");
	Department part6("背景原画部");
	Department part7("武器原画部");

	NetEase.addDepartment(&part1);
	NetEase.addDepartment(&part2);
	NetEase.addDepartment(&part3);
	NetEase.addDepartment(&part4);
	NetEase.addDepartment(&littleCompany);
	littleCompany.addDepartment(&part5);
	littleCompany.addDepartment(&part6);
	littleCompany.addDepartment(&part7);

	NetEase.getDescription(std::string(""));
	
	NetEase.removeDepartment(&part3);
	NetEase.removeDepartment(&part4);
	NetEase.getDescription(std::string(""));
	return 0;
}
int main()
{
	part0();
	part1();
	part2();
	part3();
	part4();
	part5();
	part6();	
}