cpdup(1): Fix a typo in the usage
[dragonfly.git] / bin / cpdup / misc.c
CommitLineData
3a42736d
MD
1/*
2 * MISC.C
3a42736d
MD
3 */
4
5#include "cpdup.h"
6
7void
8logstd(const char *ctl, ...)
9{
10 va_list va;
11
12 va_start(va, ctl);
13 vprintf(ctl, va);
14 va_end(va);
15}
16
17void
18logerr(const char *ctl, ...)
19{
20 va_list va;
21
22 va_start(va, ctl);
23 vfprintf(stderr, ctl, va);
24 va_end(va);
25}
26
27char *
28mprintf(const char *ctl, ...)
29{
58860d7d 30 char *ptr;
3a42736d
MD
31 va_list va;
32
58860d7d
MD
33 ptr = NULL;
34
3a42736d
MD
35 va_start(va, ctl);
36 if (vasprintf(&ptr, ctl, va) < 0)
37 fatal("malloc failed");
38 va_end(va);
39 assert(ptr != NULL);
40 return(ptr);
41}
42
577109ea
MD
43char *
44fextract(FILE *fi, int n, int *pc, int skip)
45{
46 int i;
47 int c;
48 int imax;
49 char *s;
50
51 i = 0;
52 c = *pc;
53 imax = (n < 0) ? 64 : n + 1;
54
55 s = malloc(imax);
c0538630
MD
56 if (s == NULL)
57 fatal("out of memory");
577109ea
MD
58
59 while (c != EOF) {
60 if (n == 0 || (n < 0 && (c == ' ' || c == '\n')))
61 break;
62
63 s[i++] = c;
64 if (i == imax) {
65 imax += 64;
66 s = realloc(s, imax);
c0538630
MD
67 if (s == NULL)
68 fatal("out of memory");
577109ea
MD
69 }
70 if (n > 0)
71 --n;
72 c = getc(fi);
73 }
74 if (c == skip && skip != EOF)
75 c = getc(fi);
76 *pc = c;
77 s[i] = 0;
78 return(s);
79}
80
c0538630
MD
81int16_t
82hc_bswap16(int16_t var)
83{
84 return ((var & 0xff) << 8 | (var >> 8 & 0xff));
85}
86
87int32_t
88hc_bswap32(int32_t var)
89{
90 return ((var & 0xff) << 24 | (var & 0xff00) << 8
91 | (var >> 8 & 0xff00) | (var >> 24 & 0xff));
92}
93
94int64_t
95hc_bswap64(int64_t var)
96{
97 return (hc_bswap32(var >> 32 & 0xffffffff)
98 | (int64_t) hc_bswap32(var & 0xffffffff) << 32);
99}
100
975200d7
MD
101#ifdef DEBUG_MALLOC
102
103#undef malloc
104#undef free
105
106struct malloc_info {
107 struct malloc_info *next;
108 struct malloc_info *prev;
109 const char *file;
110 int magic;
111 int line;
112};
113
114struct malloc_info DummyInfo = { &DummyInfo, &DummyInfo, NULL, 0, 0 };
115struct malloc_info *InfoList = &DummyInfo;
116
117void *
118debug_malloc(size_t bytes, const char *file, int line)
119{
120 struct malloc_info *info = malloc(sizeof(*info) + bytes);
121
122 info->magic = 0x5513A4C2;
123 info->file = file;
124 info->line = line;
125
126 info->next = InfoList;
127 info->prev = InfoList->prev;
128 info->next->prev = info;
129 info->prev->next = info;
130 return(info + 1);
131}
132
133void
134debug_free(void *ptr)
135{
136 struct malloc_info *info = (struct malloc_info *)ptr - 1;
137 struct malloc_info *scan;
138 static int report;
139
140 for (scan = DummyInfo.next; scan != &DummyInfo; scan = scan->next) {
141 if (info == scan) {
142 assert(info->magic == 0x5513A4C2);
143 info->magic = 0;
144 info->next->prev = info->prev;
145 info->prev->next = info->next;
146 free(info);
147 break;
148 }
149 }
150 if (scan == &DummyInfo)
151 free(ptr);
152
153 if ((++report & 65535) == 0) {
154 printf("--- report\n");
155 for (scan = DummyInfo.next; scan != &DummyInfo; scan = scan->next) {
156 printf("%-15s %d\n", scan->file, scan->line);
157 }
158 }
159}
160
161#endif
162
3a42736d
MD
163void
164fatal(const char *ctl, ...)
165{
166 va_list va;
167
168 if (ctl == NULL) {
169 puts("cpdup [<options>] src [dest]");
44dd1628
MD
170 puts(" -C request compressed ssh link if remote operation\n"
171 " -v[vv] verbose level (-vv is typical)\n"
293141b7 172 " -d print directories being traversed\n"
4d2076d3 173 " -u use unbuffered output for -v[vv]\n"
3a42736d
MD
174 " -I display performance summary\n"
175 " -f force update even if files look the same\n"
8f0e7bc1 176 " -F<ssh_opt> Add <ssh_opt> to options passed to ssh\n"
3a42736d 177 " -i0 do NOT confirm when removing something\n"
293141b7 178 " -j0 do not try to recreate CHR or BLK devices\n"
a2dc574c 179 " -l force line-buffered stdout/stderr\n"
3a42736d
MD
180 " -s0 disable safeties - allow files to overwrite directories\n"
181 " -q quiet operation\n"
182 " -o do not remove any files, just overwrite/add\n"
1c755102 183 );
4d858d58 184 puts(
293141b7
MD
185 " -k maintain/generate FSMID checkfile on target,\n"
186 " and compare source FSMIDs against the checkfiles\n"
187 " -K file -k+specify FSMID checkfile, else .FSMID.CHECK\n"
4d858d58
MD
188#ifndef NOMD5
189 " -m maintain/generate MD5 checkfile on source,\n"
3a42736d
MD
190 " and compare with (optional) destination,\n"
191 " copying if the compare fails\n"
192 " -M file -m+specify MD5 checkfile, else .MD5_CHECKSUMS\n"
193 " copy if md5 check fails\n"
1bcaecda 194#endif
d5fdcd00 195 " -H path hardlink from path to target instead of copying\n"
ca097fa5 196 " -R read-only slave mode for ssh remotes\n"
d5fdcd00 197 " source to target, if source matches path.\n"
0c1a843c 198 " -V verify file contents even if they appear\n"
d5fdcd00 199 " to be the same.\n"
1bcaecda 200 " -VV same as -V but ignore mtime entirely\n"
3a42736d 201 " -x use .cpignore as exclusion file\n"
110def69 202 " -X file specify exclusion file (can match full source\n"
560e4370
MD
203 " patch if the exclusion file is specified via\n"
204 " an absolute path.\n"
205 " Version 1.20 by Matt Dillon, Dima Ruban, & Oliver Fromme\n"
3a42736d
MD
206 );
207 exit(0);
208 } else {
209 va_start(va, ctl);
c0538630 210 vfprintf(stderr, ctl, va);
3a42736d 211 va_end(va);
c0538630
MD
212 putc('\n', stderr);
213 exit(EXIT_FAILURE);
3a42736d
MD
214 }
215}