编写一个简单的可执行ls ,pwd命令的shell.
并利用以下你unixshell基本结构:intmain(intargc,char**argv){while(1){char*cmd=get_next_command();...
并利用以下你unix shell 基本结构:
int main(int argc, char **argv)
{
while (1) {
char *cmd = get_next_command();
int child_pid = fork();
if (child_pid == 0) {
exec(cmd);
panic(“exec failed!”);
} else {
wait(child_pid);
}
}
}
首先,我单独编译这一部分的时候,就显示缺少头文件。当时我用了#include<stdio.h>和#include <unistd.h> .而且 大概是类似这种编写方式,这是cd命令的,但是我不理解: 展开
int main(int argc, char **argv)
{
while (1) {
char *cmd = get_next_command();
int child_pid = fork();
if (child_pid == 0) {
exec(cmd);
panic(“exec failed!”);
} else {
wait(child_pid);
}
}
}
首先,我单独编译这一部分的时候,就显示缺少头文件。当时我用了#include<stdio.h>和#include <unistd.h> .而且 大概是类似这种编写方式,这是cd命令的,但是我不理解: 展开
展开全部
写了一个,可以参考下。
楼主可以去看看“linux c编程”方面的书。
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#define MAXARG 10
#define LINSIZ 80
#define CMDSIZ 8
extern char **environ;
char *quit = "quit";
char cmdbuf[CMDSIZ][LINSIZ];
int cmdflag[CMDSIZ];
int main ()
{
int i;
for (;;)
{
printf ("mini_SH-->");
for (i = 7; i >= 0; i--)
{
cmdflag[i] = 0;
cmdbuf[i][0] = '\0';
}
if (i = get_next_command ())
docommand (i);
else
printf ("read command failed, try again!!!\n");
}
return 0;
}
int get_next_command ()
{
char c, *p;
int i = 0;
p = cmdbuf[0];
while ((c = getchar ()) != '\n')
{
if (c == ';')
{
*p = '\0';
if (++i == 6)
return (++i);
p = cmdbuf[i];
}
else if (c == '&')
{
cmdflag[i] = 1;
}
else
*p++ = c;
}
*p = '\0';
return (++i);
}
docommand (int i)
{
int j, stat, pid;
char *argl[MAXARG], args[LINSIZ];
char c, *argsp, **arglp, *p;
for (j = 0; j < i; j++)
{
arglp = argl;
argsp = args;
p = cmdbuf[j];
while ((c = *p++) != '\0')
{
while (c == ' ' || c == '\t')
c = *p++;
if (c == '\0')
{
*argsp++ = '\0';
break;
}
*arglp++ = argsp;
while ((c != ' ') && (c != '\t') && (c != '\0'))
{
*argsp++ = c;
c = *p;
if (c)
p++;
}
*argsp++ = '\0';
}
*arglp = (char *) 0;
if (strcmp (argl[0], quit) == 0)
{
printf ("Bye Bye!\n");
exit (0);
}
if ((pid = fork ()) == 0)
{
if (cmdflag[j])
setpgrp ();
if (strcmp (argl[0], "ls") == 0)
{
execve ("/bin/ls", argl, environ);
}
else if (strcmp (argl[0], "pwd") == 0)
{
execve ("/bin/pwd", argl, environ);
}
else
execve (argl[0], argl, environ);
printf ("Returned from execve: %s\n", cmdbuf[i]);
exit (10);
}
else
{
if (!cmdflag[j])
while (wait (&stat) != pid);
}
}
}
楼主可以去看看“linux c编程”方面的书。
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#define MAXARG 10
#define LINSIZ 80
#define CMDSIZ 8
extern char **environ;
char *quit = "quit";
char cmdbuf[CMDSIZ][LINSIZ];
int cmdflag[CMDSIZ];
int main ()
{
int i;
for (;;)
{
printf ("mini_SH-->");
for (i = 7; i >= 0; i--)
{
cmdflag[i] = 0;
cmdbuf[i][0] = '\0';
}
if (i = get_next_command ())
docommand (i);
else
printf ("read command failed, try again!!!\n");
}
return 0;
}
int get_next_command ()
{
char c, *p;
int i = 0;
p = cmdbuf[0];
while ((c = getchar ()) != '\n')
{
if (c == ';')
{
*p = '\0';
if (++i == 6)
return (++i);
p = cmdbuf[i];
}
else if (c == '&')
{
cmdflag[i] = 1;
}
else
*p++ = c;
}
*p = '\0';
return (++i);
}
docommand (int i)
{
int j, stat, pid;
char *argl[MAXARG], args[LINSIZ];
char c, *argsp, **arglp, *p;
for (j = 0; j < i; j++)
{
arglp = argl;
argsp = args;
p = cmdbuf[j];
while ((c = *p++) != '\0')
{
while (c == ' ' || c == '\t')
c = *p++;
if (c == '\0')
{
*argsp++ = '\0';
break;
}
*arglp++ = argsp;
while ((c != ' ') && (c != '\t') && (c != '\0'))
{
*argsp++ = c;
c = *p;
if (c)
p++;
}
*argsp++ = '\0';
}
*arglp = (char *) 0;
if (strcmp (argl[0], quit) == 0)
{
printf ("Bye Bye!\n");
exit (0);
}
if ((pid = fork ()) == 0)
{
if (cmdflag[j])
setpgrp ();
if (strcmp (argl[0], "ls") == 0)
{
execve ("/bin/ls", argl, environ);
}
else if (strcmp (argl[0], "pwd") == 0)
{
execve ("/bin/pwd", argl, environ);
}
else
execve (argl[0], argl, environ);
printf ("Returned from execve: %s\n", cmdbuf[i]);
exit (10);
}
else
{
if (!cmdflag[j])
while (wait (&stat) != pid);
}
}
}
浙江启扬智能科技有限公司
2023-06-12 广告
2023-06-12 广告
ARM,Android,Linux之间并无直接联系。ARM是嵌入式CPU,Andrioid和Linux是操作系统。 ARM不是为Linux而设计,Linux也不是为ARM而设计,二者是独立存在的。Linux和Android可以移植到以ARM...
点击进入详情页
本回答由浙江启扬智能科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |