// Filename: cpfile.c // Compile command: gcc cpfile.c -o cpfile.exe #include #include #include #define SIZ 128L // Copy from source file to target file. Adapted from The C Programming Language // by Kernighan and Ritchie. int main(int argc, const char *argv[]) { int f1, f2; // Do we have right number of arguments? if (argc != 3){ printf("Wrong number of command line arguments \n"); // exit(1); return 1; } // Can we access thge source file? if ((f1 = open(argv[1], O_RDONLY, 0)) printf("Can't open %s \n", argv[1]); return 2; -1){ == } // Can we create the target file? if ((f2 = creat(argv[2], 0644)) == printf("Can't create %s \n", argv[2]); return 3; -1){ %3D } // Copy source file contents to target file. char buf[SIZ]; int n; while ((n = read( f1, buf, SIZ)) > 0) if (write(f2, buf, n) != n) { printf("Can't write file" ); close(f1); close(f2); return 4;
Rewrite the following C program using only system calls such as open, close, read, write, clear, loctl, etc. Don't use calls from C or C++ libraries uncluding cout, cin, printf, scanf, etc.
Given C Program
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#define SIZ 128
int main (int argc,const char * argv[])
{
int f1,f2;
if(argc!=3)
{
printf("Wrong number of command line arguments\n");
// exit(1);
return 1;
}
if((f1=open(argv[1],O_RDONLY,0))==-1)
{
printf("Cant open %s\n",argv[1]);
return 2;
}
if((f2=creat(argv[2],0644))==-1)
{
printf("Cant create %s\n",argv[2]);
return 3;
}
char buf[SIZ];
int n;
while((n=read(f1,buf,SIZ))>0)
if(write(f2,buf,n)!=n)
{
printf("cant write file");
close(f1);
close(f2);
return 4;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images