- 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(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn);
- /*
- sizeof bb_common_bufsiz1 is 4096, so it should be big enough to
- hold the full path. However if the output is truncated the
- subsequent call to mkstemp would fail.
- */
- if ((i = mkstemp(&bb_common_bufsiz1[0])) == -1
- || chmod(bb_common_bufsiz1, 0600) == -1) {
- bb_perror_nomsg_and_die();
- }
- out = fdopen(i, "w+");
- if (!out) {
- close(i);
- remove(bb_common_bufsiz1);
+ 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, st.st_mode) == -1
+ || !(out = fdopen(i, "w+"))
+ ) {
+ bb_simple_perror_msg_and_die(temp_fn);