add nohup

This commit is contained in:
Connor Lane Smith 2011-06-18 06:41:28 +01:00
parent e54eb9f4bc
commit f24772dcbb
7 changed files with 103 additions and 17 deletions

View File

@ -7,9 +7,11 @@ LIB = \
util/concat.o \
util/enmasse.o \
util/eprintf.o \
util/enprintf.o \
util/estrtol.o \
util/putword.o \
util/recurse.o \
util/venprintf.o
SRC = \
basename.c \
@ -31,6 +33,7 @@ SRC = \
mkdir.c \
mkfifo.c \
nl.c \
nohup.c \
pwd.c \
rm.c \
sleep.c \

15
nohup.1 Normal file
View File

@ -0,0 +1,15 @@
.TH NOHUP 1 sbase\-VERSION
.SH NAME
nohup \- run a command immune to hangups
.SH SYNOPSIS
.B nohup
.I command
.RI [ argument ...]
.SH DESCRIPTION
.B nohup
runs the given command with the SIGHUP signal set to be ignored. If stdout is a
tty, it is appended to
.I nohup.out
in the current working directory; if stderr is a tty, it is redirected to stdout.
.SH SEE ALSO
.IR signal (7)

40
nohup.c Normal file
View File

@ -0,0 +1,40 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include "util.h"
enum { Error = 127, Found = 126 };
int
main(int argc, char *argv[])
{
int fd;
struct sigaction sa;
if(getopt(argc, argv, "") != -1)
exit(Error);
if(optind == argc)
enprintf(Error, "usage: %s command [argument...]\n", argv[0]);
sa.sa_handler = SIG_IGN;
if(sigaction(SIGHUP, &sa, NULL) == -1)
enprintf(Error, "sigaction HUP:");
if(isatty(STDOUT_FILENO)) {
if((fd = open("nohup.out", O_APPEND|O_CREAT, S_IRUSR|S_IWUSR)) == -1)
enprintf(Error, "open nohup.out:");
if(dup2(fd, STDOUT_FILENO) == -1)
enprintf(Error, "dup2:");
close(fd);
}
if(isatty(STDERR_FILENO))
if(dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
enprintf(Error, "dup2:");
execvp(argv[optind], &argv[optind]);
enprintf(errno == ENOENT ? Error : Found, "exec %s:", argv[optind]);
return Error;
}

1
util.h
View File

@ -5,6 +5,7 @@
char *agetcwd(void);
void enmasse(int, char **, int (*)(const char *, const char *));
void eprintf(const char *, ...);
void enprintf(int, const char *, ...);
long estrtol(const char *, int);
void putword(const char *);
void recurse(const char *, void (*)(const char *));

15
util/enprintf.c Normal file
View File

@ -0,0 +1,15 @@
/* See LICENSE file for copyright and license details. */
#include <stdarg.h>
#include "../util.h"
extern void venprintf(int, const char *, va_list);
void
enprintf(int status, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
venprintf(status, fmt, ap);
va_end(ap);
}

View File

@ -1,22 +1,16 @@
/* See LICENSE file for copyright and license details. */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../util.h"
extern void venprintf(int, const char *, va_list);
void
eprintf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
venprintf(EXIT_FAILURE, fmt, ap);
va_end(ap);
if(fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
}
exit(EXIT_FAILURE);
}

18
util/venprintf.c Normal file
View File

@ -0,0 +1,18 @@
/* See LICENSE file for copyright and license details. */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../util.h"
void
venprintf(int status, const char *fmt, va_list ap)
{
vfprintf(stderr, fmt, ap);
if(fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
}
exit(status);
}