IMPLEMENTATION OF SYSTEM CALLS
#include “ stdio.h “
#include “ sys/types.h “
#include “ sys/dir.h “
#include “ sys/stat.h “
#include “ fcntl.h “
#include “ unistd.h “
#define size 512
char buff[size],name[20];
int fd,n,k;
main()
{
int ch,nread,nwrite,a,pos,i,j;
long int of;
struct stat buf;
do
{
printf("\n\n\t IMPLEMENTATION OF SYSTEM CALLS");
printf("\n\t...................................");
printf("\n\t1.Create a file");
printf("\n\t2.Open a file");
printf("\n\t3.Write into the file");
printf("\n\t4.Read the file contents");
printf("\n\t5.Seek the file");
printf("\n\t6.View the status of the file");
printf("\n\t7.Close the file");
printf("\n\t8.exit");
printf("\n\tEnter Ur Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\tEnter name of file:");
scanf("%s",name);
fd=creat(name,S_IRUSRS_IWUSRS_IXUSR);
if(fd==1)
printf("\n\tError in creating file");
else
{
printf("\n\tFile created");
printf("\n\tThe descriptor is %d",fd);
}
break;
case 2:
printf("\n\t Enter the name of the file:");
scanf("%s",name);
fd=open(name,2);
if(fd==1)
printf("\n\tError in opening the file");
else
printf("\n\tFile opened with fd %d",fd);
break;
case 3:
printf("\n\tEnter the info to be stored in the file");
scanf("%s",buff);
if(write(fd,buff,sizeof(buff)) “ 0)
printf("\n\tError in writing into the file");
else
printf("\n\tWrite Successful");
break;
case 4:
printf("\n\tThe contents of the file:\n");
nread=read(fd,buff,sizeof(buff));
if(nread!= -1)
printf("\n\t%s",buff);
break;
case 5:
printf("\n\tEnter the offset:");
scanf("%d",&of);
printf("\n\tEnter the position:");
scanf("%d",&pos);
if(a== -1)
printf("\n\tSeek Error");
else
printf("\n\tSeek Ok");
break;
case 6:
i=stat(name,&buf);
j=fstat(fd,&buf);
if(i==0 && j==0)
printf("\n\tSTAT & FSTAT successful");
else
printf("\n\tstat &fstat error");
printf("\n\tFile %s has %d links:",name,buf.st_nlink);
printf("\n\tFile inode number:%d",buf.st_ino);
printf("\n\tUserid:%d",buf.st_uid);
break;
case 7:
k=close(fd);
if(k==1)
printf("\n\tError in closing the file");
else
printf("\n\tFile Closed Successfully");
break;
case 8:
printf("\n\n\t\tPress any key.");
break;
}
} while(ch!=8);
}
Thursday, February 4, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment