i wrote c code result of "ls -la" command using popen , write result c. code looks this:
unsigned int ls(char *destination, const char *username, const char *relative_path) { printf("ls imp\n"); //if(!username || !relative_path) return -1; file *ls_pipe = null; unsigned long ls_pipe_size = -1; const char ls_command[] = "ls -la "; char ls_path[255] = "/home/"; char ls_full_command[255]; char buffer[255]; bzero(buffer, 255); char *entries = null; bzero(ls_full_command, 255); strcat(ls_path, username); strcat(ls_path, relative_path); strcat(ls_full_command, ls_command); strcat(ls_full_command, ls_path); printf("after cats\n"); ls_pipe = popen(ls_full_command, "r"); if(ls_pipe == null) return -1; printf("pipe ok!"); fseek(ls_pipe, 0, seek_end); ls_pipe_size = ftell(ls_pipe); rewind(ls_pipe); printf("filesize: %lu\n", ls_pipe_size); int i; for(i = 0; < 100; i++) { fread(buffer, 1, 255, ls_pipe); printf("%s", buffer); } //entries = (char*) malloc(sizeof(char) * ls_pipe_size); //if(entries == null) return -1; printf("entries ok!\n"); //if(ls_pipe_size != fread(destination, sizeof(char), ls_pipe_size, ls_pipe)) return -1; fclose(ls_pipe); return strlen(destination); }
the problem size of pipe huge (?) , in result after proper result 3 entries start appear non-stop infinity.
is there way of reading without knowing exact number of lines of result using popen wc -l?
thanks
p.s there modifications in code when trying test what's going wrong , malloc didn't work because of insane size of pipe.
you can't seek on pipe — period. value ftell()
immaterial or erroneous. can't rewind pipe because can't seek on pipe. can read data once pipe.
so, need redesign code read indefinite amount of data.
here's reasonably working code — needed adapt mac os x , machine, instead of /home/
uses /users/
, , call ls()
uses user name. code handles buffers full of data not end null (listing 570 lines of output bin
directory). i've left interface ls
unchanged although doesn't use destination
, returning length of destination
otherwise unrelated doing. uses pclose()
close pipe. using pclose()
avoids leaving zombies around , returns exit status of executed program fclose()
not.
#include <assert.h> #include <stdio.h> #include <string.h> #include <stdlib.h> static unsigned int ls(char *destination, const char *username, const char *relative_path) { printf("ls imp\n"); assert(destination != 0 && username != 0 && relative_path != 0); const char ls_command[] = "ls -la "; char ls_path[255] = "/users/"; char ls_full_command[255]; snprintf(ls_full_command, sizeof(ls_full_command), "%s %s%s/%s", ls_command, ls_path, username, relative_path); file *ls_pipe = popen(ls_full_command, "r"); if (ls_pipe == null) return -1; printf("pipe ok!\n"); char buffer[255]; int nbytes; while ((nbytes = fread(buffer, 1, 255, ls_pipe)) > 0) printf("%.*s", nbytes, buffer); putchar('\n'); printf("entries ok!\n"); pclose(ls_pipe); return strlen(destination); } int main(void) { unsigned int length = ls("/", "jleffler", "bin"); printf("ls() returned %u\n", length); return(0); }
Comments
Post a Comment