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

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

merge -r489:506 $SVN_M/branches/stable

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