SIMULATION OF EXISTING UNIX COMMAND (REMOVE) Using c program

AIM:
To write a c program to simulate the existing UNIX command (remove) using system calls. Operating System (OS) LAB in LINUX environment
PROBLEM DEFINTION:
Check whether the file to be deleted is present using access() system call. if it is present, then delete that file using the system call using unlink() command.

System calls used,
· unlink()
· access()


1.unlink ()
Syntax:
unlink(char *name)
It removes the directory entry name and all the links for the indicated file.

2.access ()
Syntax:
access( pathname, int mode )
this system call checks whther the file indicated inside is existing or not. if it exist, the it will checks if calling process has read, write or execute permission for that file according to mode specified and various modes are

0 R-ok read
1 W-ok write
2 F-ok file exist or not
3 x-ok executed


ALGORITHM:

· Declare the argument counter and argument vector array (argc,argv).

· if argc<2>3, then display that the move command operation cannot be performed.

· if argc==3, string compare argv[2] with “- I”. if it is true moves into the interactive mode.

· Check whether the file in argv[1] exists using system call access.

· Unlink the file in argv[1] and check for error condition.

· if argc==2, then check whether the file in argv[1] exists using system call ‘access’.

· Unlink the file in argv[1] and check for error condition.

· if the file in argv[1] does not exist the exit the program.

PROGRAM SOURCE CODE
# include<stdio.h>
#include<ctype.h>
# define PERROR -1
#include<string.h>
main(argc ,argv)
int argc;
char *argv[];
{
char ch;
if((argc<2)(argc>3))
{
fprintf(stderr,"ERROR");
exit(1);
}
if(argc==3)
{
if(strcmp(argv[2],"-i")==0)
{
printf("\n Are you sure to remove the file (y/n)");
scanf("%c",&ch);
if(tolower(ch)=='y')
{
if(access(argv[1],2)==0)
{
unlink(argv[1]);
exit(0);
}
exit(0);
}
}

}

return 0;
}

CONCLUSION:
Thus the c programs to simulate the existing UNIX command (remove) using the available system calls was written and executed successfully.


Related post