Changeset 1178 in MondoRescue for branches/stable/mondo/src/common/libmondo-string.c
- Timestamp:
- Feb 17, 2007, 2:27:26 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mondo/src/common/libmondo-string.c
r1168 r1178 1 /* $Id$ */ 2 1 /* 2 $Id$ 3 */ 3 4 4 5 /** … … 14 15 #include "libmondo-string.h" 15 16 #include "libmondo-files-EXT.h" 16 #include " libmondo-gui-EXT.h"17 #include "newt-specific-EXT.h" 17 18 #include "libmondo-tools-EXT.h" 19 #include <math.h> 18 20 19 21 /*@unused@*/ … … 22 24 extern int g_current_media_number; 23 25 extern long long g_tape_posK; 24 25 26 26 27 /** … … 38 39 char *build_partition_name(char *partition, const char *drive, int partno) 39 40 { 40 char *p, *c; 41 char *p = NULL; 42 char *c = NULL; 41 43 42 44 assert(partition != NULL); … … 49 51 if (c && strncmp(c, "/disc", 5) == 0) { 50 52 /* yup it's devfs, return the "part" path */ 53 /* format /dev/.../disc */ 51 54 strcpy(c + 1, "part"); 52 55 p = c + 5; … … 67 70 68 71 69 70 71 72 73 74 75 76 77 78 79 80 72 /** 81 73 * Pad a string on both sides so it appears centered. … … 85 77 void center_string(char *in_out, int width) 86 78 { 87 char scratch[MAX_STR_LEN]; 88 char *p; 89 int i; /* purpose */ 90 int len; /* purpose */ 91 int mid; /* purpose */ 92 int x; /* purpose */ 79 char *scratch = NULL; 80 char *out = NULL; 81 char *p = NULL; 82 int i = 0; /* purpose */ 83 int len = 0; /* purpose */ 84 int mid = 0; /* purpose */ 85 int x = 0; /* purpose */ 93 86 94 87 assert(in_out != NULL); … … 98 91 return; 99 92 } 100 for (p = in_out; *p == ' '; p++); 101 strcpy(scratch, p); 102 mr_strip_spaces (scratch); 93 mr_asprintf(&scratch, in_out); 94 mr_strip_spaces(scratch); 103 95 len = (int) strlen(scratch); 104 96 mid = width / 2; … … 129 121 *q = *p; // for the final '\0' 130 122 } 131 132 133 134 /**135 * Add commas every third place in @p input.136 * @param input The string to commarize.137 * @return The string with commas.138 * @note The returned string points to static storage that will be overwritten with each call.139 */140 char *commarize(char *input)141 {142 char pos_w_commas[MAX_STR_LEN];143 static char output[MAX_STR_LEN];144 char tmp[MAX_STR_LEN];145 int j;146 147 assert(input != NULL);148 149 strcpy(tmp, input);150 if (strlen(tmp) > 6) {151 strcpy(pos_w_commas, tmp);152 j = (int) strlen(pos_w_commas);153 tmp[j - 6] = ',';154 strcpy(tmp + j - 5, pos_w_commas + j - 6);155 // tmp[j-2] = ',';156 // strcpy(tmp+j-1, pos_w_commas+j-3);157 strcpy(pos_w_commas, tmp);158 }159 if (strlen(tmp) > 3) {160 j = (int) strlen(tmp);161 strcpy(pos_w_commas, tmp);162 pos_w_commas[j - 3] = ',';163 strcpy(pos_w_commas + j - 2, tmp + j - 3);164 } else {165 strcpy(pos_w_commas, tmp);166 }167 strcpy(output, pos_w_commas);168 return (output);169 }170 171 172 173 174 175 176 177 123 178 124 … … 201 147 202 148 203 204 205 206 149 /** 207 150 * Turn a "friendly" sizestring into a number of megabytes. … … 213 156 long friendly_sizestr_to_sizelong(char *incoming) 214 157 { 215 long outval ;216 int i ;217 char *tmp ;218 char ch ;158 long outval = 0L; 159 int i = 0; 160 char *tmp = NULL; 161 char ch = ' '; 219 162 220 163 assert_string_is_neither_NULL_nor_zerolength(incoming); 221 164 222 malloc_string(tmp);223 165 if (!incoming[0]) { 224 mr_free(tmp); 225 return (0); 166 return(0L); 226 167 } 227 168 if (strchr(incoming, '.')) { 228 169 fatal_error("Please use integers only. No decimal points."); 229 170 } 230 strcpy(tmp, incoming);171 mr_asprintf(&tmp, "%s", incoming); 231 172 i = (int) strlen(tmp); 232 173 if (tmp[i - 1] == 'B' || tmp[i - 1] == 'b') { … … 237 178 tmp[i] = '\0'; 238 179 outval = atol(tmp); 180 mr_free(tmp); 181 239 182 if (ch == 'g' || ch == 'G') { 240 183 outval = outval * 1024; … … 249 192 ("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?!?!"); 250 193 popup_and_OK 251 ( "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 :-)");194 (_("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 :-)")); 252 195 fatal_error("Integer overflow."); 253 196 } else if (ch != 'm' && ch != 'M') { 254 sprintf(tmp, "Re: parameter '%s' - bad multiplier ('%c')",255 incoming, ch);197 mr_asprintf(&tmp, "Re: parameter '%s' - bad multiplier ('%c')", 198 incoming, ch); 256 199 fatal_error(tmp); 257 200 } 258 mr_free(tmp);259 201 return (outval); 260 202 } 261 262 263 264 /**265 * Add spaces to the right of @p incoming to make it @p width characters wide.266 * @param incoming The string to left-pad.267 * @param width The width to pad it to.268 * @return The left-padded string.269 * @note The returned string points to static storage that will be overwritten with each call.270 * @bug Why does center_string() modify its argument but leftpad_string() returns a modified copy?271 */272 char *leftpad_string(char *incoming, int width)273 {274 /*@ buffers ***************************************************** */275 static char output[MAX_STR_LEN];276 277 /*@ ints ******************************************************** */278 int i;279 280 /*@ end vars **************************************************** */281 assert(incoming != NULL);282 assert(width > 2);283 284 strcpy(output, incoming);285 for (i = (int) strlen(output); i < width; i++) {286 output[i] = ' ';287 }288 output[i] = '\0';289 return (output);290 }291 292 203 293 204 … … 374 285 return (outstr); 375 286 } 376 377 378 287 379 288 … … 405 314 406 315 407 408 409 410 411 316 /** 412 317 * Generate a friendly string containing "X blah blah disk(s)" … … 424 329 425 330 /*@ char ******************************************************** */ 426 char p ;331 char p = NULL; 427 332 428 333 assert(label != NULL); … … 495 400 return (output); 496 401 } 497 498 499 402 500 403 … … 530 433 mr_free(input); 531 434 } 532 533 534 535 435 536 436 … … 555 455 /*@ buffers **************************************************** */ 556 456 static char output[MAX_STR_LEN]; 557 static char suffix[MAX_STR_LEN];457 char *suffix = NULL; 558 458 559 459 /*@ end vars *************************************************** */ … … 561 461 assert_string_is_neither_NULL_nor_zerolength(path); 562 462 if (s[0] != '\0') { 563 sprintf(suffix, ".%s", s);463 mr_asprintf(&suffix, ".%s", s); 564 464 } else { 565 suffix[0] = '\0';465 mr_asprintf(&suffix, ""); 566 466 } 567 467 sprintf(output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno, sliceno, 568 468 suffix); 469 mr_free(suffix); 569 470 return (output); 570 471 } … … 597 498 598 499 599 600 601 500 /** 602 501 * Wrap @p flaws_str across three lines. The first two are no more than 74 characters wide. … … 650 549 651 550 652 653 551 /** 654 552 * Compare @p stringA and @p stringB. This uses an ASCII sort for everything … … 698 596 numA = atol(stringA + start_of_numbers_in_A); 699 597 numB = atol(stringB + start_of_numbers_in_B); 700 /*701 sprintf(tmp,"Comparing %s and %s --> %ld,%ld\n",stringA,stringB,numA,numB);702 log_to_screen(tmp);703 */704 598 return ((int) (numA - numB)); 705 599 } … … 773 667 { 774 668 int i = strlen(partition) - 1; 775 char *c ;669 char *c = NULL; 776 670 777 671 #ifdef __FreeBSD__ … … 793 687 if (c && strncmp(c, "/part", 5) == 0) { 794 688 /* yup it's devfs, return the "disc" path */ 795 str cpy(c + 1, "disc");689 strncpy(c + 1, "disc", (size_t)5); 796 690 return partition; 797 691 } … … 808 702 return partition; 809 703 } 810 811 812 813 704 814 705 … … 838 729 839 730 840 841 842 843 844 845 846 847 731 /** 848 732 * Determine the severity (1-3, 1 being low) of the fact that … … 854 738 int severity_of_difference(char *fn, char *out_reason) 855 739 { 856 int sev; 857 char *reason; 858 char *filename; 859 860 malloc_string(reason); 861 malloc_string(filename); 862 // out_reason might be null on purpose, so don't bomb if it is :) OK? 740 int sev = 0; 741 char *reason = NULL; 742 char *filename = NULL; 743 744 // out_reason might be null on purpose, so don't bomb if it is :) OK? 863 745 assert_string_is_neither_NULL_nor_zerolength(fn); 864 746 if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING))) { 865 strcpy(filename, fn + strlen(MNT_RESTORING));747 mr_asprintf(&filename, fn + strlen(MNT_RESTORING)); 866 748 } else if (fn[0] != '/') { 867 sprintf(filename, "/%s", fn);749 mr_asprintf(&filename, "/%s", fn); 868 750 } else { 869 strcpy(filename, fn); 870 } 871 872 sev = 3; 873 sprintf(reason, 874 "Changed since backup. Consider running a differential backup in a day or two."); 751 mr_asprintf(&filename, fn); 752 } 753 875 754 if (!strncmp(filename, "/var/", 5)) { 876 755 sev = 2; 877 sprintf(reason,878 "/var's contents will change regularly, inevitably.");756 mr_asprintf(&reason, 757 _("/var's contents will change regularly, inevitably.")); 879 758 } 880 759 if (!strncmp(filename, "/home", 5)) { 881 760 sev = 2; 882 sprintf(reason,883 "It's in your /home partiton. Therefore, it is important.");761 mr_asprintf(&reason, 762 _("It's in your /home partiton. Therefore, it is important.")); 884 763 } 885 764 if (!strncmp(filename, "/usr/", 5)) { 886 765 sev = 3; 887 sprintf(reason,888 "You may have installed/removed software during the backup.");766 mr_asprintf(&reason, 767 _("You may have installed/removed software during the backup.")); 889 768 } 890 769 if (!strncmp(filename, "/etc/", 5)) { 891 770 sev = 3; 892 sprintf(reason,893 "Do not edit config files while backing up your PC.");771 mr_asprintf(&reason, 772 _("Do not edit config files while backing up your PC.")); 894 773 } 895 774 if (!strcmp(filename, "/etc/adjtime") 896 775 || !strcmp(filename, "/etc/mtab")) { 897 776 sev = 1; 898 sprintf(reason, "This file changes all the time. It's OK.");777 mr_asprintf(&reason, _("This file changes all the time. It's OK.")); 899 778 } 900 779 if (!strncmp(filename, "/root/", 6)) { 901 780 sev = 3; 902 sprintf(reason, "Were you compiling/editing something in /root?"); 781 mr_asprintf(&reason, 782 _("Were you compiling/editing something in /root?")); 903 783 } 904 784 if (!strncmp(filename, "/root/.", 7)) { 905 785 sev = 2; 906 sprintf(reason, "Temp or 'dot' files changed in /root.");786 mr_asprintf(&reason, _("Temp or 'dot' files changed in /root.")); 907 787 } 908 788 if (!strncmp(filename, "/var/lib/", 9)) { 909 789 sev = 2; 910 sprintf(reason, "Did you add/remove software during backing?");790 mr_asprintf(&reason, _("Did you add/remove software during backing?")); 911 791 } 912 792 if (!strncmp(filename, "/var/lib/rpm", 12)) { 913 793 sev = 3; 914 sprintf(reason, "Did you add/remove software during backing?");794 mr_asprintf(&reason, _("Did you add/remove software during backing?")); 915 795 } 916 796 if (!strncmp(filename, "/var/lib/slocate", 16)) { 917 797 sev = 1; 918 sprintf(reason,919 "The 'update' daemon ran during backup. This does not affect the integrity of your backup.");798 mr_asprintf(&reason, 799 _("The 'update' daemon ran during backup. This does not affect the integrity of your backup.")); 920 800 } 921 801 if (!strncmp(filename, "/var/log/", 9) … … 923 803 || !strcmp(filename + strlen(filename) - 4, ".log")) { 924 804 sev = 1; 925 sprintf(reason,926 "Log files change frequently as the computer runs. Fret not.");805 mr_asprintf(&reason, 806 _("Log files change frequently as the computer runs. Fret not.")); 927 807 } 928 808 if (!strncmp(filename, "/var/spool", 10)) { 929 809 sev = 1; 930 sprintf(reason,931 "Background processes or printers were active. This does not affect the integrity of your backup.");810 mr_asprintf(&reason, 811 _("Background processes or printers were active. This does not affect the integrity of your backup.")); 932 812 } 933 813 if (!strncmp(filename, "/var/spool/mail", 10)) { 934 814 sev = 2; 935 sprintf(reason, "Mail was sent/received during backup.");815 mr_asprintf(&reason, _("Mail was sent/received during backup.")); 936 816 } 937 817 if (filename[strlen(filename) - 1] == '~') { 938 818 sev = 1; 939 sprintf(reason,940 "Backup copy of another file which was modified recently.");819 mr_asprintf(&reason, 820 _("Backup copy of another file which was modified recently.")); 941 821 } 942 822 if (strstr(filename, "cache")) { 943 823 sev = 1; 944 sprintf(reason,945 "Part of a cache of data. Caches change from time to time. Don't worry.");824 mr_asprintf(&reason, 825 _("Part of a cache of data. Caches change from time to time. Don't worry.")); 946 826 } 947 827 if (!strncmp(filename, "/var/run/", 9) … … 950 830 || strstr(filename, "/.Xauthority")) { 951 831 sev = 1; 952 sprintf(reason, 953 "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence."); 832 mr_asprintf(&reason, 833 _("Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.")); 834 } 835 mr_free(filename); 836 837 if (sev == 0) { 838 sev = 3; 839 mr_asprintf(&reason, 840 _("Changed since backup. Consider running a differential backup in a day or two.")); 954 841 } 955 842 if (out_reason) { 956 843 strcpy(out_reason, reason); 957 844 } 958 mr_free(filename); 845 959 846 mr_free(reason); 960 847 return (sev); 961 848 } 962 963 849 964 850 … … 982 868 return (res); 983 869 } 984 985 986 987 988 989 870 990 871 … … 1002 883 { 1003 884 /*@ int *********************************************** */ 1004 int percentage; 1005 int j; 885 int percentage = 0; 886 int i = 0; 887 int j = 0; 1006 888 1007 889 /*@ buffers ******************************************* */ 1008 890 static char outstr[MAX_STR_LEN]; 1009 char *pos_w_commas, *tmp;1010 891 1011 892 assert(bkpinfo != NULL); 1012 malloc_string(pos_w_commas); 1013 malloc_string(tmp); 1014 sprintf(tmp, "%lld", g_tape_posK); 1015 strcpy(pos_w_commas, commarize(tmp)); 1016 1017 1018 1019 if (bkpinfo->media_size[g_current_media_number] <= 0) 1020 // { fatal_error( "percentage_media_full_comment() - unknown media size"); } 1021 { 1022 sprintf(outstr, "Volume %d: %s kilobytes archived so far", 1023 g_current_media_number, pos_w_commas); 893 894 if (bkpinfo->media_size[g_current_media_number] <= 0) { 895 sprintf(outstr, _("Volume %d: %'lld kilobytes archived so far"), 896 g_current_media_number, g_tape_posK); 1024 897 return (outstr); 1025 898 } 1026 899 1027 /* update screen */900 /* update screen */ 1028 901 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) { 1029 902 percentage = … … 1033 906 percentage = 100; 1034 907 } 1035 sprintf(outstr, "Volume %d: [", g_current_media_number);908 sprintf(outstr, _("Volume %d: ["), g_current_media_number); 1036 909 } else { 1037 910 percentage = … … 1039 912 bkpinfo->media_size[g_current_media_number]); 1040 913 sprintf(outstr, "%s %d: [", 1041 media_descriptor_string(bkpinfo->backup_media_type),914 bkpinfo->backup_media_string, 1042 915 g_current_media_number); 1043 916 } … … 1050 923 j = (int) strlen(outstr); 1051 924 sprintf(outstr + j, "] %d%% used", percentage); 1052 mr_free(pos_w_commas);1053 mr_free(tmp);1054 925 return (outstr); 1055 926 } 927 928 1056 929 1057 930 /** … … 1063 936 char *media_descriptor_string(t_bkptype type_of_bkp) 1064 937 { 1065 static char *type_of_backup = NULL; 1066 1067 if (!type_of_backup) { 1068 malloc_string(type_of_backup); 1069 } 1070 1071 switch (type_of_bkp) { 1072 case dvd: 1073 strcpy(type_of_backup, "DVD"); 1074 break; 1075 case cdr: 1076 strcpy(type_of_backup, "CDR"); 1077 break; 1078 case cdrw: 1079 strcpy(type_of_backup, "CDRW"); 1080 break; 1081 case tape: 1082 strcpy(type_of_backup, "tape"); 1083 break; 1084 case cdstream: 1085 strcpy(type_of_backup, "CDR"); 1086 break; 1087 case udev: 1088 strcpy(type_of_backup, "udev"); 1089 break; 1090 case iso: 1091 strcpy(type_of_backup, "ISO"); 1092 break; 1093 case nfs: 1094 strcpy(type_of_backup, "nfs"); 1095 break; 1096 case usb: 1097 strcpy(type_of_backup, "USB"); 1098 break; 1099 default: 1100 strcpy(type_of_backup, "ISO"); 1101 } 1102 return (type_of_backup); 1103 } 938 static char *type_of_backup = NULL; 939 940 if (!type_of_backup) { 941 malloc_string(type_of_backup); 942 } 943 944 switch (type_of_bkp) { 945 case dvd: 946 strcpy(type_of_backup, "DVD"); 947 break; 948 case cdr: 949 strcpy(type_of_backup, "CDR"); 950 break; 951 case cdrw: 952 strcpy(type_of_backup, "CDRW"); 953 break; 954 case tape: 955 strcpy(type_of_backup, "tape"); 956 break; 957 case cdstream: 958 strcpy(type_of_backup, "CDR"); 959 break; 960 case udev: 961 strcpy(type_of_backup, "udev"); 962 break; 963 case iso: 964 strcpy(type_of_backup, "ISO"); 965 break; 966 case nfs: 967 strcpy(type_of_backup, "nfs"); 968 break; 969 case usb: 970 strcpy(type_of_backup, "USB"); 971 break; 972 default: 973 strcpy(type_of_backup, "ISO"); 974 } 975 return (type_of_backup); 976 } 977
Note:
See TracChangeset
for help on using the changeset viewer.