Shell-like Program (Basic implementation portion of Shell programs)

Source: Internet
Author: User

Directly on the code:

#include "apue.h" #include <sys/wait.h>int main (void) {    char    buf[maxline];/* form apue.h  4096 */    pid_t   pid;    int     status;    printf ("percent");  /* Print PROMT (printf requires percent to print%) *    /while (Fgets (buf, MAXLINE, stdin)! = NULL) {        if (Buf[strlen (BUF) -1] = = ' \ n ') {            Buf[strlen (BUF)-1] = 0;/* Replace newline while null */        }        if ((PID = fork ()) < 0) { C13/>err_sys ("fork Error");        } else if (PID = = 0) {            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 ("percent");    }    Exit (0);}

Many times do not like CSDN This code display, and then affixed to the vim


Test:



The function of this applet is that it cannot pass parameters to the command, for example, we cannot specify a directory name to list, we can only run it in the current working directory.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Shell-like Program (Basic implementation portion of Shell programs)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.