home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / utils / update.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-12  |  1.3 KB  |  73 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. /* This file is intended to be compiled with Turbo-C */
  3.  
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <io.h>
  8.  
  9. static void
  10. fatal(const char *s)
  11. {
  12.   fputs(s, stderr);
  13.   exit(1);
  14. }
  15.  
  16. int
  17. main(int argc, char **argv)
  18. {
  19.   int f1;
  20.   int f2;
  21.   char buf1[512];
  22.   char buf2[512];
  23.   int len1, len2;
  24.   
  25.   if (argc < 3)
  26.   {
  27.     fprintf(stderr, "Usage: update <srcfile> <destfile>\n");
  28.     fprintf(stderr, "If srcfile is different than destfile, srcfile is copied to destfile.\n");
  29.     exit(1);
  30.   }
  31.  
  32.   f1 = _open(argv[1], O_RDONLY);
  33.   if (f1 < 0)
  34.     fatal("Cannot open src file\n");
  35.  
  36.   f2 = _open(argv[2], O_RDONLY);
  37.  
  38.   if (f2 >= 0)
  39.     while (1)
  40.     {
  41.       len1 = _read(f1, buf1, 512);
  42.       len2 = _read(f2, buf2, 512);
  43.       if (len1 != len2)
  44.         break;
  45.       if (memcmp(buf1, buf2, len1))
  46.         break;
  47.       if (len1)
  48.         continue;
  49.       exit(0);
  50.     }
  51.  
  52.   if (f2 >= 0)
  53.   {
  54.     printf("File `%s' updated\n", argv[2]);
  55.     _close(f2);
  56.   }
  57.   else
  58.     printf("File `%s' created\n", argv[2]);
  59.  
  60.   lseek(f1, 0L, 0);
  61.   f2 = _creat(argv[2], 0);
  62.   while (1)
  63.   {
  64.     len1 = _read(f1, buf1, 512);
  65.     if (len1 == 0)
  66.       break;
  67.     len2 = _write(f2, buf1, len1);
  68.   }
  69.   _close(f1);
  70.   _close(f2);
  71.   exit(0);
  72. }
  73.