Do a better job of writing out the BATCH.sh file (i.e. quote special
[rsync/rsync.git] / batch.c
CommitLineData
08a740ff 1/* -*- c-file-style: "linux" -*-
71020fc3 2
6902ed17 3 Weiss 1/1999
08a740ff 4 Batch utilities for rsync.
6902ed17 5
1cd5beeb 6*/
6902ed17
MP
7
8#include "rsync.h"
9#include <time.h>
10
9b3318b0 11extern char *batch_name;
e7a69008
WD
12extern int delete_mode;
13extern int delete_excluded;
e8d3168e 14
e7a69008
WD
15static void write_arg(int fd, char *arg)
16{
17 char *x, *s;
18
19 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
20 write(fd, arg, x - arg + 1);
21 arg += x - arg + 1;
22 }
23
24 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
25 write(fd, "'", 1);
26 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
27 write(fd, s, x - s + 1);
28 write(fd, "'", 1);
29 }
30 write(fd, s, strlen(s));
31 write(fd, "'", 1);
32 return;
33 }
34
35 write(fd, arg, strlen(arg));
36}
37
38/* This routine tries to write out an equivalent --read-batch command
39 * given the user's --write-batch args. However, it doesn't really
40 * understand most of the options, so it uses some overly simple
41 * heuristics to munge the command line into something that will
42 * (hopefully) work. */
43void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
6902ed17 44{
01966df4 45 int fd, i;
e7a69008
WD
46 char *p, filename[MAXPATHLEN];
47 int need_excludes = delete_mode && !delete_excluded;
088aac85 48
893c4cc0 49 stringjoin(filename, sizeof filename,
b462781f 50 batch_name, ".sh", NULL);
01966df4
WD
51 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
52 S_IRUSR | S_IWUSR | S_IEXEC);
53 if (fd < 0) {
d62bcc17 54 rsyserr(FERROR, errno, "Batch file %s open error", filename);
1cd5beeb
MP
55 exit_cleanup(1);
56 }
088aac85 57
e7a69008
WD
58 /* Write argvs info to BATCH.sh file */
59 write_arg(fd, argv[0]);
60 for (i = 1; i < argc - file_arg_cnt; i++) {
61 p = argv[i];
62 if (strncmp(p, "--files-from", 12) == 0
63 || (!need_excludes && (strncmp(p, "--include", 9) == 0
64 || strncmp(p, "--exclude", 9) == 0))) {
65 if (strchr(p, '=') == NULL)
b462781f 66 i++;
b9f592fb 67 continue;
b462781f 68 }
e7a69008
WD
69 write(fd, " ", 1);
70 if (strncmp(p, "--write-batch", 13) == 0) {
71 write(fd, "--read-batch", 12);
72 if (p[13] == '=') {
73 write(fd, "=", 1);
74 write_arg(fd, p + 14);
75 }
088aac85 76 } else
e7a69008 77 write_arg(fd, p);
1cd5beeb 78 }
e7a69008
WD
79 if ((p = find_colon(argv[argc - 1])) != NULL) {
80 if (*++p == ':')
81 p++;
82 } else
83 p = argv[argc - 1];
84 write(fd, " ${1:-", 6);
85 write_arg(fd, p);
86 if (write(fd, "}\n", 2) != 2 || close(fd) < 0) {
d62bcc17 87 rsyserr(FERROR, errno, "Batch file %s write error", filename);
1cd5beeb
MP
88 exit_cleanup(1);
89 }
6902ed17
MP
90}
91
6902ed17
MP
92void show_flist(int index, struct file_struct **fptr)
93{
1cd5beeb
MP
94 /* for debugging show_flist(flist->count, flist->files * */
95
96 int i;
97 for (i = 0; i < index; i++) {
98 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
99 rprintf(FINFO, "flist->modtime=%#lx\n",
100 (long unsigned) fptr[i]->modtime);
101 rprintf(FINFO, "flist->length=%.0f\n",
102 (double) fptr[i]->length);
103 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
104 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
105 if (fptr[i]->dirname)
106 rprintf(FINFO, "flist->dirname=%s\n",
107 fptr[i]->dirname);
108 if (fptr[i]->basedir)
109 rprintf(FINFO, "flist->basedir=%s\n",
110 fptr[i]->basedir);
111 }
6902ed17
MP
112}
113
114void show_argvs(int argc, char *argv[])
115{
1cd5beeb 116 /* for debugging * */
6902ed17 117
1cd5beeb
MP
118 int i;
119 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
120 for (i = 0; i < argc; i++) {
121 /* if (argv[i]) */
122 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);
6902ed17 123
1cd5beeb 124 }
6902ed17 125}