Search for question
Question

#include <stdio.h> #include <unistd.h> #include <errno.h> int main (int argc, char* argv[]) { char* filepath = argv[1]; int returnval; // Check file existence returnval = access (filepath, F_OK); if (returnval == 0) printf ("\n %s exists\n", filepath); else { if (errno == ENOENT) printf("%s does not exist\n", filepath); else if (errno == EACCES) printf("%s is not accessible\n", filepath); return 0; } // Check read access ... // Check write access ... return 0; } Point: 10 1. a. Extend code snippet to check for the read and write access permissions of a given file. Program execution: ./yourProgram file_name (absolute path) Points: 20 2. Write a C program that mimics the cp command using open() system call to open source.txt file in read-only mode and copy the contents of it to destination.txt using read() and write() system calls. While reading and writing follow the below procedure: a. Read the next 100 characters from source.txt, and among characters read, replace each character '1' with character 'A' and all characters are then written in destination.txt b. Write characters "XYZ" into file destination.txt c. Repeat the previous steps until the end of file source.txt. The last read step may not have 100 characters. Program execution: /yourProgram source.txt destination.txt Points: 15 3. Complete the below code to check whether a given file is inside the given directory. Print an appropriate message based on your search. #include <string.h> #include <sys/types.h> #include <sys/dir.h> int search (char* file, char* dir){ DIR *dirptr=opendir(dir); struct dirent *entry = readdir(dirptr); while (entry != NULL) { if (strlen(entry->d_name] == strlen(file) && (strcmp(entry->d_name, file) == 0) return 0; /* return success */ entry = readdir(dirptr); } return 1; /* return failure */ } Program execution: ./yourProgram file directory (absolute path) General Instructions: Use perror sub-routine to display meaningful error messages in case of system call failures. Properly close the files using close system call after you finish read/write. Learn to use man pages to know more about file management system calls (e.g, man read) Submission Instructions • You should use only system call library functions for file manipulation • For each problem create a separate folder • All the programs MUST be clearly indented and internally documented • Make sure your programs compile and run without any errors • Save all your programs with meaningful names and zip into a single folder as: task1_yourName.zip (e.g., task1_xyz.zip)