Question

In: Computer Science

I made the command cp code in c language. But when I copy a file, file...

I made the command cp code in c language. But when I copy a file, file permissions are not copied equally. So I want to copy the file authority as well. What should I do?

#include
#include
#include
#include
#include
#include
#include
#include
#include

int main(int argc, char *argv[])
{
struct stat st;
char ch;
int src, dst;
if(argc != 3)
{
printf("argument error \n");
printf("usage: ./a.out src dest \n");
exit(0);
}
src = open(argv[1], O_RDONLY);
if(src == -1){
perror("open source");
exit(errno);
}
dst = open(argv[2], O_WRONLY | O_CREAT|O_TRUNC, st.st_mode&(S_IRWXU | S_IRWXG | S_IRWXO));
if(dst == -1){
perror("open source");
exit(errno);
}
while(read(src, &ch,1))
write(dst,&ch,1);

close(src);
close(dst);
return 0;
}

Solutions

Expert Solution

file name 1.c

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define BUF_SIZE 1024
int main(int argc, char *argv[])
{
        struct stat st;
    int inputFd, outputFd, openFlags;
    ssize_t numRead;
    char buf[BUF_SIZE];

    if(argc != 3)
        {
                printf("argument error \n");
                printf("usage: ./a.out src dest \n");
                exit(0);
        }
/*
save the stat structure of the file which you are about to copy
it contains all the information related to file including permission assign
*/
    stat(argv[1],&st);
        
/* Open input and output files */

    if((inputFd = open(argv[1], O_RDONLY) )== EACCES)
    {
        perror("access to the file is not allowed or  the file did not exist yet ");

    } 
    else{
        printf("file open successfully\n");
     }
     
    if((outputFd = open(argv[2], O_CREAT | O_WRONLY,st.st_mode & 511)) < 0)
    {
        printf("error in opening file %d",errno);
        //get error no and search in error list 
        
    }
    else{
        printf("file open successfully\n");
    }
/*
    O_CREAT flag is use to create file on not exist.
    struct stat has member st_mode which contain the file type and file mode.
    least significant 9 bits correspond  as the file permission bits.
    so we perform and operation between st_mode and 511 to reterive permission bits.
*/
    
    /* Transfer data until we encounter end of input or an error */
        printf("%d\n",outputFd );
    while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0)
    {
        if(write(outputFd,buf,numRead) <0)
        {
                printf("error write %d",errno);//get error no. and check error through list of error
        }

        }
    
    exit(0);
}

compile command

gcc -o 1 1.c

This program is successfully run in ubuntu 20.04.

for error list type below command on linux terminal

errno -l

Related Solutions

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...
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. #define SIZE 1024 #include<string.h> #include<stdio.h> #include<sys/types.h> #include<fcntl.h> #include<unistd.h> #include<stdlib.h> #include<sys/stat.h> int main(int argc, char *argv[]){ if(argc != 3){ perror("argument 부족\n"); exit(0); } struct stat frstatbuf; FILE* fr = fopen(argv[1], "r"); if(fr == NULL){ perror("read file 읽기 오류\n"); exit(0); } int frfd = fileno(fr); fstat(frfd, &frstatbuf); FILE* fw=fopen(argv[2], "w+"); int fwfd=fileno(fw); fchmod(fwfd,frstatbuf.st_mode&(S_IRWXU|S_IRWXG|S_IRWXO)); char buf[1024]; while(1){...
Linux Directories, File Properties, and the File System in C Code your version of mv command...
Linux Directories, File Properties, and the File System in C Code your version of mv command in C The mv command is more than just a wrapper around the rename system call. Write a version of mv that accepts two argument. The first argument must be the name of a file, and the second argument may be the name of a file or the name of a directory. If the destination is the name of a directory, then mv moves...
1. At the command prompt, use the where command to locate the where.exe file. Copy and...
1. At the command prompt, use the where command to locate the where.exe file. Copy and paste the full path and filename for the where.exe file into the space provided below. 2. At the command prompt, enter the following command and press enter. find "free beer" "c:\Program Files (x86)\Notepad++\*.txt" This command searches for the string "free beer" in all .TXT files in the Notepad++ program folder. According to the output of the find command, which of the files listed below...
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
Using C programming I have a file that contains earthquake data that I will copy and...
Using C programming I have a file that contains earthquake data that I will copy and paste below. I want to use either bubble or insertion sort to sort the file by latitude in ascending order, then create a new file containing the sorted data. example file to sort: time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net 2020-10-17T17:22:03.840Z,32.877,-116.2991667,0.31,1.16,ml,21,119,0.07747,0.26,ci 2020-10-17T17:17:29.980Z,34.1611667,-116.452,2.75,0.87,ml,17,66,0.05224,0.22,ci 2020-10-17T17:03:54.460Z,33.5396667,-116.4613333,8.66,0.63,ml,18,126,0.06084,0.16,ci 2020-10-17T16:55:01.080Z,63.254,-151.5232,8,1.4,ml,,,,0.9,ak
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
Below is my source code for file merging. when i run the code my merged file...
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf #include #include #include using namespace std; int main() { ifstream inFile1; ifstream inFile2; ofstream outFile1; int mClientNumber, fClientNumber; string mClientName;...
Please look at the following code. When I run it from the command line, I am...
Please look at the following code. When I run it from the command line, I am supposed to get the following results: 1: I am 1: I am I need to fix it so that the functions 'print_i' and 'print_j' print all of their lines. What do I need to add? Thank you. C source code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> // These two functions will run concurrently void* print_i(void *ptr) { printf("1: I am...
C language problem. Suppose I open I file and read a integer 24. And I want...
C language problem. Suppose I open I file and read a integer 24. And I want to store them into a array byte []. The answer should be byte[0] = 0x9F. How can I do that?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT