SIMULATION OF EXISTING UNIX COMMAND (COPY) Using c program

AIM:
To write a c program to simulate the existing UNIX command (copy) using system calls. Operating System (OS) LAB in LINUX environment
PROGRAM DESCRIPTION:
In this program we have to unit a c program that simulates the copy command for moving an existing file into a new file. In this program three system calls are used.

System calls used,
· open()
· close()
· create()
· read()
· write()

1. open ()
Syntax:
open(filename,int mode)
This system calls open the file specified in the syntax according to the mode of operation given. The mode of operation here include
RDONLY WRONLY
RDWR
APPEND


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>
# define PERROR -1
main(argc ,argv)
int argc;
char *argv[];
{
char op;
if((argc<3)(argc>4))
{
fprintf(stderr,"ERROR");
exit(1);
}
if(argc==4)
{
printf("\n Are you sure to move the file (y/n)");
scanf("%c",&op);
if(tolower(op)==’y’)
{
if(link(argv[1],argv[2])==PERROR)
{
Perror(argv[0]);
}
if(unlink(argv[1]==PERROR)
{
Perror(argv[1]);
}
}
else
{
else(1);
}
if(argc==3)
{
if(link(argv[1],argv[2])==PERROR)
{
Perror(argv[1]);
}
}
Else
{
exit(1);
}
}
if(argc==3)
{
if(link(argv[1],argv[2])==PERROR)
{
Perror(argv[1]);
}
}
exit(1);
}

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