全国统一服务热线
400-6328-059
为了满足需要,我们将使用 execvp,它的签名看起来像这样:
int execvp(const char *file, char *const argv[]);
函数名中的 vp 表明:它接受一个文件名,将在系统 $PATH 变量中搜索此文件名,它还接受将要执行的一组参数。
你可以阅读 exec 的 man 页面 以得到其它函数的更多信息。让我们看一下以下代码,它执行命令 ls -l -h -a:
execvp.c #include <unistd.h> int main() { char *argv[] = {"ls", "-l", "-h", "-a", NULL}; execvp(argv[0], argv); return 0; }
#include <unistd.h> int main() { char *argv[] = {"ls", "-l", "-h", "-a", NULL}; execvp(argv[0], argv); return 0; }
total 32 drwxr-xr-x 5 dhanush staff 170B Jun 11 11:32 . drwxr-xr-x 4 dhanush staff 136B Jun 11 11:30 .. -rwxr-xr-x 1 dhanush staff 8.7K Jun 11 11:32 a.out drwxr-xr-x 3 dhanush staff 102B Jun 11 11:32 a.out.dSYM -rw-r--r-- 1 dhanush staff 130B Jun 11 11:32
total 32 drwxr-xr-x 5 dhanush staff 170B Jun 11 11:32 . drwxr-xr-x 4 dhanush staff 136B Jun 11 11:30 .. -rwxr-xr-x 1 dhanush staff 8.7K Jun 11 11:32 a.out drwxr-xr-x 3 dhanush staff 102B Jun 11 11:32 a.out.dSYM -rw-r--r-- 1 dhanush staff 130B Jun 11 11:32
它和你在你的主 shell 中手动执行ls -l -h -a的结果完全相同。
既然我们能执行命令了,我们需要使用在部分中学到的fork 系统调用构建有用的东西。事实上我们要做到以下这些:我们看看下面的函数,它接收一个字符串作为输入。我们使用库函数 strtok 以空格分割该字符串,然后返回一个字符串数组,数组也用 NULL来终结。
include <stdlib.h> #include <string.h> char **get_input(char *input) { char **command = malloc(8 * sizeof(char *)); char *separator = " "; char *parsed; int index = 0; parsed = strtok(input, separator); while (parsed != NULL) { command[index] = parsed; index++; parsed = strtok(NULL, separator); } command[index] = NULL; return command; }
include <stdlib.h> #include <string.h> char **get_input(char *input) { char **command = malloc(8 * sizeof(char *)); char *separator = " "; char *parsed; int index = 0; parsed = strtok(input, separator); while (parsed != NULL) { command[index] = parsed; index++; parsed = strtok(NULL, separator); } command[index] = NULL; return command; }
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <readline/readline.h> #include <unistd.h> #include <sys/wait.h> int main() { char **command; char *input; pid_t child_pid; int stat_loc; while (1) { input = readline("unixsh> "); command = get_input(input); child_pid = fork(); if (child_pid == 0) { /* Never returns if the call is successful */ execvp(command[0], command); printf("This won't be printed if execvp is successuln"); } else { waitpid(child_pid, &stat_loc, WUNTRACED); } free(input); free(command); } return 0; }
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <readline/readline.h> #include <unistd.h> #include <sys/wait.h> int main() { char **command; char *input; pid_t child_pid; int stat_loc; while (1) { input = readline("unixsh> "); command = get_input(input); child_pid = fork(); if (child_pid == 0) { /* Never returns if the call is successful */ execvp(command[0], command); printf("This won't be printed if execvp is successuln"); } else { waitpid(child_pid, &stat_loc, WUNTRACED); } free(input); free(command); } return 0; }
unixsh> pwd /Users/dhanush/github.com/indradhanush.github.io/code/shell-part-2 unixsh> ls -lha total 28K drwxr-xr-x 6 root root 204 Jun 11 18:27 . drwxr-xr-x 3 root root 4.0K Jun 11 16:50 .. -rwxr-xr-x 1 root root 16K Jun 11 18:27 a.out drwxr-xr-x 3 root root 102 Jun 11 15:32 a.out.dSYM -rw-r--r-- 1 root root 130 Jun 11 15:38 execvp.c -rw-r--r-- 1 root root 997 Jun 11 18:25 shell.c unixsh>
unixsh> pwd /Users/dhanush/github.com/indradhanush.github.io/code/shell-part-2 unixsh> ls -lha total 28K drwxr-xr-x 6 root root 204 Jun 11 18:27 . drwxr-xr-x 3 root root 4.0K Jun 11 16:50 .. -rwxr-xr-x 1 root root 16K Jun 11 18:27 a.out drwxr-xr-x 3 root root 102 Jun 11 15:32 a.out.dSYM -rw-r--r-- 1 root root 130 Jun 11 15:38 execvp.c -rw-r--r-- 1 root root 997 Jun 11 18:25 shell.c unixsh>
... while (1) { input = readline("unixsh> "); command = get_input(input); child_pid = fork(); if (child_pid < 0) { perror("Fork failed"); exit(1); } ...
... while (1) { input = readline("unixsh> "); command = get_input(input); child_pid = fork(); if (child_pid < 0) { perror("Fork failed"); exit(1); } ...
... if (execvp(command[0], command) < 0) { perror(command[0]); exit(1); } ...
... if (execvp(command[0], command) < 0) { perror(command[0]); exit(1); } ...
char **get_input(char *input) { char **command = malloc(8 * sizeof(char *)); if (command == NULL) { perror("malloc failed"); exit(1); } ...
char **get_input(char *input) { char **command = malloc(8 * sizeof(char *)); if (command == NULL) { perror("malloc failed"); exit(1); } ...
cd: No such file or directory cd: No such file or directory
int cd(char *path) { return chdir(path); }
while (1) { input = readline("unixsh> "); command = get_input(input); if (strcmp(command[0], "cd") == 0) { if (cd(command[1]) < 0) { perror(command[1]); } /* Skip the fork */ continue; } ...
while (1) { input = readline("unixsh> "); command = get_input(input); if (strcmp(command[0], "cd") == 0) { if (cd(command[1]) < 0) { perror(command[1]); } /* Skip the fork */ continue; } ...
Shell unixsh> pwd /Users/dhanush/github.com/indradhanush.github.io/code/shell-part-2 unixsh> cd / unixsh> pwd / unixsh>
unixsh> pwd /Users/dhanush/github.com/indradhanush.github.io/code/shell-part-2 unixsh> cd / unixsh> pwd / unixsh>
第二部分到此结束。在下一篇文章中,我们将探讨信号的主题以及实现对用户中断(Ctrl-C)的处理。敬请期待。
想要了解更多的C语言应用技术点击咨询小编
尊重原创文章,转载请注明出处与链接:http://www.mxiao.cn/1700/new/203541/违者必究! 以上就是武汉粤嵌嵌入式培训机构 小编为您整理学习c++开发课程需要先开始学习C语言吗哪些课程的全部内容。
武汉畅学嵌入式专业课程培训班
武汉嵌入式人工智能120天培训班课程
武汉嵌入式开发就业班
武汉嵌入式软件工程师就业班(C/C++方向)