Ignore:
Timestamp:
Jul 24, 2009, 9:19:38 PM (16 years ago)
Author:
Bruno Cornec
Message:

find_tape_device_and_size => mr_find_tape_device which allocates 1 string to be freed by caller

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.10/mondo/src/common/libmondo-stream.c

    r2296 r2303  
    250250
    251251/**
    252  * Determine the name and size of the tape device. Tries the SCSI tape for
     252 * Determine the name of the tape device. Tries the SCSI tape for
    253253 * this platform, then the IDE tape, then "/dev/st0", then "/dev/osst0".
    254  * @param dev Where to put the found tape device.
    255  * @param siz Where to put the tape size (a string like "4GB")
    256  * @return 0 if success, nonzero if failure (in which @p dev and @p siz are undefined).
    257  */
    258 int find_tape_device_and_size(char *dev, char *siz)
    259 {
    260     char tmp[MAX_STR_LEN];
     254 * @return the allocated tape string if success, NULL if failure
     255 */
     256char *mr_find_tape_device(void)
     257{
     258    char *tmp = NULL;
    261259    char *command = NULL;
    262     char cdr_exe[MAX_STR_LEN];
     260    char *cdr_exe = NULL;
     261    char *dev = NULL;
    263262    int res;
    264263
    265264    log_to_screen("I am looking for your tape streamer. Please wait.");
    266     dev[0] = '\0';
    267     if (siz != NULL) {
    268         siz[0] = '\0';
    269     }
    270265    if (find_home_of_exe("cdrecord")) {
    271         strcpy(cdr_exe, "cdrecord");
    272     } else {
    273         strcpy(cdr_exe, "dvdrecord");
     266        mr_asprintf(&cdr_exe, "cdrecord");
     267    } else {
     268        mr_asprintf(&cdr_exe, "dvdrecord");
    274269    }
    275270    mr_asprintf(&command, "%s -scanbus 2> /dev/null | grep -i tape | wc -l", cdr_exe);
    276     strcpy(tmp, call_program_and_get_last_line_of_output(command));
     271    mr_asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
    277272    mr_free(command);
    278273
    279274    if (atoi(tmp) != 1) {
    280         log_it
    281             ("Either too few or too many tape streamers for me to detect...");
    282         strcpy(dev, VANILLA_SCSI_TAPE);
    283         return 1;
    284     }
     275        log_it("Either too few or too many tape streamers for me to detect...");
     276        mr_asprintf(&dev, "%s", VANILLA_SCSI_TAPE);
     277        mr_free(tmp);
     278        return(dev);
     279    }
     280    mr_free(tmp);
     281
    285282    mr_asprintf(&command, "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep -i TAPE | cut -d' ' -f2 | head -n1", cdr_exe);
    286     strcpy(tmp, call_program_and_get_last_line_of_output(command));
     283    mr_asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
    287284    mr_free(command);
    288285
    289286    if (strlen(tmp) < 2) {
    290287        log_it("Could not find tape device");
    291         return 1;
    292     }
     288        mr_free(tmp);
     289        return(dev);
     290    }
     291    mr_free(tmp);
    293292    mr_asprintf(&command, "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep -i TAPE | cut -d' ' -f3 | cut -d')' -f1 | head -n1", cdr_exe);
    294     strcpy(tmp, call_program_and_get_last_line_of_output(command));
     293    mr_free(cdr_exe);
     294
     295    mr_asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
    295296    mr_free(command);
    296297
    297     strcpy(dev, VANILLA_SCSI_TAPE);
     298    mr_asprintf(&dev, "%s", VANILLA_SCSI_TAPE);
    298299    dev[strlen(dev) - 1] = '\0';
    299     strcat(dev, tmp);           // e.g. '/dev/st0' becomes '/dev/stN'
     300    mr_strcat(dev, tmp);            // e.g. '/dev/st0' becomes '/dev/stN'
     301    mr_free(tmp);
     302
    300303    res = 0;
    301304    if (!mt_says_tape_exists(dev)) {
    302         strcpy(dev, ALT_TAPE);
     305        mr_free(dev);
     306        mr_asprintf(&dev, "%s", ALT_TAPE);
    303307        if (!mt_says_tape_exists(dev)) {
    304308            log_it("Cannot openin %s", dev);
    305             strcpy(dev, "/dev/st0");
     309            mr_free(dev);
     310            mr_asprintf(&dev, "/dev/st0");
    306311            if (!mt_says_tape_exists(dev)) {
    307312                log_it("Cannot openin %s", dev);
    308                 strcpy(dev, "/dev/osst0");
     313                mr_free(dev);
     314                mr_asprintf(&dev, "/dev/osst0");
    309315                if (!mt_says_tape_exists(dev)) {
    310316                    res++;
     
    318324    log_it("At this point, dev = %s and res = %d", dev, res);
    319325
    320     strcpy(tmp, call_program_and_get_last_line_of_output("\
    321 cdrecord -scanbus 2> /dev/null | tr -s '\t' ' ' | \
    322 grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep -i TAPE | \
    323 awk '{for(i=1; i<NF; i++) { if (index($i, \"GB\")>0) { print $i;};};};'"));
     326    mr_asprintf(&tmp, call_program_and_get_last_line_of_output("cdrecord -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep -i TAPE | awk '{for(i=1; i<NF; i++) { if (index($i, \"GB\")>0) { print $i;};};};'"));
    324327
    325328    if (mt_says_tape_exists(dev)) {
     
    327330    } else {
    328331        log_it("Turning %s", dev);
    329         strcpy(tmp, (strrchr(dev, '/') != NULL) ? strrchr(dev, '/') : dev);
    330         sprintf(dev, "/dev/os%s", tmp);
     332        mr_free(tmp);
     333        mr_asprintf(&tmp, (strrchr(dev, '/') != NULL) ? strrchr(dev, '/') : dev);
     334        mr_free(dev);
     335        mr_asprintf(&dev, "/dev/os%s", tmp);
    331336        log_it("...into %s", dev);
    332337        if (mt_says_tape_exists(dev)) {
     
    340345
    341346    if (res) {
    342         return (res);
     347        mr_free(tmp);
     348        return (dev);
    343349    }
    344350
    345351    if (strlen(tmp) < 2) {
    346352        log_it("Warning - size of tape unknown");
    347         return (0);
    348     } else {
    349         if (siz != NULL) {
    350             strcpy(siz, tmp);
    351         }
    352         return (0);
    353     }
    354 }
    355 
    356 
    357 
    358 
     353    }
     354    mr_free(tmp);
     355    return (NULL);
     356}
    359357
    360358
Note: See TracChangeset for help on using the changeset viewer.