繁体   English   中英

如何从终端输入(linux)运行程序?

[英]How to run a program from terminal input(linux)?

虽然我认为我已经掌握了fork()exec()wait()pid在C中工作,但我还没有找到一种如何在程序中运行个人程序的方法。

这是我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h> /* for fork() */
#include<sys/types.h> /* for pid_t */
#include<sys/wait.h> /* fpr wait() */

int main(int argc, char* argv[])
{
    char  fileName[255];
    pid_t pid;

    switch (pid = fork()) {
    case -1: //Did not fork properly
        perror("fork");
        break;

    case 0: //child
        execv(fileName[0],fileName);

        puts("Oh my. If this prints, execv() must have failed");
        exit(EXIT_FAILURE);
        break;
    default: //parent
        //Infinite Loop
        while (1) {
            printf(" %s > ", argv[0]);
            scanf("%s", fileName); // gets filename
            if (fileName[0] == '\0') continue;
            printf("\n Entered file: %s",fileName); // prints the fileName
            waitpid(pid,0,0); /* wait for child to exit() */
            break;
        }
    }

    return 0;
}

我的问题如下:

  1. 我想把一个字符串作为输入,我想将其范围限制为255个字符。 char fileName[255]然后是scanf("%s", fileName); 要走的路? 我应该使用getLine()或其他功能吗?

  2. 假设输入正确。 如何执行说现有的hello world程序。 输入是否会存储在*argv[] 我发现在我可以使用的其他程序中

     static char *argv[] = { "echo", "Foo is my name." , NULL }; execv("/bin/echo", argv); 

    为了回应“Foo是我的名字”。 我可以用helloWorld程序做类似的事情吗?

您将一个字符作为命令名称传递,并将名称字符串作为参数列表的开头传递 - 就好像execv()的原型是int execv(char cmd, char *args)

实际的原型是: int execv(char *cmd, char **args) ,所以你需要:

char *args[2] = { fileName, 0 };

execv(args[0], args);

我假设您将fileName设置为某个有意义的值 - 未显示。 例如,它可能是"./local_program" 它将被视为可执行文件的路径名。

如果要读取名称,则可以使用fgets()getline() ,但是您需要删除换行符:

if (fgets(fileName, sizeof(fileName), stdin) != 0)
{
    fileName[strcspn(fileName, "\n")] = '\0';
    …as before…
}

要么:

char *fileName = 0;
size_t length = 0;
if (getline(&fileName, &length, stdin) != -1)  /* Not EOF! */
{
    fileName[strcspn(fileName, "\n")] = '\0';
    …as before…
}
free(fileName);

strcspn()的使用避免了特殊情况下过长的命令行,因此fileName没有换行符。 请注意,这不会尝试将输入行拆分为命令名和空格中的参数或类似的任何东西。 这是shell实现的下一个级别:

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

int main(void)
{
    char fileName[256];
    if (fgets(fileName, sizeof(fileName), stdin) != 0)
    {
        fileName[strcspn(fileName, "\n")] = '\0';
        char *args[129];
        char **argv = args;
        char *cmd = fileName;
        const char *whisp = " \t\f\r\b\n";
        /* Ugh — strtok()?  OK while only handling white space separators */
        char *token;
        while ((token = strtok(cmd, whisp)) != 0)
        {
            *argv++ = token;
            cmd = 0;
        }
        *argv = 0;

        execv(args[0], args);
        fprintf(stderr, "Oops!\n");
    }
    return 1;
}

我不需要检查args数组的溢出,因为输入的256个字符减去终止null,不能被分割以产生超过128个单字符参数,每个参数与相邻的一个空格字符分开。 使用strtok()是一个临时的创可贴。 一旦你需要处理真正的shell语法(管道,I / O重定向,引用带空格的字符串等), strtok()就是错误的工具。 (它 - strtok() - 也是在任何库函数中使用的错误函数。如果必须在库代码中使用类似strtok()的函数,请在Unix上使用POSIX strtok_r()或在Windows上使用Microsoft的strtok_s() 。)

就目前而言,您需要修复代码中的编译错误:

g++ -std=c++11 -g -Wall -Wextra -Wwrite-strings     36501711.cpp   -o 36501711
36501711.cpp: In function ‘int main(int, char**)’:
36501711.cpp:18:25: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
         execv(fileName[0],fileName);
                         ^
36501711.cpp:18:35: error: cannot convert ‘char*’ to ‘char* const*’ for argument ‘2’ to ‘int execv(const char*, char* const*)’
         execv(fileName[0],fileName);
                                   ^
36501711.cpp: At global scope:
36501711.cpp:7:5: warning: unused parameter ‘argc’ [-Wunused-parameter]
 int main(int argc, char* argv[])
     ^

警告(关于未使用的argc )是无害的,但错误是真实的,需要修复。

您需要 fork 之前初始化fileName ,否则子fileName将不会有任何有效数据:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h> /* for fork() */
#include<sys/types.h> /* for pid_t */
#include<sys/wait.h> /* fpr wait() */

int main(int argc, char* argv[])
{
    char  fileName[255];
    pid_t pid;

    //Infinite Loop
    while (1) {
        printf(" %s > ", argv[0]);
        scanf("%s", fileName);
        if (fileName[0] == '\0')
            break;
        printf("\n Entered file: %s\n", fileName);

        pid = fork();
        switch (pid) {
        case -1: //Did not fork properly
            perror("fork");
            break;

        case 0: //child
            execl(fileName, fileName, 0);
            perror("exec");
            exit(EXIT_FAILURE);
            break;

        default: //parent
            waitpid(pid,0,0); /* wait for child to exit() */
            break;
        }
    }

    return EXIT_SUCCESS;
}

暂无
暂无

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

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