0

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);```
N.Spiess
  • 57
  • 8
  • Does this answer your question? [How to find the 'sizeof' (a pointer pointing to an array)?](https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array) – kaylum Oct 11 '21 at 21:43
  • That's my bad, was an older version of the code. I was trying anything I could... should be size = sizeof(args) / sizeof(args[0]). Edited to reflect original code. And Kaylum, that's the logic I'm using but I can't get it to accurately match the token amount that the user inputs. – N.Spiess Oct 11 '21 at 21:44
  • `#define _XOPEN_SOURCE 500` has to be put _before_ `#include`s to have any affect on these includes. – KamilCuk Oct 11 '21 at 21:46
  • That `size = sizeof(...` line will probably always set the size to one because on most platforms, a `char *` and a `char **` are the same size. If you want to count the number of parameters, you have to actually do that. – David Schwartz Oct 11 '21 at 21:47
  • @N.Spiess What logic? The question "Is there a way to find out the size of the array that ptr is pointing to" has answer "No, you can't.". You can never get the size of an array or the number of elements through a pointer. `my_getline` needs some way to tell you the size of the array by either returning it or setting an end marker value in the array. – kaylum Oct 11 '21 at 21:48
  • @kaylum, that's essentially is what I'm asking b/c I cannot figure out how to get the number of elements. So you're saying I have to refer to the my_getline() then to get the amount of elements? – N.Spiess Oct 11 '21 at 21:49
  • `my_getline` needs to tell you that size by either returning it or setting an end marker value in the array. – kaylum Oct 11 '21 at 21:50

0 Answers0