// 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;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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.

// Filename: cpfile.c
// Compile command: gcc cpfile.c -o cpfile.exe
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#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)) == -1){
printf("Can't create %s \n", argv[2]);
return 3;
}
// 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;
Transcribed Image Text:// Filename: cpfile.c // Compile command: gcc cpfile.c -o cpfile.exe #include <stdio.h> #include <fcntl.h> #include <unistd.h> #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)) == -1){ printf("Can't create %s \n", argv[2]); return 3; } // 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;
Expert Solution
Step 1

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education