coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

coreutils/src/orphan.c (59 lines, 1.3 KiB) -rw-r--r-- blame download

012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
// orphan.c, version 1.0.1
// OMKOV coreutils orphan
// Copyright (C) 2020, Jakob Wakeling
// MIT Licence

#include "util/error.h"
#include "util/optget.h"

#include <fcntl.h>
#include <unistd.h>

#include <errno.h>
#include <stdio.h>

#define VERSION "1.0.1"

static struct lop lops[] = {
	{ "help",    ARG_NUL, 256 },
	{ "version", ARG_NUL, 257 },
	{ NULL, 0, 0 }
};

static void hlp(void);
static void ver(void);

int main(int ac, char *av[]) { A0 = av[0];
	struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops;
	for (int o; (o = optget(&opt, av, 0)) != -1;) switch (o) {
	case 256: { hlp(); return 0; }
	case 257: { ver(); return 0; }
	default: { return 1; }
	}
	
	if (opt.ind == ac) { error(1, "missing operand"); }
	
	switch (fork()) {
	case -1: { error(1, "%s", serr()); }
	case  0: {
		execvp(av[opt.ind], &av[opt.ind]);
		warn("%s: %s", av[opt.ind], serr());
	}
	default: { return 0; }
	}
}

static void hlp(void) {
	puts("orphan - invoke a command as an orphan\n");
	puts("usage: orphan command [argument...]\n");
	puts("options:");
	puts("  --help     Display help information");
	puts("  --version  Display version information");
}

static void ver(void) {
	puts("OMKOV cryptutils orphan, version " VERSION);
	puts("Copyright (C) 2020, Jakob Wakeling");
	puts("MIT Licence (https://opensource.org/licenses/MIT)");
}