朋友不会做作业,哪位好心帮忙?在LINUX下开发一个小程序。

gdyaojie 2005-03-09 05:21:12
具体题目见下面网址,是专升本的实习题目:

http://www.open.ha.cn/linux/shiyan2.htm

下面为部分内容:
-----------------------------------------------
实验二  命令解释程序


1、目的 :

通过本实验熟悉UNIX或Linux操作系统及C语言。熟悉系统调用的编程能力,程序中允许使用库函数。

2、内容:

利用C语言编写一个微型命令解释程序,接受并解释以下命令:


⑴dir 列当前目录
⑵cop 文件1 文件2 拷贝文件
⑶era文件名 删除文件
⑷dis字符串 显示字符串
⑸end 结束,退出

3、要求:

⑴进行命令合法性检查,若不合法,显示出错信息,等待重新输入;
⑵命令前后有空格为合法命令。

-------------------------------------------------------------
如果有人做出来,加分,谢谢大家了。
...全文
333 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
TangChin 2005-03-10
  • 打赏
  • 举报
回复
我最近闲的很,给你写了一份。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXSIZE 100
static char * usercmd[] = {"dir","cop","era","dis","end"};
static char * syscmd[] = {"ls -l","cp","rm -f","echo"};
static char * usage =
"Welcome to use myshell, the follows are supported command\n"
"dir -- show current directory and file\n"
"cop file1 file2 -- copy file1 to file2 \n"
"era file -- delete file\n"
"dis string -- display string on screen\n"
"end -- exit myshell \n"
"NOTICE: Blank is avail before or after command\n";

int main(int argc, char * argv[])
{
char *input=NULL;
char *cmd=NULL,*para=NULL;
char buf[MAXSIZE];
int i,count;
input = malloc(MAXSIZE);
cmd = malloc(MAXSIZE);
para = malloc(MAXSIZE);
if (input == NULL || cmd == NULL || para == NULL)
{
perror("malloc failed");
exit(-1);
}

while(1)
{
printf("myshell>");
fgets(input, 100, stdin);
*(input+(strlen(input)-1)) = '\0';//delete '\n'
while(*input != '\0')
{
if (*input != ' ' && *input != '\t')
break;
++input;
}
if (*input == '\0')//all input is blank
{
printf("%s",usage);
continue;
}
//division input to command and parameter parts
count = strcspn(input, " \t");
strncpy(cmd, input, count);
*(cmd + count) = '\0';
strncpy(para, input+count,strlen(input)-count);
*(para+(strlen(input)-count))='\0';

if (0 == strcmp(cmd, usercmd[0]))
{
//根据这道题,分配的buf够用了,应该不会越界。
//如果要求很严格的话,这里用strncpy,strncat。
strcpy(buf,syscmd[0]);
strcat(buf,para);
system(buf);
continue;
}
if (0 == strcmp(cmd, usercmd[1]))
{
strcpy(buf,syscmd[1]);
strcat(buf,para);
system(buf);
continue;
}
if (0 == strcmp(cmd, usercmd[2]))
{
strcpy(buf,syscmd[2]);
strcat(buf,para);
system(buf);
continue;
}
if (0 == strcmp(cmd, usercmd[3]))
{
strcpy(buf,syscmd[3]);
strcat(buf,para);
system(buf);
continue;
}
if (0 == strcmp(cmd, usercmd[4]))
{
printf("Exit myshell\n");
break;
}
printf("%s",usage);
}
return 0;
}
gdyaojie 2005-03-10
  • 打赏
  • 举报
回复
好说好说呀,要多少分?开个价,我是太久没有写过这种程序了。帮不上忙。
piaozi2003 2005-03-10
  • 打赏
  • 举报
回复
我最烦自己不想就让别人写代码,如果你决的这个实习题目中有哪些问题不知道解决或者不知道如何下手,我会尽量帮助你。写代码,就别想了!
gdyaojie 2005-03-10
  • 打赏
  • 举报
回复
谢谢TangChin(我爱我家)
raym_xie 2005-03-09
  • 打赏
  • 举报
回复
怎么只有说简单的,没有贴出来的。我也想知道,别光说,贴出来最实际吧。
raygts 2005-03-09
  • 打赏
  • 举报
回复
太简单了
raym_xie 2005-03-09
  • 打赏
  • 举报
回复
piaozi2003同学,贴个源代码看看。
naturemickey 2005-03-09
  • 打赏
  • 举报
回复
除了第三个我都作过。
不难嘛!
piaozi2003 2005-03-09
  • 打赏
  • 举报
回复
说句心里话,这题目不是很难!

但如果要求一定的健壮性和移植性的话,还是挺麻烦的!关键看怎么要求了!

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧