[hackers] [sbase] add nohup || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Sat, 18 Jun 2011 07:45:20 +0200 (CEST)

changeset: 84:322aff19c976
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Sat Jun 18 06:41:28 2011 +0100
files: Makefile nohup.1 nohup.c util.h util/enprintf.c util/eprintf.c util/venprintf.c
description:
add nohup

diff -r 23deb4b8e5a9 -r 322aff19c976 Makefile
--- a/Makefile Thu Jun 16 21:20:17 2011 +0100
+++ b/Makefile Sat Jun 18 06:41:28 2011 +0100
@@ -2,14 +2,16 @@
 
 HDR = text.h util.h
 LIB = \
- util/afgets.o \
- util/agetcwd.o \
- util/concat.o \
- util/enmasse.o \
- util/eprintf.o \
- util/estrtol.o \
- util/putword.o \
- util/recurse.o \
+ util/afgets.o \
+ util/agetcwd.o \
+ 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 @@
         mkdir.c \
         mkfifo.c \
         nl.c \
+ nohup.c \
         pwd.c \
         rm.c \
         sleep.c \
diff -r 23deb4b8e5a9 -r 322aff19c976 nohup.1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nohup.1 Sat Jun 18 06:41:28 2011 +0100
@@ -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)
diff -r 23deb4b8e5a9 -r 322aff19c976 nohup.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nohup.c Sat Jun 18 06:41:28 2011 +0100
@@ -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;
+}
diff -r 23deb4b8e5a9 -r 322aff19c976 util.h
--- a/util.h Thu Jun 16 21:20:17 2011 +0100
+++ b/util.h Sat Jun 18 06:41:28 2011 +0100
@@ -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 *));
diff -r 23deb4b8e5a9 -r 322aff19c976 util/enprintf.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/util/enprintf.c Sat Jun 18 06:41:28 2011 +0100
@@ -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);
+}
diff -r 23deb4b8e5a9 -r 322aff19c976 util/eprintf.c
--- a/util/eprintf.c Thu Jun 16 21:20:17 2011 +0100
+++ b/util/eprintf.c Sat Jun 18 06:41:28 2011 +0100
@@ -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);
 }
diff -r 23deb4b8e5a9 -r 322aff19c976 util/venprintf.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/util/venprintf.c Sat Jun 18 06:41:28 2011 +0100
@@ -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);
+}
Received on Sat Jun 18 2011 - 07:45:20 CEST

This archive was generated by hypermail 2.2.0 : Sat Jun 18 2011 - 07:48:05 CEST