popen("ps -fu $USER" ,"r") doesn't give all original output result limited to 81 char long

StackOverflow https://stackoverflow.com/questions/19488971

  •  01-07-2022
  •  | 
  •  

Question

im using linux redhat and in my code i have this simple fllow that supposed to read each line of the ps -fu $USER output it does read but not all of the line only half of it .

const char* szPs      = "ps -fu $USER";      // The PS command.
bool bSubProcessFound = false;
char szBuf[102400];  //big buffer
FILE* pPsOut = popen(szPs, "r");
  if (pPsOut == NULL) {

     bFailure = true;
     return (0);
  } 
// Go thru all commands.
  std::string sPsLine;

  bool bFound = false;
  int sizeofbug = sizeof(szBuf); //just checking size
  while (fgets(szBuf, sizeof(szBuf), pPsOut) != NULL) {

    LogWrite("befor   process: %s in ps", szBuf);
    // Erase new line at the end of ps string.
    szBuf[strlen(szBuf) - 1] = '\0';
    // Extract command line.
    sPsLine = szBuf;
    printf("after   process: %s in ps", sPsLine.c_str());
  } 

and for example if i run the ps -fu $USER command in the shell im getting :

fooy    28407 28401  0 09:59 ?        00:00:04 java -Dappsubprocessname=catalina_gui -classpath .:/home/fooy/simple_java_server TCPServer
fooy    26256 26242  2 Oct20 pts/13   00:23:57 /usr/bin/java -Xms128m -Xmx512m -Dorg.eclipse.equinox.p2.reconciler.dropins.directory=/usr/share/eclipse/dropins -XX:CompileCommand=exclude,

but when i run the code im getting :

fooy    28407 28401  0 09:59 ?        00:00:03 java -Dappsubprocessname=catalin
fooy    26256 26242  1 Oct20 pts/13   00:23:20 /usr/bin/java -Xms128m -Xmx512m

why does it cut the lines its looks like there is 81 chars limit ? even simple command like this :

char* in = NULL;
  size_t len =0;
  FILE * psAux = popen("/bin/ps ax", "r");
  while(getline(&in,&sizeofbug,psAux)!=-1)
  {
      fputs(in,stdout);
  }

and the output is MAX 81 each line length:

3377 ?        Ss     0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
 3378 ?        S      0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
 3380 ?        S      0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
 3381 ?        S      0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
 3382 ?        S      0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
 3383 ?        S      0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
 3384 ?        S      0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
 3521 ?        S      0:15 [flush-8:32]
 3616 ?        Ss     0:00 sshd: foo123 [priv]
 3624 ?        S      0:00 sshd: foo123@notty
 3625 ?        Ss     0:00 tcsh -c /usr/libexec/openssh/sftp-server
 3639 ?        S      0:00 /usr/libexec/openssh/sftp-server
 3640 ?        Ss     0:00 tcsh -c /usr/libexec/openssh/sftp-server
 3655 ?        S      0:00 /usr/libexec/openssh/sftp-server
 4150 ?        Sl     0:25 SSSBatchCCC.exe -name ValueTxImporter -ORBInitRef Nam
 4154 ?        S      0:29 [flush-8:48]
 4323 ?        S      0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
 4515 ?        Ss     0:00 sshd: wwwww02 [priv]
 4526 ?        S      0:00 sshd: wwwww02@notty
 4527 ?        Ss     0:00 tcsh -c /usr/libexec/openssh/sftp-server
 4547 ?        S      0:00 /usr/libexec/openssh/sftp-server
 4657 ?        S      0:01 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
 4742 ?        Ss     0:00 sshd: foo123 [priv]
 4749 ?        Ss     0:00 sshd: foo123 [priv]
 4754 ?        S      0:00 sshd: foo123@pts/7
 4756 ?        S      0:00 sshd: foo123@notty
 4757 ?        Ss     0:00 tcsh -c /usr/libexec/openssh/sftp-server
 4773 ?        S      0:00 /usr/libexec/openssh/sftp-server
 4782 pts/7    Ss     0:00 -tcsh
 5183 ?        Ss    46:18 /usr/sbin/abrtd
 5228 ?        Ss     2:24 abrt-dump-oops -d /var/spool/abrt -rwx /var/log/messa
 5337 pts/13   S+     0:00 /bin/sh /usr/bin/eclipse
 5338 pts/13   S+     0:00 /usr/lib64/eclipse/eclipse
 5352 pts/13   Sl+    6:37 /usr/bin/java -Xms128m -Xmx512m -Dorg.eclipse.equinox
 5472 ?        S      0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
 5519 ?        Sl    11:14 libvirtd --daemon
 5864 ?        S      0:01 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
 6363 ?        S<     0:00 /sbin/udevd -d
Était-ce utile?

La solution

It's ps itself which cuts the output.

From man ps:

If ps can not determine display width, as when output is redirected (piped) into a file or another command, the output width is undefined. (it may be 80, unlimited, determined by the TERM variable, and so on)

So if executing ps trough popen() one explicitly needs to tell ps how many "columns" it shall assume when printing output.

Again from man ps:

OUTPUT MODIFIERS
...
--cols n set screen width
--columns n set screen width

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top