# 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
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 ();
Main ()
{
Char copybuf [s], pastebuf [s], copyname [s];
Int Len = 0, tag = 0, cord, I;
Do {printf ("/n mainmenu/N ");
Printf ("1 tree-display/N");/* display directory */
Printf ("2 tree-creat/N");/* Directory Creation */
Printf ("3 File-creat/Open/N");/* open the file for read/write */
Printf ("4 tree-Open/N");/* change the current directory */
Printf ("5 tree-back/N");/* return to the upper-level directory */
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");/* file attributes */
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 );
If (null = DIR)
Return;
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 ("%-10 s", "Mode :");
Printf ("% 10 s", "read"); printf ("% 10 s", "write ");
Printf ("% 10 s", "execute/N"); printf ("% 10 s", "all user ");
If (Mode & 0400) = s_irusr) printf ("% 10 s", "true ");
Else printf ("% 10 s", "false ");
If (Mode & 0200) = s_iwusr) printf ("% 10 s", "true ");
Else printf ("% 10 s", "false ");
If (Mode & 0100) = s_ixusr) printf ("% 10 s", "True/N ");
Else printf ("% 10 s", "false/N ");
Printf ("% 10 s", "group ");
If (Mode & 040) = s_irgrp) printf ("% 10 s", "true ");
Else printf ("% 10 s", "false ");
If (Mode & 020) = s_iwgrp) printf ("% 10 s", "true ");
Else printf ("% 10 s", "false ");
If (Mode & 010) = s_ixgrp) printf ("% 10 s", "True/N ");
Else printf ("% 10 s", "false/N ");
Printf ("% 10 s", "other user ");
If (Mode & 04) = s_iroth) printf ("% 10 s", "true ");
Else printf ("% 10 s", "false ");
If (Mode & 02) = s_iwoth) printf ("% 10 s", "true ");
Else printf ("% 10 s", "false ");
If (Mode & 01) = s_ixoth) printf ("% 10 s", "True/N ");
Else printf ("% 10 s", "false/N ");
Printf ("sizeld/N", Buf. st_size );
Printf ("BLK sizeld/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 ");
}