Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you help me fix my code C language Unix/Linux Utilities This program is a file copy utility that takes either two command line arguments

Can you help me fix my code

C language

Unix/Linux Utilities

This program is a file copy utility that takes either two command line arguments (Sourcefile and Destinationfile) or three arguments (Sourcefile-1, Sourcefile-2, ..., Sourcefile-n and Directory) and copies the contents of the source file(s) to the destination file(s) or directory.

I need

correctly parses the command line into source and destination components

correctly copies 1 source file to a destination file correctly copies a source file to a specified directory correctly copies multiple source files to a specified directory.

the directory cannot be copied.

Now is ok copies multiple source files to a specified directory, but cannot copies a source file to a specified directory.

Code

#include #include #include #include #include

#define MAX 4096

int copyFile(char src, chardst) { struct stat st; if (stat(src, &st) != 0 || !S_ISREG(st.st_mode)) { printf("Error: %s is not a regular file ", src); return 1; }

FILE srcFile = fopen(src, "r"); if (srcFile == NULL) { printf("Error opening source file: %s ", strerror(errno)); return 1; }

FILEdstFile = fopen(dst, "w"); if (dstFile == NULL) { printf("Error opening destination file: %s ", strerror(errno)); return 1; }

char buffer[MAX]; size_t bytesRead; while ((bytesRead = fread(buffer, 1, MAX, srcFile)) > 0) { fwrite(buffer, 1, bytesRead, dstFile); }

fclose(srcFile); fclose(dstFile);

return 0; }

int main(int argc, char argv[]) { struct stat st;

if (argc < 3) { printf("Usage: tucp Sourcefile Destinationfile "); printf("Usage: tucp Sourcefile Directory "); printf("Usage: tucp Sourcefile-1 Sourcefile-2 Sourcefile-3 Sourcefile-n Directory "); return 1; } else if (argc == 3) { charsource = argv[1]; char *dest = argv[2];

if (stat(dest, &st) == 0) { // destination file exists or is a directory if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode)) { printf("Error: %s is not a file or a directory ", dest); return 1; } else if (S_ISREG(st.st_mode)) { // destination is a file return copyFile(source, dest); } else { // destination is a directory char destFile[MAX]; strcpy(destFile, dest); strcat(destFile, "/"); strcat(destFile, source);

return copyFile(source, destFile); } } else { // destination file does not exist return copyFile(source, dest); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Influences on Nonverbal Communication?

Answered: 1 week ago