Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360416
  • 博文数量: 78
  • 博客积分: 3380
  • 博客等级: 中校
  • 技术积分: 857
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-16 19:39
文章分类

全部博文(78)

文章存档

2011年(31)

2010年(47)

分类: LINUX

2010-08-21 16:34:45

/* ************************************************************************
 *       Filename:  FileManager.c
 *    Description: 
 *        Version:  1.0
 *        Created:  2010年08月21日 13时10分34秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:  chengbin_liu
 *        Company: 
 * ************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#define SIZE 100
//================================================================================
void welcome()
{
 printf("**********************************************************\n");
 printf("\tmainmenu\n");
 printf("\t1,tree-display\n");
 printf("\t2,tree-creat\n");
 printf("\t3,file-create/open\n");
 printf("\t4,tree-open\n");
 printf("\t5,tree-back\n");
 printf("\t6,tree/file-copy\n");
 printf("\t7,tree/file-paste\n");
 printf("\t8,tree-delete\n");
 printf("\t9,file-delete\n");
 printf("\t10,tree/file-property\n");
 printf("\t11,tree/file-rename\n");
 printf("\t12,tree/file-changesize\n");
 printf("\t13,end\n");
 printf("************************************************************\n");
}
//=============================================================================
//show directory
void treedisplay()
{
 DIR *dir;
 char buf[SIZE];
 struct dirent *ent;
 getcwd(buf,SIZE);
 dir=opendir(buf);
 while((ent=readdir(dir))!=NULL)
  puts(ent->d_name);
 closedir(dir);
}
//============================================================================
//create the directory
void treecreate()
{
 int fd;
 char pathname[SIZE];
 do
 {
  printf("new tree==");
  scanf("%s",pathname);
  fd=mkdir(pathname,O_CREAT);
  if(fd==-1)
  {
   perror("create failed,try again!\n");
  }
  else
  {
   chmod(pathname,0761);
   printf("create successizes!\n");
  }
 }while(fd==-1);
}
//=============================================================================
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);
}
//============================================================================
//open the file and readwrite
void filecreateopen()
{
 int fd;
 char choose, pathname[SIZE];
 printf("new/open==");
 scanf("%s",pathname);
 fd=open(pathname,O_CREAT | O_APPEND | O_RDWR);
 if(fd==-1)
 {
  perror("file create/open failed,try again.....\n");
  close(fd);
  exit(1);
 }
 else
 {
  chmod(pathname,0761);
  printf("file create/open success!\n");
  fileread(pathname);
 }
 printf("\ninput the text? (y/n)");
 scanf("%c",&choose);
 scanf("%c",&choose);
 if(choose=='y') filewrite(pathname);
  fileread(pathname);
 printf("\n");
 close(fd);
}
//============================================================================
//=============================================================================
//change the current directory
void treeopen()
{
 int fd;
 char pathname[SIZE],buf[SIZE];
 system("ls -l");
 printf("open tree==");
 scanf("%s",pathname);
 fd=chdir(pathname);
 if(fd==-1)
 {
  perror("tree open failed ,try again...");
  close(fd);
  return;
 }
 else
 {
  system("ls -l");
  getcwd(buf,SIZE);
  printf("%s\n",buf);
  printf("tree open ok..\n");
 }
}
//=============================================================================
//turn back previous directory
void treeback()
{
 char buf[SIZE];
 chdir("..");
 system("ls -l");
 getcwd(buf,SIZE);
 printf("%s\n",buf);
}
//=============================================================================
//copy operation
long treefilecopy(char copybuf[],char copyname[])
{
 int fd;
 struct stat buf;
 long size;
 system("ls -l");
 printf("copy file /tree==");scanf("%s",copyname);
 fd=open(copyname,O_RDONLY);
 if(fd==-1)
 {
  perror("failed ,try again!..");
  return 0;
 }
 else
 stat(copyname,&buf);
 size=buf.st_size;
 read(fd,copybuf,size);
 return size;
}
//============================================================================
//paste operation
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..");
  return;
 }
 write(fd,pastebuf,len);
 system("ls -l");
}
//=============================================================================
//delete the directory
void treedelete()
{
 int fd;
 char pathname[SIZE];
 system("ls -l");
 printf("delete tree==");
 scanf("%s",pathname);
 fd=rmdir(pathname);
 if(fd==-1)
 {
  perror("delete tree failed,try again..");
  close(fd);
  return;
 }
 else
 {
  system("ls -l");
  printf("delete success\n!");
 }
}
//============================================================================
//delete the file
void filedelete()
{
 int fd;
 char pathname[SIZE];
 system("ls -l");
 printf("delete file==");
 scanf("%s",pathname);
 fd=unlink(pathname);
 if(fd==-1)
 {
  perror("delete file failed,try again...\n");
  return;
 }
 else
 {
  system("ls -l");
  printf("delete file success!\n");
 }
}
//============================================================================
//file property
void treefileproperty()
{
 int fd;
 struct stat buf;
 mode_t mode;
 char pathname[SIZE];
 system("ls -l");
 printf("file/tree==");scanf("%s",pathname);
 fd=open(pathname,O_RDWR);
 if(fd==-1)
 {
  perror("failed try again..");
  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);
}
//================================================================================
//rename tree or file
void treefilerename()
{
 int fd;
 char oldname[SIZE],newname[SIZE];
 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..");
  return;
 }
 else
 {
  system("ls -l");
  printf("rename success!\n");
 }
}
//================================================================================
//change tree or file size
void changesize()
{
 int fd;
 int size;
 char pathname[SIZE];
 printf("change file ==");scanf("%s",pathname);
 printf("change size==");scanf("%d",&size);
 fd=truncate(pathname,size);
 if(fd==-1)
 {
  perror("truncate failed ,try again...");
  return;
 }
 else
 {
  system("ls -l");
  printf("truncate success!\n");
 }
}
//============================================================================
int main()
{
 int cord;
 int len=0,tag=0,i;
 char copybuf[SIZE],pastebuf[SIZE],copyname[SIZE];
 welcome();
 while(1)
 {
  printf("please input the cord:");
  scanf("%d",&cord);
  switch(cord)
  {
   case 1:
    treedisplay();
    break;
   case 2:
    treecreate();
    break;
   case 3:
    filecreateopen();
    break;
   case 4:
    treeopen();
    break;
   case 5:
    treeback();
    break;
   case 6:
    {
     len=treefilecopy(copybuf,copyname);
     tag=1;
     for(i=0;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:
     treefilerename();
     break;
   case 12:
     changesize();
     break;
   case 13:
       {
        system("clear");
      exit(0);
     }
     break;
   default:break;
  }
 }
 return 0;
}

 
阅读(743) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-08-24 14:51:40

Download More than 1000 free IT eBooks: http://free-ebooks.appspot.com