Skip to content

Commit 128a9c2

Browse files
Mikachu2333ccmywish
authored andcommitted
改进 chsrc_overwrite_file 函数
1 parent 9aa000c commit 128a9c2

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/framework/core.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,16 +1781,26 @@ chsrc_overwrite_file (const char *str, const char *filename)
17811781
char *dir = xy_parent_dir (file);
17821782
chsrc_ensure_dir (dir);
17831783

1784-
char *cmd = NULL;
1785-
if (xy.on_windows)
1784+
FILE *f = fopen (file, "w");
1785+
if (NULL==f)
17861786
{
1787-
cmd = xy_strcat (4, "echo ", str, " > ", file);
1787+
char *msg = ENGLISH ? xy_2strcat ("Unable to open file to overwrite: ", file)
1788+
: xy_2strcat ("无法打开文件以覆盖: ", file);
1789+
chsrc_error2 (msg);
1790+
exit (Exit_UserCause);
17881791
}
1789-
else
1792+
1793+
size_t len = strlen (str);
1794+
size_t ret = fwrite (str, len, 1, f);
1795+
if (ret != 1)
17901796
{
1791-
cmd = xy_strcat (4, "echo '", str, "' > ", file);
1797+
char *msg = ENGLISH ? xy_2strcat ("Write failed to ", file)
1798+
: xy_2strcat ("写入文件失败: ", file);
1799+
chsrc_error2 (msg);
1800+
exit (Exit_UserCause);
17921801
}
1793-
chsrc_run_as_a_service (cmd);
1802+
1803+
fclose (f);
17941804

17951805
log_anyway:
17961806
/* 输出recipe指定的文件名 */

0 commit comments

Comments
 (0)