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

Last change on this file since 1 was 1, checked in by bcornec, 14 years ago

Initial import from latest mondo-2.04_cvs_20050503/mindi-1.04_cvs_20050503 on http://www.mondorescue.org

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