Listing 3 Copies files to a directory

/* cp.c:  Copy files */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>

extern int filecopy(char *, char *);
static void cp(char *, char *);

main(int argc, char **argv)
{
   int i;
   struct stat finfo;
   char *target = argv[argc-1];

   /* Make sure target is a directory */
   assert(argc >= 3);
   assert(stat(target,&finfo) == 0 &&
         (finfo.st_mode & S_IFDIR));

   /* Copy files */
   for (i = 1; i < argc-1; ++i)
       cp(argv[i], target);
   return 0;
}

static void cp(char *file, char *target)
{
   static char new newfile[FILENAME_MAX];

   /*Combine target and source file for a full pathname */
   sprintf(newfile,%s/%s",target,file);
   fprintf(stderr,"copying %s to %s\n",file,newfile);
   if (filecopy (file,newfile) != 0)
      fputs("cp: Copy failed\n",stderr);
}

/* End of File */