aboutsummaryrefslogtreecommitdiffstats
path: root/src/c_bsdsetsid.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/c_bsdsetsid.c')
-rw-r--r--src/c_bsdsetsid.c95
1 files changed, 90 insertions, 5 deletions
diff --git a/src/c_bsdsetsid.c b/src/c_bsdsetsid.c
index 28099f3..2452553 100644
--- a/src/c_bsdsetsid.c
+++ b/src/c_bsdsetsid.c
@@ -39,20 +39,105 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* bsdsetsid: src/c_bsdsetsid.c
- * Wed Nov 25 22:16:26 CET 2020
+ * Wed Nov 25 22:31:26 CET 2020
* Joe
*
* This is the entrypoint of the program.
*/
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "c_bsdsetsid.h"
+
+static char
+c_args
+(int argc,
+ const char* argv[],
+ const char* envp[],
+ bool_t* wopt)
+{
+ (void)envp;
+ 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,
const char* argv[],
const char* envp[])
{
- (void)argc;
- (void)argv;
- (void)envp;
- return (0);
+ int sets_ret;
+ int exec_ret;
+ pid_t pid;
+ bool_t wopt;
+
+ wopt = FALSE;
+ if (c_args(argc, argv, envp, &wopt) != 0) {
+ return (EXIT_FAILURE);
+ }
+ pid = fork();
+ if (pid == -1) {
+ dprintf(
+ STDERR_FILENO,
+ "%s: fork: %s\n",
+ C_PROGNAME,
+ strerror(errno)
+ );
+ return (EXIT_FAILURE);
+ }
+ else if (pid == 0) {
+ sets_ret = setsid();
+ if (sets_ret == -1) {
+ dprintf(
+ STDERR_FILENO,
+ "%s: setsid: %s\n",
+ C_PROGNAME,
+ strerror(errno)
+ );
+ return (EXIT_FAILURE);
+ }
+ exec_ret = execve(
+ argv[1 + wopt],
+ (char* const*)argv + (1 + wopt),
+ (char* const*)envp
+ );
+ if (exec_ret == -1) {
+ dprintf(
+ STDERR_FILENO,
+ "%s: execve: %s\n",
+ C_PROGNAME,
+ strerror(errno)
+ );
+ return (EXIT_FAILURE);
+ }
+ }
+ else {
+ return (EXIT_SUCCESS);
+ }
+ return (EXIT_SUCCESS);
}