#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
struct stat stbuf;
char file[10],old[20],new[20],filename[20];
int amode,ch;
printf("\n1.Rename 2.Remove 3.Change access 4.Display access mode");
do
{
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter file to rename:");
scanf("%s",old);
printf("\nEnter the new name:");
scanf("%s",new);
if(rename(old,new)==0)
printf("\n%s is renamed as %s",old,new);
else
printf("\nError in renaming %s",old); break;
case 2:printf("Enter filename to be removed:");
scanf("%s",file);
if(remove(file)==0)
printf("\n%s is removed",file);
else
printf("\nError in removing %s",file); break;
case 3:printf("\nEnter the name of the file to change its access mode:");
scanf("%s",filename);
if((stat(filename,&stbuf))!=0)
{
printf("\nError in moving status info of %s to the strct variable
stbuf..",filename); break;
}
if(stbuf.st_mode & S_IWRITE)
{
printf("\n%s is Changed to READ ACCESS ONLY",filename);
amode=S_IREAD;
}
else
{
printf("\n%s is Changed to WRITE ACCESS ONLY",filename);
amode=S_IWRITE;
}
if(chmod(filename,amode)!=0)
{
printf("\nError in changing the access mode...!");
break;
} break;
case 4:printf("\nCheck the access mode:READ/WRITE mode:");
printf("\nEnter name of a file:");
scanf("%s",filename);
if((access(filename,R_OK))==0)
printf("\n%s has READ permission only..",filename);
if((access(filename,W_OK))==0)
printf("\n%s has WRITE permission only..",filename);
else
printf("\n%s does not have read/write permission");
break;
default:printf("\nInvalid choice...!"); break;
}
}while(ch<=4);
}
OUTPUT
1.Rename 2.Remove 3.Change access 4.Display access mode
Enter your choice:1
Enter file to rename:sum.sh
Enter the new name:add.sh
sum.sh is renamed as add.sh
Enter your choice:2
Enter filename to be removed:add.sh
add.sh is removed
Enter your choice:3
Enter the name of the file to change its access mode:first.c
first.c is Changed to READ ACCESS ONLY
Enter your choice:4
Check the access mode:READ/WRITE mode:
Enter name of a file:first.c
first.c has READ permission only..
Enter your choice:5
Invalid choice…!
No comments:
Post a Comment