inetd: comment tweak. no code changes
[oweals/busybox.git] / coreutils / dos2unix.c
index 7776133f40e5eb3ab16fe07ee9f8c322700d14e7..309cbc3b819b962e388fd338d513934af9680eb0 100644 (file)
@@ -24,45 +24,47 @@ static void convert(char *fn, int conv_type)
 {
        FILE *in, *out;
        int i;
-#define name_buf bb_common_bufsiz1
+       char *temp_fn = temp_fn; /* for compiler */
+       char *resolved_fn = resolved_fn;
 
        in = stdin;
        out = stdout;
        if (fn != NULL) {
-               in = xfopen(fn, "rw");
-               /*
-                  The file is then created with mode read/write and
-                  permissions 0666 for glibc 2.0.6 and earlier or
-                  0600 for glibc 2.0.7 and later.
-                */
-               snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn);
-               i = mkstemp(&name_buf[0]);
+               struct stat st;
+
+               resolved_fn = xmalloc_follow_symlinks(fn);
+               if (resolved_fn == NULL)
+                       bb_simple_perror_msg_and_die(fn);
+               in = xfopen_for_read(resolved_fn);
+               fstat(fileno(in), &st);
+
+               temp_fn = xasprintf("%sXXXXXX", resolved_fn);
+               i = mkstemp(temp_fn);
                if (i == -1
-                || fchmod(i, 0600) == -1
+                || fchmod(i, st.st_mode) == -1
                 || !(out = fdopen(i, "w+"))
                ) {
-                       bb_perror_nomsg_and_die();
+                       bb_simple_perror_msg_and_die(temp_fn);
                }
        }
 
        while ((i = fgetc(in)) != EOF) {
                if (i == '\r')
                        continue;
-               if (i == '\n') {
+               if (i == '\n')
                        if (conv_type == CT_UNIX2DOS)
                                fputc('\r', out);
-                       fputc('\n', out);
-                       continue;
-               }
                fputc(i, out);
        }
 
        if (fn != NULL) {
                if (fclose(in) < 0 || fclose(out) < 0) {
-                       unlink(name_buf);
+                       unlink(temp_fn);
                        bb_perror_nomsg_and_die();
                }
-               xrename(name_buf, fn);
+               xrename(temp_fn, resolved_fn);
+               free(temp_fn);
+               free(resolved_fn);
        }
 }