very small size reduce for nohup applet

This commit is contained in:
"Vladimir N. Oleynik" 2005-09-22 13:26:23 +00:00
parent 24cca8d958
commit 1f0ac23c20

View File

@ -40,12 +40,12 @@ static inline int set_cloexec_flag (int desc)
#define set_cloexec_flag(desc) (0) #define set_cloexec_flag(desc) (0)
#endif #endif
static int fd_reopen (int desired_fd, char const *file, int flags, mode_t mode) static int fd_reopen (int desired_fd, char const *file, int flags)
{ {
int fd; int fd;
close (desired_fd); close (desired_fd);
fd = open (file, flags, mode); fd = open (file, flags | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd == desired_fd || fd < 0) if (fd == desired_fd || fd < 0)
return fd; return fd;
else { else {
@ -115,27 +115,25 @@ int nohup_main (int argc, char **argv)
Note that it is deliberately opened for *writing*, Note that it is deliberately opened for *writing*,
to ensure any read evokes an error. */ to ensure any read evokes an error. */
if (isatty (STDIN_FILENO)) if (isatty (STDIN_FILENO))
fd_reopen (STDIN_FILENO, "/dev/null", O_WRONLY, 0); fd_reopen (STDIN_FILENO, "/dev/null", 0);
/* If standard output is a tty, redirect it (appending) to a file. /* If standard output is a tty, redirect it (appending) to a file.
First try nohup.out, then $HOME/nohup.out. */ First try nohup.out, then $HOME/nohup.out. */
if (isatty (STDOUT_FILENO)) { if (isatty (STDOUT_FILENO)) {
char *in_home = NULL; char *in_home = NULL;
char const *file = "nohup.out"; char const *file = "nohup.out";
int fd = fd_reopen (STDOUT_FILENO, file, int fd = fd_reopen (STDOUT_FILENO, file, O_CREAT | O_APPEND);
O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR);
if (fd < 0) { if (fd < 0) {
if ((in_home = getenv ("HOME")) != NULL) { if ((in_home = getenv ("HOME")) != NULL) {
in_home = concat_path_file(in_home, file); in_home = concat_path_file(in_home, file);
fd = fd_reopen (STDOUT_FILENO, in_home, fd = fd_reopen (STDOUT_FILENO, in_home, O_CREAT | O_APPEND);
O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR);
} }
if (fd < 0) { if (fd < 0) {
bb_perror_msg("failed to open '%s'", file); bb_perror_msg("failed to open '%s'", file);
if (in_home) if (in_home)
bb_perror_msg("failed to open '%s'",in_home); bb_perror_msg("failed to open '%s'",in_home);
exit (NOHUP_FAILURE); return (NOHUP_FAILURE);
} }
file = in_home; file = in_home;
} }
@ -185,4 +183,3 @@ int nohup_main (int argc, char **argv)
return (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE); return (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
} }
} }