//IMPLEMENTATION OF ls SYSTEM CALL
#include<stdio.h>
#include<dirent.h>
int main(int argc,char *argv[])
{
struct dirent *dp;
DIR *d;
if ( argc!=2 )
printf("No directory name found...");
else
{
if (( d=opendir(argv[1])) == NULL )
printf("Directory cannot be opened");
else
{
while (dp=readdir(d))
printf("\n%s",dp->d_name);0
closedir(d);
exit(0);
}
}
}
OUTPUT:
[student@localhost ex5]$ ./a.out ice
ice
process.c
shan
process2.c
file2.txt
//IMPLEMENTATION OF cat SYSTEM CALL
#include<stdio.h>
#include<sys/stat.h>
#include<string.h>
int main()
{
char fname[21],data[0];
int fd;
printf("Enter filename to be created:");
scanf("%s",fname);
if (( fd=creat(fname,S_IRUSR|S_IWUSR|S_IXUSR ))<0)
printf("\nFile Creation Error...!");
else
printf("\nEnter your data:");
while(fgets(data,50,stdin))
{
data[strlen(data)-1]='\0';
printf("FD=%d",fd);
printf("\n Data=%s\n",data);
if(strcmp(data,"END")==0)
break;
if((write(fd,data,strlen(data)))!=strlen(data))
{
//printf("\n Data1=%s",data);
printf("\nWriteError1...!");
}
}
if((write(fd,"\n",1))!=1)
printf("\nWrite Error2...!");
close(fd);
}
OUTPUT
Enter filename to be created:abinaya
Enter your datatype END to exit:operating system
END
AFTER CREATING FILE
operating system
//FILE MANIPULATIONS
#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