c - How to handle an invalid/garbage value while trying to use popen function that returns a permission denied -


I am trying to write a program that will look for user-owned files. After that, print out the output to earn it to peer or some other program. The problem occurs when I enter the directory as / , most initial search will be denied with permission. But it seems that a zero / trash or some variable is assigned, although it indicates that the codes are returned to the denial of permission:

  static structure {char file_owned [8192] [1024]; }Information; Zero get_file_owned (four * username) {FILE * stream3; Extern file * popen (); Four commands [1024]; Four buff [1024]; Int i; Sprintf (command, "find / -use% s -ls", user name); If (! (Stream 3 = popen ("command", "r"))) {exit (1); } i = 0; While (fgets (fond, size (fond), section 3) = null) {sprintf (info.file_owned [i], "% s", buff); // It prints in some file_modified but now I know what is i ++; } Paclose (stream 3); Return; } Int main (int argc, char ** argv) {static char * username; Int i; User name = "ficari"; Get_file_owned (username); For (i = 0; i <-10; i ++) {printf ("% s \ n", information. Prognosis [ii]); } Return 0; }   

This output is:

  find: `/ proc / 1657 / task / 1659 / ns`: permission denied search:` / proc / 1713 / Task / 1713 / ns`: Permission denied ... ... There are lots of outputs of this type ... Segmentation fault (core dump)   

I due to permissions Because of this, the refusal will then print a garbage in which process. How should I manage output to be deprived of permission? I have tried to handle a null inside the get_file_owned function but it does not resolve it.

  while (fgets (buff, size (buff), stream 3)! = Null) {if (buff == NULL) {continue; } Sprintf (info.file_owned [i], "% s", buff); I ++; }   

I also tried to use gdb to know about the division's fault. This is the result:

  Warning: Pathnote can not be read for load map: the input / output error core was generated by './a.out'. Program terminated with signal 11, Segmentation fault. # 0 _IO_getline info (fp = 0x9ec50b8, buf = 0x2 & lt; address & gt ;, n = 254, delim = 10, extract_delim = 1, eof = 0x0) at iogetline.c: 91 iogetline.c : No such file or directory.   

Please note that if I tried to run it in my home directory / home / fikrie / document . There is no division error or error.

Edit: I have also run this program in the form of a root. Change the structure type to:

  struct {char file_idden [1024]; } Information [1024]; // I tried to change it even in 8094 The array for this structure is not enough just for that.    

find probably returns 8192 lines. Therefore, while loop array is more than file_idden .

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -