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