question about popen in C

so if i set an element of a file_pointer array to 0, and then i compare NULL to that value 0, will it crash or just result to false, cause i set the file pointer array and pid array to 0, after closing its respective file pointer. this is like removing the struct from the chain of structs, but using an array method.

also
for the popen_new function
the instruction given was ls; ls

And how was the instruction given? Did you read its output after opening or not? IOW post your complete code.

i tested using

    FILE *file_pointer; char output[512];     file_pointer = popen_new("ls; ls", "r");     size_t bytes = read(fileno(file_pointer), output, 512);      printf(output);     printf("%d\n", pclose_new(file_pointer));

i cant see whats wrong here

would the problem be fixed if i used write command to print the output to stdout instead of printf?

Maybe there's more than 512 bytes of output, and you're forcing it to close before it's finished? This will break the pipe, causing it to get the signal SIGPIPE which will kill it prematurely, which will give you a weird exit code.

You can also use the fread command since you've converted it to a FILE * anyway.

fread(buf, 1, 512, fp);

ok i changed it to fread and fwrite and it works now.

also if i change a value of a file pointer array to 0, and then if it gets compared to a NULL value will it crash? or should i just change it to a NULL value.

also when testing when i changed all the fread/fwrite amount to 1000 for maximum bytes used, i get errors like
/bin/sh: hd��ls: not found

but if i keep it to like 512 it works fine, why is this?

also can you explain again why the command "ls; ls" prints the files line by line for each filename and not all in a line?

if i do fwrite, i get the output like

___ ___ ___ ___
___ ___ ___ ___

but otherwise when the execl command runs the output is like
___
___
___
___
___
etc

I repeat: Setting an array element doesn't cause a program to crash. (Unless you went past the end of the array). Invalid pointers can't do anything -- they just sit there. Even intentionally setting an invalid pointer won't cause a program to crash right then and there.

Your program crashes when it tries to dereference an invalid pointer.

In short:

int *x[2]; // Doesn't crash
x[0]=NULL; // doesn't crash.
x[0]=0; // same as NULL, but generates compiler warning.  NULL is better.
x[0]=0xdeadbeef; // Doesn't crash, but generates compiler warning.
printf("contents of pointer are %d\n", *(x[0])); // CRASH!

Post your complete code. I can't tell why from here but it sounds like you forgot to check how many bytes you actually read again. You can't assume you got 1000, you could easily have gotten less!

Furthermore, don't write 1000 bytes unless you actually have 1000 bytes to write. Only write the number of bytes you actually have...

There is a system call isatty() which you can use to check if an open file descriptor is a terminal or not.

When standard output is a terminal, ls checks to see how wide your terminal is and splits up its columns accordingly.

When standard output is not a terminal it knows nothing about your screen and just prints one column.

Try ls | cat , it will print one column for exactly the same reason.

This is because you're letting ls print directly to the screen. It will know that standard output is a terminal by checking isatty(), and check how wide your screen is, and print columns accordingly.

#define MAX 1000
int main(int argc, char **argv) {
    int i; char comm[MAX]; 
    for (i = 1; i<argc; i++) {
        strcat(comm,argv);
        strcat(comm," ");
    }
    printf("%d\n",system_new(comm));
    FILE *file_pointer; char output[MAX];
    file_pointer = popen_new(comm, "r");
    fread(output, 1, MAX, file_pointer); 
    printf(output);
    printf("%d\n",pclose_new(file_pointer));
    file_pointer = popen_new(comm, "w");
    fwrite(output, 1, MAX, file_pointer); 
    printf("%d\n",pclose_new(file_pointer));
    return 0;
}

and i gave the input like
./my_prog "ls; ls"

also how do i use isatty() for the execl command to print it horizontally and not vertically?

For the fourth time, stop using printf for this! It doesn't do what you think it does!

#define MAX 1000
int main(int argc, char **argv) {
    int i; char comm[MAX]; 

    // As described before, arrays on the stack are undefined until
    // you put something in them.  so strcat might find something in there
    // already.  Put a null terminator at the beginning to inform it that it's
    // empty.
    comm[0]='\0';

    for (i = 1; i<argc; i++) {
        strcat(comm,argv);
        strcat(comm," ");
    }
    // This is OK because you're using a string.
    printf("%d\n",system_new(comm));

    FILE *file_pointer; char output[MAX];
    file_pointer = popen_new(comm, "r");
    size_t size=fread(output, 1, MAX, file_pointer); 
    // THIS IS WRONG for many reasons!
    // 1) Never use printf without a format string!  If 'output' contains %s or
    // something, that will cause printf to try and read from the stack, which
    // will crash, because there's nothing there!
    // 2) 'output' isn't a proper character string anyway.  PRINTF CANNOT
    // PRINT IT.  It can't know where it ends, it'll either end too soon or
    // too late.
    // printf(output);
    fwrite(output, 1, size, stdout);
    printf("%d\n",pclose_new(file_pointer));
    file_pointer = popen_new(comm, "w");
    // You're writing MAX bytes even though we may have read far less than MAX bytes.
    // That ends up printing bytes we never set to anything before, which
    // are undefined -- meaning could be anything.  In your case you get 
    // garbage.
    // Only write as many bytes as you actually read!
    // fwrite(output, 1, MAX, file_pointer); 
    fwrite(output, 1, size, file_pointer);
    printf("%d\n",pclose_new(file_pointer));
    return 0;
}

You don't. You can only control whether ls gets a tty by actually giving it a tty. On some systems though, you can force ls to print columns with the -C option. I don't know if this will work for you though, since I still don't know what your system is after weeks of asking.

is this it?
Linux mathlab 2.6.24-22-server

Yes, it is. Your less should be able to use -C, then.

$ ls
a    b    c    d
$ ls | cat
a
b
c
d
$ ls -C | cat
a    b    c    d
$

where do I put the -C using execl?
execl("/bin/sh", "/bin/sh", "-c", comm, NULL);

would it be like
execl("/bin/sh", "/bin/sh", "-c", "-C", comm, NULL);

?