create dst file, overwriting one if it already exists */
| 513 | |
| 514 | /* create dst file, overwriting one if it already exists */ |
| 515 | static NHFILE * |
| 516 | create_dstfile(char *fnam, enum saveformats mystyle) |
| 517 | { |
| 518 | int fd, ret; |
| 519 | unsigned ln = 0; |
| 520 | FILE *cf; |
| 521 | NHFILE *nhfp = (NHFILE *) 0; |
| 522 | const char *fq_name; |
| 523 | char dstfnam[2048]; |
| 524 | char *dsttmp; |
| 525 | boolean dst_file_exists = FALSE, ren_file_exists = FALSE; |
| 526 | |
| 527 | nhUse(ret); |
| 528 | Snprintf(dstfnam, sizeof dstfnam, "%s", fnam); |
| 529 | if ((cf = fopen(dstfnam, RDBMODE)) != (FILE *) 0) { |
| 530 | dst_file_exists = TRUE; |
| 531 | (void) fclose(cf); |
| 532 | } |
| 533 | |
| 534 | if (dst_file_exists) { |
| 535 | if (chosen_unconvert) { |
| 536 | ln = strlen(fnam); |
| 537 | if (renidx >= 0) { |
| 538 | ln += strlen(rensuffixes[renidx]) + 1; /* +1 for '.' */ |
| 539 | } else if (renidx == -2) { |
| 540 | ln += strlen(thisdatamodel) + 1; /* +1 for '.' */ |
| 541 | } else { |
| 542 | ln += strlen(thisdatamodel) + 1 |
| 543 | + 4; /* +1 for '.'; +4 for "not_" */ |
| 544 | } |
| 545 | dsttmp = (char *) alloc(ln + 1); |
| 546 | Strcpy(dsttmp, fnam); |
| 547 | Strcat(dsttmp, "."); |
| 548 | if (renidx >= 0) { |
| 549 | Strcat(dsttmp, rensuffixes[renidx]); |
| 550 | } else if (renidx == -2) { |
| 551 | Strcat(dsttmp, thisdatamodel); |
| 552 | } else { |
| 553 | Strcat(dsttmp, "not_"); |
| 554 | Strcat(dsttmp, thisdatamodel); |
| 555 | } |
| 556 | if ((cf = fopen(dsttmp, RDBMODE)) != (FILE *) 0) { |
| 557 | ren_file_exists = TRUE; |
| 558 | (void) fclose(cf); |
| 559 | } |
| 560 | if (ren_file_exists) { |
| 561 | (void) unlink(dsttmp); |
| 562 | } |
| 563 | ret = rename(fnam, dsttmp); |
| 564 | free((genericptr_t) dsttmp), dsttmp = 0; |
| 565 | } else { |
| 566 | if ((cf = fopen(dstfnam, RDBMODE)) != (FILE *) 0) { |
| 567 | ren_file_exists = TRUE; |
| 568 | (void) fclose(cf); |
| 569 | } |
| 570 | if (ren_file_exists) { |
| 571 | (void) unlink(dstfnam); |
| 572 | } |
no test coverage detected