我对 c 中的 execlp() 有疑问。
我有以下程序:
#include <stdio.h>
#include <unistd.h>
#include <sys/unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
#include <limits.h>
#include <signal.h>
void INThandler(int);
int main(int argc, char* argv[]) {
struct passwd *pwd;
char *lgn;
char *cwd;
char buff[PATH_MAX + 1];
char s1[10], s2[10];
/*Um den Namen zu bekommen*/
lgn = getlogin();
pwd = getpwnam(lgn);
/*Um den Hostnamen zu bekommen*/
char hostname[128];
gethostname(hostname, sizeof hostname);
/*Um das Arbeitsverzeichnis zu bekommen*/
cwd = getcwd(buff, PATH_MAX + 1);
if((cwd!=NULL)&& hostname!=NULL && ((lgn=getlogin())!=NULL ||
(pwd!=NULL)))
{
signal(SIGINT, INThandler);
while(1)
{
printf("%s@%s %s$", pwd->pw_name, hostname, cwd);
if(scanf("%s %s",s1, s2)<1)
return 1;
printf("Befehl: %s\nArgument: %s\n",s1,s2);
execlp(s1, s1, NULL);
printf("Zhopa");
return 1;
}
}
return 0;
}
void INThandler(int sig) {
char c;
signal(sig, SIG_IGN);
printf("Wollen Sie Program Verlassen? [y/n]");
c = getchar();
if(c == 'y' || c=='Y')
exit(0);
else
signal(SIGINT, INThandler);
getchar();
}
它应该打印用户名@hostname 文件夹$,然后将 linux 命令作为参数“ls -al”,然后它应该使用 execlp() 启动它,但它不像我认为的那样工作。
我在这里阅读了有关此命令的所有文章,但我想,我仍然不明白如何使用它。
我会很感激有人的帮助。