资源描述:
《实验6- LINUX环境编程-2.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验6、LINUX环境编程-2学生姓名:李亚军学号:6100412196专业班级:卓越计科121班1.实验目的结合文件操作,理解LINUX环境中的命令行参数的使用。2.实验内容1.编写LINUXC程序,实现简单的LINUX命令ls功能。3.实验步骤(1)代码myls.c#include#include#include#include#include#include#include#include#include
2、#include#include#include#include#include#include#include#defineOPT_v0x0001/*-v选项:查看本软件版本*/#defineOPT_h0x0002/*-h选项:查看本软件帮助*/#defineOPT_l0x0004/*-l选项:长型(long)显示*/#defineOPT_a0x0008/*-a选项:显示所有文件(all)*/#defineOPT_R0x0010/*-R选项
3、:显示子目录内容*/#defineOPT_f0x0020/*-f选项:不排序*/#defineDONE_SUCCESS0typedefstruct_Node{charpath[1024];charname[256];intlength;structstatst;struct_Node*pnext;}nNode;intcreatelslink(char*path,intflag,nNode**head){DIR*dp;structdirent*entry;structstatstatbuf;nNode*p;charabspath[1024];if((dp=opendir(path))=
4、=NULL){fprintf(stderr,"cannotopendirectory:%s",path);return-1;}if(chdir(path)==-1){fprintf(stderr,"cannotcddirectory:%s",path);return-2;}if(NULL==getcwd(abspath,1024)){fprintf(stderr,"getcwderror!");return-3;}while((entry=readdir(dp))!=NULL){lstat(entry->d_name,&statbuf);if((!(flag&OPT_a)
5、)&&(entry->d_name[0]=='.'))/*没有选项-a则不显示.开头的文件(或目录)名*/continue;else{p=(nNode*)malloc(sizeof(nNode));p->pnext=*head;*head=p;(*head)->length=strlen(entry->d_name);strcpy((*head)->path,abspath);strcpy((*head)->name,entry->d_name);memcpy(&((*head)->st),&statbuf,sizeof(structstat));}}if(-1==chdir("..
6、")){fprintf(stderr,"cannotcddirectory:..");return-3;}if((closedir(dp))==-1){fprintf(stderr,"cannotclosedp");return-4;}returnDONE_SUCCESS;}voidsortlslink(nNode*head,intflag){nNode*p,*q,r;intmsuccess;if(!head)return;if(flag&OPT_f){;}else{p=head;while(p->pnext)p=p->pnext;while(p!=head){q=head;
7、msuccess=1;while(q->pnext){if(strcmp(q->name,q->pnext->name)>0){memcpy(&r,q,sizeof(nNode));strcpy(q->name,q->pnext->name);strcpy(q->path,q->pnext->path);q->length=q->pnext->length;memcpy(&q->st,&(q->pnext->st),sizeof(q->st));strcp