diff options
| author | Ron Yorston <rmy@pobox.com> | 2023-04-27 21:00:00 +0100 |
|---|---|---|
| committer | Ron Yorston <rmy@pobox.com> | 2023-04-27 21:00:00 +0100 |
| commit | e790046619f539356b950a5b07cba2f477401b4b (patch) | |
| tree | d4afee3a50f38654ded864d208650f75a17242ec /miscutils | |
| parent | 5eb812d75edd830fc263a04c69f4849d3336a743 (diff) | |
| download | busybox-w32-e790046619f539356b950a5b07cba2f477401b4b.tar.gz busybox-w32-e790046619f539356b950a5b07cba2f477401b4b.tar.bz2 busybox-w32-e790046619f539356b950a5b07cba2f477401b4b.zip | |
iconv: use temporary file for output
On Linux iconv allows the output file specified with '-o' to be
the same as the input file. Do the same for our version by creating
a temporary output file and renaming it on completion.
Costs 64-88 bytes.
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/iconv.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/miscutils/iconv.c b/miscutils/iconv.c index ec3c0c92d..debb45704 100644 --- a/miscutils/iconv.c +++ b/miscutils/iconv.c | |||
| @@ -1717,6 +1717,7 @@ int iconv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | |||
| 1717 | int iconv_main(int argc, char **argv) | 1717 | int iconv_main(int argc, char **argv) |
| 1718 | { | 1718 | { |
| 1719 | const char *fromcode = "", *tocode = "", *outfile; | 1719 | const char *fromcode = "", *tocode = "", *outfile; |
| 1720 | char *tmpname = NULL; | ||
| 1720 | int i, opt; | 1721 | int i, opt; |
| 1721 | iconv_t cd; | 1722 | iconv_t cd; |
| 1722 | FILE *in; | 1723 | FILE *in; |
| @@ -1733,8 +1734,11 @@ int iconv_main(int argc, char **argv) | |||
| 1733 | return 0; | 1734 | return 0; |
| 1734 | } | 1735 | } |
| 1735 | 1736 | ||
| 1736 | if (opt & OPT_o) | 1737 | if (opt & OPT_o) { |
| 1737 | out = xfopen(outfile, "wb"); | 1738 | tmpname = xasprintf("%sXXXXXX", outfile); |
| 1739 | mktemp(tmpname); | ||
| 1740 | out = xfopen(tmpname, "wb"); | ||
| 1741 | } | ||
| 1738 | 1742 | ||
| 1739 | if (opt & OPT_c) | 1743 | if (opt & OPT_c) |
| 1740 | tocode = xasprintf("%s//IGNORE", tocode); | 1744 | tocode = xasprintf("%s//IGNORE", tocode); |
| @@ -1755,6 +1759,11 @@ int iconv_main(int argc, char **argv) | |||
| 1755 | fclose(in); | 1759 | fclose(in); |
| 1756 | } | 1760 | } |
| 1757 | 1761 | ||
| 1762 | if (tmpname) { | ||
| 1763 | fclose(out); | ||
| 1764 | xrename(tmpname, outfile); | ||
| 1765 | } | ||
| 1766 | |||
| 1758 | if (ENABLE_FEATURE_CLEAN_UP) | 1767 | if (ENABLE_FEATURE_CLEAN_UP) |
| 1759 | iconv_close(cd); | 1768 | iconv_close(cd); |
| 1760 | return 0; | 1769 | return 0; |
