Question: Executed the below code but files from directory not copied or moved #include #include #include #include #include #include #define MAX_EXTENSIONS 6 // flag to determine

Executed the below code but files from directory not copied or moved

#include

#include

#include

#include

#include

#include

#define MAX_EXTENSIONS 6

// flag to determine if copy or move operation is to be performed

int copy = 0;

int move = 0;

// source and destination directories

char *src_dir, *dst_dir;

// extensions array to store file extensions

char *extensions[MAX_EXTENSIONS];

// number of extensions

int num_extensions = 0;

// function to copy files

int copy_file(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {

// if the file type is regular file

if (typeflag == FTW_F) {

// get the file extension

char *ext = strrchr(fpath, '.');

if (ext) {

int i;

// check if the file extension matches with the provided extension list

for (i = 0; i < num_extensions; i++) {

if (strcmp(ext, extensions[i]) == 0) {

break;

}

}

// if extension is not in the list, return

if (i == num_extensions) {

return 0;

}

} else if (num_extensions > 0) {

// if extension list is provided but file does not have extension, return

return 0;

}

// generate destination file path

char dst_path[4096];

snprintf(dst_path, 4096, "%s/%s", dst_dir, fpath + strlen(src_dir) + 1);

// open source file for reading

FILE *src = fopen(fpath, "r");

if (!src) {

perror("fopen");

return -1;

}

// open destination file for writing

FILE *dst = fopen(dst_path, "w");

if (!dst) {

perror("fopen");

fclose(src);

return -1;

}

// copy contents from source to destination

char buf[4096];

size_t n;

while ((n = fread(buf, 1, 4096, src)) > 0) {

if (fwrite(buf, 1, n, dst) != n) {

perror("fwrite");

fclose(src);

fclose(dst);

return -1;

}

}

// close source and destination files

fclose(src);

fclose(dst);

}

return 0;

}

int main(int argc, char *argv[]) {

int i;

for (i = 1; i < argc; i++) {

if (strcmp(argv[i], "-cp") == 0) {

copy = 1;

} else if (strcmp(argv[i], "-mv") == 0) {

move = 1;

} else if (src_dir == NULL)

{

// If source directory is not specified

printf("Error: Source directory not specified\n");

return -1;

} else if (dst_dir == NULL)

{

// If destination directory is not specified

printf("Error: Destination directory not specified\n");

return -1;

}

// Check if the source directory exists

if (access(src_dir, F_OK) == -1)

{

printf("Error: Source directory does not exist\n");

return -1;

}

// Create the destination directory if it does not exist

struct stat st = {0};

if (stat(dst_dir, &st) == -1)

{

if (mkdir(dst_dir, 0700) == -1)

{

printf("Error: Failed to create destination directory\n");

return -1;

}

}

// Call the nftw function to traverse the file tree

if (nftw(src_dir, process_file, MAX_OPEN_FD, FTW_PHYS) == -1)

{

printf("Error: Failed to traverse the file tree\n");

return -1;

}

printf("Directory copy/move completed successfully\n");

return 0;

}

Step 2/2Final answer

Was this answer helpful?

1

0

Write a C program dircmx that copies or moves an entire directory sub-tree rooted at a specific path in the home directory to a specific destination folder (also in the home directory) along with only those file types specified by the extension list. Synopsis : dircmx [source_dir] [destination_dir] [options]  Both source_dir and destination_dir can be either absolute or relative paths, but must belong to the home directory hierarchy.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!