Demo entry 6780753

shell模拟程序

   

Submitted by anonymous on Dec 28, 2018 at 16:13
Language: C. Code size: 555 Bytes.

//shell.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>
#include <string.h>
 
int main() {
	char comm[20];
	pid_t pid;
	int ret;
	while (1) {
		printf("root@Liucy:");
		scanf("%s", comm);
		if (strcmp(comm, "exit") == 0)
			break;
		else {
			pid = fork();
			if (pid == -1) {
				perror("fork");
				exit(0);
			} else if (pid == 0) {
				ret = execlp(comm, comm, NULL);
				if (ret == -1) {
					exit(1);
				}
			} else {
				wait(NULL);
			}
		}
 
	}
	return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).