I am working with some skeleton code in C. The main point is to create a shell and one of the things we have to do is multiple piping, which requires me to find the length of the array of the user input so that I can loop through the tokens.
For some reason, I cannot get a consistent and accurate reading of the array. My understanding of memory processing in C is a little funky so I'm thinking that might be it, but I'm looking for another perspective here.
Passing through the command "ls -l | more", gives me either 1 or 8.
I also eliminated any variables that weren't necessary and the remaining portion of the code, since they aren't really necessary to understand what is going on.
Here is my code:
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#define _XOPEN_SOURCE 500
extern char **my_getline();
/*
* Handle exit signals from child processes
*/
void sig_handler(int signal) {
int status;
int result = wait(&status);
printf("Wait returned %d\n", result);
}
/*
* The main shell function
*/
void free(void *ptr);
int main() {
int i;
char **args;
int result;
// Set up the signal handler
sigset(SIGCHLD, sig_handler);
// Loop forever
while(1) {
// Print out the prompt and get the input
printf("->");
args = my_getline();
int size;
size = sizeof(args) / sizeof(args[0]);
printf("Array size : %d\n", size);```