简体   繁体   English

为什么系统调用 system() 在这个 C 程序中没有按预期工作?

[英]Why does the system call system() not work as intended in this C program?

This is a C program that connects two prcoesses (the parent and child) to a pipe. The child process runs a python script that filters a phrase (String) in an RSS feed and the parent process captures the URL and opens it in a browser.这是一个 C 程序,它将两个进程(父进程和子进程)连接到一个 pipe。子进程运行一个 python 脚本,该脚本过滤 RSS 提要中的一个短语(字符串),父进程捕获 URL 并在浏览器中打开它. This is the source code这是源代码

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

void open_url(char *url)
{
    char launch[255];

    sprintf(launch, "cmd /c start %s", url);
    system(launch);
}

void error_msg(char *msg)
{
    printf("msg: %s\n", msg);
    fprintf(stderr, "%s: %s\n", msg, strerror(errno));
    exit(1);
}

int main(int argc, char* argv[])
{
    char *phrase = argv[1];
    char *vars[] = {"RSS_FEED=https://rss.app/feeds/tUpJh41L1MCpL3dl.xml", NULL};
    int fd[2];

    if(pipe(fd) == -1)
        error_msg("Can't open a pipe");

    pid_t pid;
    pid = fork();
    if (pid == -1)
        error_msg("Can't fork process");

    if (!pid)
    {
        dup2(fd[1], 1);
        close(fd[0]);

       if (execle(
               "C:/Users/LENOVO/AppData/Local/Programs/Python/Python310/python",
               "C:/Users/LENOVO/AppData/Local/Program/Python/Python310/python",
               "./rssgossip.py", "-u", phrase, NULL, vars) == -1
          )
           error_msg("Can't run script");
    }

    dup2(fd[0], 0);
    close(fd[1]);

    char line[255];

    while(fgets(line, 255, stdin))
    {
        if (line[0] == '\t')
            open_url(line + 1);
    }

    return (0);
}

The program compiles without any errors, but when the parent calls system(launch) in open_url(), here's the issue.该程序编译没有任何错误,但是当父级在 open_url() 中调用 system(launch) 时,问题就出在这里。 It surprisingly executes only the first part of the command stored in launch which is "cmd /c start" and ignores the url. More surprisingly is when I debugged the program through a simple printf statement, printf("%s", launch), I replaced the same output of the printf statement with the launch variable so instead of system(launch) -> system("cmd /c start 'url'") and it executed the url in a browser as intended.令人惊讶的是,它只执行存储在 launch 中的命令的第一部分,即“cmd /c start”,而忽略了 url。更令人惊讶的是,当我通过一个简单的 printf 语句 printf("%s", launch) 调试程序时,我将 printf 语句的相同 output 替换为启动变量,而不是 system(launch) -> system("cmd /c start 'url'") 并按预期在浏览器中执行 url。

EDIT: This is the direct downloading link of the python script (rssgossip.py) used in this program if you would like to try it to get what I'm trying to do clearer.编辑:这是该程序中使用的 python 脚本 (rssgossip.py) 的直接下载链接,如果您想尝试它以更清楚地了解我正在尝试做的事情。 I recommend you running the script as a Python script first (with Python interpreter) to understand what actually this script does.我建议您首先将脚本作为 Python 脚本运行(使用 Python 解释器)以了解该脚本的实际作用。 To run it as a Python script, you would need to define RSS_FEED environment variable and assign an rss feed to it RSS_FEED=<rss_feed_url> (you can use the rss_feed_url used in this program) and finally run it as python rssgossip.py '<any_phrase>'要将其作为 Python 脚本运行,您需要定义 RSS_FEED 环境变量并为其分配一个 rss 提要 RSS_FEED=<rss_feed_url>(您可以使用此程序中使用的 rss_feed_url),最后将其作为 python rssgossip.py '<任何短语>'

when i tried this code and it ran fine当我尝试这段代码并且运行良好时

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

void open_url(char *url)
{
    char launch[255];

    sprintf(launch, "cmd /c start "" %s", url);
    printf("%s",launch);
    system(launch);

}

int main()
{
    char url[] = "C:/emu8086/emu8086.exe";
    open_url(url);
}

may be the problem at parameter which you input可能是你输入的参数有问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM