Problem with understanding this code
I have problems with unstanding this code. This code is for Minix but i think this should also work on Linux. What is the function of these:
int strstripl(char *inbuf, int n)
int strstripr(char *inbuf, int n)
int read_line(char *line) /* read a command*/
void parse(char *buf, char **args)
int strsepp(char *input, char *token, int sep)
///////////////////////////////////////////////////////////////////////////////////
Code:
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
/* Strips n chars off inbuf to the left side*/
int strstripl(char *inbuf, int n) {
char *origbuf = inbuf;
if (n > strlen(inbuf))
return(-1);
origbuf = (origbuf + n);
while( (*inbuf++ = *origbuf++) != '\0');
return(0);
}
/* Strips n chars off inbuf to the left side*/
int strstripr(char *inbuf, int n) {
if (n > strlen(inbuf))
return(-1);
n = (strlen(inbuf) -n);
inbuf = inbuf +n;
*inbuf = '\0';
return(0);
}
int strsepp(char *input, char *token, int sep) {
char *cmd;
int tokenpos;
if ((cmd = strchr(input, sep)) == NULL) {
return(-1);
}
tokenpos = (strlen(input)-strlen(cmd));
strncpy(token, input, tokenpos);
strstripl(input, tokenpos+1);
return(0);
}
int read_line(char *line) {
int c;
while( (c = getchar()) != 10)
*line++ = c;
*line++ = '\0';
}
void parse(char *buf, char **args)
{
while (*buf != '\0') {
/* Argument is to replace the whitespace og the terminater with a NULL */
while ((*buf == ' ') || (*buf == '\t'))
*buf++ = '\0';
/* Save the argument. */
*args++ = buf;
/* Skip over the argument. */
while ((*buf != '\0') && (*buf != ' ') && (*buf != '\t'))
buf++;
}
/* Zero terminate the last argument */
*args = NULL;
}
int main(int argcc, char *argvv[]) {
char inputline[255];
char *tokenstr;
char *input;
char *argv[80];
int pid, status;
int BGRND = 0;
for(;;) {
/* Write the prompt */
printf("cesh>");
/* Read the input */
read_line(inputline);
/* If the line is containing a ; at the end it is an syntak error */
if (inputline[strlen(inputline)-1] == ';') {
printf("Syntax error.\n");
}
else
{
/* ambigous place a ; at the end of the string to make strsep work right */
input = strcat(inputline, ";");
for(;;) {
/* allocate mem for the string token */
tokenstr = calloc(255,8);
/* is we are at the end of the string do a break */
if ( strsepp(input, tokenstr, 59) == -1)
break;
/* if the user types exit quit the program */
if (strncmp("exit", tokenstr, 4) == 0)
return(0);
if (tokenstr[strlen(tokenstr)-1] == '&') {
BGRND = 1;
tokenstr[strlen(tokenstr)-1] = '\0';
}
/* now parse the argument list into single string pointers to the execvp call */
parse(tokenstr, argv);
/* fork the process and exit if somthing goes wrong */
if ((pid = fork()) < 0)
return(0);
/* here is the child, execute and exit the process */
if (pid == 0) {
if ((execvp(argv[0], argv)) == -1)
printf("%s: Command not found.\n", tokenstr);
return(0);
}
/* here is the father wait for the child to exit */
if (BGRND == 0)
while (waitpid(-1, &status, 0) != pid);
/* free the tokenstrign */
free(tokenstr);
BGRND = 0;
} /* end tokenizer loop here */
} /* end else */
} /*end forever lop */
return(0);
}