这是一般性问题:程序必须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 eelsI'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更多推荐
发布评论