0% found this document useful (0 votes)
36 views

Os Ex 3 Final Print

The document describes the implementation of the "cp" and "cat" commands in C programming language. It includes the program code to copy or concatenate files based on different combinations of command line arguments. The cp code copies the source file to the destination file with or without overwrite confirmation. The cat code displays, creates, appends or concatenates files based on the arguments provided. It handles cases like reading from one file and writing to another, or reading multiple files and writing the contents to a new file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Os Ex 3 Final Print

The document describes the implementation of the "cp" and "cat" commands in C programming language. It includes the program code to copy or concatenate files based on different combinations of command line arguments. The cp code copies the source file to the destination file with or without overwrite confirmation. The cat code displays, creates, appends or concatenates files based on the arguments provided. It handles cases like reading from one file and writing to another, or reading multiple files and writing the contents to a new file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

LOKKESHWARAN J | CSE -A

IMPLEMENTATION OF CP COMMAND :
PROGRAM CODE :
#include <syscall.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#define BUFSIZE 1024


char buf[BUFSIZE];
int main(int argc, char** argv)
{
int fd1, fd2, r;
char ch;

/* mycp -i sourcefilename destinationfilename */


if ((argc==4 )&& ((strcmp(argv[1],"-i")==0)))
{
fd1 = open(argv[2],O_RDONLY);
if (fd1==-1)
{

printf("\nOPEN ERROR ..!! %s\n", argv[1]);


return 1;

creat(argv[3],S_IRWXU);
fd2 =open(argv[3],O_CREAT|O_WRONLY|O_APPEND);
if (fd2==-1)
{

printf("\nOPEN ERROR %s \n", argv[2]);


return 1;

printf("\nDO U WANT TO OVERWRITE ?? y/n ? " );


scanf("%c",&ch);
if(ch=='y')
{

r = read(fd1, buf, BUFSIZE);

LOKKESHWARAN J | CSE -A

write(fd2, buf, r);


printf("\n FILE COPIED SUCCESSFULLY >>!!! ");
}
else
{

printf(" \n FILE NOT COPIED !!!!! "); }

close(fd1);
close(fd2);
return 0;
}

/* mycp sourcefilename destinationfilename */


if(argc==3)
{
fd1 = open(argv[1],O_RDONLY);
if (fd1==-1)
{ printf("\nOPEN ERROR...!!! %s\n", argv[1]);
return 1;

creat(argv[2],S_IRWXU);
fd2 =open(argv[2],O_CREAT|O_WRONLY);
if (fd2==-1)
{

printf("\nOPEN ERROR...!!! %s\n", argv[2]);


return 1;

r = read(fd1, buf, BUFSIZE);


write(fd2, buf, r);
printf("\n FILE COPIED SUCCESFULLY..!!! ");
close(fd1);
close(fd2);
return 0;
}
else
printf("\n SYNTAX ERROR .....!!!!");
return 0;
}

LOKKESHWARAN J | CSE -A

SAMPLE INPUT AND OUTPUT :

mycp sourcefilename destinationfilename

mycp -i sourcefilename destinationfilename

LOKKESHWARAN J | CSE -A

IMPLEMENTATION OF CAT COMMAND

PROGRAM CODE :
#include <syscall.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define BUFSIZE 1024
char buf1[BUFSIZE],buf2[BUFSIZE];
int main(int argc,char**argv)
{
int fd,fd1,fd2;
int r,r1;
/*********************************************************************/

/* mycat filename*/
if(argc==2)
{
fd=open(argv[1],O_RDONLY);
if (fd==-1)
{ printf("\n FILE OPEN ERROR..!!" ); }
r=read(fd,buf1,BUFSIZE);
if (r==-1)
{ printf("\n FILE READ ERROR...!!"); }
write(1,buf1,r);
printf(" \nFILE DISPLAYED ..!!!\n");
}

/**************************************************************************/

LOKKESHWARAN J | CSE -A

/**************************************************************************/

/* mycat > filename */


else
if ((argc==3) && ((strcmp(argv[1],">")==0)))
{
creat(argv[2],S_IRWXU);
fd1 =open(argv[2],O_CREAT|O_WRONLY);
if (fd1==-1)
{
printf("\nOPEN ERROR ...!!! %s\n", argv[2]);
return 1;
}
printf("\n Enter The Text Into The File : \n");
gets(buf1);
r=strlen(buf1);
write(fd1,buf1,r);
printf("\nFILE CREATED ...!!!\n");
close(fd1);

}
/**************************************************************************/

/* mycat filename1 > filename2 */


else
if((argc==4) && ((strcmp(argv[2],">")==0)))
{
fd1 = open(argv[1],O_RDONLY);
if (fd1==-1)
{
printf("\nOPEN ERROR...!!! %s\n", argv[1]);
return 1;
}

LOKKESHWARAN J | CSE -A

creat(argv[3],S_IRWXU);
fd2 =open(argv[3],O_CREAT|O_WRONLY);
if (fd2==-1)
{
printf("\nFILE CREATION ERROR ...!! %s\n", argv[2]);
return 1;
}
r = read(fd1, buf1, BUFSIZE);
write(fd2, buf1, r);
printf("\n FILE COPIED \n");
close(fd1);
close(fd2);
}
/**************************************************************************/

/* mycat filename1 >> filename2 */


else
if ((argc==4) && ((strcmp(argv[2],">>")==0)))
{
fd1 = open(argv[1],O_RDONLY);
if (fd1==-1)
{
printf("\nOPEN ERROR ..!!! %s\n", argv[1]);
return 1;
}
r = read(fd1, buf1, BUFSIZE);
if(r==-1)
{ printf("\nREAD ERROR ..!!! \n");
return(1);
}
fd2 =open(argv[3],O_CREAT|O_WRONLY|O_APPEND);
if(fd2==-1)

LOKKESHWARAN J | CSE -A

{
printf("\nOPEN ERROR ..!!! %s\n", argv[3]);
return 1;
}
write(fd2, buf1, r);
printf("\n FILE APPENDED ..!!!\n");
close(fd1);
close(fd2);
}
/**************************************************************************/

/* mycat filename1 filename2 .. filenamen */


else
if(argc==4)
{
fd=open(argv[1],O_RDONLY);
if (fd==-1)
{
printf("\nOPEN ERROR ...!! %s\n", argv[1]);
}
r=read(fd,buf1,BUFSIZE);
if (r==-1)
{
printf("\nREAD ERROR ...!!!\n");
}

fd1=open(argv[2],O_RDONLY);
if (fd1==-1)
{
printf("\nOPEN ERROR ...!! %s\n", argv[2]);
}

LOKKESHWARAN J | CSE -A

r1=read(fd1,buf2,BUFSIZE);
if (r1==-1)
{
printf("\nREAD ERROR ...!!!\n");
}

creat(argv[3],S_IRWXU);
fd2 =open(argv[3],O_CREAT|O_WRONLY);
write(fd2,buf1,r);
fd2 =open(argv[3],O_CREAT|O_WRONLY|O_APPEND);
write(fd2,buf2,r1);
printf("\n FILE CONCANATED ...!!! \n");
close(fd);
close(fd1);
close(fd2);

}
/**************************************************************************/
else
{
printf("\n<<<<< SYNTAX ERROR >>>!!!!\n ");
}

LOKKESHWARAN J | CSE -A

SAMPLE INPUT AND OUTPUT :

mycat filename :

mycat > filename :

mycat filename1 > filename2 :

LOKKESHWARAN J | CSE -A

mycat filename1 filename2 .. filenamen :

mycat filename1 >> filename2 :

You might also like