diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2016-01-17 03:50:36 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2016-01-17 03:50:36 +0100 |
commit | c7e47cf6273830a59f5d3f822e4a6855a80312c6 (patch) | |
tree | ebbde78e8dcc73a762f241ced0204915e77177f4 | |
parent | ccf7f0e4d3aed3bd9f46a239d9033cd773e67ab8 (diff) | |
download | busybox-w32-c7e47cf6273830a59f5d3f822e4a6855a80312c6.tar.gz busybox-w32-c7e47cf6273830a59f5d3f822e4a6855a80312c6.tar.bz2 busybox-w32-c7e47cf6273830a59f5d3f822e4a6855a80312c6.zip |
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>
-rw-r--r-- | coreutils/dos2unix.c | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c index 07398bdfa..ccb74a113 100644 --- a/coreutils/dos2unix.c +++ b/coreutils/dos2unix.c | |||
@@ -41,7 +41,7 @@ enum { | |||
41 | static void convert(char *fn, int conv_type) | 41 | static void convert(char *fn, int conv_type) |
42 | { | 42 | { |
43 | FILE *in, *out; | 43 | FILE *in, *out; |
44 | int i; | 44 | int ch; |
45 | char *temp_fn = temp_fn; /* for compiler */ | 45 | char *temp_fn = temp_fn; /* for compiler */ |
46 | char *resolved_fn = resolved_fn; | 46 | char *resolved_fn = resolved_fn; |
47 | 47 | ||
@@ -49,28 +49,30 @@ static void convert(char *fn, int conv_type) | |||
49 | out = stdout; | 49 | out = stdout; |
50 | if (fn != NULL) { | 50 | if (fn != NULL) { |
51 | struct stat st; | 51 | struct stat st; |
52 | int fd; | ||
52 | 53 | ||
53 | resolved_fn = xmalloc_follow_symlinks(fn); | 54 | resolved_fn = xmalloc_follow_symlinks(fn); |
54 | if (resolved_fn == NULL) | 55 | if (resolved_fn == NULL) |
55 | bb_simple_perror_msg_and_die(fn); | 56 | bb_simple_perror_msg_and_die(fn); |
56 | in = xfopen_for_read(resolved_fn); | 57 | in = xfopen_for_read(resolved_fn); |
57 | fstat(fileno(in), &st); | 58 | xfstat(fileno(in), &st, resolved_fn); |
58 | 59 | ||
59 | temp_fn = xasprintf("%sXXXXXX", resolved_fn); | 60 | temp_fn = xasprintf("%sXXXXXX", resolved_fn); |
60 | i = xmkstemp(temp_fn); | 61 | fd = xmkstemp(temp_fn); |
61 | if (fchmod(i, st.st_mode) == -1) | 62 | if (fchmod(fd, st.st_mode) == -1) |
62 | bb_simple_perror_msg_and_die(temp_fn); | 63 | bb_simple_perror_msg_and_die(temp_fn); |
64 | fchown(fd, st.st_uid, st.st_gid); | ||
63 | 65 | ||
64 | out = xfdopen_for_write(i); | 66 | out = xfdopen_for_write(fd); |
65 | } | 67 | } |
66 | 68 | ||
67 | while ((i = fgetc(in)) != EOF) { | 69 | while ((ch = fgetc(in)) != EOF) { |
68 | if (i == '\r') | 70 | if (ch == '\r') |
69 | continue; | 71 | continue; |
70 | if (i == '\n') | 72 | if (ch == '\n') |
71 | if (conv_type == CT_UNIX2DOS) | 73 | if (conv_type == CT_UNIX2DOS) |
72 | fputc('\r', out); | 74 | fputc('\r', out); |
73 | fputc(i, out); | 75 | fputc(ch, out); |
74 | } | 76 | } |
75 | 77 | ||
76 | if (fn != NULL) { | 78 | if (fn != NULL) { |