static int open_output(void) { dpm.encoding = validate_encoding(dpm.encoding, 0, 0); if(dpm.name == NULL) { dpm.flag |= PF_AUTO_SPLIT_FILE; dpm.name = NULL; } else { dpm.flag &= ~PF_AUTO_SPLIT_FILE; if((dpm.fd = raw_output_open(dpm.name)) == -1) return -1; } return 0; }
static int auto_raw_output_open(const char *input_filename) { char *output_filename = (char *)safe_malloc(strlen(input_filename) + 5); char *ext, *p; strcpy(output_filename, input_filename); if((ext = strrchr(output_filename, '.')) == NULL) ext = output_filename + strlen(output_filename); else { /* strip ".gz" */ if(strcasecmp(ext, ".gz") == 0) { *ext = '\0'; if((ext = strrchr(output_filename, '.')) == NULL) ext = output_filename + strlen(output_filename); } } /* replace '.' and '#' before ext */ for(p = output_filename; p < ext; p++) if(*p == '.' || *p == '#') *p = '_'; if(*ext && isupper(*(ext + 1))) { strcpy(ext, encoding_ext(dpm.encoding)); ext++; while(*ext) { if(islower(*ext)) *ext = toupper(*ext); ext++; } } else strcpy(ext, encoding_ext(dpm.encoding)); if((dpm.fd = raw_output_open(output_filename)) < 0) { free(output_filename); return -1; } if(dpm.name != NULL) free(dpm.name); dpm.name = output_filename; ctl->cmsg(CMSG_INFO, VERB_NORMAL, "Output %s", dpm.name); return 0; }