source: MondoRescue/trunk/mondo/mondo/common/libmondo-string.c@ 89

Last change on this file since 89 was 89, checked in by bcornec, 18 years ago

merge r87:88 of the 2.04_berlios branch
indent some files

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