dos2unix: try to preserve ownership. closes 8311

function                                             old     new   delta
dos2unix_main                                        426     441     +15

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2016-01-17 03:50:36 +01:00
parent ccf7f0e4d3
commit c7e47cf627

View File

@ -41,7 +41,7 @@ enum {
static void convert(char *fn, int conv_type) static void convert(char *fn, int conv_type)
{ {
FILE *in, *out; FILE *in, *out;
int i; int ch;
char *temp_fn = temp_fn; /* for compiler */ char *temp_fn = temp_fn; /* for compiler */
char *resolved_fn = resolved_fn; char *resolved_fn = resolved_fn;
@ -49,28 +49,30 @@ static void convert(char *fn, int conv_type)
out = stdout; out = stdout;
if (fn != NULL) { if (fn != NULL) {
struct stat st; struct stat st;
int fd;
resolved_fn = xmalloc_follow_symlinks(fn); resolved_fn = xmalloc_follow_symlinks(fn);
if (resolved_fn == NULL) if (resolved_fn == NULL)
bb_simple_perror_msg_and_die(fn); bb_simple_perror_msg_and_die(fn);
in = xfopen_for_read(resolved_fn); in = xfopen_for_read(resolved_fn);
fstat(fileno(in), &st); xfstat(fileno(in), &st, resolved_fn);
temp_fn = xasprintf("%sXXXXXX", resolved_fn); temp_fn = xasprintf("%sXXXXXX", resolved_fn);
i = xmkstemp(temp_fn); fd = xmkstemp(temp_fn);
if (fchmod(i, st.st_mode) == -1) if (fchmod(fd, st.st_mode) == -1)
bb_simple_perror_msg_and_die(temp_fn); bb_simple_perror_msg_and_die(temp_fn);
fchown(fd, st.st_uid, st.st_gid);
out = xfdopen_for_write(i); out = xfdopen_for_write(fd);
} }
while ((i = fgetc(in)) != EOF) { while ((ch = fgetc(in)) != EOF) {
if (i == '\r') if (ch == '\r')
continue; continue;
if (i == '\n') if (ch == '\n')
if (conv_type == CT_UNIX2DOS) if (conv_type == CT_UNIX2DOS)
fputc('\r', out); fputc('\r', out);
fputc(i, out); fputc(ch, out);
} }
if (fn != NULL) { if (fn != NULL) {