Question

In: Computer Science

Systems Programming - File Operations in Linux using C Preventing copying the same file What does...

Systems Programming - File Operations in Linux using C

Preventing copying the same file

  1. What does the standard cp do if you try to copy a file onto itself?
    $ cp file1 file1
    cp: 'file1' and 'file1' are the same file
    
  2. Modify cp1.c to handle the situation and include comments.

/**
* @file cp1.c
* @brief Uses read and write with tunable buffer size
* usage: cp1 src dest

*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

#define BUFFERSIZE 4096
#define COPYMODE 0644

void oops(char *, char *);

int main(int argc, char *argv[])
{
int infd, outfd, nChars;
char buf[BUFFERSIZE];

// check args
if (argc != 3)
{
fprintf(stderr, "usage: %s source destination\n", *argv);
exit(1);
}

// open files
if ((infd = open(argv[1], O_RDONLY)) == -1)
{
oops("Cannot open ", argv[1]);
}

if ((outfd = creat(argv[2], COPYMODE)) == -1)
{
oops("Cannot open ", argv[2]);
}

// copy files
while ((nChars = read(infd, buf, BUFFERSIZE)) > 0)
{
if (write(outfd, buf, nChars) != nChars)
{
oops("Write error to ", argv[2]);
}
}
if (nChars == -1)
{
oops("Read error from ", argv[1]);
}

if (close(infd) == -1 || close(outfd) == -1)
{
oops("Error closing files", "");
}

return 0;
}

/**
* @brief Displays error to the standard error and
* prints error by the system call using perror().
* Program terminates when this function is called.
*
* @param s1 type of error
* @param s2 file that cause the error
*/
void oops(char *s1, char *s2)
{
fprintf(stderr, "Error: %s ", s1);
perror(s2);
exit(1);
}

Solutions

Expert Solution

Following is the modified cp1.c file to handle the situation.

Here "strcmp()" function is used to compare the arguments. If both the arguments are same then the error can be thrown.

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include<string.h>
#define BUFFERSIZE 4096
#define COPYMODE 0644

void oops(char *, char *);

int main(int argc, char *argv[])
{
int infd, outfd, nChars;
char buf[BUFFERSIZE];

// check args
if (argc != 3)
{
fprintf(stderr, "usage: %s source destination\n", *argv);
exit(1);
}

// compare the arguments using strcmp and verify if they are same
// if both are same then the error will be displayed.
if (strcmp(argv[1],argv[2])==0)
{
fprintf(stderr,"%s and %s are same file\n",argv[1],argv[2]);
exit(1);
}

// open files
if ((infd = open(argv[1], O_RDONLY)) == -1)
{
oops("Cannot open ", argv[1]);
}

if ((outfd = creat(argv[2], COPYMODE)) == -1)
{
oops("Cannot open ", argv[2]);
}

// copy files
while ((nChars = read(infd, buf, BUFFERSIZE)) > 0)
{
if (write(outfd, buf, nChars) != nChars)
{
oops("Write error to ", argv[2]);
}
}
if (nChars == -1)
{
oops("Read error from ", argv[1]);
}

if (close(infd) == -1 || close(outfd) == -1)
{
oops("Error closing files", "");
}

return 0;
}

/**
* @brief Displays error to the standard error and
* prints error by the system call using perror().
* Program terminates when this function is called.
*
* @param s1 type of error
* @param s2 file that cause the error
*/
void oops(char *s1, char *s2)
{
fprintf(stderr, "Error: %s ", s1);
perror(s2);
exit(1);

}

Output

./a.out file1 file1

file1 and file1 are same file


Related Solutions

Systems Programming - File Operations Create your version of the tail command in Linux using C...
Systems Programming - File Operations Create your version of the tail command in Linux using C The lseek system call allows you to move the current position anywhere in a file. The call lseek(fd, 0, SEEK_END) moves the current position to the end of the file. The tail command displays the last ten liens of a file. Try it. tail has to move, not to the end of the file, but to a spot ten lines before the end of...
Systems Programming in Linux using C Remake the version of the ls -ialR command in C.....
Systems Programming in Linux using C Remake the version of the ls -ialR command in C.. In this version of ls, you must also consider symbolic file types. Make sure you have file header comments and function header comments. Also, write inline comments whenever necessary.
Linux Directories, File Properties, and the File System in C Understanding Unix/Linux Programming Your version of...
Linux Directories, File Properties, and the File System in C Understanding Unix/Linux Programming Your version of mv command 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...
a) b) c) Compare and contrast systems programming in Windows as against systems programming in Unix/Linux....
a) b) c) Compare and contrast systems programming in Windows as against systems programming in Unix/Linux. CR, 8 Explain the term structured exception handling (SEH) as used in systems programming and give a practical example of how it can be used to handle errors in a block of code. AP, 7 Write a C/C++ system program to delete an unwanted file in the Windows file system. Compile and run the program and copy the source code into your answer booklet....
C program for copying data from one directory to another using multi threading for linux using...
C program for copying data from one directory to another using multi threading for linux using command line arrangement. Program will take two arguments: 1: Source 2: Destination it will copy all the files from source to destination recursively Deadline: 4 hours from now.
IN C PROGRAMMING LINUX how to use makefile to include .a static library and .h file...
IN C PROGRAMMING LINUX how to use makefile to include .a static library and .h file from another directory in C? I have a header file myheader.h and a static library libmylib.a file in directory1. In directory2, I'm writing a program which uses them. Suppose I have main.c in directory2 which uses myheader.h and libmylib.a. How do I create a Makefile to compile and link them? LIBB = -L/../directory1/libmylib.a HEADER = -L/../directory1/myheader.h main: main.o gcc $(HEADER) $(LIBB) main.o: main.c gcc...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now encrypt the text file using Caesar Cipher (Use key value as 5). Display the contents of the updated file. #include <iostream> using namespace std; int main() { // write code here }
What have been the major changes in the file systems for Windows and for Linux?
What have been the major changes in the file systems for Windows and for Linux?
C++ Programming Simply explain what the difference between "including" a header file and using a compiled...
C++ Programming Simply explain what the difference between "including" a header file and using a compiled library file during linking? Where are the C++ header files and compiled C++ library files on your computer?
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT