1 | /* |
---|
2 | $Id: libmondo-string.c 1241 2007-03-12 01:22:55Z bruno $ |
---|
3 | */ |
---|
4 | |
---|
5 | /** |
---|
6 | * @file |
---|
7 | * Functions for handling strings. |
---|
8 | */ |
---|
9 | |
---|
10 | #include "my-stuff.h" |
---|
11 | #include "mr_mem.h" |
---|
12 | #include "mr_str.h" |
---|
13 | |
---|
14 | #include "mondostructures.h" |
---|
15 | #include "libmondo-string.h" |
---|
16 | #include "libmondo-files-EXT.h" |
---|
17 | #include "newt-specific-EXT.h" |
---|
18 | #include "libmondo-tools-EXT.h" |
---|
19 | #include <math.h> |
---|
20 | |
---|
21 | /*@unused@*/ |
---|
22 | //static char cvsid[] = "$Id: libmondo-string.c 1241 2007-03-12 01:22:55Z bruno $"; |
---|
23 | |
---|
24 | extern int g_current_media_number; |
---|
25 | extern long long g_tape_posK; |
---|
26 | |
---|
27 | /** |
---|
28 | * @addtogroup stringGroup |
---|
29 | * @{ |
---|
30 | */ |
---|
31 | /** |
---|
32 | * Build a partition name from a drive and a partition number. |
---|
33 | * @param drive The drive basename of the partition name (e.g. /dev/hda) |
---|
34 | * @param partno The partition number (e.g. 1) |
---|
35 | * @param partition Where to put the partition name (e.g. /dev/hda1) |
---|
36 | * @return @p partition. |
---|
37 | * @note If @p drive ends in a digit, then 'p' (on Linux) or 's' (on *BSD) is added before @p partno. |
---|
38 | */ |
---|
39 | char *build_partition_name(char *partition, const char *drive, int partno) |
---|
40 | { |
---|
41 | char *p = NULL; |
---|
42 | char *c = NULL; |
---|
43 | |
---|
44 | assert(partition != NULL); |
---|
45 | assert_string_is_neither_NULL_nor_zerolength(drive); |
---|
46 | assert(partno >= 0); |
---|
47 | |
---|
48 | p = strcpy(partition, drive); |
---|
49 | /* is this a devfs device path? */ |
---|
50 | c = strrchr(partition, '/'); |
---|
51 | if (c && strncmp(c, "/disc", 5) == 0) { |
---|
52 | /* yup it's devfs, return the "part" path */ |
---|
53 | /* format /dev/.../disc */ |
---|
54 | strcpy(c + 1, "part"); |
---|
55 | p = c + 5; |
---|
56 | } else { |
---|
57 | p += strlen(p); |
---|
58 | if (isdigit(p[-1])) { |
---|
59 | *p++ = |
---|
60 | #ifdef BSD |
---|
61 | 's'; |
---|
62 | #else |
---|
63 | 'p'; |
---|
64 | #endif |
---|
65 | } |
---|
66 | } |
---|
67 | sprintf(p, "%d", partno); |
---|
68 | return (partition); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | /** |
---|
73 | * Pad a string on both sides so it appears centered. |
---|
74 | * @param in_out The string to be center-padded (modified). |
---|
75 | * @param width The width of the final result. |
---|
76 | */ |
---|
77 | void center_string(char *in_out, int width) |
---|
78 | { |
---|
79 | char *scratch = NULL; |
---|
80 | int i = 0; /* purpose */ |
---|
81 | int len = 0; /* purpose */ |
---|
82 | int mid = 0; /* purpose */ |
---|
83 | int x = 0; /* purpose */ |
---|
84 | |
---|
85 | assert(in_out != NULL); |
---|
86 | assert(width > 2); |
---|
87 | |
---|
88 | if (strlen(in_out) == 0) { |
---|
89 | return; |
---|
90 | } |
---|
91 | mr_asprintf(&scratch, in_out); |
---|
92 | mr_strip_spaces(scratch); |
---|
93 | len = (int) strlen(scratch); |
---|
94 | mid = width / 2; |
---|
95 | x = mid - len / 2; |
---|
96 | for (i = 0; i < x; i++) { |
---|
97 | in_out[i] = ' '; |
---|
98 | } |
---|
99 | in_out[i] = '\0'; |
---|
100 | strcat(in_out, scratch); |
---|
101 | for (i = x + len ; i < width - 1; i++) { |
---|
102 | in_out[i] = ' '; |
---|
103 | } |
---|
104 | in_out[i] = '\0'; |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /** |
---|
109 | * Turn an entry from the RAID editor's disklist into a GUI-friendly string. |
---|
110 | * The format is: the device left-aligned and padded to 24 places, followed by a space and the |
---|
111 | * index, right-aligned and padded to eight places. The total string length |
---|
112 | * is exactly 33. |
---|
113 | * @param disklist The disklist to operate on. |
---|
114 | * @param lino The line number from @p disklist to convert to a string. |
---|
115 | * @return The string form of the disklist entry. |
---|
116 | * @note The returned string points to static storage and will be overwritten with each call. |
---|
117 | */ |
---|
118 | char *disklist_entry_to_string(struct list_of_disks *disklist, int lino) |
---|
119 | { |
---|
120 | |
---|
121 | /*@ buffers ********************************************************** */ |
---|
122 | static char output[MAX_STR_LEN]; |
---|
123 | |
---|
124 | assert(disklist != NULL); |
---|
125 | |
---|
126 | sprintf(output, "%-24s %8d", disklist->el[lino].device, |
---|
127 | disklist->el[lino].index); |
---|
128 | return (output); |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | /** |
---|
133 | * Turn a "friendly" sizestring into a number of megabytes. |
---|
134 | * Supports the suffixes 'k'/'K', 'm'/'M', and 'g'/'G'. Calls |
---|
135 | * fatal_error() if an unknown suffix is encountered. |
---|
136 | * @param incoming The sizestring to convert (e.g. "40m", "2g"). |
---|
137 | * @return The size in megabytes. |
---|
138 | */ |
---|
139 | long friendly_sizestr_to_sizelong(char *incoming) |
---|
140 | { |
---|
141 | long outval = 0L; |
---|
142 | int i = 0; |
---|
143 | char *tmp = NULL; |
---|
144 | char ch = ' '; |
---|
145 | |
---|
146 | assert_string_is_neither_NULL_nor_zerolength(incoming); |
---|
147 | |
---|
148 | if (!incoming[0]) { |
---|
149 | return(0L); |
---|
150 | } |
---|
151 | if (strchr(incoming, '.')) { |
---|
152 | fatal_error("Please use integers only. No decimal points."); |
---|
153 | } |
---|
154 | mr_asprintf(&tmp, "%s", incoming); |
---|
155 | i = (int) strlen(tmp); |
---|
156 | if (tmp[i - 1] == 'B' || tmp[i - 1] == 'b') { |
---|
157 | tmp[i - 1] = '\0'; |
---|
158 | } |
---|
159 | for (i = 0; i < (int) strlen(tmp) && isdigit(tmp[i]); i++); |
---|
160 | ch = tmp[i]; |
---|
161 | tmp[i] = '\0'; |
---|
162 | outval = atol(tmp); |
---|
163 | mr_free(tmp); |
---|
164 | |
---|
165 | if (ch == 'g' || ch == 'G') { |
---|
166 | outval = outval * 1024; |
---|
167 | } else if (ch == 'k' || ch == 'K') { |
---|
168 | outval = outval / 1024; |
---|
169 | } else if (ch == 't' || ch == 'T') // terabyte |
---|
170 | { |
---|
171 | outval *= 1048576; |
---|
172 | } else if (ch == 'Y' || ch == 'y') // yottabyte - the biggest measure in the info file |
---|
173 | { |
---|
174 | log_it |
---|
175 | ("Oh my gosh. You actually think a YOTTABYTE will get you anywhere? What're you going to do with 1,208,925,819,614,629,174,706,176 bytes of data?!?!"); |
---|
176 | popup_and_OK |
---|
177 | (_("That sizespec is more than 1,208,925,819,614,629,174,706,176 bytes. You have a shocking amount of data. Please send a screenshot to the list :-)")); |
---|
178 | fatal_error("Integer overflow."); |
---|
179 | } else if (ch != 'm' && ch != 'M') { |
---|
180 | mr_asprintf(&tmp, "Re: parameter '%s' - bad multiplier ('%c')", |
---|
181 | incoming, ch); |
---|
182 | fatal_error(tmp); |
---|
183 | } |
---|
184 | return (outval); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | /** |
---|
189 | * Turn a marker byte (e.g. BLK_START_OF_BACKUP) into a string (e.g. "BLK_START_OF_BACKUP"). |
---|
190 | * Unknown markers are identified as "BLK_UNKNOWN (%d)" where %d is the decimal value. |
---|
191 | * @param marker The marker byte to stringify. |
---|
192 | * @return @p marker as a string. |
---|
193 | * @note The returned string points to static storage that will be overwritten with each call. |
---|
194 | */ |
---|
195 | char *marker_to_string(int marker) |
---|
196 | { |
---|
197 | /*@ buffer ****************************************************** */ |
---|
198 | static char outstr[MAX_STR_LEN]; |
---|
199 | |
---|
200 | |
---|
201 | /*@ end vars *************************************************** */ |
---|
202 | |
---|
203 | switch (marker) { |
---|
204 | case BLK_START_OF_BACKUP: |
---|
205 | strcpy(outstr, "BLK_START_OF_BACKUP"); |
---|
206 | break; |
---|
207 | case BLK_START_OF_TAPE: |
---|
208 | strcpy(outstr, "BLK_START_OF_TAPE"); |
---|
209 | break; |
---|
210 | case BLK_START_AN_AFIO_OR_SLICE: |
---|
211 | strcpy(outstr, "BLK_START_AN_AFIO_OR_SLICE"); |
---|
212 | break; |
---|
213 | case BLK_STOP_AN_AFIO_OR_SLICE: |
---|
214 | strcpy(outstr, "BLK_STOP_AN_AFIO_OR_SLICE"); |
---|
215 | break; |
---|
216 | case BLK_START_AFIOBALLS: |
---|
217 | strcpy(outstr, "BLK_START_AFIOBALLS"); |
---|
218 | break; |
---|
219 | case BLK_STOP_AFIOBALLS: |
---|
220 | strcpy(outstr, "BLK_STOP_AFIOBALLS"); |
---|
221 | break; |
---|
222 | case BLK_STOP_BIGGIEFILES: |
---|
223 | strcpy(outstr, "BLK_STOP_BIGGIEFILES"); |
---|
224 | break; |
---|
225 | case BLK_START_A_NORMBIGGIE: |
---|
226 | strcpy(outstr, "BLK_START_A_NORMBIGGIE"); |
---|
227 | break; |
---|
228 | case BLK_START_A_PIHBIGGIE: |
---|
229 | strcpy(outstr, "BLK_START_A_PIHBIGGIE"); |
---|
230 | break; |
---|
231 | case BLK_START_EXTENDED_ATTRIBUTES: |
---|
232 | strcpy(outstr, "BLK_START_EXTENDED_ATTRIBUTES"); |
---|
233 | break; |
---|
234 | case BLK_STOP_EXTENDED_ATTRIBUTES: |
---|
235 | strcpy(outstr, "BLK_STOP_EXTENDED_ATTRIBUTES"); |
---|
236 | break; |
---|
237 | case BLK_START_EXAT_FILE: |
---|
238 | strcpy(outstr, "BLK_START_EXAT_FILE"); |
---|
239 | break; |
---|
240 | case BLK_STOP_EXAT_FILE: |
---|
241 | strcpy(outstr, "BLK_STOP_EXAT_FILE"); |
---|
242 | break; |
---|
243 | case BLK_START_BIGGIEFILES: |
---|
244 | strcpy(outstr, "BLK_START_BIGGIEFILES"); |
---|
245 | break; |
---|
246 | case BLK_STOP_A_BIGGIE: |
---|
247 | strcpy(outstr, "BLK_STOP_A_BIGGIE"); |
---|
248 | break; |
---|
249 | case BLK_END_OF_TAPE: |
---|
250 | strcpy(outstr, "BLK_END_OF_TAPE"); |
---|
251 | break; |
---|
252 | case BLK_END_OF_BACKUP: |
---|
253 | strcpy(outstr, "BLK_END_OF_BACKUP"); |
---|
254 | break; |
---|
255 | case BLK_ABORTED_BACKUP: |
---|
256 | strcpy(outstr, "BLK_ABORTED_BACKUP"); |
---|
257 | break; |
---|
258 | case BLK_START_FILE: |
---|
259 | strcpy(outstr, "BLK_START_FILE"); |
---|
260 | break; |
---|
261 | case BLK_STOP_FILE: |
---|
262 | strcpy(outstr, "BLK_STOP_FILE"); |
---|
263 | break; |
---|
264 | default: |
---|
265 | sprintf(outstr, "BLK_UNKNOWN (%d)", marker); |
---|
266 | break; |
---|
267 | } |
---|
268 | return (outstr); |
---|
269 | } |
---|
270 | |
---|
271 | |
---|
272 | /** |
---|
273 | * Turn a line from the mountlist into a GUI-friendly string. |
---|
274 | * The format is as follows: the left-aligned @p device field padded to 24 places, |
---|
275 | * a space, the left-aligned @p mountpoint field again padded to 24 places, a space, |
---|
276 | * the left-aligned @p format field padded to 10 places, a space, and the right-aligned |
---|
277 | * @p size field (in MB) padded to 8 places. The total string length is exactly 69. |
---|
278 | * @param mountlist The mountlist to operate on. |
---|
279 | * @param lino The line number in @p mountlist to stringify. |
---|
280 | * @return The string form of <tt>mountlist</tt>-\>el[<tt>lino</tt>]. |
---|
281 | * @note The returned string points to static storage and will be overwritten with each call. |
---|
282 | */ |
---|
283 | char *mountlist_entry_to_string(struct mountlist_itself *mountlist, |
---|
284 | int lino) |
---|
285 | { |
---|
286 | |
---|
287 | /*@ buffer *********************************************************** */ |
---|
288 | static char output[MAX_STR_LEN]; |
---|
289 | |
---|
290 | assert(mountlist != NULL); |
---|
291 | |
---|
292 | sprintf(output, "%-24s %-24s %-10s %8lld", mountlist->el[lino].device, |
---|
293 | mountlist->el[lino].mountpoint, mountlist->el[lino].format, |
---|
294 | mountlist->el[lino].size / 1024); |
---|
295 | return (output); |
---|
296 | } |
---|
297 | |
---|
298 | |
---|
299 | /** |
---|
300 | * Generate a friendly string containing "X blah blah disk(s)" |
---|
301 | * @param noof_disks The number of disks (the X). |
---|
302 | * @param label The "blah blah" part in the middle. If you leave this blank |
---|
303 | * there will be a weird double space in the middle, so pass *something*. |
---|
304 | * @return The string containing "X blah blah disk(s)". |
---|
305 | * @note The returned string points to static storage and will be overwritten with each call. |
---|
306 | */ |
---|
307 | char *number_of_disks_as_string(int noof_disks, char *label) |
---|
308 | { |
---|
309 | |
---|
310 | /*@ buffers ********************************************************* */ |
---|
311 | static char output[MAX_STR_LEN]; |
---|
312 | |
---|
313 | /*@ char ******************************************************** */ |
---|
314 | char p = ' '; |
---|
315 | |
---|
316 | assert(label != NULL); |
---|
317 | |
---|
318 | if (noof_disks > 1) { |
---|
319 | p = 's'; |
---|
320 | } else { |
---|
321 | p = ' '; |
---|
322 | } |
---|
323 | sprintf(output, "%d %s disk%c", noof_disks, label, p); |
---|
324 | while (strlen(output) < 14) { |
---|
325 | strcat(output, " "); |
---|
326 | } |
---|
327 | return (output); |
---|
328 | } |
---|
329 | |
---|
330 | |
---|
331 | |
---|
332 | /** |
---|
333 | * Change @p i into a friendly string. If @p i is \<= 10 then write out the |
---|
334 | * number (e.g. "one", "two", ..., "nine", "ten", "11", ...). |
---|
335 | * @param i The number to stringify. |
---|
336 | * @return The string form of @p i. |
---|
337 | * @note The returned value points to static strorage that will be overwritten with each call. |
---|
338 | */ |
---|
339 | char *number_to_text(int i) |
---|
340 | { |
---|
341 | |
---|
342 | /*@ buffers ***************************************************** */ |
---|
343 | static char output[MAX_STR_LEN]; |
---|
344 | |
---|
345 | |
---|
346 | /*@ end vars *************************************************** */ |
---|
347 | |
---|
348 | switch (i) { |
---|
349 | case 0: |
---|
350 | strcpy(output, "zero"); |
---|
351 | break; |
---|
352 | case 1: |
---|
353 | strcpy(output, "one"); |
---|
354 | break; |
---|
355 | case 2: |
---|
356 | strcpy(output, "two"); |
---|
357 | break; |
---|
358 | case 3: |
---|
359 | strcpy(output, "three"); |
---|
360 | break; |
---|
361 | case 4: |
---|
362 | strcpy(output, "four"); |
---|
363 | break; |
---|
364 | case 5: |
---|
365 | strcpy(output, "five"); |
---|
366 | break; |
---|
367 | case 6: |
---|
368 | strcpy(output, "six"); |
---|
369 | break; |
---|
370 | case 7: |
---|
371 | strcpy(output, "seven"); |
---|
372 | break; |
---|
373 | case 8: |
---|
374 | strcpy(output, "eight"); |
---|
375 | break; |
---|
376 | case 9: |
---|
377 | strcpy(output, "nine"); |
---|
378 | case 10: |
---|
379 | strcpy(output, "ten"); |
---|
380 | default: |
---|
381 | sprintf(output, "%d", i); |
---|
382 | } |
---|
383 | return (output); |
---|
384 | } |
---|
385 | |
---|
386 | |
---|
387 | /** |
---|
388 | * Replace all occurences of @p token with @p value while copying @p ip to @p output. |
---|
389 | * @param ip The input string containing zero or more <tt>token</tt>s. |
---|
390 | * @param output The output string written with the <tt>token</tt>s replaced by @p value. |
---|
391 | * @param token The token to be relaced with @p value. |
---|
392 | * @param value The value to replace @p token. |
---|
393 | */ |
---|
394 | void resolve_naff_tokens(char *output, char *ip, char *value, char *token) |
---|
395 | { |
---|
396 | /*@ buffers *** */ |
---|
397 | char *input; |
---|
398 | |
---|
399 | /*@ pointers * */ |
---|
400 | char *p; |
---|
401 | |
---|
402 | input = mr_malloc(2000); |
---|
403 | // BERLIOS: seems to cause invalid errors |
---|
404 | //assert_string_is_neither_NULL_nor_zerolength(ip); |
---|
405 | assert_string_is_neither_NULL_nor_zerolength(token); |
---|
406 | assert(value != NULL); |
---|
407 | |
---|
408 | strcpy(output, ip); /* just in case the token doesn't appear in string at all */ |
---|
409 | for (strcpy(input, ip); strstr(input, token); strcpy(input, output)) { |
---|
410 | strcpy(output, input); |
---|
411 | p = strstr(output, token); |
---|
412 | *p = '\0'; |
---|
413 | strcat(output, value); |
---|
414 | p = strstr(input, token) + strlen(token); |
---|
415 | strcat(output, p); |
---|
416 | } |
---|
417 | mr_free(input); |
---|
418 | } |
---|
419 | |
---|
420 | |
---|
421 | /** |
---|
422 | * Generate the filename of slice @p sliceno of biggiefile @p bigfileno |
---|
423 | * in @p path with suffix @p s. The format is as follows: @p path, followed |
---|
424 | * by "/slice-" and @p bigfileno zero-padded to 7 places, followed by |
---|
425 | * a dot and @p sliceno zero-padded to 5 places, followed by ".dat" and the |
---|
426 | * suffix. The string is a minimum of 24 characters long. |
---|
427 | * @param bigfileno The biggiefile number. Starts from 0. |
---|
428 | * @param sliceno The slice number of biggiefile @p bigfileno. 0 is a "header" |
---|
429 | * slice (no suffix) containing the biggiestruct, then are the compressed |
---|
430 | * slices, then an empty uncompressed "trailer" slice. |
---|
431 | * @param path The path to append (with a / in the middle) to the slice filename. |
---|
432 | * @param s If not "" then add a "." and this to the end. |
---|
433 | * @return The slice filename. |
---|
434 | * @note The returned value points to static storage and will be overwritten with each call. |
---|
435 | */ |
---|
436 | char *slice_fname(long bigfileno, long sliceno, char *path, char *s) |
---|
437 | { |
---|
438 | |
---|
439 | /*@ buffers **************************************************** */ |
---|
440 | static char output[MAX_STR_LEN]; |
---|
441 | char *suffix = NULL; |
---|
442 | |
---|
443 | /*@ end vars *************************************************** */ |
---|
444 | |
---|
445 | assert_string_is_neither_NULL_nor_zerolength(path); |
---|
446 | if (s[0] != '\0') { |
---|
447 | mr_asprintf(&suffix, ".%s", s); |
---|
448 | } else { |
---|
449 | mr_asprintf(&suffix, ""); |
---|
450 | } |
---|
451 | sprintf(output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno, sliceno, |
---|
452 | suffix); |
---|
453 | mr_free(suffix); |
---|
454 | return (output); |
---|
455 | } |
---|
456 | |
---|
457 | |
---|
458 | /** |
---|
459 | * Generate a spinning symbol based on integer @p i. |
---|
460 | * The symbol rotates through the characters / - \ | to form an ASCII "spinner" |
---|
461 | * if successively written to the same location on screen. |
---|
462 | * @param i The amount of progress or whatever else to use to determine the character |
---|
463 | * for this iteration of the spinner. |
---|
464 | * @return The character for this iteration. |
---|
465 | */ |
---|
466 | int special_dot_char(int i) |
---|
467 | { |
---|
468 | switch (i % 4) { |
---|
469 | case 0: |
---|
470 | return ('/'); |
---|
471 | case 1: |
---|
472 | return ('-'); |
---|
473 | case 2: |
---|
474 | return ('\\'); |
---|
475 | case 3: |
---|
476 | return ('|'); |
---|
477 | default: |
---|
478 | return ('.'); |
---|
479 | } |
---|
480 | return ('.'); |
---|
481 | } |
---|
482 | |
---|
483 | |
---|
484 | /** |
---|
485 | * Wrap @p flaws_str across three lines. The first two are no more than 74 characters wide. |
---|
486 | * @param flaws_str The original string to split. |
---|
487 | * @param flaws_str_A Where to put the first 74-or-less characters. |
---|
488 | * @param flaws_str_B Where to put the second 74-or-less characters. |
---|
489 | * @param flaws_str_C Where to put the rest. |
---|
490 | * @param res The result of the original evaluate_mountlist() operation. |
---|
491 | * @return TRUE if res == 0, FALSE otherwise. |
---|
492 | */ |
---|
493 | bool |
---|
494 | spread_flaws_across_three_lines(char *flaws_str, char *flaws_str_A, |
---|
495 | char *flaws_str_B, char *flaws_str_C, |
---|
496 | int res) |
---|
497 | { |
---|
498 | |
---|
499 | /*@ int ************************************************************* */ |
---|
500 | int i = 0; |
---|
501 | |
---|
502 | /*@ initialize ****************************************************** */ |
---|
503 | assert(flaws_str_A != NULL); |
---|
504 | assert(flaws_str_B != NULL); |
---|
505 | assert(flaws_str_C != NULL); |
---|
506 | assert(flaws_str != NULL); |
---|
507 | |
---|
508 | flaws_str_A[0] = flaws_str_B[0] = flaws_str_C[0] = '\0'; |
---|
509 | |
---|
510 | |
---|
511 | if (!res && !strlen(flaws_str)) { |
---|
512 | return (TRUE); |
---|
513 | } |
---|
514 | if (strlen(flaws_str) > 0) { |
---|
515 | sprintf(flaws_str_A, "%s", flaws_str + 1); |
---|
516 | } |
---|
517 | if (strlen(flaws_str_A) >= 74) { |
---|
518 | for (i = 74; flaws_str_A[i] != ' '; i--); |
---|
519 | strcpy(flaws_str_B, flaws_str_A + i + 1); |
---|
520 | flaws_str_A[i] = '\0'; |
---|
521 | } |
---|
522 | if (strlen(flaws_str_B) >= 74) { |
---|
523 | for (i = 74; flaws_str_B[i] != ' '; i--); |
---|
524 | strcpy(flaws_str_C, flaws_str_B + i + 1); |
---|
525 | flaws_str_B[i] = '\0'; |
---|
526 | } |
---|
527 | if (res) { |
---|
528 | return (FALSE); |
---|
529 | } else { |
---|
530 | return (TRUE); |
---|
531 | } |
---|
532 | } |
---|
533 | |
---|
534 | |
---|
535 | /** |
---|
536 | * Compare @p stringA and @p stringB. This uses an ASCII sort for everything |
---|
537 | * up to the digits on the end but a numerical sort for the digits on the end. |
---|
538 | * @param stringA The first string to compare. |
---|
539 | * @param stringB The second string to compare. |
---|
540 | * @return The same as strcmp() - <0 if A<B, 0 if A=B, >0 if A>B. |
---|
541 | * @note This function only does a numerical sort on the @e last set of numbers. If |
---|
542 | * there are any in the middle those will be sorted ASCIIbetically. |
---|
543 | */ |
---|
544 | int strcmp_inc_numbers(char *stringA, char *stringB) |
---|
545 | { |
---|
546 | /*@ int ********************************************************* */ |
---|
547 | int i; |
---|
548 | int start_of_numbers_in_A; |
---|
549 | int start_of_numbers_in_B; |
---|
550 | int res; |
---|
551 | |
---|
552 | /*@ long ******************************************************* */ |
---|
553 | long numA; |
---|
554 | long numB; |
---|
555 | |
---|
556 | /*@ end vars *************************************************** */ |
---|
557 | assert(stringA != NULL); |
---|
558 | assert(stringB != NULL); |
---|
559 | |
---|
560 | if (strlen(stringA) == strlen(stringB)) { |
---|
561 | return (strcmp(stringA, stringB)); |
---|
562 | } |
---|
563 | for (i = (int) strlen(stringA); i > 0 && isdigit(stringA[i - 1]); i--); |
---|
564 | if (i == (int) strlen(stringA)) { |
---|
565 | return (strcmp(stringA, stringB)); |
---|
566 | } |
---|
567 | start_of_numbers_in_A = i; |
---|
568 | for (i = (int) strlen(stringB); i > 0 && isdigit(stringB[i - 1]); i--); |
---|
569 | if (i == (int) strlen(stringB)) { |
---|
570 | return (strcmp(stringA, stringB)); |
---|
571 | } |
---|
572 | start_of_numbers_in_B = i; |
---|
573 | if (start_of_numbers_in_A != start_of_numbers_in_B) { |
---|
574 | return (strcmp(stringA, stringB)); |
---|
575 | } |
---|
576 | res = strncmp(stringA, stringB, (size_t) i); |
---|
577 | if (res) { |
---|
578 | return (res); |
---|
579 | } |
---|
580 | numA = atol(stringA + start_of_numbers_in_A); |
---|
581 | numB = atol(stringB + start_of_numbers_in_B); |
---|
582 | return ((int) (numA - numB)); |
---|
583 | } |
---|
584 | |
---|
585 | |
---|
586 | |
---|
587 | /** |
---|
588 | * Strip excess baggage from @p input, which should be a line from afio. |
---|
589 | * For now this copies the whole line unless it finds a set of quotes, in which case |
---|
590 | * it copies their contents only. |
---|
591 | * @param input The input line (presumably from afio). |
---|
592 | * @return The stripped line. |
---|
593 | * @note The returned string points to static storage that will be overwritten with each call. |
---|
594 | */ |
---|
595 | char *strip_afio_output_line(char *input) |
---|
596 | { |
---|
597 | /*@ buffer ****************************************************** */ |
---|
598 | static char output[MAX_STR_LEN]; |
---|
599 | |
---|
600 | /*@ pointers **************************************************** */ |
---|
601 | char *p; |
---|
602 | char *q; |
---|
603 | /*@ end vars *************************************************** */ |
---|
604 | |
---|
605 | assert(input != NULL); |
---|
606 | strcpy(output, input); |
---|
607 | p = strchr(input, '\"'); |
---|
608 | if (p) { |
---|
609 | q = strchr(++p, '\"'); |
---|
610 | if (q) { |
---|
611 | strcpy(output, p); |
---|
612 | *(strchr(output, '\"')) = '\0'; |
---|
613 | } |
---|
614 | } |
---|
615 | return (output); |
---|
616 | } |
---|
617 | |
---|
618 | |
---|
619 | /** |
---|
620 | * If there are double quotes "" around @p incoming then remove them. |
---|
621 | * This does not affect other quotes that may be embedded within the string. |
---|
622 | * @param incoming The string to trim quotes from (modified). |
---|
623 | * @return @p incoming. |
---|
624 | */ |
---|
625 | char *trim_empty_quotes(char *incoming) |
---|
626 | { |
---|
627 | /*@ buffer ****************************************************** */ |
---|
628 | static char outgoing[MAX_STR_LEN]; |
---|
629 | |
---|
630 | /*@ end vars *************************************************** */ |
---|
631 | assert(incoming != NULL); |
---|
632 | |
---|
633 | if (incoming[0] == '\"' && incoming[strlen(incoming) - 1] == '\"') { |
---|
634 | strcpy(outgoing, incoming + 1); |
---|
635 | outgoing[strlen(outgoing) - 1] = '\0'; |
---|
636 | } else { |
---|
637 | strcpy(outgoing, incoming); |
---|
638 | } |
---|
639 | return (outgoing); |
---|
640 | } |
---|
641 | |
---|
642 | |
---|
643 | |
---|
644 | |
---|
645 | /** |
---|
646 | * Remove any partition info from @p partition, leaving just the drive name. |
---|
647 | * @param partition The partition name soon-to-become drive name. (modified) |
---|
648 | * @return @p partition. |
---|
649 | */ |
---|
650 | char *truncate_to_drive_name(char *partition) |
---|
651 | { |
---|
652 | int i = strlen(partition) - 1; |
---|
653 | char *c = NULL; |
---|
654 | |
---|
655 | #ifdef __FreeBSD__ |
---|
656 | |
---|
657 | if (islower(partition[i])) // BSD subpartition |
---|
658 | i--; |
---|
659 | if (partition[i - 1] == 's') { |
---|
660 | while (isdigit(partition[i])) |
---|
661 | i--; |
---|
662 | i--; |
---|
663 | } |
---|
664 | partition[i + 1] = '\0'; |
---|
665 | |
---|
666 | #else |
---|
667 | |
---|
668 | assert_string_is_neither_NULL_nor_zerolength(partition); |
---|
669 | /* first see if it's a devfs style device */ |
---|
670 | c = strrchr(partition, '/'); |
---|
671 | if (c && strncmp(c, "/part", 5) == 0) { |
---|
672 | /* yup it's devfs, return the "disc" path */ |
---|
673 | strncpy(c + 1, "disc", (size_t)5); |
---|
674 | return partition; |
---|
675 | } |
---|
676 | |
---|
677 | for (i = strlen(partition); isdigit(partition[i - 1]); i--) |
---|
678 | continue; |
---|
679 | if (partition[i - 1] == 'p' && isdigit(partition[i - 2])) { |
---|
680 | i--; |
---|
681 | } |
---|
682 | partition[i] = '\0'; |
---|
683 | |
---|
684 | #endif |
---|
685 | |
---|
686 | return partition; |
---|
687 | } |
---|
688 | |
---|
689 | |
---|
690 | /** |
---|
691 | * Turn a RAID level number (-1 to 5) into a friendly string. The string |
---|
692 | * is either "Linear RAID" for -1, or " RAID %-2d " (%d = @p raid_level) |
---|
693 | * for anything else. |
---|
694 | * @param raid_level The RAID level to stringify. |
---|
695 | * @return The string form of @p raid_level. |
---|
696 | * @note The returned value points to static storage that will be overwritten with each call. |
---|
697 | */ |
---|
698 | char *turn_raid_level_number_to_string(int raid_level) |
---|
699 | { |
---|
700 | |
---|
701 | /*@ buffer ********************************************************** */ |
---|
702 | static char output[MAX_STR_LEN]; |
---|
703 | |
---|
704 | |
---|
705 | |
---|
706 | if (raid_level >= 0) { |
---|
707 | sprintf(output, " RAID %-2d ", raid_level); |
---|
708 | } else { |
---|
709 | sprintf(output, "Linear RAID"); |
---|
710 | } |
---|
711 | return (output); |
---|
712 | } |
---|
713 | |
---|
714 | |
---|
715 | /** |
---|
716 | * Determine the severity (1-3, 1 being low) of the fact that |
---|
717 | * @p fn changed in the live filesystem (verify/compare). |
---|
718 | * @param fn The filename that changed. |
---|
719 | * @param out_reason If non-NULL, a descriptive reason for the difference will be copied here. |
---|
720 | * @return The severity (1-3). |
---|
721 | */ |
---|
722 | int severity_of_difference(char *fn, char *out_reason) |
---|
723 | { |
---|
724 | int sev = 0; |
---|
725 | char *reason = NULL; |
---|
726 | char *filename = NULL; |
---|
727 | |
---|
728 | // out_reason might be null on purpose, so don't bomb if it is :) OK? |
---|
729 | assert_string_is_neither_NULL_nor_zerolength(fn); |
---|
730 | if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING))) { |
---|
731 | mr_asprintf(&filename, fn + strlen(MNT_RESTORING)); |
---|
732 | } else if (fn[0] != '/') { |
---|
733 | mr_asprintf(&filename, "/%s", fn); |
---|
734 | } else { |
---|
735 | mr_asprintf(&filename, fn); |
---|
736 | } |
---|
737 | |
---|
738 | if (!strncmp(filename, "/var/", 5)) { |
---|
739 | sev = 2; |
---|
740 | mr_asprintf(&reason, |
---|
741 | _("/var's contents will change regularly, inevitably.")); |
---|
742 | } |
---|
743 | if (!strncmp(filename, "/home", 5)) { |
---|
744 | sev = 2; |
---|
745 | mr_asprintf(&reason, |
---|
746 | _("It's in your /home partiton. Therefore, it is important.")); |
---|
747 | } |
---|
748 | if (!strncmp(filename, "/usr/", 5)) { |
---|
749 | sev = 3; |
---|
750 | mr_asprintf(&reason, |
---|
751 | _("You may have installed/removed software during the backup.")); |
---|
752 | } |
---|
753 | if (!strncmp(filename, "/etc/", 5)) { |
---|
754 | sev = 3; |
---|
755 | mr_asprintf(&reason, |
---|
756 | _("Do not edit config files while backing up your PC.")); |
---|
757 | } |
---|
758 | if (!strcmp(filename, "/etc/adjtime") |
---|
759 | || !strcmp(filename, "/etc/mtab")) { |
---|
760 | sev = 1; |
---|
761 | mr_asprintf(&reason, _("This file changes all the time. It's OK.")); |
---|
762 | } |
---|
763 | if (!strncmp(filename, "/root/", 6)) { |
---|
764 | sev = 3; |
---|
765 | mr_asprintf(&reason, |
---|
766 | _("Were you compiling/editing something in /root?")); |
---|
767 | } |
---|
768 | if (!strncmp(filename, "/root/.", 7)) { |
---|
769 | sev = 2; |
---|
770 | mr_asprintf(&reason, _("Temp or 'dot' files changed in /root.")); |
---|
771 | } |
---|
772 | if (!strncmp(filename, "/var/lib/", 9)) { |
---|
773 | sev = 2; |
---|
774 | mr_asprintf(&reason, _("Did you add/remove software during backing?")); |
---|
775 | } |
---|
776 | if (!strncmp(filename, "/var/lib/rpm", 12)) { |
---|
777 | sev = 3; |
---|
778 | mr_asprintf(&reason, _("Did you add/remove software during backing?")); |
---|
779 | } |
---|
780 | if (!strncmp(filename, "/var/lib/slocate", 16)) { |
---|
781 | sev = 1; |
---|
782 | mr_asprintf(&reason, |
---|
783 | _("The 'update' daemon ran during backup. This does not affect the integrity of your backup.")); |
---|
784 | } |
---|
785 | if (!strncmp(filename, "/var/log/", 9) |
---|
786 | || strstr(filename, "/.xsession") |
---|
787 | || !strcmp(filename + strlen(filename) - 4, ".log")) { |
---|
788 | sev = 1; |
---|
789 | mr_asprintf(&reason, |
---|
790 | _("Log files change frequently as the computer runs. Fret not.")); |
---|
791 | } |
---|
792 | if (!strncmp(filename, "/var/spool", 10)) { |
---|
793 | sev = 1; |
---|
794 | mr_asprintf(&reason, |
---|
795 | _("Background processes or printers were active. This does not affect the integrity of your backup.")); |
---|
796 | } |
---|
797 | if (!strncmp(filename, "/var/spool/mail", 10)) { |
---|
798 | sev = 2; |
---|
799 | mr_asprintf(&reason, _("Mail was sent/received during backup.")); |
---|
800 | } |
---|
801 | if (filename[strlen(filename) - 1] == '~') { |
---|
802 | sev = 1; |
---|
803 | mr_asprintf(&reason, |
---|
804 | _("Backup copy of another file which was modified recently.")); |
---|
805 | } |
---|
806 | if (strstr(filename, "cache")) { |
---|
807 | sev = 1; |
---|
808 | mr_asprintf(&reason, |
---|
809 | _("Part of a cache of data. Caches change from time to time. Don't worry.")); |
---|
810 | } |
---|
811 | if (!strncmp(filename, "/var/run/", 9) |
---|
812 | || !strncmp(filename, "/var/lock", 8) |
---|
813 | || strstr(filename, "/.DCOPserver") || strstr(filename, "/.MCOP") |
---|
814 | || strstr(filename, "/.Xauthority")) { |
---|
815 | sev = 1; |
---|
816 | mr_asprintf(&reason, |
---|
817 | _("Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.")); |
---|
818 | } |
---|
819 | mr_free(filename); |
---|
820 | |
---|
821 | if (sev == 0) { |
---|
822 | sev = 3; |
---|
823 | mr_asprintf(&reason, |
---|
824 | _("Changed since backup. Consider running a differential backup in a day or two.")); |
---|
825 | } |
---|
826 | if (out_reason) { |
---|
827 | strcpy(out_reason, reason); |
---|
828 | } |
---|
829 | |
---|
830 | mr_free(reason); |
---|
831 | return (sev); |
---|
832 | } |
---|
833 | |
---|
834 | |
---|
835 | /** |
---|
836 | * Compare the filenames in two filelist entries (s_filelist_entry*) casted |
---|
837 | * to void*. |
---|
838 | * @param va The first filelist entry, cast as a @c void pointer. |
---|
839 | * @param vb The second filelist entry, cast as a @c void pointer. |
---|
840 | * @return The return value of strcmp(). |
---|
841 | */ |
---|
842 | int compare_two_filelist_entries(void *va, void *vb) |
---|
843 | { |
---|
844 | static int res; |
---|
845 | struct s_filelist_entry *fa, *fb; |
---|
846 | |
---|
847 | assert(va != NULL); |
---|
848 | assert(vb != NULL); |
---|
849 | fa = (struct s_filelist_entry *) va; |
---|
850 | fb = (struct s_filelist_entry *) vb; |
---|
851 | res = strcmp(fa->filename, fb->filename); |
---|
852 | return (res); |
---|
853 | } |
---|
854 | |
---|
855 | |
---|
856 | /** |
---|
857 | * Generate a line intended to be passed to update_evalcall_form(), indicating |
---|
858 | * the current media fill percentage (or number of kilobytes if size is not known). |
---|
859 | * @param bkpinfo The backup media structure. Fields used: |
---|
860 | * - @c bkpinfo->backup_media_type |
---|
861 | * - @c bkpinfo->media_size |
---|
862 | * - @c bkpinfo->scratchdir |
---|
863 | * @return The string indicating media fill. |
---|
864 | * @note The returned string points to static storage that will be overwritten with each call. |
---|
865 | */ |
---|
866 | char *percent_media_full_comment(struct s_bkpinfo *bkpinfo) |
---|
867 | { |
---|
868 | /*@ int *********************************************** */ |
---|
869 | int percentage = 0; |
---|
870 | int j = 0; |
---|
871 | |
---|
872 | /*@ buffers ******************************************* */ |
---|
873 | static char outstr[MAX_STR_LEN]; |
---|
874 | |
---|
875 | assert(bkpinfo != NULL); |
---|
876 | |
---|
877 | if (bkpinfo->media_size[g_current_media_number] <= 0) { |
---|
878 | sprintf(outstr, _("Volume %d: %'lld kilobytes archived so far"), |
---|
879 | g_current_media_number, g_tape_posK); |
---|
880 | return (outstr); |
---|
881 | } |
---|
882 | |
---|
883 | /* update screen */ |
---|
884 | if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) { |
---|
885 | percentage = |
---|
886 | (int) (g_tape_posK / 10 / |
---|
887 | bkpinfo->media_size[g_current_media_number]); |
---|
888 | if (percentage > 100) { |
---|
889 | percentage = 100; |
---|
890 | } |
---|
891 | sprintf(outstr, _("Volume %d: ["), g_current_media_number); |
---|
892 | } else { |
---|
893 | percentage = |
---|
894 | (int) (space_occupied_by_cd(bkpinfo->scratchdir) * 100 / 1024 / |
---|
895 | bkpinfo->media_size[g_current_media_number]); |
---|
896 | sprintf(outstr, "%s %d: [", |
---|
897 | bkpinfo->backup_media_string, |
---|
898 | g_current_media_number); |
---|
899 | } |
---|
900 | for (j = 0; j < percentage; j += 5) { |
---|
901 | strcat(outstr, "*"); |
---|
902 | } |
---|
903 | for (; j < 100; j += 5) { |
---|
904 | strcat(outstr, "."); |
---|
905 | } |
---|
906 | j = (int) strlen(outstr); |
---|
907 | sprintf(outstr + j, "] %d%% used", percentage); |
---|
908 | return (outstr); |
---|
909 | } |
---|
910 | |
---|
911 | |
---|
912 | |
---|
913 | /** |
---|
914 | * Get a string form of @p type_of_bkp. |
---|
915 | * @param type_of_bkp The backup type to stringify. |
---|
916 | * @return The stringification of @p type_of_bkp. |
---|
917 | * @note The returned string points to static storage that will be overwritten with each call. |
---|
918 | */ |
---|
919 | char *media_descriptor_string(t_bkptype type_of_bkp) |
---|
920 | { |
---|
921 | static char *type_of_backup = NULL; |
---|
922 | |
---|
923 | if (!type_of_backup) { |
---|
924 | malloc_string(type_of_backup); |
---|
925 | } |
---|
926 | |
---|
927 | switch (type_of_bkp) { |
---|
928 | case dvd: |
---|
929 | strcpy(type_of_backup, "DVD"); |
---|
930 | break; |
---|
931 | case cdr: |
---|
932 | strcpy(type_of_backup, "CDR"); |
---|
933 | break; |
---|
934 | case cdrw: |
---|
935 | strcpy(type_of_backup, "CDRW"); |
---|
936 | break; |
---|
937 | case tape: |
---|
938 | strcpy(type_of_backup, "tape"); |
---|
939 | break; |
---|
940 | case cdstream: |
---|
941 | strcpy(type_of_backup, "CDR"); |
---|
942 | break; |
---|
943 | case udev: |
---|
944 | strcpy(type_of_backup, "udev"); |
---|
945 | break; |
---|
946 | case iso: |
---|
947 | strcpy(type_of_backup, "ISO"); |
---|
948 | break; |
---|
949 | case nfs: |
---|
950 | strcpy(type_of_backup, "nfs"); |
---|
951 | break; |
---|
952 | case usb: |
---|
953 | strcpy(type_of_backup, "USB"); |
---|
954 | break; |
---|
955 | default: |
---|
956 | strcpy(type_of_backup, "ISO"); |
---|
957 | } |
---|
958 | return (type_of_backup); |
---|
959 | } |
---|
960 | |
---|