gpt4 book ai didi

c++ - popen ("ps -fu $USER","r") 没有给出限制为 81 个字符长的所有原始输出结果

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:01:37 32 4
gpt4 key购买 nike

我使用的是 linux redhat,在我的代码中我有这个简单的流程应该读取 ps -fu $USER 输出的每一行它确实读取了但不是所有行只有一半。

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());
}

例如,如果我在我得到的 shell 中运行 ps -fu $USER 命令:

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,

但是当我运行我得到的代码时:

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

为什么它会切断看起来有 81 个字符限制的行?甚至像这样的简单命令:

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

并且每行长度的输出是 MAX 81:

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

最佳答案

ps 本身削减了输出。

来自 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)

因此,如果通过 popen() 执行 ps,则需要明确告诉 ps 它在打印输出时应采用多少“列”。

再次来自 man ps :

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

关于c++ - popen ("ps -fu $USER","r") 没有给出限制为 81 个字符长的所有原始输出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19488971/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com