将用户C字符串输入获取到c中的exec()函数中(Getting user C String input into exec() function in c)

编程入门 行业动态 更新时间:2024-10-25 15:19:42
将用户C字符串输入获取到c中的exec()函数中(Getting user C String input into exec() function in c)

这是一般性问题:程序必须fork()和wait()才能完成子进程。 这个孩子将exec()另一个用户名为INPUT的程序。

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main(void) { int status; char input[BUFSIZ]; printf(">"); scanf("%s",input); char *args[] = {"./lab1"}; pid_t pid = fork(); if(pid==0){ execvp(args[0],args); }else if(pid<0){ perror("Fork fail"); }else{ wait(&status); printf("My Child Information is: %d\n", pid); } return 0; }

我的问题是让用户输入一个程序名称来运行(在“>”提示符下)并将该输入输入execvp(或其他exec()函数,如果有人有任何想法)

Here is the general problem: The program must fork() and wait() for the child to finish. The child will exec() another program whose name is INPUT by the user.

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main(void) { int status; char input[BUFSIZ]; printf(">"); scanf("%s",input); char *args[] = {"./lab1"}; pid_t pid = fork(); if(pid==0){ execvp(args[0],args); }else if(pid<0){ perror("Fork fail"); }else{ wait(&status); printf("My Child Information is: %d\n", pid); } return 0; }

My problem is getting the user to input a program name to run (at the ">" prompt) and getting that input into execvp (or another exec() function if anyone has any ideas)

最满意答案

我现在要暂时放弃使用scanf("%s") ,尽管你应该知道它并不是健壮的代码 。

这里的基本任务是获取用户输入的字符数组,并以某种方式将其转换为适合传递给execvp的字符指针数组。

您可以使用strtok将输入字符串标记为由空格分隔的标记,并使用malloc/realloc以确保在数组中有足够的元素来存储字符串。

或者,由于您已经存在潜在的缓冲区溢出问题,因此仅使用固定大小的数组可能就足够了。


例如,下面的程序显示了这样做的一种方法,它使用固定的字符串echo my hovercraft is full of eels并标记它适合执行:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> static char *myStrDup (char *str) { char *other = malloc (strlen (str) + 1); if (other != NULL) strcpy (other, str); return other; } int main (void) { char inBuf[] = "echo my hovercraft is full of eels"; char *argv[100]; int argc = 0; char *str = strtok (inBuf, " "); while (str != NULL) { argv[argc++] = myStrDup (str); str = strtok (NULL, " "); } argv[argc] = NULL; for (int i = 0; i < argc; i++) printf ("Arg #%d = '%s'\n", i, argv[i]); putchar ('\n'); execvp (argv[0], argv); return 0; }

然后它输出标记的参数并执行它:

Arg #0 = 'echo' Arg #1 = 'my' Arg #2 = 'hovercraft' Arg #3 = 'is' Arg #4 = 'full' Arg #5 = 'of' Arg #6 = 'eels' my hovercraft is full of eels

I'm going to hold off lambasting you for using scanf("%s") for now, though you should be aware it's really not robust code.

Your basic task here is going to be taking a character array entered by the user and somehow turning that into an array of character pointers suitable for passing to execvp.

You can use strtok to tokenise the input string into tokens separated by spaces, and malloc/realloc to ensure you have enough elements in an array to store the strings.

Alternatively, since you already have a potential buffer overflow issue, it may be good enough to just use a fixed size array.


For example, the following program shows one way of doing this, it uses a fixed string echo my hovercraft is full of eels and tokenises it to be suitable for execution:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> static char *myStrDup (char *str) { char *other = malloc (strlen (str) + 1); if (other != NULL) strcpy (other, str); return other; } int main (void) { char inBuf[] = "echo my hovercraft is full of eels"; char *argv[100]; int argc = 0; char *str = strtok (inBuf, " "); while (str != NULL) { argv[argc++] = myStrDup (str); str = strtok (NULL, " "); } argv[argc] = NULL; for (int i = 0; i < argc; i++) printf ("Arg #%d = '%s'\n", i, argv[i]); putchar ('\n'); execvp (argv[0], argv); return 0; }

Then it outputs the tokenised arguments and executes it:

Arg #0 = 'echo' Arg #1 = 'my' Arg #2 = 'hovercraft' Arg #3 = 'is' Arg #4 = 'full' Arg #5 = 'of' Arg #6 = 'eels' my hovercraft is full of eels

更多推荐

本文发布于:2023-07-25 22:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   函数   用户   input   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!