diff --git a/Makefile b/Makefile index 188277c..1d1de2d 100644 --- a/Makefile +++ b/Makefile @@ -171,3 +171,8 @@ build/bin/swab: src/swab.rs build rustlibs true: build/bin/true build/bin/true: src/true.c build $(CC) $(CFLAGS) -o $@ src/true.c + +.PHONY: zn +zn: build/bin/zn +build/bin/zn: src/zn.c build + $(CC) $(CFLAGS) -o $@ src/zn.c diff --git a/src/zn.c b/src/zn.c new file mode 100644 index 0000000..c3fdf6c --- /dev/null +++ b/src/zn.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2024 DTB + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see https://www.gnu.org/licenses/. + */ + +#include /* _POSIX_MAX_CANON */ +#include /* fgets(3) */ +#include /* strtok(3) */ +#include /* EX_OSERR */ +#include /* fork(2) */ +#include /* wait(2), WIFEXITED, WEXITSTATUS */ + +static char *sep = " \t\v\n"; + +static char ** +get_args(char *s) { + size_t c = 0; + static char *v[10]; + + if ((v[c] = strtok(s, sep)) == NULL) { return NULL; } + + while (++c < sizeof v / sizeof *v) { + if ((v[c] = strtok(NULL, sep)) == NULL) { return v; } + } + + return v; +} + +static char * +get_line(FILE *stream) { + static char buffer[_POSIX_MAX_CANON]; + + return fgets(buffer, sizeof *buffer * MAX_CANON, stream); +} + +int main(int argc, char *argv[]) { + char **a; + char *s; + + while ((s = get_line(stdin)) != NULL) { + a = get_args(s); + switch (fork()) { + case -1: perror(argv[0]); return EX_OSERR; + case 0: execvp(a[0], a); + default: (void)wait(NULL); + } + } +}