shell1.c

来自「unix环境高级编程一书的源码」· C语言 代码 · 共 32 行

C
32
字号
#include	<sys/types.h>#include	<sys/wait.h>#include	"ourhdr.h"intmain(void){	char	buf[MAXLINE];	pid_t	pid;	int		status;	printf("%% ");	/* print prompt (printf requires %% to print %) */	while (fgets(buf, MAXLINE, stdin) != NULL) {		buf[strlen(buf) - 1] = 0;	/* replace newline with null */		if ( (pid = fork()) < 0)			err_sys("fork error");		else if (pid == 0) {		/* child */			execlp(buf, buf, (char *) 0);			err_ret("couldn't execute: %s", buf);			exit(127);		}		/* parent */		if ( (pid = waitpid(pid, &status, 0)) < 0)			err_sys("waitpid error");		printf("%% ");	}	exit(0);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?