| 1 | /* updt.c -- Copy a file if required
|
|---|
| 2 | Copyright (c) 1994-1995 by Eberhard Mattes
|
|---|
| 3 |
|
|---|
| 4 | This file is part of emx.
|
|---|
| 5 |
|
|---|
| 6 | emx is free software; you can redistribute it and/or modify it
|
|---|
| 7 | under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | emx is distributed in the hope that it will be useful,
|
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | GNU General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with emx; see the file COPYING. If not, write to
|
|---|
| 18 | the Free Software Foundation, 59 Temple Place - Suite 330,
|
|---|
| 19 | Boston, MA 02111-1307, USA. */
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | #include <stdio.h>
|
|---|
| 23 | #include <stdlib.h>
|
|---|
| 24 | #include <string.h>
|
|---|
| 25 | #include <getopt.h>
|
|---|
| 26 | #include <io.h>
|
|---|
| 27 | #include <sys/types.h>
|
|---|
| 28 | #include <sys/stat.h>
|
|---|
| 29 |
|
|---|
| 30 | #define FALSE 0
|
|---|
| 31 | #define TRUE 1
|
|---|
| 32 |
|
|---|
| 33 | int main (int argc, char *argv[]);
|
|---|
| 34 | static void usage (void);
|
|---|
| 35 |
|
|---|
| 36 | static void usage (void)
|
|---|
| 37 | {
|
|---|
| 38 | fputs ("Usage: updt [-t] [-v] <source_file> <target_file>\n", stderr);
|
|---|
| 39 | exit (1);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | int main (int argc, char *argv[])
|
|---|
| 44 | {
|
|---|
| 45 | FILE *source_file, *target_file;
|
|---|
| 46 | char *source_fname, *target_fname;
|
|---|
| 47 | struct stat source_stat, target_stat;
|
|---|
| 48 | int c1, c2, c, opt_t, opt_v;
|
|---|
| 49 |
|
|---|
| 50 | opt_t = FALSE; opt_v = FALSE;
|
|---|
| 51 | while ((c = getopt (argc, argv, "tv")) != EOF)
|
|---|
| 52 | {
|
|---|
| 53 | switch (c)
|
|---|
| 54 | {
|
|---|
| 55 | case 't':
|
|---|
| 56 | opt_t = TRUE;
|
|---|
| 57 | break;
|
|---|
| 58 | case 'v':
|
|---|
| 59 | opt_v = TRUE;
|
|---|
| 60 | break;
|
|---|
| 61 | default:
|
|---|
| 62 | usage();
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | if (argc - optind != 2)
|
|---|
| 66 | usage ();
|
|---|
| 67 | source_fname = argv[optind+0];
|
|---|
| 68 | target_fname = argv[optind+1];
|
|---|
| 69 | source_file = fopen (source_fname, "rb");
|
|---|
| 70 | if (source_file == NULL)
|
|---|
| 71 | {
|
|---|
| 72 | fprintf (stderr, "updt: cannot open source file `%s'\n", source_fname);
|
|---|
| 73 | return 1;
|
|---|
| 74 | }
|
|---|
| 75 | target_file = fopen (target_fname, "rb");
|
|---|
| 76 | if (target_file != NULL)
|
|---|
| 77 | {
|
|---|
| 78 | if (opt_t)
|
|---|
| 79 | {
|
|---|
| 80 | if (stat (source_fname, &source_stat) != 0)
|
|---|
| 81 | {
|
|---|
| 82 | fprintf (stderr, "updt: cannot access `%s'\n", source_fname);
|
|---|
| 83 | return 1;
|
|---|
| 84 | }
|
|---|
| 85 | if (stat (target_fname, &target_stat) != 0)
|
|---|
| 86 | {
|
|---|
| 87 | fprintf (stderr, "updt: cannot access `%s'\n", target_fname);
|
|---|
| 88 | return 1;
|
|---|
| 89 | }
|
|---|
| 90 | if (target_stat.st_mtime >= source_stat.st_mtime)
|
|---|
| 91 | {
|
|---|
| 92 | if (opt_v)
|
|---|
| 93 | fputs ("updt: source not newer than target "
|
|---|
| 94 | "-- not copying\n", stderr);
|
|---|
| 95 | return 0;
|
|---|
| 96 | }
|
|---|
| 97 | if (opt_v)
|
|---|
| 98 | fputs ("updt: source newer than target "
|
|---|
| 99 | "-- copying\n", stderr);
|
|---|
| 100 | }
|
|---|
| 101 | else
|
|---|
| 102 | {
|
|---|
| 103 | for (;;)
|
|---|
| 104 | {
|
|---|
| 105 | c1 = getc (source_file);
|
|---|
| 106 | c2 = getc (target_file);
|
|---|
| 107 | if (c1 != c2 || c1 == EOF)
|
|---|
| 108 | break;
|
|---|
| 109 | }
|
|---|
| 110 | if (c1 == EOF && c2 == EOF && !ferror (source_file) &&
|
|---|
| 111 | !ferror (target_file))
|
|---|
| 112 | {
|
|---|
| 113 | if (opt_v)
|
|---|
| 114 | fputs ("updt: files match -- not copying\n", stderr);
|
|---|
| 115 | return 0;
|
|---|
| 116 | }
|
|---|
| 117 | if (opt_v)
|
|---|
| 118 | fputs ("updt: files don't match -- copying\n", stderr);
|
|---|
| 119 | }
|
|---|
| 120 | (void)fclose (target_file);
|
|---|
| 121 | }
|
|---|
| 122 | else if (opt_v)
|
|---|
| 123 | fputs ("updt: target does not exist -- copying\n", stderr);
|
|---|
| 124 | rewind (source_file);
|
|---|
| 125 | target_file = fopen (target_fname, "wb");
|
|---|
| 126 | if (target_file == NULL)
|
|---|
| 127 | {
|
|---|
| 128 | fprintf (stderr, "updt: cannot open target file `%s'\n", target_fname);
|
|---|
| 129 | return 1;
|
|---|
| 130 | }
|
|---|
| 131 | for (;;)
|
|---|
| 132 | {
|
|---|
| 133 | c1 = getc (source_file);
|
|---|
| 134 | if (c1 == EOF)
|
|---|
| 135 | break;
|
|---|
| 136 | if (putc (c1, target_file) == EOF)
|
|---|
| 137 | break;
|
|---|
| 138 | }
|
|---|
| 139 | if (ferror (source_file) || fclose (source_file) != 0)
|
|---|
| 140 | {
|
|---|
| 141 | fprintf (stderr, "updt: cannot read source file `%s'\n", source_fname);
|
|---|
| 142 | return 1;
|
|---|
| 143 | }
|
|---|
| 144 | if (ferror (target_file) || fflush (target_file) != 0 ||
|
|---|
| 145 | fclose (target_file) != 0)
|
|---|
| 146 | {
|
|---|
| 147 | fprintf (stderr, "updt: cannot write target file `%s'\n",
|
|---|
| 148 | target_fname);
|
|---|
| 149 | return 1;
|
|---|
| 150 | }
|
|---|
| 151 | return 0;
|
|---|
| 152 | }
|
|---|