简体   繁体   中英

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. 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. 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.

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. I recommend you running the script as a Python script first (with Python interpreter) to understand what actually this script does. 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>'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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