Changeset 688 in MondoRescue for trunk/mondo/mondo/common/newt-specific.c


Ignore:
Timestamp:
Jul 17, 2006, 3:44:46 PM (18 years ago)
Author:
bcornec
Message:

Huge memory management patch.
Still not finished but a lot as been done.
What remains is around some functions returning strings, and some structure members.
(Could not finish due to laptop failure !)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/mondo/mondo/common/newt-specific.c

    r588 r688  
    1313#define MAX_NEWT_COMMENT_LEN 200
    1414
    15 #if __cplusplus
    16 extern "C" {
    17 #endif
     15#include <unistd.h>
    1816
    1917#include "my-stuff.h"
     
    111109        if (g_text_mode) {
    112110            while (1) {
    113                 system("sync");
     111                sync();
    114112                printf
    115113                    ("---promptdialogYN---1--- %s\r\n---promptdialogYN---Q--- [yes] [no] ---\r\n--> ",
     
    130128                    return (FALSE);
    131129                } else {
    132                     system("sync");
     130                    sync();
    133131                    printf
    134132                        (_("Please enter either YES or NO (or yes or no, or y or n, or...)\n"));
     
    156154        assert_string_is_neither_NULL_nor_zerolength(prompt);
    157155        if (g_text_mode) {
    158             system("sync");
     156            sync();
    159157            printf
    160158                ("---promptdialogOKC---1--- %s\r\n---promptdialogOKC---Q--- [OK] [Cancel] ---\r\n--> ",
     
    279277        paranoid_system("killall mindi 2> /dev/null");
    280278        kill_anything_like_this("/mondo/do-not");
    281         kill_anything_like_this("tmp.mondo");
     279        kill_anything_like_this("mondo.tmp");
    282280        kill_anything_like_this("ntfsclone");
    283281        sync();
     
    291289        paranoid_free(tmp);
    292290
    293         if (g_erase_tmpdir_and_scratchdir[0]) {
     291        if (g_erase_tmpdir_and_scratchdir) {
    294292            run_program_and_log_output(g_erase_tmpdir_and_scratchdir, 5);
    295293        }
     
    339337    void
    340338     finish(int signal) {
    341         char *command;
    342         malloc_string(command);
     339        char *command = NULL;
    343340
    344341        /*  if (signal==0) { popup_and_OK("Please press <enter> to quit."); } */
     
    349346        chdir("/");
    350347        run_program_and_log_output("umount " MNT_CDROM, FALSE);
    351         run_program_and_log_output("rm -Rf /mondo.scratch.* /tmp.mondo.*",
     348        run_program_and_log_output("rm -Rf /mondo.scratch.* /mondo.tmp.*",
    352349                                   FALSE);
    353350        if (g_erase_tmpdir_and_scratchdir) {
     
    394391
    395392        /*@ buffers ********************************************************** */
    396         char *command;
    397         char *tmp;
     393        char *command = NULL;
     394        char *tmp = NULL;
    398395
    399396        /*@ pointers ********************************************************* */
    400         FILE *fin;
     397        FILE *fin = NULL;
    401398
    402399        /*@ int ************************************************************** */
    403400        int i = 0;
     401        size_t n = 0;
    404402
    405403        assert_string_is_neither_NULL_nor_zerolength(filename);
     
    420418        } else {
    421419            for (i = 0; i < g_noof_log_lines; i++) {
    422                 for (err_log_lines[i][0] = '\0';
    423                      strlen(err_log_lines[i]) < 2 && !feof(fin);) {
    424                     (void) fgets(err_log_lines[i], MAX_NEWT_COMMENT_LEN,
    425                                  fin);
     420                for (;
     421                    strlen(err_log_lines[i]) < 2 && !feof(fin);) {
     422                    getline(&(err_log_lines[i]), &n, fin);
    426423                    strip_spaces(err_log_lines[i]);
    427424                    if (!strncmp(err_log_lines[i], "root:", 5)) {
    428425                        asprintf(&tmp, "%s", err_log_lines[i] + 6);
    429                         strcpy(err_log_lines[i], tmp);
    430                         paranoid_free(tmp);
     426                        paranoid_free(err_log_lines[i]);
     427                        err_log_lines[i] = tmp;
    431428                    }
    432429                    if (feof(fin)) {
     
    456453
    457454        /*@ buffers ********************************************************** */
    458         char *output;
     455        char *output = NULL;
    459456
    460457
     
    472469
    473470        if (err_log_lines) {
     471            paranoid_free(&err_log_lines[0]);
    474472            for (i = 1; i < g_noof_log_lines; i++) {
    475                 strcpy(err_log_lines[i - 1],
    476                        "                                                                                ");
    477                 strcpy(err_log_lines[i - 1], err_log_lines[i]);
     473                err_log_lines[i - 1] = err_log_lines[i];
    478474            }
    479475        }
     
    487483        }
    488484        if (err_log_lines)
    489             strcpy(err_log_lines[g_noof_log_lines - 1], output);
     485            err_log_lines[g_noof_log_lines - 1] = output;
    490486        if (g_text_mode) {
    491487            printf("%s\n", output);
     
    493489            refresh_log_screen();
    494490        }
    495         paranoid_free(output);
    496     }
    497 
    498 
     491    }
    499492
    500493
     
    670663 * @param b The blurb (e.g. what you want the user to enter).
    671664 * @param output The string to put the user's answer in. It has to be freed by the caller
    672  * @param maxsize The size in bytes allocated to @p output.
    673665 * @return TRUE if the user pressed OK, FALSE if they pressed Cancel.
    674666 */
    675     bool popup_and_get_string(char *title, char *b, char *output,
    676                               int maxsize) {
     667    bool popup_and_get_string(char *title, char *b, char *output) {
    677668
    678669        /*@ newt ************************************************************ */
     
    685676
    686677        /*@ pointers ********************************************************* */
    687         char *entry_value;
     678        char *entry_value = NULL;
    688679
    689680        /*@ buffers ********************************************************** */
    690         char *blurb;
    691         char *original_contents;
     681        char *blurb = NULL;
    692682        size_t n = 0;
     683        bool ret = TRUE;
    693684
    694685        assert_string_is_neither_NULL_nor_zerolength(title);
     
    699690                ("---promptstring---1--- %s\r\n---promptstring---2--- %s\r\n---promptstring---Q---\r\n-->  ",
    700691                 title, b);
     692            paranoid_free(output);
    701693            (void) getline(&output, &n, stdin);
    702694            if (output[strlen(output) - 1] == '\n')
    703695                output[strlen(output) - 1] = '\0';
    704             return (TRUE);
     696            return (ret);
    705697        }
    706698        asprintf(&blurb, b);
    707699        text = newtTextboxReflowed(2, 1, blurb, 48, 5, 5, 0);
    708         original_contents = output;
    709700
    710701        type_here =
    711702            newtEntry(2, newtTextboxGetNumLines(text) + 2,
    712                       original_contents, 50,
    713 #ifdef __cplusplus
    714                       0, NEWT_FLAG_RETURNEXIT
    715 #else
    716                       (void *) &entry_value, NEWT_FLAG_RETURNEXIT
    717 #endif
     703                      output, 50,
     704                      &entry_value, NEWT_FLAG_RETURNEXIT
    718705            );
    719706        b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, _("  OK  "));
    720707        b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, _("Cancel"));
    721         //  newtOpenWindow (8, 5, 54, newtTextboxGetNumLines (text) + 9, title);
    722708        newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
    723709        myForm = newtForm(NULL, NULL, 0);
     
    727713        newtPushHelpLine(blurb);
    728714        paranoid_free(blurb);
     715
    729716        b_res = newtRunForm(myForm);
    730         output = entry_value;
    731717        newtPopHelpLine();
     718        if (b_res == b_2) {
     719            ret = FALSE;
     720        } else {
     721            // Copy entry_value before destroying the form
     722            // clearing potentially output before
     723            paranoid_alloc(output,entry_value);
     724        }
    732725        newtFormDestroy(myForm);
    733726        newtPopWindow();
    734         if (b_res == b_2) {
    735             paranoid_free(output);
    736             output = original_contents;
    737             paranoid_free(original_contents);
    738             return (FALSE);
    739         } else {
    740             paranoid_free(original_contents);
    741             return (TRUE);
    742         }
     727        return(ret);
    743728    }
    744729
     
    846831        newtRefresh();
    847832        for (i = g_noof_log_lines - 1; i >= 0; i--) {
    848             err_log_lines[i][79] = '\0';
     833            //BERLIOS : removed for now, Think it's useless : err_log_lines[i][79] = '\0';
    849834            newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
    850835                             err_log_lines[i]);
     
    884869
    885870        for (i = 0; i < g_noof_log_lines; i++) {
    886             err_log_lines[i] = (char *) malloc(MAX_NEWT_COMMENT_LEN);
    887             if (!err_log_lines[i]) {
    888                 fatal_error("Out of memory");
    889             }
    890         }
    891 
    892         for (i = 0; i < g_noof_log_lines; i++) {
    893             err_log_lines[i][0] = '\0';
     871            err_log_lines[i] = NULL;
    894872        }
    895873    }
     
    15751553
    15761554
    1577 #if __cplusplus
    1578 }                               /* extern "C" */
    1579 #endif
    1580 
    1581 
    15821555void wait_until_software_raids_are_prepped(char *mdstat_file,
    15831556                                           int wait_for_percentage);
Note: See TracChangeset for help on using the changeset viewer.