By kowit010
1. The problem statement, all variables and given/known data:
We are supposed to write a C program that parses a command line, separates it into each command (further separates each command into its argument vectors), and then creates a multi-stage execution pipeline.
Example: “ls -l | grep ^d | wc -l”
The program must also print the results to a LOGFILE of each command created, the PIDs of each command, and the exit status of all commands. Finally, the program needs to wait for each program to execute, kill each command if Cntl-C is hit while the pipeline is executing and start over, or simply end the program if Cntl-C is hit beforehand.
2. Relevant commands, code, scripts, algorithms:
This is the code we have to work with. I apologize for the super-long massive block of code, and any problems with formatting or displaying this properly here, but this is what we must work with, and believe me, no one is more frustrated trying to comprehend this than me, since I am not very good at C programming:
file: piper.c
/***********************************************************************************************
CSci 4061 Spring 2013
Assignment# 3: Piper program for executing pipe commands
Student name: [hidden for confidentiality], [hidden for confidentiality]
Student ID: [hidden for confidentiality], [hidden for confidentiality]
X500 id: [hidden for confidentiality], [hidden for confidentiality]
Operating system on which you tested your code: Linux
CSELABS machine: [hidden for confidentiality]
GROUP INSTRUCTION: Please make only ONLY one submission when working in a group.
***********************************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include <errno.h>
#include
#define DEBUG
#define MAX_INPUT_LINE_LENGTH 2048 // Maximum length of the input pipeline command
// such as "ls -l | sort -d +4 | cat "
#define MAX_CMDS_NUM 8 // maximum number of commands in a pipe list
// In the above pipeline we have 3 commands
#define MAX_CMD_LENGTH 256 // A command has no more than 255 characters
volatile int interrupt = 0;//used for handling Cntl-C
FILE *logfp;
int run=1,run2=1;
int prev[2],curr[2];
int num_cmds = 0;
char *cmds[MAX_CMDS_NUM+1];
char *cmds2[MAX_CMDS_NUM+1][256];
int cmd_pids[MAX_CMDS_NUM];
int cmd_status[MAX_CMDS_NUM];
int stat;
int printCount=1,success=0;
/*******************************************************************************/
/* ...read more
Source: FULL ARTICLE at The UNIX and Linux Forums