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