#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/param.h>
#include <string.h>
#include <unistd.h>

void ccp(char* name,char* d1,char* d2)
{ char f1[MAXPATHLEN+1], f2[MAXPATHLEN+1];
 strcpy(f1,d1); strcpy(f2,d2); strcat(f1,"/");
 strcat(f2,"/"); strcat(f1,name); strcat(f2,name);
 if ( access(f2,F_OK) == -1 || newer(f1,f2) )
   printf("copy(%s,%s)\n", f1, f2);
}

/* if last modify time of file1 is more recent than file2 */
int newer(char* file1,char* file2)
{     return(mtime(file1) > mtime(file2));
}

/* obtain last modify time of file */
int mtime(char* file)
{ struct stat stb;
 if (stat(file,&stb) < 0)
   {   perror("");  exit(1); }
 return(stb.st_mtime);
}

int main(int argc,char* argv[])
{ short i;
 DIR *dirp1;
 struct dirent *dp;
 if (argc < 3)    /* need at least two args */
   {      fprintf(stderr, "%s: wrong number of arguments", argv[0]);
   exit(1);
   }
 else if (argc > 3)   /* files specified */
   {      for ( i = 3; i < argc; i++)
     ccp(argv[i],argv[1],argv[2]) ;
   return(0);
   }
 /* now exactly two args */
 if ((dirp1 = opendir(argv[1])) == NULL)
   {    fprintf(stderr, "%s: can not open %s", argv[0], argv[1]);
   exit(1);
   }
 for (dp = readdir(dirp1); dp != NULL; dp = readdir(dirp1))
   if (strncmp(dp->d_name,".", 1))
     ccp(dp->d_name,argv[1],argv[2]);
}
