C fscanf让我重复一行

时间:2014-02-21 11:24:54

标签: c linux pid scanf ps

我正在尝试通过重定向'ps'命令的输出来显示每个进程的详细信息。我已将ps输出重定向到文件。我正在阅读它并使用fscanf显示Pid和Command。但我发现我得到了一些重复的行,在某些情况下,Pids是负值。

void TR69_DM_GetProcDetails(char *FileName, int32_t *ProcCount, struct ProcessInfo *ProcVar)
{
    char CharBuf[BUF_SIZE] ;
    int32_t i = 0;

    int Pid;
    char Cmd[100];

    FILE *FileDesc;
    FileDesc = fopen (FileName, "r");

    printf("\nFile pointer %ld\n", ftell(FileDesc));
    fscanf (FileDesc, "%[^\n]\n", CharBuf);
    puts(CharBuf);

    printf("\nFile pointer %ld\n", ftell(FileDesc));

    while ( EOF != fscanf (FileDesc, "%*s %lu %*d %*d %*s %*s %*s %s", &Pid, Cmd))
    {
        printf("\n PID is %d and cmd is %s", Pid, Cmd);
    //  i++;
    }

}

我得到以下输出样本:

 PID is 2104 and cmd is /usr/lib/indicator-datetime/indicator-datetime-service
 PID is 2107 and cmd is /usr/lib/indicator-messages/indicator-messages-service
 PID is 2114 and cmd is /usr/lib/indicator-session/indicator-session-service
 PID is 2134 and cmd is /usr/lib/geoclue/geoclue-master
 PID is 2142 and cmd is gnome-screensaver
 PID is 2145 and cmd is /usr/lib/gnome-disk-utility/gdu-notification-daemon
 PID is 2342 and cmd is /usr/bin/python
 PID is 2342 and cmd is /usr/bin/python
 PID is 4316 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is 4444 and cmd is /usr/sbin/ntpd
 PID is 4444 and cmd is /usr/sbin/ntpd
 PID is 4444 and cmd is /usr/sbin/ntpd
 PID is 4444 and cmd is /usr/sbin/ntpd
 PID is 113 and cmd is /usr/sbin/ntpd
 PID is 113 and cmd is /usr/sbin/ntpd
 PID is 5411 and cmd is sshd:
 PID is 5411 and cmd is sshd:
 PID is 5411 and cmd is sshd:
 PID is 5584 and cmd is sshd:
 PID is 5584 and cmd is sshd:
 PID is 5585 and cmd is -bash
 PID is 6790 and cmd is /usr/lib/firefox-4.0.1/firefox-bin
 PID is 7007 and cmd is /usr/lib/firefox-4.0.1/plugin-container
 PID is 7007 and cmd is /usr/lib/firefox-4.0.1/plugin-container
 PID is 7007 and cmd is /usr/lib/firefox-4.0.1/plugin-container
 PID is 6790 and cmd is /usr/lib/firefox-4.0.1/plugin-container
 PID is 6790 and cmd is /usr/lib/firefox-4.0.1/plugin-container
 PID is 6790 and cmd is /usr/lib/firefox-4.0.1/plugin-container
 PID is 7023 and cmd is /usr/lib/nspluginwrapper/i386/linux/npviewer.bin
 PID is 7023 and cmd is /usr/lib/nspluginwrapper/i386/linux/npviewer.bin
 PID is 7023 and cmd is /usr/lib/nspluginwrapper/i386/linux/npviewer.bin
 PID is 7023 and cmd is /usr/lib/nspluginwrapper/i386/linux/npviewer.bin
 PID is 7023 and cmd is /usr/lib/nspluginwrapper/i386/linux/npviewer.bin
 PID is 8390 and cmd is man
 PID is 8390 and cmd is man
 PID is 8402 and cmd is pager
 PID is 8402 and cmd is pager
 PID is 8827 and cmd is ping
 PID is 8827 and cmd is ping
 PID is 8869 and cmd is bash
 PID is 8907 and cmd is bash
 PID is 12637 and cmd is cat
 PID is 12637 and cmd is cat
 PID is 15164 and cmd is tftp
 PID is 15185 and cmd is vim
 PID is 15185 and cmd is vim
 PID is 16827 and cmd is telnet
 PID is 16827 and cmd is telnet
 PID is 17117 and cmd is sshd:
 PID is 17117 and cmd is sshd:
 PID is 17117 and cmd is sshd:
 PID is 17226 and cmd is sshd:
 PID is 17226 and cmd is sshd:
 PID is 17227 and cmd is -sh
 PID is 18120 and cmd is su
 PID is 18127 and cmd is bash
 PID is 18896 and cmd is sshd:
 PID is 18896 and cmd is sshd:
 PID is 18896 and cmd is sshd:
 PID is 19069 and cmd is sshd:
 PID is 19069 and cmd is sshd:
 PID is 19070 and cmd is -sh
 PID is 20313 and cmd is gedit
 PID is 20313 and cmd is gedit
 PID is 20313 and cmd is gedit
 PID is 20334 and cmd is ssh
 PID is 20334 and cmd is ssh
 PID is 20337 and cmd is [kworker/1:1]
 PID is 20341 and cmd is su
 PID is 20349 and cmd is bash
 PID is 20371 and cmd is [kworker/0:1]
 PID is 20375 and cmd is [kworker/0:2]
 PID is 20377 and cmd is [kworker/1:0]
 PID is 21342 and cmd is [kjournald]
 PID is 23384 and cmd is /opt/Adobe/Reader9/Reader/intellinux/bin/acroread
 PID is 23384 and cmd is /opt/Adobe/Reader9/Reader/intellinux/bin/acroread
 PID is 23495 and cmd is gnome-terminal
 PID is 23498 and cmd is gnome-pty-helper
 PID is 23499 and cmd is bash
 PID is 26446 and cmd is ssh
 PID is 26446 and cmd is ssh
 PID is 26733 and cmd is sshd:
 PID is 26733 and cmd is sshd:
 PID is 26843 and cmd is -bash
 PID is 26943 and cmd is sshd:
 PID is 26943 and cmd is sshd:
 PID is 27036 and cmd is sshd:
 PID is 27036 and cmd is sshd:
 PID is 27036 and cmd is sshd:
 PID is 27052 and cmd is /usr/lib/openssh/sftp-server
 PID is 27149 and cmd is sshd:
 PID is 27149 and cmd is sshd:
 PID is 27150 and cmd is -bash
 PID is 27734 and cmd is ssh
 PID is 27734 and cmd is ssh
 PID is 28265 and cmd is bin/TR69_DM
 PID is 0 and cmd is bin/TR69_DM
 PID is 28266 and cmd is ps
 PID is 28266 and cmd is ps
 PID is 29510 and cmd is su
 PID is 29510 and cmd is su
 PID is 29517 and cmd is bash
 PID is 29951 and cmd is minicom
 PID is 30056 and cmd is bash
 PID is 30293 and cmd is bash
 PID is 30329 and cmd is ssh
 PID is 30329 and cmd is ssh
 PID is 30597 and cmd is bash
 PID is 30632 and cmd is ssh
 PID is 30632 and cmd is ssh
 PID is 31508 and cmd is bash

可能出了什么问题?

1 个答案:

答案 0 :(得分:3)

fscanf()返回分配的数量。检查EOF是不够的,因为某些分配可能已经完成而其余分配没有,这意味着后续代码将处理陈旧或未初始化的值。当格式出现意外时,以下循环将停止第一行的处理并避免过时值:

while ( 2 == fscanf (FileDesc, "%*s %lu %*d %*d %*s %*s %*s %s", &Pid, Cmd))
{
}

改进的解决方案将使用fgets()读取一行,然后使用sscanf()来解析读取行。这将允许您检测EOF并允许多次尝试使用不同的格式解析该行:

char line[1024];
while (fgets(line, sizeof(line), stdin))
{
    if (2 == sscanf (line, "%*s %lu %*d %*d %*s %*s %*s %s", &Pid, Cmd))
    {
    }
    else if (2 == sscanf (line, "some-other-format", &Pid, Cmd))
    {
    }
    else
    {
        fprintf(stderr, "Unable to parse '%s'\n", line);
    }
}

此外,通过指定sscanf()应写入Cmd的最大字符数,防止潜在的缓冲区溢出。这应该小于Cmd的大小,为终止空字符留出空间。例如:

if (2 == sscanf(line, "%lu %255s", &Pid, Cmd))