Linux Network Programming Learning (iii)-----Process Control instances

Source: Internet
Author: User

This section focuses on a process-controlled instance that receives commands and executes commands in the foreground or background, and can handle a command line consisting of several commands named Samllsh.

The basic logic is

while (EOF not typed) {     get command line execution from user terminal  }

SETP1: Obtain the command line content, with the Uerin function, the processing step first shows the prompt, the specific content of the prompt is passed to the function by the user parameters, and then each time from the keyboard to read a character, stored in the inpbuf, the end Userin return the number of characters or EOF (end of file), NewLine characters are also stored in inpbuf.

The code is as follows:

#include "SMALLSH.H"/* program buffers and pointers */static char inpbuf[maxbuf],tokbuf[2*maxbuf],*ptr = Inpbuf,*tok = tokbuf;/* Userin () function * /int Userin (chat* p) {int c,count;ptr = Inpbuf;tok = tokbuf;/* display prompt */printf ("%s", p); for (count = 0;;) {if (C=getchar ()) ==eof) return (EOF), if (Count < maxbuf) inpbuf[count++] = c;if (c = = ' \ n ' && count < Maxbuf) { Inpbuf[count] = ' + '; return (count);} /* If line is too long reenter */if (c = = ' \ n ') {printf ("Smallsh:input lines too long\n"); count=0;printf ("%s", p);}}

Where the header file samllsh.h content is

#include <stdio.h> #define EOL 1/* Line end */#define ARG 2#define AMPERSAND 3#define semicolon 4#define maxarg 512/* command Maximum number of row parameters */#define MAXBUF 512/* Input line Maximum length */#define FOREGROUND 0#define BACKGROUND 1

  

Linux Network Programming Learning (iii)-----Process Control instances

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.