0

I want to have a successful copy file program, where I can have the result: ./a.out 1.c examples3/16.c to be successful, where I can successfully make a new directory for my copied file. How can you do this, because I get errors trying to do it. I can copy the 1.c file successfully in one place, and I can copy the file into an existing directory but not successful in copying the file into a newly made directory. How can you do this and fix this problem?

code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#define BUFFERSIZE      4096
#define COPYMODE        0644
int file_exist (char *filename)
{
 struct stat   buffer;   
 return (stat (filename, &buffer) == 0);
}

int main(int ac, char *av[])
{
int ch;  //ch - character no.                               
struct stat sb;
char directory[120];
FILE *source, *target;
/* from http://stackoverflow.com/questions/30215462/how-to-get-the-source-files-the-file-which-i-want-to-copy-and-the-copied-file/30217023#30217023 */
if (ac <= 2) {
    printf("Enter source and destination file names\n");
    exit(EXIT_FAILURE);
}

if ( strcmp(av[1], av[2]) ==0 )
{
    printf("the files are the same\n");
    exit(1);
}

if (file_exist (av[2]))
 {
  printf ("the destination file exists\n");
  exit(1);
 }
 //printf("which directory do you want to send your destination file?");
 //scanf("%s", directory);

 char serv_name[1000];
 mkdir("newdir/", S_IRWXU | S_IRWXG | S_IRWXO);
 snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
 FILE* f = fopen(serv_name, "w");
 if (f < 0) {
    perror("CLIENT:\n");
    exit(1);
 }  

 source = fopen(av[1], "r");//getting and opening source file
 if( source == NULL ) {
    printf("Press any key to exit...\n");
    exit(EXIT_FAILURE);
 }

 target = fopen(av[2], "w");//getting and opening destination file

 if( target == NULL ) {
    fclose(source);
    printf("Press any key to exit...\n");
    exit(EXIT_FAILURE);
 }

 while( ( ch = fgetc(source) ) != EOF )
    fputc(ch, target);

 fclose(source);
 fclose(target);
 printf("File copied successfully.\n");
 /* from http://stackoverflow.com/questions/30215462/how-to-get-the-source-files-the-file-which-i-want-to-copy-and-the-copied-file/30217023#30217023 */
 if (stat(av[1], &sb) == -1) {
    perror("stat");
    exit(1); //exit(EXIT_SUCCESS);
  }
  else
  if (chmod(av[2], sb.st_mode & 07777))//http://stackoverflow.com/questions/18032574/how-can-i-copy-permissions-from-a-file-that-already-exists
  {
    perror("chmod");
  }

  printf("Source File: %s, Inode number: %d, Mode: 0x%04X\n", av[1],  (unsigned)sb.st_ino, (unsigned)sb.st_mode);

  if (stat(av[2], &sb) == -1) {
    perror("stat");
    exit(1);
  }

  char *str;
  str = (char *) malloc(15);
  strcpy(str, av[2]);

  if (stat(av[2], &sb) == -1)    {//http://stackoverflow.com/questions/7430248/creating-a-new-directory-in-c
    mkdir(av[2], 0700);
   }
  printf("Destination File: %s, inode number: %d, Address = %u, Mode: 0x%04X\n", av[2], (unsigned)sb.st_ino, str, (unsigned)sb.st_mode);

   free(str);
   return 0;
   }
user4728257
  • 25
  • 10

1 Answers1

3

I can successfully make a new directory for my copied file. [...] but not successful in copying the file into a new directory or different

The problem may come from the directory creation:

If you call you program with: ./a.out foo bar

snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
FILE* f = fopen(serv_name, "w");

Your program will try to open newdir/bar: OK

If you call you program with: ./a.out foo /path/to/bar

snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
FILE* f = fopen(serv_name, "w");

Your program will try to open newdir//path/to/bar: That will certainly fail.

So your problem comes from:

snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
FILE* f = fopen(serv_name, "w");
if (f < 0) {
    perror("CLIENT:\n");
    exit(1);
} 

You cannot open f if the tree directory is not correct.

Mathieu
  • 8,840
  • 7
  • 32
  • 45