写unix shell时打印到屏幕上的一个小问题

软件和网站开发以及相关技术探讨
回复
Feiurk
帖子: 6
注册时间: 2016-10-17 8:56

写unix shell时打印到屏幕上的一个小问题

#1

帖子 Feiurk » 2016-10-30 21:25

shell_my.c内容如下

代码: 全选

#include <stdio.h>
#include <unistd.h>

#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */

/**
 * setup() reads in the next command line, separating it into distinct tokens
 * using whitespace as delimiters. setup() sets the args parameter as a 
 * null-terminated string.
 */

void setup(char inputBuffer[], char *args[],int *background)
{
    int length, /* # of characters in the command line */
        i,      /* loop index for accessing inputBuffer array */
        start,  /* index where beginning of next command parameter is */
        ct;     /* index of where to place the next parameter into args[] */
    
    ct = 0;
    
    /* read what the user enters on the command line */
    length = read(STDIN_FILENO, inputBuffer, MAX_LINE);  

    start = -1;
    if (length == 0)
        exit(0);            /* ^d was entered, end of user command stream */
    if (length < 0){
        perror("error reading the command");
	exit(-1);           /* terminate with error code of -1 */
    }

    /* examine every character in the inputBuffer */
    for (i=0;i<length;i++) { 
        switch (inputBuffer[i]){
	    case ' ':
	    case '\t' :               /* argument separators */
		if(start != -1){
                    args[ct] = &inputBuffer[start];    /* set up pointer */
		    ct++;
		}
                inputBuffer[i] = '\0'; /* add a null char; make a C string */
		start = -1;
		break;

            case '\n':                 /* should be the final char examined */
		if (start != -1){
                    args[ct] = &inputBuffer[start];     
		    ct++;
		}
                inputBuffer[i] = '\0';
                args[ct] = NULL; /* no more arguments to this command */
		break;

	    default :             /* some other character */
		if (start == -1)
		    start = i;
                if (inputBuffer[i] == '&'){
		    *background  = 1;
                    inputBuffer[i] = '\0';
		}
	} 
     }    
     args[ct] = NULL; /* just in case the input line was > 80 */
} 

int main(void)
{
char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
    int background;             /* equals 1 if a command is followed by '&' */
    char *args[MAX_LINE/2+1];/* command line (of 80) has max of 40 arguments */
    

    while (1){            /* Program terminates normally inside setup */
	background = 0;
	printf("COMMAND->\n");
    // printf("COMMAND->");
    // cout<<"COMMAND->";
        setup(inputBuffer,args,&background);       /* get next command */

    int pid = fork();
    if(pid == 0){
        execvp(args[0], args);
    }
    else{
        if(!background)
            waitpid(pid, NULL, 0)
        else{
            setup(args[0], args, background);
        }
    }
	/* the steps are:
	 (1) fork a child process using fork()
	 (2) the child process will invoke execvp()
	 (3) if background == 0, the parent will wait, 
		otherwise returns to the setup() function. */
    }
}
terminal里进入该c文件所在目录
g++ -o shell shell_my.c
./shell

发现只有在printf("COMMAND->\n")时,屏幕上才会每次打出COMMAND,然后另起一行输入命令;如果是被注释掉的另外两种写法,就什么也不显示,直接输入指令,只在退出时(即按ctrl+d时)会连续打印出与之前输入指令相同数目的COMMAND,请问这是为什么?本人新手。
头像
lilydjwg
论坛版主
帖子: 4249
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 写unix shell时打印到屏幕上的一个小问题

#2

帖子 lilydjwg » 2016-10-30 22:21

libc 默认 stdout 指向终端时是行缓冲。详见:
Linux C编程一站式学习 2.10. C标准库的I/O缓冲区

PS: 那个不叫「屏幕」,叫「终端」。
头像
lilydjwg
论坛版主
帖子: 4249
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 写unix shell时打印到屏幕上的一个小问题

#3

帖子 lilydjwg » 2016-10-30 22:21

另外不要用 g++ 编译 C 程序;它是编译 C++ 程序的。
Feiurk
帖子: 6
注册时间: 2016-10-17 8:56

Re: 写unix shell时打印到屏幕上的一个小问题

#4

帖子 Feiurk » 2016-10-31 10:21

lilydjwg 写了:libc 默认 stdout 指向终端时是行缓冲。详见:
Linux C编程一站式学习 2.10. C标准库的I/O缓冲区

PS: 那个不叫「屏幕」,叫「终端」。

谢谢回答,现在明白了。刚刚入门还有很多不懂。

还有个问题,请问args[ct] = &inputBuffer[start];是什么意思呢?为什么用&?谢谢
回复