Question

In: Computer Science

This code is an expression of cp command in c language. But I don't understand this...

This code is an expression of cp command in c language. But I don't understand this code very well. Please explain in notes one by one.(in detail)

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<errno.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>

int main(int argc, char *argv[])
{
char ch;
int src, dst;
struct stat st;

if(argc != 3)
{
printf("argument error \n");
printf("usage: ./a.out src dest \n");
exit(0);
}

if (stat(argv[1], &st) == -1) {
perror("stat : ");
exit(-1);
}

src = open(argv[1], O_RDONLY);
if(src == -1){
perror("open source");
exit(errno);
}
dst = open(argv[2], O_WRONLY | O_CREAT|O_TRUNC);
if(dst == -1){
perror("open source");
exit(errno);
}
while(read(src, &ch,1)){
write(dst,&ch,1);
}

close(src);
close(dst);

if (chmod(argv[2], st.st_mode)) {
perror("chmod : ");
return -1;
}

return 0;
}

Solutions

Expert Solution

i) First check if both source and the destination files are received from the command line argument and exit if argc counter is not equal to 3.

ii) Open the source file with read only flag set.

iii) Open the destination file with the respective flags & modes.

O_WRONLY :Open the file in write only mode

O_TRUNC : Truncates the contents of the existing file

O_CREAT :Creates a new file if it doesn’t exist

iv) Start data transfer from source file to destination file till it reaches EOF ((read(src, &ch,1)==0)

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<errno.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>

int main(int argc, char *argv[])
{
char ch;
int src, dst;
struct stat st;
        
//Check if both src & dest files are received
if(argc != 3)
{
printf("argument error \n");
printf("usage: ./a.out src dest \n");
exit(0);
}

if (stat(argv[1], &st) == -1) {
perror("stat : ");
exit(-1);
}
//Open source file
src = open(argv[1], O_RDONLY);
if(src == -1){
perror("open source");
exit(errno);
}
//Open destination file with respective flags & modes O_CREAT & O_TRUNC is to truncate existing file 
dst = open(argv[2], O_WRONLY | O_CREAT|O_TRUNC);
if(dst == -1){
perror("open source");
exit(errno);
}
//Start data transfer from src file to dst file till it reaches EOF
while(read(src, &ch,1)){
write(dst,&ch,1);
}

close(src);
close(dst);

if (chmod(argv[2], st.st_mode)) {
perror("chmod : ");
return -1;
}

return 0;
}

)


Related Solutions

What is the advantage of ln command over cp command?
What is the advantage of ln command over cp command?
PHP Language I have XAMPP and I don't know how to display my PHP code. For...
PHP Language I have XAMPP and I don't know how to display my PHP code. For instance, when I create a basic HTML webpage I just open the code into a web browser and it shows me a basic web page. When I try to show a PHP calculation it doesn't show. What are the steps in displaying my PHP doc?
please I don't understand this code. Can you put comments to explain the statements. Also, if...
please I don't understand this code. Can you put comments to explain the statements. Also, if there any way to rewrite this code to make it easier, that gonna help me a lot. import java.io.*; import java.util.*; public class State {    private int citi1x,citi1y; private int pop1; private int citi2x,citi2y; private int pop2; private int citi3x,citi3y; private int pop3; private int citi4x,citi4y; private int pop4; private int plantx,planty; public int getCity1X(){ return citi1x; } public int getCity1Y(){ return citi1y;...
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
I have written code in C programming that checks where the command line arguments are floats...
I have written code in C programming that checks where the command line arguments are floats or not. For example, if I type "./math 1 1 0 0 2.5 3" in the terminal, my program realizes they are all floats but if I type "./math 1 1 0 0 2.5 g", it recognizes that not all arguments are floats and gives an error message. I want to take my code further such that after typing in "./math 1 1 0...
Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class...
Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class to solve: (Gaddis) Programming Challenger 3. Car Class p. 802 Ch. 13 • Implement a menu of options • Add a motor vehicle to the tree • Remove a motor vehicle to the tree • Search by vehicle model • Vehicle Inventory Updates • Take one of the tours to verify the contents of the tree. Car Class Write a class named Car that...
I don't understand how the bonds chart work.
I don't understand how the bonds chart work.
HI, I hope you are doing well. I really don't understand this question and don't know...
HI, I hope you are doing well. I really don't understand this question and don't know how to solve it at all because I am completely new to this c++ programming. can you please explain each line of code with long and clear comments? please think of me as someone who doesn't know to code at all. and I want this code to be written in c++ thank you very much and I will make sure to leave thumbs up....
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT