Look for strcasecmp in -lresolv for Unixware.
[rsync/rsync.git] / options.c
CommitLineData
7a6421fa
AT
1/*
2 Copyright (C) Andrew Tridgell 1998
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17*/
18
19/* options parsing code */
20
21#include "rsync.h"
22
23
24int make_backups = 0;
25int whole_file = 0;
26int copy_links = 0;
27int preserve_links = 0;
28int preserve_hard_links = 0;
29int preserve_perms = 0;
30int preserve_devices = 0;
31int preserve_uid = 0;
32int preserve_gid = 0;
33int preserve_times = 0;
34int update_only = 0;
35int cvs_exclude = 0;
36int dry_run=0;
37int local_server=0;
38int ignore_times=0;
39int delete_mode=0;
40int one_file_system=0;
41int remote_version=0;
42int sparse_files=0;
43int do_compression=0;
44int am_root=0;
45int orig_umask=0;
46int relative_paths=0;
47int numeric_ids = 0;
48int force_delete = 0;
49int io_timeout = 0;
50int io_error = 0;
51int read_only = 0;
52int module_id = -1;
53int am_server = 0;
54int am_sender=0;
55int recurse = 0;
56int am_daemon=0;
8d9dc9f9 57int am_client=0;
a800434a 58int do_stats=0;
eb86d661 59int do_progress=0;
c95da96a 60int keep_partial=0;
d853783f 61int safe_symlinks=0;
7a6421fa
AT
62
63int block_size=BLOCK_SIZE;
64
65char *backup_suffix = BACKUP_SUFFIX;
66char *tmpdir = NULL;
375a4556 67char *compare_dest = NULL;
7a6421fa
AT
68char *config_file = RSYNCD_CONF;
69char *shell_cmd = NULL;
b6062654 70char *log_format = NULL;
7a6421fa
AT
71
72char *rsync_path = RSYNC_NAME;
73int rsync_port = RSYNC_PORT;
74
75int verbose = 0;
76int always_checksum = 0;
f7632fc6 77int list_only = 0;
7a6421fa
AT
78
79void usage(int F)
80{
81 rprintf(F,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
82 VERSION);
704f908e
AT
83
84 rprintf(F,"rsync is a file transfer program capable of efficient remote update\nvia a fast differencing algorithm.\n\n");
85
86 rprintf(F,"Usage: rsync [OPTION]... SRC [USER@]HOST:DEST\n");
87 rprintf(F," or rsync [OPTION]... [USER@]HOST:SRC DEST\n");
88 rprintf(F," or rsync [OPTION]... SRC DEST\n");
14d43f1f 89 rprintf(F," or rsync [OPTION]... [USER@]HOST::SRC [DEST]\n");
704f908e 90 rprintf(F," or rsync [OPTION]... SRC [USER@]HOST::DEST\n");
14d43f1f 91 rprintf(F," or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]\n");
704f908e
AT
92 rprintf(F,"\nOptions\n");
93 rprintf(F," -v, --verbose increase verbosity\n");
94 rprintf(F," -c, --checksum always checksum\n");
95 rprintf(F," -a, --archive archive mode\n");
96 rprintf(F," -r, --recursive recurse into directories\n");
97 rprintf(F," -R, --relative use relative path names\n");
98 rprintf(F," -b, --backup make backups (default ~ extension)\n");
99 rprintf(F," -u, --update update only (don't overwrite newer files)\n");
100 rprintf(F," -l, --links preserve soft links\n");
101 rprintf(F," -L, --copy-links treat soft links like regular files\n");
d853783f 102 rprintf(F," --safe-links ignore links outside the destination tree\n");
704f908e
AT
103 rprintf(F," -H, --hard-links preserve hard links\n");
104 rprintf(F," -p, --perms preserve permissions\n");
105 rprintf(F," -o, --owner preserve owner (root only)\n");
106 rprintf(F," -g, --group preserve group\n");
107 rprintf(F," -D, --devices preserve devices (root only)\n");
108 rprintf(F," -t, --times preserve times\n");
109 rprintf(F," -S, --sparse handle sparse files efficiently\n");
110 rprintf(F," -n, --dry-run show what would have been transferred\n");
111 rprintf(F," -W, --whole-file copy whole files, no incremental checks\n");
112 rprintf(F," -x, --one-file-system don't cross filesystem boundaries\n");
113 rprintf(F," -B, --block-size=SIZE checksum blocking size\n");
114 rprintf(F," -e, --rsh=COMMAND specify rsh replacement\n");
115 rprintf(F," --rsync-path=PATH specify path to rsync on the remote machine\n");
116 rprintf(F," -C, --cvs-exclude auto ignore files in the same way CVS does\n");
117 rprintf(F," --delete delete files that don't exist on the sending side\n");
c95da96a 118 rprintf(F," --partial keep partially transferred files\n");
704f908e
AT
119 rprintf(F," --force force deletion of directories even if not empty\n");
120 rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n");
121 rprintf(F," --timeout=TIME set IO timeout in seconds\n");
122 rprintf(F," -I, --ignore-times don't exclude files that match length and time\n");
123 rprintf(F," -T --temp-dir=DIR create temporary files in directory DIR\n");
375a4556 124 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
704f908e 125 rprintf(F," -z, --compress compress file data\n");
2acf81eb 126 rprintf(F," --exclude=PATTERN exclude files matching PATTERN\n");
858fb9eb 127 rprintf(F," --exclude-from=FILE exclude patterns listed in FILE\n");
2acf81eb 128 rprintf(F," --include=PATTERN don't exclude files matching PATTERN\n");
858fb9eb 129 rprintf(F," --include-from=FILE don't exclude patterns listed in FILE\n");
704f908e
AT
130 rprintf(F," --suffix=SUFFIX override backup suffix\n");
131 rprintf(F," --version print version number\n");
132 rprintf(F," --daemon run as a rsync daemon\n");
133 rprintf(F," --config=FILE specify alternate rsyncd.conf file\n");
134 rprintf(F," --port=PORT specify alternate rsyncd port number\n");
135 rprintf(F," --stats give some file transfer stats\n");
eb86d661 136 rprintf(F," --progress show progress during transfer\n");
b6062654 137 rprintf(F," --log-format=FORMAT log file transfers using specified format\n");
704f908e 138 rprintf(F," -h, --help show this help screen\n");
7a6421fa
AT
139
140 rprintf(F,"\n");
141 rprintf(F,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
142 rprintf(F,"the block size defaults to %d\n",BLOCK_SIZE);
b72f24c7
AT
143
144 rprintf(F,"\nPlease see the rsync(1) and rsyncd.conf(5) man pages for full documentation\n");
145 rprintf(F,"See http://samba.anu.edu.au/rsync/ for updates and bug reports\n");
7a6421fa
AT
146}
147
148enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
149 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH,
2b6b4d53 150 OPT_FORCE,OPT_TIMEOUT,OPT_DAEMON,OPT_CONFIG,OPT_PORT,
d853783f 151 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_STATS, OPT_PARTIAL, OPT_PROGRESS,
b6062654 152 OPT_SAFE_LINKS, OPT_COMPARE_DEST, OPT_LOG_FORMAT};
7a6421fa
AT
153
154static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z";
155
156static struct option long_options[] = {
157 {"version", 0, 0, OPT_VERSION},
158 {"server", 0, 0, OPT_SERVER},
159 {"sender", 0, 0, OPT_SENDER},
160 {"delete", 0, 0, OPT_DELETE},
161 {"force", 0, 0, OPT_FORCE},
162 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
163 {"exclude", 1, 0, OPT_EXCLUDE},
164 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
2b6b4d53
AT
165 {"include", 1, 0, OPT_INCLUDE},
166 {"include-from",1, 0, OPT_INCLUDE_FROM},
7a6421fa
AT
167 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
168 {"one-file-system",0, 0, 'x'},
169 {"ignore-times",0, 0, 'I'},
170 {"help", 0, 0, 'h'},
171 {"dry-run", 0, 0, 'n'},
172 {"sparse", 0, 0, 'S'},
173 {"cvs-exclude", 0, 0, 'C'},
174 {"archive", 0, 0, 'a'},
175 {"checksum", 0, 0, 'c'},
176 {"backup", 0, 0, 'b'},
177 {"update", 0, 0, 'u'},
178 {"verbose", 0, 0, 'v'},
179 {"recursive", 0, 0, 'r'},
180 {"relative", 0, 0, 'R'},
181 {"devices", 0, 0, 'D'},
182 {"perms", 0, 0, 'p'},
183 {"links", 0, 0, 'l'},
184 {"copy-links", 0, 0, 'L'},
d853783f 185 {"safe-links", 0, 0, OPT_SAFE_LINKS},
7a6421fa
AT
186 {"whole-file", 0, 0, 'W'},
187 {"hard-links", 0, 0, 'H'},
188 {"owner", 0, 0, 'o'},
189 {"group", 0, 0, 'g'},
190 {"times", 0, 0, 't'},
191 {"rsh", 1, 0, 'e'},
192 {"suffix", 1, 0, OPT_SUFFIX},
193 {"block-size", 1, 0, 'B'},
194 {"timeout", 1, 0, OPT_TIMEOUT},
195 {"temp-dir", 1, 0, 'T'},
375a4556 196 {"compare-dest", 1, 0, OPT_COMPARE_DEST},
7a6421fa
AT
197 {"compress", 0, 0, 'z'},
198 {"daemon", 0, 0, OPT_DAEMON},
a800434a 199 {"stats", 0, 0, OPT_STATS},
eb86d661 200 {"progress", 0, 0, OPT_PROGRESS},
c95da96a 201 {"partial", 0, 0, OPT_PARTIAL},
7a6421fa
AT
202 {"config", 1, 0, OPT_CONFIG},
203 {"port", 1, 0, OPT_PORT},
b6062654 204 {"log-format", 1, 0, OPT_LOG_FORMAT},
7a6421fa
AT
205 {0,0,0,0}};
206
b11ed3b1
AT
207
208int parse_arguments(int argc, char *argv[])
7a6421fa 209{
d853783f
AT
210 int opt;
211 int option_index;
212
213 while ((opt = getopt_long(argc, argv,
214 short_options, long_options, &option_index))
215 != -1) {
216 switch (opt) {
217 case OPT_VERSION:
218 rprintf(FINFO,"rsync version %s protocol version %d\n\n",
219 VERSION,PROTOCOL_VERSION);
220 rprintf(FINFO,"Written by Andrew Tridgell and Paul Mackerras\n");
221 exit_cleanup(0);
222
223 case OPT_SUFFIX:
224 backup_suffix = optarg;
225 break;
226
227 case OPT_RSYNC_PATH:
228 rsync_path = optarg;
229 break;
230
231 case 'I':
232 ignore_times = 1;
233 break;
234
235 case 'x':
236 one_file_system=1;
237 break;
238
239 case OPT_DELETE:
240 delete_mode = 1;
241 break;
242
243 case OPT_FORCE:
244 force_delete = 1;
245 break;
246
247 case OPT_NUMERIC_IDS:
248 numeric_ids = 1;
249 break;
250
251 case OPT_EXCLUDE:
252 add_exclude(optarg, 0);
253 break;
254
255 case OPT_INCLUDE:
256 add_exclude(optarg, 1);
257 break;
258
259 case OPT_EXCLUDE_FROM:
260 add_exclude_file(optarg,1, 0);
261 break;
262
263 case OPT_INCLUDE_FROM:
264 add_exclude_file(optarg,1, 1);
265 break;
266
267 case OPT_SAFE_LINKS:
268 safe_symlinks=1;
269 break;
270
271 case 'h':
272 usage(FINFO);
273 exit_cleanup(0);
274
275 case 'b':
276 make_backups=1;
277 break;
278
279 case 'n':
280 dry_run=1;
281 break;
282
283 case 'S':
284 sparse_files=1;
285 break;
286
287 case 'C':
288 cvs_exclude=1;
289 break;
290
291 case 'u':
292 update_only=1;
293 break;
294
295 case 'l':
296 preserve_links=1;
297 break;
298
299 case 'L':
300 copy_links=1;
301 break;
302
303 case 'W':
304 whole_file=1;
305 break;
306
307 case 'H':
7a6421fa 308#if SUPPORT_HARD_LINKS
d853783f 309 preserve_hard_links=1;
7a6421fa 310#else
d853783f 311 rprintf(FERROR,"ERROR: hard links not supported on this platform\n");
b11ed3b1 312 return 0;
7a6421fa 313#endif
d853783f 314 break;
7a6421fa 315
d853783f
AT
316 case 'p':
317 preserve_perms=1;
318 break;
7a6421fa 319
d853783f
AT
320 case 'o':
321 preserve_uid=1;
322 break;
7a6421fa 323
d853783f
AT
324 case 'g':
325 preserve_gid=1;
326 break;
7a6421fa 327
d853783f
AT
328 case 'D':
329 preserve_devices=1;
330 break;
7a6421fa 331
d853783f
AT
332 case 't':
333 preserve_times=1;
334 break;
7a6421fa 335
d853783f
AT
336 case 'c':
337 always_checksum=1;
338 break;
7a6421fa 339
d853783f
AT
340 case 'v':
341 verbose++;
342 break;
7a6421fa 343
d853783f
AT
344 case 'a':
345 recurse=1;
7a6421fa 346#if SUPPORT_LINKS
d853783f 347 preserve_links=1;
7a6421fa 348#endif
d853783f
AT
349 preserve_perms=1;
350 preserve_times=1;
351 preserve_gid=1;
352 if (am_root) {
353 preserve_devices=1;
354 preserve_uid=1;
355 }
356 break;
357
358 case OPT_SERVER:
359 am_server = 1;
360 break;
361
362 case OPT_SENDER:
363 if (!am_server) {
364 usage(FERROR);
65417579 365 exit_cleanup(RERR_SYNTAX);
d853783f
AT
366 }
367 am_sender = 1;
368 break;
369
370 case 'r':
371 recurse = 1;
372 break;
373
374 case 'R':
375 relative_paths = 1;
376 break;
377
378 case 'e':
379 shell_cmd = optarg;
380 break;
381
382 case 'B':
383 block_size = atoi(optarg);
384 break;
385
386 case OPT_TIMEOUT:
387 io_timeout = atoi(optarg);
388 break;
389
390 case 'T':
391 tmpdir = optarg;
392 break;
393
375a4556
DD
394 case OPT_COMPARE_DEST:
395 compare_dest = optarg;
396 break;
397
d853783f
AT
398 case 'z':
399 do_compression = 1;
400 break;
401
402 case OPT_DAEMON:
403 am_daemon = 1;
404 break;
405
406 case OPT_STATS:
407 do_stats = 1;
408 break;
409
410 case OPT_PROGRESS:
411 do_progress = 1;
412 break;
413
414 case OPT_PARTIAL:
415 keep_partial = 1;
416 break;
417
418 case OPT_CONFIG:
419 config_file = optarg;
420 break;
421
422 case OPT_PORT:
423 rsync_port = atoi(optarg);
424 break;
425
b6062654
AT
426 case OPT_LOG_FORMAT:
427 log_format = optarg;
428 break;
429
d853783f 430 default:
b11ed3b1 431 return 0;
d853783f 432 }
7a6421fa 433 }
b11ed3b1 434 return 1;
7a6421fa
AT
435}
436
437
438void server_options(char **args,int *argc)
439{
d853783f
AT
440 int ac = *argc;
441 static char argstr[50];
442 static char bsize[30];
443 static char iotime[30];
444 int i, x;
445
446 args[ac++] = "--server";
447
448 if (!am_sender)
449 args[ac++] = "--sender";
450
451 x = 1;
452 argstr[0] = '-';
453 for (i=0;i<verbose;i++)
454 argstr[x++] = 'v';
455 if (make_backups)
456 argstr[x++] = 'b';
457 if (update_only)
458 argstr[x++] = 'u';
459 if (dry_run)
460 argstr[x++] = 'n';
461 if (preserve_links)
462 argstr[x++] = 'l';
463 if (copy_links)
464 argstr[x++] = 'L';
465 if (whole_file)
466 argstr[x++] = 'W';
467 if (preserve_hard_links)
468 argstr[x++] = 'H';
469 if (preserve_uid)
470 argstr[x++] = 'o';
471 if (preserve_gid)
472 argstr[x++] = 'g';
473 if (preserve_devices)
474 argstr[x++] = 'D';
475 if (preserve_times)
476 argstr[x++] = 't';
477 if (preserve_perms)
478 argstr[x++] = 'p';
479 if (recurse)
480 argstr[x++] = 'r';
481 if (always_checksum)
482 argstr[x++] = 'c';
483 if (cvs_exclude)
484 argstr[x++] = 'C';
485 if (ignore_times)
486 argstr[x++] = 'I';
487 if (relative_paths)
488 argstr[x++] = 'R';
489 if (one_file_system)
490 argstr[x++] = 'x';
491 if (sparse_files)
492 argstr[x++] = 'S';
493 if (do_compression)
494 argstr[x++] = 'z';
495 argstr[x] = 0;
496
497 if (x != 1) args[ac++] = argstr;
498
499 if (block_size != BLOCK_SIZE) {
500 sprintf(bsize,"-B%d",block_size);
501 args[ac++] = bsize;
502 }
503
504 if (io_timeout) {
505 sprintf(iotime,"--timeout=%d",io_timeout);
506 args[ac++] = iotime;
507 }
508
509 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
510 args[ac++] = "--suffix";
511 args[ac++] = backup_suffix;
512 }
513
514 if (delete_mode)
515 args[ac++] = "--delete";
516
517 if (keep_partial)
518 args[ac++] = "--partial";
519
520 if (force_delete)
521 args[ac++] = "--force";
522
523 if (safe_symlinks)
524 args[ac++] = "--safe-links";
525
526 if (numeric_ids)
527 args[ac++] = "--numeric-ids";
528
529 if (tmpdir) {
530 args[ac++] = "--temp-dir";
531 args[ac++] = tmpdir;
532 }
533
375a4556
DD
534 if (compare_dest && am_sender) {
535 /* the server only needs this option if it is not the sender,
536 * and it may be an older version that doesn't know this
537 * option, so don't send it if client is the sender.
538 */
539 args[ac++] = "--compare-dest";
540 args[ac++] = compare_dest;
541 }
542
543
d853783f 544 *argc = ac;
7a6421fa
AT
545}
546