From bada9b0ba2db5173cdb9b3bc507825a0deff2d80 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 29 Nov 2020 17:16:20 +0000 Subject: Added man, Makefile can install FossilOrigin-Name: 345456287af27b824c56e03b1eeebd9f73fe6562 --- Makefile | 25 +++++++++-- man/bsdsetsid.1 | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/c_args.c | 80 +++++++++++++++++++++++++++++++++++ src/c_args.h | 61 +++++++++++++++++++++++++++ src/c_bsdsetsid.c | 37 ++-------------- src/c_bsdsetsid.h | 10 ++--- src/c_fork.c | 22 ++-------- 7 files changed, 296 insertions(+), 62 deletions(-) create mode 100644 man/bsdsetsid.1 create mode 100644 src/c_args.c create mode 100644 src/c_args.h diff --git a/Makefile b/Makefile index 3807bdb..dbae9e4 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ # ======================== # # bsdsetsid: Makefile -# Thu Nov 26 22:01:36 CET 2020 +# Sun Nov 29 18:15:56 CET 2020 # Joe # # BSD Makefile @@ -19,13 +19,15 @@ default: all SRCS_DIR := src +MAN_DIR := man # OBJS_DIR := obj -PREFIX ?= /usr/local/ +PREFIX ?= /usr/local DESTDIR := MAKEOBJDIR := ./ SRCS := c_bsdsetsid SRCS += c_fork +SRCS += c_args SRCS := ${SRCS:S/$/.c/g} SRCS := ${SRCS:S/^/${SRCS_DIR}\//g} @@ -36,7 +38,7 @@ INCS := ${SRCS:.c=.h} NAME := bsdsetsid -CC := cc +CC ?= cc CFLAGS := -std=c89 CFLAGS += -Wall CFLAGS += -Wextra @@ -46,6 +48,9 @@ CFLAGS += -pedantic RM := rm -f MKDIR := mkdir -p SED := sed -i '' +GZIP := gzip +GUNZIP := gunzip +INSTALL := install .OBJDIR: ./ .SUFFIXES: .c.o .o @@ -65,7 +70,19 @@ clean: ${RM} ${OBJS} ${NAME} vgcore* # ${RM} -R ${OBJS_DIR} -.PHONY: all clean depend +install: ${NAME} + ${GZIP} ${MAN_DIR}/${NAME}.1 + ${MKDIR} ${PREFIX}/man/man1 + ${INSTALL} -m0444 ${MAN_DIR}/${NAME}.1.gz ${PREFIX}/man/man1/${NAME}.1.gz + ${GUNZIP} ${MAN_DIR}/${NAME}.1.gz + ${MKDIR} ${PREFIX}/bin + ${INSTALL} -m0555 ${NAME} ${PREFIX}/bin/${NAME} + +uninstall: + ${RM} ${PREFIX}/man/man1/${NAME}.1.gz + ${RM} ${PREFIX}/bin/${NAME} + +.PHONY: all clean install # Files prefixes index # -------------------- diff --git a/man/bsdsetsid.1 b/man/bsdsetsid.1 new file mode 100644 index 0000000..9c29a9d --- /dev/null +++ b/man/bsdsetsid.1 @@ -0,0 +1,123 @@ +.\" ======================== +.\" ===== =============== +.\" ====== ================ +.\" ====== ================ +.\" ====== ==== ==== == +.\" ====== === == = = +.\" ====== === = == = +.\" = === === = == ==== +.\" = === === = == = = +.\" == ===== ==== == +.\" ======================== +.\" +.\" SPDX-License-Identifier: BSD-3-Clause +.\" +.\" Copyright (c) 2020 Joe +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. Neither the name of the organization nor the +.\" names of its contributors may be used to endorse or promote products +.\" derived from this software without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY JOE ''AS IS'' AND ANY +.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +.\" DISCLAIMED. IN NO EVENT SHALL JOE BE LIABLE FOR ANY +.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +.\" +.\" bsdsetsid: man/bsdsetsid.1 +.\" Sun Nov 29 18:09:44 CET 2020 +.\" Joe +.\" +.\" Manpage for bsdsetsid. +.\" Contact rbousset@42lyon.fr to correct errors and typos. +.de Text +.nop \)\\$* +.. +.de squoted_text +.Text \\$3\(oq\fB\\$1\f[]\(cq\\$2 +.. +.de dquoted_text +.Text \\$3\(lq\fB\\$1\f[]\(rq\\$2 +.. +.de file_example +.PP +.RS +\`\\$1\' \-\> \`\\$2\' +.RE +.PP +.. +. +. +.TH bsdsetsid 1 "29 November 2020" "bsdsetsid 1.0" +. +. +.SH NAME +.B bsdsetsid +. +. +.SH SYNOPSIS +.SY bsdsetsid +.RB [ \-hw ] +.I utility +.RI [ arguments ] +.YS +. +. +.SH DESCRIPTION +.PP +The +.B bsdsetsid +utility invokes +.I utility +whith its +.I arguments +detached from its running shell. Unlike the util-linux +.BR setsid , +.B bsdsetsid +always calls +.IR fork (2) +to run the new process. +. +. +.SH OPTIONS +.TP +.B \-h +Output a brief help message. +.TP +.B \-w +Wait for the execution of the program to end, and return the exit status of +this program as the exit status of +.BR bsdsetsid . +. +. +.SH ENVIRONMENT +.TP +PATH +Used to locate the requested +.I utility +if the name contains no \(oq/\(cq characters. +. +. +.SH AUTHORS +Joe +. +. +.SH SEE ALSO +.IR sh (1), +.IR setsid (2), +.IR fork (2), +.IR wait4 (2), +.IR execve (2) diff --git a/src/c_args.c b/src/c_args.c new file mode 100644 index 0000000..616b979 --- /dev/null +++ b/src/c_args.c @@ -0,0 +1,80 @@ +/* + * ======================== + * ===== =============== + * ====== ================ + * ====== ================ + * ====== ==== ==== == + * ====== === == = = + * ====== === = == = + * = === === = == ==== + * = === === = == = = + * == ===== ==== == + * ======================== + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2020 Joe + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the organization nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY JOE ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JOE BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * bsdsetsid: src/c_args.c + * Sun Nov 29 16:43:56 CET 2020 + * Joe + * + * Arguments handling here. + */ + +#include +#include +#include +#include + +#include "c_args.h" +#include "c_bsdsetsid.h" + +void +c_args +(int argc, + const char* argv[], + bool_t* wopt) +{ + if (argc == 1) { + dprintf(STDERR_FILENO, "%s\n", C_USAGE_FMT); + exit(EXIT_FAILURE); + } + if (strncmp(argv[1], C_W_OPT, 3) == 0) { + *wopt = TRUE; + if (argc == 2) { + dprintf(STDERR_FILENO, "%s\n", C_USAGE_FMT); + exit(EXIT_FAILURE); + } + } + if (strncmp(argv[1], C_H_OPT, 3) == 0) { + *wopt = TRUE; + if (argc == 2) { + dprintf(STDERR_FILENO, "%s\n", C_HELP_FMT); + exit(EXIT_SUCCESS); + } + } +} diff --git a/src/c_args.h b/src/c_args.h new file mode 100644 index 0000000..4c90c00 --- /dev/null +++ b/src/c_args.h @@ -0,0 +1,61 @@ +/* + * ======================== + * ===== =============== + * ====== ================ + * ====== ================ + * ====== ==== ==== == + * ====== === == = = + * ====== === = == = + * = === === = == ==== + * = === === = == = = + * == ===== ==== == + * ======================== + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2020 Joe + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the organization nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY JOE ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JOE BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * bsdsetsid: src/c_args.h + * Sun Nov 29 17:02:19 CET 2020 + * Joe + */ + +#ifndef __C_ARGS_H__ +#define __C_ARGS_H__ + +#include "c_bsdsetsid.h" + +#define C_USAGE_FMT "usage: bsdsetsid [-hw] utility [arguments]" +#define C_HELP_FMT \ + "usage: bsdsetsid [-hw] utility [arguments]\n\n"\ + "options: -h\tdisplay this help\n"\ + " -w\twait program to exit, and use the same return" +#define C_H_OPT "-h" +#define C_W_OPT "-w" + +void c_args(int, const char*[], bool_t*); + +#endif /* ifndef __C_ARGS_H__ */ diff --git a/src/c_bsdsetsid.c b/src/c_bsdsetsid.c index 7d3a76d..f8e2067 100644 --- a/src/c_bsdsetsid.c +++ b/src/c_bsdsetsid.c @@ -39,7 +39,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * bsdsetsid: src/c_bsdsetsid.c - * Thu Nov 26 22:12:37 CET 2020 + * Sun Nov 29 16:42:26 CET 2020 * Joe * * This is the entrypoint of the program. @@ -51,39 +51,10 @@ #include #include +#include "c_args.h" #include "c_bsdsetsid.h" #include "c_fork.h" -static char -c_args -(int argc, - const char* argv[], - bool_t* wopt) -{ - if (argc == 1) { - dprintf( - STDERR_FILENO, - "%s: %s\n", - C_PROGNAME, - C_ERR_NOARG - ); - return (1); - } - if (strncmp(argv[1], C_W_OPT, 3) == 0) { - *wopt = TRUE; - if (argc == 2) { - dprintf( - STDERR_FILENO, - "%s: %s\n", - C_PROGNAME, - C_ERR_NOARG - ); - return (1); - } - } - return (0); -} - int main (int argc, @@ -93,9 +64,7 @@ main bool_t wopt; wopt = FALSE; - if (c_args(argc, argv, &wopt) != 0) { - return (EXIT_FAILURE); - } + c_args(argc, argv, &wopt); c_fork(argv, envp, wopt); return (EXIT_SUCCESS); } diff --git a/src/c_bsdsetsid.h b/src/c_bsdsetsid.h index d7f7724..740bd7d 100644 --- a/src/c_bsdsetsid.h +++ b/src/c_bsdsetsid.h @@ -39,7 +39,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * bsdsetsid: src/c_bsdsetsid.h - * Fri Nov 27 01:48:28 CET 2020 + * Sun Nov 29 17:02:13 CET 2020 * Joe * * This is the entrypoint of the program. @@ -47,6 +47,8 @@ #ifndef __C_BSDSETSID_H__ #define __C_BSDSETSID_H__ +#define C_PROGNAME "bsdsetsid" + typedef char bool_t; typedef void* ptr_t; @@ -55,8 +57,4 @@ enum bool_e { TRUE }; -#define C_PROGNAME "bsdsetsid" -#define C_ERR_NOARG "no program specified" -#define C_W_OPT "-w" - -#endif +#endif /* ifndef __C_BSDSETSID_H__ */ diff --git a/src/c_fork.c b/src/c_fork.c index ee754ba..61a3016 100644 --- a/src/c_fork.c +++ b/src/c_fork.c @@ -39,7 +39,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * bsdsetsid: src/c_fork.c - * Fri Nov 27 01:48:00 CET 2020 + * Sun Nov 29 15:26:58 CET 2020 * Joe * * The program's main fork(2). @@ -121,22 +121,13 @@ c_fork_child u.pid = setsid(); if (u.pid == -1) { - dprintf( - STDERR_FILENO, - "%s: setsid: %s\n", - C_PROGNAME, - strerror(errno) - ); + dprintf(STDERR_FILENO, "%s: setsid: %s\n", C_PROGNAME, strerror(errno)); exit(EXIT_FAILURE); } path = mem; u.ret = c_get_path(argv[1 + wopt], envp, path); if (u.ret == 1) { - dprintf( - STDERR_FILENO, - "%s: PATH not set\n", - C_PROGNAME - ); + dprintf(STDERR_FILENO, "%s: PATH not set\n", C_PROGNAME); exit(EXIT_FAILURE); } else if (u.ret == 2) { @@ -191,12 +182,7 @@ c_fork_parent static void c_fork_error(void) { - dprintf( - STDERR_FILENO, - "%s: fork: %s\n", - C_PROGNAME, - strerror(errno) - ); + dprintf(STDERR_FILENO, "%s: fork: %s\n", C_PROGNAME, strerror(errno)); exit(EXIT_FAILURE); } -- cgit v1.2.3