|
昨晚通宵到早上五点做好的
学lINUX以来写的第一个小程序
大家有什么意见尽管说,什么都可以说,
小弟感激不尽
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<fcntl.h>
- #include<unistd.h>
- #include<sys/stat.h>
- #include<sys/dir.h>
- #include<dirent.h>
- #include <sys/types.h>
- #define s 100
- main()
- {
- char copybuf[s],pastebuf[s],copyname[s];
- int len=0,tag=0,cord,i;
- void treedisplay();
- void treecreat();
- void filecreatopen();
- void fileread(char pathname[]);
- void filewrite(char pathname[]);
- void treeopen();
- void treeback();
- long treefilecopy(char copybuf[],char copyname[]);
- void treefilepaste(int len,char pastebuf[],char copyname[]);
- void treedelete();
- void filedelete();
- void treefileproperty();
- void displayproperty();
- void modifypurview(char pathname[]);
- void treefilerename();
- void changesize();
-
- do {printf("\n mainmenu \n");
- printf(" 1 tree-display \n");/*显示目录*/
- printf(" 2 tree-creat \n");/*目录创建*/
- printf(" 3 file-creat/open \n");/*打开文件进行读写*/
- printf(" 4 tree-open \n");/*改变当前目录*/
- printf(" 5 tree-back \n");/*返回上一级目录*/
- printf(" 6 tree/file-copy \n");
- printf(" 7 tree/file-paste \n");
- printf(" 8 tree-delete \n");
- printf(" 9 file-delete \n");
- printf(" 10 tree/file-property \n");/*文件属性*/
- printf(" 11 end \n");
- printf("------------------------------\n");
- printf("input your choice(1-11)");
- scanf("%d",&cord);
- switch(cord)
- {case 1:{treedisplay();
- }break;
- case 2:{treecreat();
- }break;
- case 3:{filecreatopen();
- }break;
- case 4:{treeopen();
- }break;
- case 5:{treeback();
- }break;
- case 6:{len=treefilecopy(copybuf,copyname); tag=1;
- for(i=0;i<len;i++)pastebuf[i]=copybuf[i];
- }break;
- case 7:{if(tag&&len)treefilepaste(len,pastebuf,copyname);
- else printf("paste failed\n");
- }break;
- case 8:{treedelete();
- }break;
- case 9:{filedelete();
- }break;
- case 10:{treefileproperty();
- }break;
- case 11:{system("clear");exit(0);
- }break;
- }
- }while(cord<=11);
- }
- void treedisplay()
- {
- DIR *dir;char buf[s];
- struct dirent *ent;
- getcwd(buf,s);
- dir=opendir(buf);
- while((ent=readdir(dir))!=NULL)
- puts(ent->d_name);
- closedir(dir);
- }
- void treecreat()
- {
- int fd;char pathname[s];
- do{
- printf("new tree=");scanf("%s",pathname);
- fd=mkdir(pathname,O_CREAT);
- if(fd==-1)
- perror("creat failed,try again,the error");
- else
- {chmod(pathname,0777);
- printf("creat success!\n");
- }
- }while(fd==-1);
- }
- void filecreatopen()
- {
- int fd;char ch,choose,pathname[s];
- printf("new/open file=");scanf("%s",pathname);
- fd=open(pathname,O_CREAT|O_APPEND|O_RDWR);
- if(fd==-1)
- {perror("filecreat/open failed,try again,the error");
- close(fd);return;
- }
- else
- {chmod(pathname,0777);
- printf("creat/open success!\n");
- fileread(pathname);
- }
- printf("\ninput the text?(y/n)");
- scanf("%c",&choose);
- scanf("%c",&choose);
- if(choose=='y')filewrite(pathname);
- fileread(pathname);
- close(fd);
- }
- void fileread(char pathname[])
- {
- char ch;FILE *fp;
- fp=fopen(pathname,"r");
- ch=fgetc(fp);
- while(ch!=EOF)
- {putchar(ch);
- ch=fgetc(fp);
- }
- fclose(fp);
- }
- void filewrite(char pathname[])
- {
- char ch;FILE *fp;long where;
- fp=fopen(pathname,"rb+");
- printf("input the where of write=");
- scanf("%ld",&where);
- fseek(fp,where,SEEK_SET);
- printf("'#' for the EOF,begin:\n");
- ch=getchar();
- while(ch!='#')
- {
- fwrite(&ch,1,1,fp);
- ch=getchar();
- }
- fclose(fp);
- }
- void treeopen()
- {
- int fd;char pathname[s],buf[s];
- system("ls -l");
- printf("open tree=");scanf("%s",pathname);
- fd=chdir(pathname);
- if(fd==-1)
- {perror("treeopen failed,try again,the error");
- close(fd);return;
- }
- else {
- system("ls -l");
- getcwd(buf,s);
- printf("%s\n",buf);
- printf("treeopen success!\n");
- }
- }
- void treeback()
- {
- char buf[s];
- chdir("..");
- system("ls -l");
- getcwd(buf,s);
- printf("%s\n",buf);
- }
- long treefilecopy(char copybuf[],char copyname[])
- {
- struct stat buf;int fd;
- long size;/*off_t*/
- system("ls -l");
- printf("copy file/tree=");scanf("%s",copyname);
- fd=open(copyname,O_RDONLY);
- if(fd==-1)
- {perror("failed,try again,the error");
- return 0;
- }
- stat(copyname,&buf);
- size=buf.st_size;
- read(fd,copybuf,size);
- return size;
- }
- void treefilepaste(int len,char pastebuf[],char copyname[])
- {int fd;char pastename[]={"re-"};
- strcat(pastename,copyname);
- fd=open(pastename,O_CREAT|O_APPEND|O_RDWR);
- if(fd==-1)
- {perror("failed,try again,the error");
- return;
- }
- write(fd,pastebuf,len);
- system("ls -l");
- }
- void treedelete()
- {
- int fd;char pathname[s];
- system("ls -l");
- printf("delete tree=");scanf("%s",pathname);
- fd=rmdir(pathname);
- if(fd==-1)
- {perror("treedelete failed,try again,the error");
- return;
- }
- else {
- system("ls -l");
- printf("treedelete success!\n");
- }
- }
- void filedelete()
- {
- int fd;char pathname[s];
- system("ls -l");
- printf("delete file=");scanf("%s",pathname);
- fd=unlink(pathname);
- if(fd==-1)
- {perror("filedelete failed,try again,the error");
- return;
- }
- else {
- system("ls -l");
- printf("filedelete success!\n");
- }
- }
- void treefileproperty()
- {int cord;char choose;
- do { printf("\n treefileproperty \n");
- printf(" 1 display-property \n");
- printf(" 2 tree/file-rename \n");
- printf(" 3 file-chsize \n");
- printf(" 4 back mainmenu \n");
- printf("------------------------------\n");
- printf("input your choice(1-4)");
- scanf("%d",&cord);
- switch(cord)
- {case 1:{displayproperty();
- }break;
- case 2:{treefilerename();
- }break;
- case 3:{changesize();
- }break;
- case 4:{system("clear");return;
- }break;
- }
- }while(cord<=3);
- }
- void displayproperty()
- {
- struct stat buf;
- mode_t mode;
- int fd;char pathname[s],choose;
- system("ls -l");
- printf("file/tree=");scanf("%s",pathname);
- fd=open(pathname,O_RDWR);
- if(fd==-1)
- {perror("failed,try again,the error");
- return;
- }
- stat(pathname,&buf);
- mode=buf.st_mode;
- printf("%-10s","mode:");
- printf("%10s","read");printf("%10s","write");
- printf("%10s","execute\n");printf("%10s","all user");
- if((mode&0400)==S_IRUSR)printf("%10s","true");
- else printf("%10s","false");
- if((mode&0200)==S_IWUSR)printf("%10s","true");
- else printf("%10s","false");
- if((mode&0100)==S_IXUSR)printf("%10s","true\n");
- else printf("%10s","false\n");
- printf("%10s","group");
- if((mode&040)==S_IRGRP)printf("%10s","true");
- else printf("%10s","false");
- if((mode&020)==S_IWGRP)printf("%10s","true");
- else printf("%10s","false");
- if((mode&010)==S_IXGRP)printf("%10s","true\n");
- else printf("%10s","false\n");
- printf("%10s","other user");
- if((mode&04)==S_IROTH)printf("%10s","true");
- else printf("%10s","false");
- if((mode&02)==S_IWOTH)printf("%10s","true");
- else printf("%10s","false");
- if((mode&01)==S_IXOTH)printf("%10s","true\n");
- else printf("%10s","false\n");
- printf("size:%ld\n",buf.st_size);
- printf("BLK size:%ld\n",buf.st_blksize);
- printf("modify the purview?(y/n)");
- scanf("%c",&choose);scanf("%c",&choose);
- if(choose=='y')modifypurview(pathname);
- }
- void modifypurview(char pathname[])
- {
- char choose;int c=0;
- printf("all user read?(y/n)");
- scanf("%c",&choose);scanf("%c",&choose);
- if(choose=='y'){c=c+0400;chmod(pathname,c);}
- printf("all user write?(y/n)");
- scanf("%c",&choose);scanf("%c",&choose);
- if(choose=='y'){c=c+0200;chmod(pathname,c);}
- printf("all user execute?(y/n)");
- scanf("%c",&choose);scanf("%c",&choose);
- if(choose=='y'){c=c+0100;chmod(pathname,c);}
- printf("group read?(y/n)");
- scanf("%c",&choose);scanf("%c",&choose);
- if(choose=='y'){c=c+040;chmod(pathname,c);}
- printf("group write?(y/n)");
- scanf("%c",&choose);scanf("%c",&choose);
- if(choose=='y'){c=c+020;chmod(pathname,c);}
- printf("group execute?(y/n)");
- scanf("%c",&choose);scanf("%c",&choose);
- if(choose=='y'){c=c+010;chmod(pathname,c);}
- printf("other user read?(y/n)");
- scanf("%c",&choose);scanf("%c",&choose);
- if(choose=='y'){c=c+04;chmod(pathname,c);}
- printf("other user write?(y/n)");
- scanf("%c",&choose);scanf("%c",&choose);
- if(choose=='y'){c=c+02;chmod(pathname,c);}
- printf("other user execute?(y/n)");
- scanf("%c",&choose);scanf("%c",&choose);
- if(choose=='y'){c=c+01;chmod(pathname,c);}
- if(!c)chmod(pathname,c);
- displayproperty();
- }
- void treefilerename()
- {
- char oldname[s],newname[s];int fd;
- system("ls -l");
- printf("old name=");scanf("%s",oldname);
- printf("new name=");scanf("%s",newname);
- fd=rename(oldname,newname);
- if(fd==-1)
- {perror("rename failed,try again,the error");
- return;
- }
- else {
- system("ls -l");
- printf("rename success!\n");
- }
- }
- void changesize()
- { int fd,size;char pathname[s];
- printf("change file=");scanf("%s",pathname);
- printf("change size=");scanf("%d",&size);
- fd=truncate(pathname, size);
- if(fd==-1)perror("failed,try again,the error");
- else printf("success!\n");
- }
复制代码 |
|