commit: d59ed2b9b6ea4656d1035df2b048618b82b25eb1
parent 7932f1f9f6bad98421775e7c35d5d6842fc908b8
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sun, 24 Sep 2023 17:08:11 +0200
Move standard/common utils into ../utils-std repository
Diffstat:
D | cmd/base64.c | 171 | ------------------------------------------------------------------------------- |
D | cmd/basename.1 | 39 | --------------------------------------- |
D | cmd/basename.c | 64 | ---------------------------------------------------------------- |
D | cmd/cat.1 | 34 | ---------------------------------- |
D | cmd/cat.c | 119 | ------------------------------------------------------------------------------- |
D | cmd/chroot.1 | 52 | ---------------------------------------------------- |
D | cmd/chroot.c | 62 | -------------------------------------------------------------- |
D | cmd/date.1 | 55 | ------------------------------------------------------- |
D | cmd/date.c | 107 | ------------------------------------------------------------------------------- |
D | cmd/dirname.1 | 30 | ------------------------------ |
D | cmd/dirname.c | 28 | ---------------------------- |
D | cmd/echo.1 | 44 | -------------------------------------------- |
D | cmd/echo.c | 77 | ----------------------------------------------------------------------------- |
D | cmd/env.1 | 71 | ----------------------------------------------------------------------- |
D | cmd/env.c | 132 | ------------------------------------------------------------------------------- |
D | cmd/false.1 | 18 | ------------------ |
D | cmd/false.c | 8 | -------- |
D | cmd/id.c | 420 | ------------------------------------------------------------------------------- |
D | cmd/link.1 | 28 | ---------------------------- |
D | cmd/link.c | 25 | ------------------------- |
D | cmd/nproc.c | 51 | --------------------------------------------------- |
D | cmd/pwd.c | 36 | ------------------------------------ |
D | cmd/seq.c | 120 | ------------------------------------------------------------------------------- |
D | cmd/sleep.1 | 30 | ------------------------------ |
D | cmd/sleep.c | 138 | ------------------------------------------------------------------------------- |
D | cmd/strings.1 | 58 | ---------------------------------------------------------- |
D | cmd/strings.c | 212 | ------------------------------------------------------------------------------- |
D | cmd/sync.1 | 22 | ---------------------- |
D | cmd/sync.c | 14 | -------------- |
D | cmd/tee.c | 105 | ------------------------------------------------------------------------------- |
D | cmd/time.1 | 48 | ------------------------------------------------ |
D | cmd/time.c | 112 | ------------------------------------------------------------------------------- |
D | cmd/touch.1 | 85 | ------------------------------------------------------------------------------- |
D | cmd/touch.c | 136 | ------------------------------------------------------------------------------- |
D | cmd/true.1 | 18 | ------------------ |
D | cmd/true.c | 8 | -------- |
D | cmd/tty.c | 38 | -------------------------------------- |
D | cmd/unlink.1 | 25 | ------------------------- |
D | cmd/unlink.c | 22 | ---------------------- |
M | test-cmd/Kyuafile | 18 | ------------------ |
D | test-cmd/base64 | 80 | ------------------------------------------------------------------------------- |
D | test-cmd/basename | 80 | ------------------------------------------------------------------------------- |
D | test-cmd/cat | 78 | ------------------------------------------------------------------------------ |
D | test-cmd/date | 93 | ------------------------------------------------------------------------------- |
D | test-cmd/dirname | 38 | -------------------------------------- |
D | test-cmd/echo | 40 | ---------------------------------------- |
D | test-cmd/env | 73 | ------------------------------------------------------------------------- |
D | test-cmd/false | 25 | ------------------------- |
D | test-cmd/id | 164 | ------------------------------------------------------------------------------- |
D | test-cmd/link | 35 | ----------------------------------- |
D | test-cmd/pwd | 49 | ------------------------------------------------- |
D | test-cmd/seq | 75 | --------------------------------------------------------------------------- |
D | test-cmd/sleep.t | 23 | ----------------------- |
D | test-cmd/strings | 123 | ------------------------------------------------------------------------------- |
D | test-cmd/tee | 92 | ------------------------------------------------------------------------------- |
D | test-cmd/time.t | 31 | ------------------------------- |
D | test-cmd/touch | 205 | ------------------------------------------------------------------------------- |
D | test-cmd/true | 25 | ------------------------- |
D | test-cmd/tty | 43 | ------------------------------------------- |
D | test-cmd/unlink | 24 | ------------------------ |
60 files changed, 0 insertions(+), 4276 deletions(-)
diff --git a/cmd/base64.c b/cmd/base64.c
@@ -1,171 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-#include <assert.h> /* assert */
-#include <errno.h> /* errno */
-#include <fcntl.h> /* open(), O_RDONLY */
-#include <stdint.h> /* uint8_t */
-#include <stdio.h> /* fprintf(), BUFSIZ */
-#include <string.h> /* strerror(), strncmp() */
-#include <unistd.h> /* read(), write(), close(), getopt(), opt* */
-
-// 64(26+26+10+2)
-static char b64_encmap[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-static size_t c_out = 0;
-// TODO: -w option ; 76 is lowest of all base64 related RFCs
-static size_t wrap_nl = 76;
-
-static inline uint8_t
-b64_get(uint8_t pos)
-{
- assert(pos <= 64);
- return b64_encmap[pos];
-}
-
-static void
-b64encode(uint8_t out[4], uint8_t in[3])
-{
- out[0] = b64_get(in[0] >> 2);
- out[1] = b64_get((uint8_t)(((in[0] & 0x03) << 4) | (in[1] >> 4)));
- out[2] = b64_get((uint8_t)(((in[1] & 0x0f) << 2) | (in[2] >> 6)));
- out[3] = b64_get(in[2] & 0x3f);
-}
-
-static int
-encode(int fd, const char *fdname)
-{
- ssize_t c = 0;
- char ibuf[3];
-
- while((c = read(fd, ibuf, sizeof(ibuf))) > 0)
- {
- uint8_t obuf[4] = "----";
- ssize_t pad = 0;
- assert(pad >= 0);
-
- for(; c < 3; pad++, c++)
- {
- ibuf[c] = 0;
- }
- assert(c == 3);
-
- b64encode(obuf, (uint8_t *)ibuf);
-
- for(; pad > 0; pad--)
- {
- obuf[4 - pad] = '=';
- }
-
- if(write(1, (char *)obuf, 4) != 4)
- {
- fprintf(stderr, "base64: Error writing: %s\n", strerror(errno));
- return 1;
- }
- c_out += 4;
-
- if(wrap_nl != 0 && c_out >= wrap_nl)
- {
- c_out = 0;
- if(write(1, "\n", 1) != 1)
- {
- fprintf(stderr, "base64: Error writing: %s\n", strerror(errno));
- return 1;
- }
- }
- }
-
- if(c < 0)
- {
- fprintf(stderr, "base64: Error reading ‘%s’: %s\n", fdname, strerror(errno));
- return 1;
- }
-
- return 0;
-}
-
-static int
-decode(int fd, const char *fdname)
-{
- return 1;
-}
-
-int
-main(int argc, char *argv[])
-{
- int (*process)(int, const char *) = &encode;
-
- int c = 0, ret = 0;
-
- while((c = getopt(argc, argv, ":d")) != -1)
- {
- switch(c)
- {
- case 'd':
- process = &decode;
- break;
- case ':':
- fprintf(stderr, "base64: Error: Missing operand for option: ‘-%c’\n", optopt);
- return 1;
- case '?':
- fprintf(stderr, "base64: Error: Unrecognised option: ‘-%c’\n", optopt);
- return 1;
- }
- }
-
- argc -= optind;
- argv += optind;
-
- if(argc <= 0)
- {
- ret = process(0, "<stdin>");
- goto end;
- }
-
- for(int argi = 0; argi < argc; argi++)
- {
- if(strncmp(argv[argi], "-", 2) == 0)
- {
- if(process(0, "<stdin>") != 0)
- {
- ret = 1;
- goto end;
- }
- }
- else if(strncmp(argv[argi], "--", 3) == 0)
- {
- continue;
- }
- else
- {
- int fd = open(argv[argi], O_RDONLY);
- if(fd < 0)
- {
- fprintf(stderr, "base64: Error opening ‘%s’: %s\n", argv[argi], strerror(errno));
- ret = 1;
- goto end;
- }
-
- if(process(fd, argv[argi]) != 0)
- {
- ret = 1;
- goto end;
- }
-
- if(close(fd) < 0)
- {
- fprintf(stderr, "base64: Error closing ‘%s’: %s\n", argv[argi], strerror(errno));
- ret = 1;
- goto end;
- }
- }
- }
-
-end:
- if(c_out > 0)
- {
- printf("\n");
- }
- return ret;
-}
diff --git a/cmd/basename.1 b/cmd/basename.1
@@ -1,39 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2021-04-05
-.Dt BASENAME 1
-.Os
-.Sh NAME
-.Nm basename
-.Nd return last path compoment of a pathname
-.Sh SYNOPSIS
-.Nm
-.Op Ar string
-.Op Ar suffix
-.Sh DESCRIPTION
-When
-.Ar string
-isn't given
-.Dq \&.
-is returned.
-Otherwise it gives the last path compoment of
-.Ar string
-with removing the last part matching
-.Ar suffix
-when applicable.
-.Sh EXIT STATUS
-.Ex -std
-.Sh SEE ALSO
-.Xr basename 3
-.Sh STANDARDS
-.Nm
-is close but not compliant with the
-.St -p1003.1-2008
-specification as it uses
-.Xr basename 3
-directly instead of extracting the non-directory portion of
-.Ar string
-with it's own algorithm.
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/basename.c b/cmd/basename.c
@@ -1,64 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#include <assert.h> // assert()
-#include <libgen.h> // basename()
-#include <stdio.h> // fprintf(), puts(), perror()
-#include <string.h> // strlen(), strncmp()
-
-static char *
-suffix_basename(char *name, char *suffix)
-{
- assert(name);
- assert(suffix);
- char *string = basename(name);
-
- size_t suflen = suffix ? strlen(suffix) : 0;
- size_t len = strlen(string);
-
- if(suflen < len && strcmp(&string[len - suflen], suffix) == 0)
- {
- string[len - suflen] = '\0';
- }
-
- return string;
-}
-
-int
-main(int argc, char *argv[])
-{
- int ret = 0;
-
- if((argc > 1) && (strncmp(argv[1], "--", 3) == 0))
- {
- argv++;
- argc--;
- }
-
- switch(argc)
- {
- case 1:
- ret = printf(".\n");
- break;
- case 2:
- ret = puts(basename(argv[1]));
- break;
- case 3:
- ret = puts(suffix_basename(argv[1], argv[2]));
- break;
- default:
- fprintf(stderr, "usage: basename string [suffix]\n");
- return 1;
- }
-
- if(ret < 0)
- {
- perror("basename: puts");
- return 1;
- }
- else
- {
- return 0;
- }
-}
diff --git a/cmd/cat.1 b/cmd/cat.1
@@ -1,34 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2022-02-13
-.Dt CAT 1
-.Os
-.Sh NAME
-.Nm cat
-.Nd concatenate files
-.Sh SYNOPSIS
-.Nm
-.Op Fl u
-.Op Ar files ...
-.Sh DESCRIPTION
-.Nm
-reads each
-.Ar file
-in sequence and writes it on the standard output.
-If no
-.Ar file
-is given,
-.Nm
-reads from the standard input.
-.Sh OPTIONS
-.Fl u is ignored, present only for POSIX compatibility.
-.Sh EXIT STATUS
-.Ex -std
-.Sh STANDARDS
-.Nm
-is mostly compliant with the
-.St -p1003.1-2008
-specification.
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/cat.c b/cmd/cat.c
@@ -1,119 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-#include <errno.h> /* errno */
-#include <fcntl.h> /* open(), O_RDONLY */
-#include <stdio.h> /* fprintf(), BUFSIZ */
-#include <string.h> /* strerror(), strncmp() */
-#include <unistd.h> /* read(), write(), close() */
-
-int
-concat(int fd, const char *fdname)
-{
- ssize_t c;
- char buf[BUFSIZ];
-
- while((c = read(fd, buf, sizeof(buf))) > 0)
- {
- if(write(1, buf, (size_t)c) < 0)
- {
- fprintf(stderr, "cat: Error writing: %s\n", strerror(errno));
- return 1;
- }
- }
-
- if(c < 0)
- {
- fprintf(stderr, "cat: Error reading ‘%s’: %s\n", fdname, strerror(errno));
- return 1;
- }
-
- return 0;
-}
-
-// FreeBSD also has sendfile(2) but it's only for sockets
-#ifdef __linux__
-#include <sys/sendfile.h>
-// Grabbed from /usr/src/hare/stdlib/io/+linux/platform_file.ha
-#ifndef SENDFILE_MAX
-#define SENDFILE_MAX 2147479552
-#endif // SENDFILE_MAX
-int
-fd_copy(int fd, const char *fdname)
-{
- ssize_t c = 0;
- off_t off = 0;
-
- while((c = sendfile(1, fd, &off, SENDFILE_MAX)) > 0)
- ;
-
- if(c < 0)
- {
- if(errno == EINVAL && off == 0)
- {
- return concat(fd, fdname);
- }
- fprintf(stderr, "cat: Error copying ‘%s’: %s\n", fdname, strerror(errno));
- return 1;
- }
-
- return 0;
-}
-#else // __linux__
-#define fd_copy concat
-#endif // __linux__
-
-int
-main(int argc, char *argv[])
-{
- if(argc <= 1)
- {
- return concat(0, "<stdin>");
- }
-
- // For POSIX compatibility
- if(strncmp(argv[1], "-u", 3) == 0)
- {
- argc--;
- argv++;
- }
-
- for(int argi = 1; argi < argc; argi++)
- {
- if(strncmp(argv[argi], "-", 2) == 0)
- {
- if(concat(0, "<stdin>") != 0)
- {
- return 1;
- }
- }
- else if(strncmp(argv[argi], "--", 3) == 0)
- {
- continue;
- }
- else
- {
- int fd = open(argv[argi], O_RDONLY);
- if(fd < 0)
- {
- fprintf(stderr, "cat: Error opening ‘%s’: %s\n", argv[argi], strerror(errno));
- return 1;
- }
-
- if(fd_copy(fd, argv[argi]) != 0)
- {
- return 1;
- }
-
- if(close(fd) < 0)
- {
- fprintf(stderr, "cat: Error closing ‘%s’: %s\n", argv[argi], strerror(errno));
- return 1;
- }
- }
- }
-
- return 0;
-}
diff --git a/cmd/chroot.1 b/cmd/chroot.1
@@ -1,52 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2023-08-04
-.Dt CHROOT 1
-.Os
-.Sh NAME
-.Nm chroot
-.Nd run in another root directory
-.Sh SYNOPSIS
-.Nm
-.Ar newroot
-.Op Ar command Op Ar args ...
-.Sh DESCRIPTION
-.Nm
-runs
-.Ar command
-in the root directory at
-.Ar newroot .
-If no
-.Ar command
-was given,
-.Nm
-instead runs
-.Lc $SHELL -i
-with
-.Ev SHELL
-itself defaulting to
-.Pa /bin/sh .
-.Sh EXIT STATUS
-If
-.Ar command
-is invoked, the exit status of
-.Nm
-shall be the exit status of
-.Ar command ;
-Otherwise, the
-.Nm
-utility shall exit with one of the following values:
-.Bl -tag -width Ds
-.It 125
-An error occured in
-.Nm
-.It 126
-.Ar command
-was found but couldn't be invoked.
-.It 127
-.Ar command
-wasn't found.
-.El
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/chroot.c b/cmd/chroot.c
@@ -1,62 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-#define _DEFAULT_SOURCE // chroot
-#include <assert.h> // assert
-#include <errno.h> // errno
-#include <stdbool.h> // false
-#include <stdio.h> // fprintf, perror
-#include <stdlib.h> // getenv
-#include <unistd.h> // chroot, execl, execv
-
-int
-main(int argc, char *argv[])
-{
- if(argc < 2)
- {
- fprintf(stderr, "chroot: Needs arguments\n");
- fprintf(stderr, "Usage: chroot <newroot> [command [args ...]]\n");
- return 125;
- }
-
- if(chroot(argv[1]) < 0)
- {
- perror("chroot");
- return 125;
- }
-
- if(chdir("/") < 0)
- {
- perror("chdir");
- return 125;
- }
-
- int ret = 0;
- errno = 0;
- if(argc == 2)
- {
- char *shell = getenv("SHELL");
- if(shell == NULL) shell = "/bin/sh";
-
- /* flawfinder: ignore. No restrictions on commands is intended */
- ret = execlp(shell, shell, "-i", NULL);
- }
- else
- {
- argv += 2;
- /* flawfinder: ignore. No restrictions on commands is intended */
- ret = execvp(argv[0], argv);
- }
-
- if(ret != 0)
- {
- perror("chroot: exec");
- if(errno == ENOENT) return 127;
-
- return 126;
- }
-
- assert(false);
-}
diff --git a/cmd/date.1 b/cmd/date.1
@@ -1,55 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2023-06-03
-.Dt DATE 1
-.Os
-.Sh NAME
-.Nm date
-.Nd display date and time
-.Sh SYNOPSIS
-.Nm
-.Op Fl u
-.Op Fl d Ar datetime
-.Op Cm + Ns Ar format
-.Sh DESCRIPTION
-When
-.Nm
-is invoked without arguments it displays the current datetime
-Otherwise, depending on the options specified, will print the datetime in a user-defined way.
-.Bl -tag -width Ds
-.It Fl d Ar datetime
-.Ar datetime
-can contain the Unix timestamp (number of seconds before and after 1970-01-01T00:00:00Z) prefixed by an @ (at) symbol,
-or a date-time formatted as
-.Ql YYYY-MM-DDThh:mm:SS[frac][Z] ,
-see
-.Xr touch 1
-for more details on the format.
-.It Fl u
-Use UTC (coordinated universal time) instead of the local time.
-.El
-.Pp
-The plus operand
-.Pq Sq +
-specifies in which format the datetime should be displayed, the format string is specified in the
-.Xr strftime 3
-manual page.
-.Sh ENVIRONMENT
-Look at the manual page of
-.Xr strftime 3
-for the environment variables, typical ones are
-.Ev TZ ,
-.Ev LC_TIME and
-.Ev LC_ALL but that depends on your system.
-.Sh EXIT STATUS
-.Ex -std
-.Sh SEE ALSO
-.Xr strftime 3
-.Sh STANDARDS
-.Nm
-is mostly compliant with the
-.St -p1003.1-2008
-specification.
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/date.c b/cmd/date.c
@@ -1,107 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-
-#include "../lib/iso_parse.h" /* iso_parse */
-
-#include <errno.h> /* errno */
-#include <locale.h> /* setlocale() */
-#include <stdio.h> /* BUFSIZ, perror(), puts() */
-#include <stdlib.h> /* exit(), strtol() */
-#include <time.h> /* time, localtime, tm, strftime */
-#include <unistd.h> /* getopt(), optarg, optind */
-
-void
-usage()
-{
- fprintf(stderr, "date [-uR][-d datetime] [+format]\n");
-}
-
-int
-main(int argc, char *argv[])
-{
- char outstr[BUFSIZ];
- struct tm *tm;
- time_t now;
- char *format = "%c";
- int uflag = 0;
- int c;
-
- setlocale(LC_ALL, "");
-
- now = time(NULL);
- if(now == (time_t)-1)
- {
- perror("date: time");
- exit(EXIT_FAILURE);
- }
-
- while((c = getopt(argc, argv, ":d:uR")) != -1)
- {
- switch(c)
- {
- case 'd': /* Custom datetime */
- now = iso_parse(optarg).tv_sec;
- break;
- case 'u': /* UTC timezone */
- uflag++;
- break;
- case 'R': /* Email (RFC 5322) format */
- format = "%a, %d %b %Y %H:%M:%S %z";
- break;
- case ':':
- fprintf(stderr, "date: Error: Missing operand for option: '-%c'\n", optopt);
- usage();
- return 1;
- case '?':
- fprintf(stderr, "date: Error: Unrecognised option: '-%c'\n", optopt);
- usage();
- return 1;
- }
- }
-
- if(uflag)
- {
- tm = gmtime(&now);
-
- if(tm == NULL)
- {
- perror("date: gmtime");
- exit(EXIT_FAILURE);
- }
- }
- else
- {
- tm = localtime(&now);
-
- if(tm == NULL)
- {
- perror("date: localtime");
- exit(EXIT_FAILURE);
- }
- }
-
- argc -= optind;
- argv += optind;
-
- (void)argc;
-
- if(*argv && **argv == '+') format = *argv + 1;
-
- errno = 0;
- if(strftime(outstr, sizeof(outstr), format, tm) == 0 && errno != 0)
- {
- perror("date: strftime");
- exit(EXIT_FAILURE);
- }
-
- if(puts(outstr) < 0)
- {
- perror("date: puts");
- exit(EXIT_FAILURE);
- }
-
- return 0;
-}
diff --git a/cmd/dirname.1 b/cmd/dirname.1
@@ -1,30 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2021-04-05
-.Dt DIRNAME 1
-.Os
-.Sh NAME
-.Nm dirname
-.Nd return the parent directory of a file pathname
-.Sh SYNOPSIS
-.Nm
-.Ar string
-.Sh DESCRIPTION
-Returns the parent directory of
-.Ar string .
-.Sh EXIT STATUS
-.Ex -std
-.Sh SEE ALSO
-.Xr direname 3
-.Sh STANDARDS
-.Nm
-is close but not compliant with the
-.St -p1003.1-2008
-specification as it uses
-.Xr dirname 3
-directly instead of extracting the non-directory portion of
-.Ar string
-with it's own algorithm.
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/dirname.c b/cmd/dirname.c
@@ -1,28 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#include <libgen.h> // dirname()
-#include <stdio.h> // puts()
-#include <string.h> // strcmp()
-
-int
-main(int argc, char *argv[])
-{
- if(argc != 2)
- {
- if((argc == 3) && (strcmp(argv[1], "--") == 0))
- {
- argv++;
- argc--;
- }
- else
- {
- fputs("usage: dirname string\n", stderr);
- return 1;
- }
- }
-
- puts(dirname(argv[1]));
- return 0;
-}
diff --git a/cmd/echo.1 b/cmd/echo.1
@@ -1,44 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2023-05-31
-.Dt ECHO 1
-.Os
-.Sh NAME
-.Nm echo
-.Nd write arguments to standard output
-.Sh SYNOPSIS
-.Nm
-.Op Ar string ...
-.Sh DESCRIPTION
-.Nm
-buffers all of it's arguments and writes them all at once to standard output, followed by a newline.
-If there is no arguments, only the newline is written.
-.Pp
-The
-.Fl -
-operand, which generally denotes an end to option processing, is treated as part of
-.Ar string .
-.Pp
-.Nm
-supports the following options:
-.Bl -tag -width Ds
-.It Fl n
-Do not print the trailing newline character.
-.El
-.Pp
-Should also be noted that this version of
-.Nm
-isn't XSI-compliant as
-.Fl n
-is parsed as an option and backslash operators aren't supported.
-See
-.Xr printf 1
-for such an utility.
-.Sh EXIT STATUS
-.Ex -std
-.Sh STANDARDS
-Not XSI-compliant but should be compliant to
-.St -p1003.1-2008
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/echo.c b/cmd/echo.c
@@ -1,77 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-#include <stdbool.h> /* bool */
-#include <stdio.h> /* perror */
-#include <stdlib.h> /* malloc */
-#include <string.h> /* strlen */
-#include <unistd.h> /* write */
-
-int
-main(int argc, char *argv[])
-{
- size_t arg_len = 0;
- char *buffer, *buffer_p;
- int err = 0;
- bool opt_n = false;
-
- if((argc >= 2) && (strncmp(argv[1], "-n", 3) == 0))
- {
- opt_n = true;
- argc--;
- argv++;
- }
-
- for(int i = 1; i < argc; i++)
- {
- arg_len += strlen(argv[i]) + 1; // str + space|newline
- }
-
- if(arg_len == 0)
- {
- if(opt_n) return 0;
-
- if(write(1, "\n", 1) < 1)
- {
- perror("echo: write(1, \"\n\", 1)");
- return 1;
- }
-
- return 0;
- }
-
- if(opt_n) arg_len--; // no newline
-
- buffer = malloc(arg_len);
- if(buffer == NULL)
- {
- perror("echo: malloc(arg_len)");
- return 1;
- }
-
- buffer_p = buffer;
- for(int i = 1; i < argc; i++)
- {
- size_t len = strlen(argv[i]);
- memcpy(buffer_p, argv[i], len);
- buffer_p += len;
-
- if(i < argc - 1)
- {
- *buffer_p++ = ' ';
- }
- }
- if(!opt_n) *buffer_p++ = '\n';
-
- if(write(1, buffer, arg_len) < (ssize_t)arg_len)
- {
- perror("echo: write(1, buffer, arg_len)");
- err++;
- }
-
- free(buffer);
-
- return err;
-}
diff --git a/cmd/env.1 b/cmd/env.1
@@ -1,71 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2022-04-19
-.Dt ENV 1
-.Os
-.Sh NAME
-.Nm env
-.Nd control and print environment
-.Sh SYNOPSIS
-.Nm
-.Op Fl i
-.Op Fl u Ar name
-.Op Fl -unset= Ns Ar name
-.Op Ar name Ns = Ns Ar value
-.Op Ar command Op Ar argument ...
-.Sh DESCRIPTION
-.Nm
-with no arguments prints the environment, or if specified runs
-.Ar command .
-.Pp
-The environment can be modified via the following options:
-.Bl -tag -width Ds
-.It Fl i
-Ignore the existing environment.
-.It Fl u Ar name | Fl -unset= Ns Ar name
-Removes the variable named
-.Ar name
-from the new environment.
-.It Ar name Ns = Ns Ar value
-Adds the variable named
-.Ar name
-with the value
-.Ar value
-into the new environment, it cannot itself contain the
-.Qq =
-character.
-.El
-.Sh EXIT STATUS
-If
-.Ar command
-is invoked, the exit status of
-.Nm
-shall be the exit status of
-.Ar command ;
-Otherwise, the env utility shall exit with one of the following values:
-.Bl -tag -width Ds
-.It 0
-.Nm
-completed successfully.
-.It 1
-An error occured in
-.Nm
-.It 126
-.Ar command
-was found but couldn't be invoked.
-.It 127
-.Ar command
-wasn't found.
-.El
-.Sh STANDARDS
-.Nm
-is compliant with the
-.St -p1003.1-2008
-specification.
-.Pp
-The
-.Fl u
-flag is an extension known to be present in FreeBSD, NetBSD, GNU coreutils, BusyBox, ...
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/env.c b/cmd/env.c
@@ -1,132 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-#include <assert.h> // assert
-#include <errno.h> // errno
-#include <stdbool.h> // bool, true, false
-#include <stdio.h> // puts, fprintf
-#include <stdlib.h> // putenv
-#include <string.h> // strchr, strerror
-#include <unistd.h> // getopt, opt*
-
-extern char **environ;
-char *envclear[1];
-
-int export()
-{
- int i = 0;
-
- for(; environ[i] != NULL; i++)
- {
- if(puts(environ[i]) < 0)
- {
- perror("env: puts(environ[i])");
- return 1;
- }
- }
-
- return 0;
-}
-
-void
-usage()
-{
- fprintf(stderr, "env [-i] [-u key | --unset=key] [key=value ...] [command [args]]\n");
-}
-
-int
-main(int argc, char *argv[])
-{
- int c;
- bool flag_i = false;
- char *val;
-
- /* flawfinder: ignore. Old implementations of getopt should fix themselves */
- while((c = getopt(argc, argv, ":iu:-:")) != -1)
- {
- switch(c)
- {
- case 'i':
- flag_i = true;
- break;
- case 'u':
- unsetenv(optarg);
- break;
- case '-':
- val = strchr(optarg, '=');
- if(val == NULL)
- {
- fprintf(stderr, "env: Error: Missing = in long option\n");
- return 1;
- }
-
- *val = 0;
- val++;
-
- if(strcmp(optarg, "unset") != 0)
- {
- fprintf(stderr, "env: Error: Unknown long option --%s\n", optarg);
- return 1;
- }
- unsetenv(val);
-
- break;
- case ':':
- fprintf(stderr, "env: Error: Missing operand for option: '-%c'\n", optopt);
- usage();
- return 1;
- case '?':
- fprintf(stderr, "env: Error: Unrecognised option: '-%c'\n", optopt);
- usage();
- return 1;
- default:
- assert(false);
- }
- }
-
- argc -= optind;
- argv += optind;
-
- if(flag_i)
- {
- environ = envclear;
- envclear[0] = NULL;
- }
-
- for(; argv[0]; argv++, argc--)
- {
- char *sep = strchr(argv[0], '=');
- if(sep == NULL)
- {
- break;
- }
-
- *sep = 0;
- sep++;
-
- if(setenv(argv[0], sep, 1))
- {
- fprintf(stderr, "env: setenv(%s, %s, 1): %s\n", argv[0], sep, strerror(errno));
- return 1;
- }
- }
-
- if(argc < 1)
- {
- return export();
- }
-
- assert(argv[0]);
- errno = 0;
- /* flawfinder: ignore. No restrictions on commands is intended */
- if(execvp(argv[0], argv) < 0)
- {
- fprintf(stderr, "env: execvp(\"%s\", ...): %s\n", argv[0], strerror(errno));
-
- return (errno == ENOENT) ? 127 : 126;
- }
-
- assert(false);
-}
diff --git a/cmd/false.1 b/cmd/false.1
@@ -1,18 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2022-02-13
-.Dt FALSE 1
-.Os
-.Sh NAME
-.Nm false
-.Nd do nothing, unsuccessfully
-.Sh SYNOPSIS
-.Nm
-.Sh DESCRIPTION
-.Nm
-will return with exit status code one.
-.Sh EXIT STATUS
-One.
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/false.c b/cmd/false.c
@@ -1,8 +0,0 @@
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-int
-main(void)
-{
- return 1;
-}
diff --git a/cmd/id.c b/cmd/id.c
@@ -1,420 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _DEFAULT_SOURCE // getgrouplist (4.4BSD+)
-#include <grp.h> // getgrgid, getgroups, getgrouplist(glibc)
-#include <pwd.h> // getpwuid
-#include <stdbool.h> // bool
-#include <stdio.h> // printf, perror
-#include <stdlib.h> // malloc, free
-#include <sys/types.h> // uid_t
-#include <unistd.h> // getuid, getgid, getopt, opt*, getgrouplist(FreeBSD, NetBSD)
-
-bool name_flag = false;
-
-static int
-simple_list_groups(struct passwd *pw, int ngroups, gid_t *groups)
-{
- for(int i = 0; i < ngroups; i++)
- {
- if(name_flag)
- {
- struct group *lgr = getgrgid(groups[i]);
- if(lgr == NULL)
- {
- return 1;
- }
-
- int sep = ' ';
- if(i == ngroups - 1)
- {
- sep = '\n';
- }
-
- int ret = printf("%s%c", lgr->gr_name, sep);
- if(ret < 0)
- {
- return 1;
- }
- }
- else
- {
- int sep = ' ';
- if(i == ngroups - 1)
- {
- sep = '\n';
- }
-
- int ret = printf("%u%c", groups[i], sep);
- if(ret < 0)
- {
- return 1;
- }
- }
- }
-
- return 0;
-}
-
-static int
-list_groups(struct passwd *pw, int ngroups, gid_t *groups)
-{
- printf(" groups=");
-
- for(int i = 0; i < ngroups; i++)
- {
- struct group *lgr = getgrgid(groups[i]);
-
- if(name_flag)
- {
- if(lgr == NULL)
- {
- return 1;
- }
-
- int sep = ' ';
- if(i == ngroups - 1)
- {
- sep = '\0';
- }
-
- int ret = printf("%s%c", lgr->gr_name, sep);
- if(ret < 0)
- {
- return 1;
- }
- }
- else
- {
- int ret = printf("%u", groups[i]);
- if(ret < 0)
- {
- return 1;
- }
-
- if(lgr != NULL)
- {
- int ret = printf("(%s)", lgr->gr_name);
- if(ret < 0)
- {
- return 1;
- }
- }
-
- if(i != ngroups - 1)
- {
- printf(",");
- }
- }
- }
-
- return 0;
-}
-
-int
-print_gid(char *field, struct group *gr, gid_t gid)
-{
- if(gr && gr->gr_name)
- {
- if(name_flag)
- {
- return printf("%s=%s", field, gr->gr_name);
- }
-
- return printf("%s=%u(%s)", field, gid, gr->gr_name);
- }
- else
- {
- if(name_flag)
- {
- return -1;
- }
-
- return printf("%s=%u", field, gid);
- }
-}
-
-int
-print_uid(char *field, struct passwd *pw, uid_t uid)
-{
- if(pw && pw->pw_name)
- {
- if(name_flag)
- {
- return printf("%s=%s", field, pw->pw_name);
- }
-
- return printf("%s=%u(%s)", field, uid, pw->pw_name);
- }
- else
- {
- if(name_flag)
- {
- return -1;
- }
-
- return printf("%s=%u", field, uid);
- }
-}
-
-void
-safe_getpwuid(uid_t uid, struct passwd *res)
-{
- struct passwd *pw = getpwuid(uid);
-
- if(pw != NULL)
- {
- *res = *pw;
- }
-}
-
-enum id_modes
-{
- ID_NORMAL,
- ID_GROUPS,
- ID_GID,
- ID_UID,
-};
-
-void
-usage()
-{
- fprintf(stderr, "Usage: id [-Ggu] [-nr] [user]\n");
-}
-
-int
-main(int argc, char *argv[])
-{
- int ret = 0, c = 0;
- enum id_modes mode = ID_NORMAL;
- bool real_flag = false;
-
- int ngroups = 0;
- int ngroups_max = (int)sysconf(_SC_NGROUPS_MAX) + 1;
- gid_t *groups = malloc(sizeof(gid_t) * ngroups_max);
- if(groups == NULL)
- {
- perror("groups malloc");
- return 1;
- }
-
- // geteuid, getuid, getegid, getgid shall always be successful
- uid_t uid = getuid();
- uid_t euid = geteuid();
- gid_t gid = getgid();
- gid_t egid = getegid();
-
- struct passwd pw = {.pw_uid = uid, .pw_gid = gid};
- struct passwd epw = {.pw_uid = euid, .pw_gid = egid};
-
- /* flawfinder: ignore. Old implementations of getopt should fix themselves */
- while((c = getopt(argc, argv, ":Ggunr")) != EOF)
- {
- switch(c)
- {
- case 'G':
- mode = ID_GROUPS;
- break;
- case 'u':
- mode = ID_UID;
- break;
- case 'g':
- mode = ID_GID;
- break;
- case 'n':
- name_flag = true;
- break;
- case 'r':
- real_flag = true;
- break;
- default:
- usage();
- free(groups);
- return 1;
- }
- }
-
- argc -= optind;
- argv += optind;
-
- if(argc == 0)
- {
- safe_getpwuid(uid, &pw);
- safe_getpwuid(euid, &epw);
-
- // Get groups from currently running process instead of configuration
- ngroups = getgroups(ngroups_max, groups);
- if(ngroups < 0)
- {
- perror("getgroups");
- goto failure;
- }
- }
- else if(argc == 1)
- {
- struct passwd *pw_n = getpwnam(argv[0]);
-
- if(pw_n == NULL)
- {
- goto failure;
- }
- pw = *pw_n;
- epw = *pw_n;
-
- uid = pw.pw_uid;
- euid = epw.pw_uid;
- gid = pw.pw_gid;
- egid = epw.pw_gid;
-
- // Can only get groups from configuration
- ngroups = getgrouplist(pw.pw_name, pw.pw_gid, groups, &ngroups_max);
- if(ngroups < 0)
- {
- perror("getgrouplist");
- goto failure;
- }
- }
- else
- {
- usage();
- }
-
- struct group *gr = getgrgid(gid);
- struct group *egr = getgrgid(egid);
-
- if(mode == ID_GID)
- {
- if(!real_flag)
- {
- gid = egid;
- }
-
- if(!name_flag)
- {
- ret = printf("%u\n", gid);
- }
- else
- {
- if(gr == NULL || gr->gr_name == NULL)
- {
- ret--;
- fprintf(stderr, "id: cannot find name for group ID %u\n", gid);
- printf("%u\n", gid);
- }
- else
- {
- ret = printf("%s\n", gr->gr_name);
- }
- }
-
- if(ret < 0)
- {
- goto failure;
- }
-
- goto done;
- }
-
- if(mode == ID_UID)
- {
- if(!real_flag)
- {
- uid = euid;
- }
-
- if(!name_flag)
- {
- ret = printf("%u\n", uid);
- }
- else
- {
- if(pw.pw_name == NULL)
- {
- ret--;
- fprintf(stderr, "id: cannot find name for user ID %u\n", uid);
- printf("%u\n", uid);
- }
- else
- {
- ret = printf("%s\n", pw.pw_name);
- }
- }
-
- if(ret < 0)
- {
- goto failure;
- }
-
- goto done;
- }
-
- if(mode == ID_GROUPS)
- {
- if(real_flag)
- {
- ret = simple_list_groups(&pw, ngroups, groups);
- }
- else
- {
- ret = simple_list_groups(&epw, ngroups, groups);
- }
-
- if(ret != 0)
- {
- goto failure;
- }
-
- goto done;
- }
-
- ret = print_uid("uid", &pw, uid);
- if(ret < 0)
- {
- goto failure;
- }
-
- if(euid != uid)
- {
- ret = print_uid(" euid", &epw, euid);
- if(ret < 0)
- {
- goto failure;
- }
- }
-
- ret = print_gid(" gid", gr, gid);
- if(ret < 0)
- {
- goto failure;
- }
-
- if(egid != gid)
- {
- ret = print_gid(" egid", egr, egid);
-
- if(ret < 0)
- {
- goto failure;
- }
- }
-
- if(list_groups(&pw, ngroups, groups) != 0)
- {
- goto failure;
- }
-
- ret = printf("\n");
- if(ret < 0)
- {
- goto failure;
- }
-
-done:
- free(groups);
- return 0;
-
-failure:
- free(groups);
- return 1;
-}
diff --git a/cmd/link.1 b/cmd/link.1
@@ -1,28 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2022-04-22
-.Dt LINK 1
-.Os
-.Sh NAME
-.Nm link
-.Nd call
-.Xr link 3
-function
-.Sh SYNOPSIS
-.Nm
-.Ar reference
-.Ar destination
-.Sh DESCRIPTION
-.Nm
-will call
-.Xr link 3
-with
-.Ar reference
-and
-.Ar destination ,
-returning an error message in case of failure.
-.Sh EXIT STATUS
-.Ex -std
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/link.c b/cmd/link.c
@@ -1,25 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-#include <stdio.h> /* perror, fprintf */
-#include <unistd.h> /* link */
-
-int
-main(int argc, char *argv[])
-{
- if(argc != 3)
- {
- fprintf(stderr, "usage: link <reference> <destination>\n");
- return 1;
- }
-
- if(link(argv[1], argv[2]) != 0)
- {
- perror("link");
- return 1;
- }
-
- return 0;
-}
diff --git a/cmd/nproc.c b/cmd/nproc.c
@@ -1,51 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _DEFAULT_SOURCE // _SC_NPROCESSORS_CONF
-#include <stdio.h> // printf
-#include <unistd.h> // sysconf, getopt, opt*
-
-void
-usage()
-{
- fprintf(stderr, "Usage: nproc [-a]\n");
-}
-
-int
-main(int argc, char *argv[])
-{
- // _SC_NPROCESSORS_CONF &_SC_NPROCESSORS_ONLN aren't in POSIX yet but have been accepted
- // https://www.austingroupbugs.net/view.php?id=339
- int target = _SC_NPROCESSORS_ONLN;
-
- int c;
- while((c = getopt(argc, argv, ":a")) != EOF)
- {
- switch(c)
- {
- case 'a': // all processors
- target = _SC_NPROCESSORS_CONF;
- break;
- case ':':
- fprintf(stderr, "nproc: Error: Missing operand for option: '-%c'\n", optopt);
- usage();
- return 1;
- case '?':
- fprintf(stderr, "nproc: Error: Unrecognised option: '-%c'\n", optopt);
- usage();
- return 1;
- }
- }
-
- long np = sysconf(target);
- if(np < 0)
- {
- perror("sysconf");
- return 1;
- }
-
- printf("%ld\n", np);
-
- return 0;
-}
diff --git a/cmd/pwd.c b/cmd/pwd.c
@@ -1,36 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-#include <stdio.h> /* puts, perror, printf */
-#include <unistd.h> /* getcwd() */
-
-int
-main(int argc, char *argv[])
-{
- char pwd[BUFSIZ];
-
- if(argc != 1)
- {
- fprintf(stderr, "usage: pwd\n");
- return 1;
- }
-
- if(getcwd(pwd, sizeof(pwd)) == NULL)
- {
- perror("pwd: getcwd");
- return 1;
- }
- else
- {
- if(puts(pwd) < 0)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
-}
diff --git a/cmd/seq.c b/cmd/seq.c
@@ -1,120 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-#include <errno.h> // errno
-#include <stdbool.h> // bool, true, false
-#include <stdio.h> // puts, fprintf
-#include <stdlib.h> // atoi, exit
-#include <unistd.h> // getopt, optarg, optind
-
-char *separator = "\n";
-bool zero_pad = false;
-
-void
-seq(long i, long step, long last)
-{
- if(i == last)
- {
- printf("%li%s", i, separator);
- }
- else if(i < last)
- {
- for(; i <= last; i += step)
- printf("%li%s", i, separator);
- }
- else if(i > last)
- {
- for(; i >= last; i -= step)
- printf("%li%s", i, separator);
- }
-}
-
-static long unsigned int
-safe_labs(long int a)
-{
- if(a >= 0)
- {
- return (long unsigned int)a;
- }
- else
- {
- return (long unsigned int)-a;
- }
-}
-
-long
-get_num(char *str)
-{
- errno = 0;
-
- long num = strtol(str, NULL, 10);
-
- if(errno != 0)
- {
- perror("seq: strtol:");
- exit(1);
- }
-
- return num;
-}
-
-void
-usage(void)
-{
- fprintf(stderr, "usage: seq [-w] [-s separator] [first [step]] last\n");
-}
-
-int
-main(int argc, char *argv[])
-{
- int c;
-
- /* flawfinder: ignore. Old implementations of getopt should fix themselves */
- while((c = getopt(argc, argv, ":ws:")) != -1)
- {
- switch(c)
- {
- case 'w':
- zero_pad = true;
- break;
- case 's':
- separator = optarg;
- break;
- case '?':
- usage();
- return 1;
- }
- }
-
- argc -= optind;
- argv += optind;
-
- long first = 1;
- long step = 1;
- long last = 1;
-
- switch(argc)
- {
- case 1:
- last = get_num(argv[0]);
- break;
- case 2:
- first = get_num(argv[0]);
- last = get_num(argv[1]);
- break;
- case 3:
- first = get_num(argv[0]);
- step = (long)safe_labs(get_num(argv[1]));
- last = get_num(argv[2]);
- break;
- default:
- usage();
- return 1;
- }
-
- seq(first, step, last);
-
- return 0;
-}
diff --git a/cmd/sleep.1 b/cmd/sleep.1
@@ -1,30 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2022-11-19
-.Dt SLEEP 1
-.Os
-.Sh NAME
-.Nm sleep
-.Nd delay for a specified amount of time
-.Sh SYNOPSIS
-.Nm
-.Ar duration ...
-.Sh DESCRIPTION
-The
-.Nm
-utily shell suspends execution for the total of each
-.Ar duration
-argument.
-.Pp
-.Ar duration
-is a non-negative decimal number including floats, optionally followed by a suffix: s for seconds (default), m for minutes, h for hours.
-Longer durations are taken as out of scope.
-.Sh EXIT STATUS
-.Ex -std
-.Sh SEE ALSO
-.Xr at 1 ,
-.Xr crontab 1 ,
-.Xr nanosleep 3
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/sleep.c b/cmd/sleep.c
@@ -1,138 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-#include <errno.h> // errno
-#include <stdio.h> // fprintf, perror, sscanf
-#include <stdlib.h> // exit
-#include <time.h> // nanosleep, timespec
-
-// Maybe should be moved in ./lib with iso_parse
-static struct timespec
-strtodur(char *s)
-{
- struct timespec dur = {.tv_sec = 0, .tv_nsec = 0};
-
- if(s[0] == 0)
- {
- fprintf(stderr, "sleep: Got an empty string as duration\n");
- return dur;
- }
-
- int parsed = 0;
- if(s[0] != '.' && s[0] != ',')
- {
- errno = 0;
- int ret = sscanf(s, "%10ld%n", &dur.tv_sec, &parsed);
- if(ret < 1)
- {
- if(errno == 0)
- {
- fprintf(stderr, "sleep: Not a number: %s\n", s);
- }
- else
- {
- perror("sleep: sscanf");
- }
- exit(EXIT_FAILURE);
- }
-
- s += parsed;
-
- if(s[0] == 0) return dur;
- }
-
- if(s[0] == '.' || s[0] == ',')
- {
- float fraction = 0.0;
- if(s[1] == 0) return dur;
- if(s[0] == ',') s[0] = '.';
-
- parsed = 0;
- errno = 0;
- if(sscanf(s, "%10f%n", &fraction, &parsed) < 1)
- {
- if(errno == 0)
- {
- fprintf(stderr, "sleep: Decimal part is not a number: %s\n", s);
- }
- else
- {
- perror("sleep: sscanf");
- }
- exit(EXIT_FAILURE);
- }
-
- dur.tv_nsec = fraction * 1000000000;
- s += parsed;
- }
-
- if(s[0] == 0) return dur;
-
- if(s[1] != 0)
- {
- fprintf(stderr, "sleep: suffix '%s' is too long, should be only one character\n", s);
- exit(1);
- }
-
- switch(s[0])
- {
- case 's': // seconds
- break;
- case 'm': // minutes
- dur.tv_sec *= 60;
- break;
- case 'h': // hours
- dur.tv_sec *= 60 * 60;
- break;
- default:
- fprintf(stderr, "sleep: Unknown suffix %c\n", s[0]);
- exit(1);
- }
-
- return dur;
-}
-
-int
-main(int argc, char *argv[])
-{
- struct timespec dur = {.tv_sec = 0, .tv_nsec = 0};
- for(int i = 1; i < argc; i++)
- {
- struct timespec arg_dur = strtodur(argv[i]);
-
- dur.tv_sec += arg_dur.tv_sec;
- dur.tv_nsec += arg_dur.tv_nsec;
- if(dur.tv_nsec > 999999999)
- {
- dur.tv_nsec = 0;
- dur.tv_sec += 1;
- }
- }
- if(dur.tv_sec == 0 && dur.tv_nsec == 0)
- {
- fprintf(stderr, "sleep: Got a duration of 0\n");
- return 1;
- }
-
- //fprintf(stderr, "sleep: going to sleep for %ld.%09ld seconds\n", dur.tv_sec, dur.tv_nsec);
-
- errno = 0;
- if(nanosleep(&dur, &dur) < 0)
- {
- if(errno == EINTR)
- {
- fprintf(stderr,
- "sleep: Interrupted during sleep, still had %ld.%09ld seconds remaining\n",
- dur.tv_sec,
- dur.tv_nsec);
- }
- else
- {
- perror("sleep: nanosleep");
- }
- }
-
- return 0;
-}
diff --git a/cmd/strings.1 b/cmd/strings.1
@@ -1,58 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2023-08-11
-.Dt STRINGS 1
-.Os
-.Sh NAME
-.Nm strings
-.Nd find printable strings
-.Sh SYNOPSIS
-.Nm
-.Op Fl a
-.Op Fl t Ar format
-.Op Fl n Ar number
-.Op Ar files ...
-.Sh DESCRIPTION
-.Nm
-reads each
-.Ar file
-in sequence and writes printable strings longer than 4 or
-.Ar number .
-If no
-.Ar file
-is given,
-.Nm
-reads from the standard input.
-.Sh OPTIONS
-.Bl -tag -width Ds
-.It Fl a
-Ignored, present for POSIX compatibility.
-Files are always scanned in their entirety and it is considered a flaw to do
-otherwise.
-.It Fl n Ar number
-Change the minimum string length (default: 4).
-.It Fl t Ar format
-Write the byte offset of each string found, the format is dependend on the
-format argument:
-.Bl -tag -width d
-.It d
-Decimal
-.It o
-Octal
-.It x
-Hexadecimal
-.El
-.El
-.Sh EXIT STATUS
-.Ex -std
-.Sh SEE ALSO
-.Xr nm 1 ,
-.Xr isprint 3
-.Sh STANDARDS
-.Nm
-is compliant with the
-.St -p1003.1-2008
-specification.
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/strings.c b/cmd/strings.c
@@ -1,212 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-#include <ctype.h> /* isprint() */
-#include <errno.h> /* errno */
-#include <fcntl.h> /* open(), O_RDONLY */
-#include <limits.h> /* LONG_MIN, LONG_MAX */
-#include <stdio.h> /* fprintf(), BUFSIZ */
-#include <stdlib.h> /* strtol() */
-#include <string.h> /* strerror(), strncmp(), memset() */
-#include <unistd.h> /* read(), write(), close(), getopt(), optarg, optind */
-
-size_t opt_min_strlen = 4;
-char *opt_offset_format = NULL;
-
-int
-print_string(char *buffer, size_t offset)
-{
- int ret = 0;
-
- if(opt_offset_format == NULL)
- {
- ret = printf("%s\n", buffer);
- }
- else
- {
- /* flawfinder: ignore. opt_offset_format isn't user-provided */
- ret = printf(opt_offset_format, offset, buffer);
- }
-
- if(ret < 0)
- {
- fprintf(stderr, "strings: Error writing: %s\n", strerror(errno));
- return 1;
- }
- else
- {
- return 0;
- }
-}
-
-int
-concat(int fd, const char *fdname)
-{
- ssize_t c;
- char read_buf[4096];
- char write_buf[4096];
- size_t write_pos = 0;
- size_t offset = 0;
-
- memset(write_buf, 0, sizeof(write_buf));
-
- while((c = read(fd, read_buf, sizeof(read_buf))) > 0)
- {
- int read_pos = 0;
- char b = 0;
-
- for(; read_pos < c; read_pos++)
- {
- b = read_buf[read_pos];
-
- if(isprint(b) && write_pos < 4096)
- {
- write_buf[write_pos++] = b;
- }
- else
- {
- if(write_pos >= opt_min_strlen)
- {
- write_buf[write_pos + 1] = 0;
-
- if(print_string(write_buf, offset) != 0)
- {
- return 1;
- }
- }
-
- offset += write_pos;
- offset++;
- write_pos = 0;
- memset(write_buf, 0, sizeof(write_buf));
- }
- }
- }
-
- if(c < 0)
- {
- fprintf(stderr, "strings: Error reading ‘%s’: %s\n", fdname, strerror(errno));
- return 1;
- }
-
- return 0;
-}
-
-void
-usage()
-{
- fprintf(stderr, "strings: [-a] [-t format] [-n number] [file...]\n");
-}
-
-int
-main(int argc, char *argv[])
-{
- int c;
- const char *errstr = NULL;
-
- /* flawfinder: ignore. Old implementations of getopt should fix themselves */
- while((c = getopt(argc, argv, ":an:t:")) != -1)
- {
- switch(c)
- {
- case 'a':
- /* Structure is always ignored */
- break;
- case 'n':
- errno = 0;
- char *endptr = "";
- opt_min_strlen = strtol(optarg, &endptr, 10);
- if(*endptr != 0)
- {
- // extraneous characters is invalid
- errno = EINVAL;
- }
- if(errno != 0)
- {
- fprintf(stderr, "strings: Option `-n %s`: %s\n", optarg, strerror(errno));
- usage();
- return 1;
- }
- if(opt_min_strlen == LLONG_MIN || opt_min_strlen < 1)
- {
- fprintf(stderr, "strings: Option `-n %s` is too small\n", optarg);
- usage();
- return 1;
- }
- if(opt_min_strlen == LLONG_MAX || opt_min_strlen > 4096)
- {
- fprintf(stderr, "strings: Option `-n %s` is too large\n", optarg);
- usage();
- return 1;
- }
- break;
- case 't':
- if(strnlen(optarg, 2) > 1)
- {
- usage();
- return 1;
- }
-
- switch(optarg[0])
- {
- case 'o':
- opt_offset_format = "%zo %s\n";
- break;
- case 'x':
- opt_offset_format = "%zx %s\n";
- break;
- case 'd':
- opt_offset_format = "%zd %s\n";
- break;
- default:
- fprintf(stderr, "strings: Unknown format: %s\n", optarg);
- usage();
- return 1;
- }
- break;
- }
- }
-
- argc -= optind;
- argv += optind;
-
- if(argc < 1)
- {
- return concat(0, "<stdin>");
- }
-
- for(int argi = 0; argi < argc; argi++)
- {
- if(strncmp(argv[argi], "-", 2) == 0)
- {
- if(concat(0, "<stdin>") != 0)
- {
- return 1;
- }
- }
- else
- {
- int fd = open(argv[argi], O_RDONLY);
- if(fd <= 0)
- {
- fprintf(stderr, "strings: Error opening ‘%s’: %s\n", argv[argi], strerror(errno));
- return 1;
- }
-
- if(concat(fd, argv[argi]) != 0)
- {
- return 1;
- }
-
- if(close(fd) < 0)
- {
- fprintf(stderr, "strings: Error closing ‘%s’: %s\n", argv[argi], strerror(errno));
- return 1;
- }
- }
- }
-
- return 0;
-}
diff --git a/cmd/sync.1 b/cmd/sync.1
@@ -1,22 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2021-12-27
-.Dt SYNC 1
-.Os
-.Sh NAME
-.Nm sync
-.Nd Schedule file system updates
-.Sh SYNOPSIS
-.Nm
-.Sh DESCRIPTION
-.Nm
-shall cause all information in memory that updates file systems to be scheduled for writing out to all file systems.
-.Sh EXIT STATUS
-.Ex -std
-.Sh SEE ALSO
-.Xr sync 2
-.Sh STANDARDS
-No applicable one known.
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/sync.c b/cmd/sync.c
@@ -1,14 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _XOPEN_SOURCE 700 // XSI but not mere POSIX
-#include <unistd.h> // sync()
-
-int
-main(void)
-{
- // Maybe consider hooking up to fsync(3p)
- sync();
- return 0;
-}
diff --git a/cmd/tee.c b/cmd/tee.c
@@ -1,105 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-#include <assert.h> /* assert() */
-#include <errno.h> /* errno */
-#include <stdio.h> /* fprintf(), fgetc(), fputc(), fclose(), fopen() */
-#include <stdlib.h> /* malloc(), free(), abort() */
-#include <string.h> /* strerror() */
-#include <unistd.h> /* getopt(), opt… */
-
-void
-cleanup(FILE **fds)
-{
- if(fds != NULL)
- {
- free(fds);
- }
-}
-
-int
-main(int argc, char *argv[])
-{
- const char *mode = "w";
- FILE **fds = {NULL}; // Shut up GCC
- int c;
-
- while((c = getopt(argc, argv, ":ai")) != -1)
- {
- switch(c)
- {
- case 'a':
- mode = "a";
- break;
- case 'i': /* ignore SIGINT */;
- break;
- }
- }
-
- argc -= optind;
- argv += optind;
-
- if(argc > 0)
- {
- fds = malloc(sizeof(FILE *) * (size_t)argc);
-
- if(!fds)
- {
- fprintf(stderr, "tee: Cannot allocate fd array: %s\n", strerror(errno));
- return 1;
- }
- }
-
- for(int argi = 0; argi < argc; argi++)
- {
- assert(argv[argi]);
-
- // POSIX: implementations shouldn't treat '-' as stdin
- fds[argi] = fopen(argv[argi], mode);
-
- if(fds[argi] == NULL)
- {
- fprintf(stderr, "tee: Error opening ‘%s’: %s\n", argv[argi], strerror(errno));
- cleanup(fds);
- return 1;
- }
- }
-
- // main loop, note that failed writes shouldn't make tee exit
- int err = 0;
-
- while((c = fgetc(stdin)) != EOF)
- {
- if(fputc(c, stdout) == EOF)
- {
- fprintf(stderr, "tee: Error writing ‘<stdout>’: %s\n", strerror(errno));
- err = 1;
- errno = 0;
- }
-
- for(int argi = 0; argi < argc; argi++)
- {
- if(fputc(c, fds[argi]) == EOF)
- {
- fprintf(stderr, "tee: Error writing to argument %d: %s\n", argi, strerror(errno));
- err = 1;
- errno = 0;
- }
- }
- }
-
- // cleanup
- for(int argi = 0; argi < argc; argi++)
- {
- if(fclose(fds[argi]) != 0)
- {
- abort();
- }
- }
-
- cleanup(fds);
-
- return err;
-}
diff --git a/cmd/time.1 b/cmd/time.1
@@ -1,48 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2022-12-01
-.Dt TIME 1
-.Os
-.Sh NAME
-.Nm time
-.Nd measure time used by a command
-.Sh SYNOPSIS
-.Nm
-.Op Fl p
-.Ar command
-.Op Ar argument...
-.Sh DESCRIPTION
-.Nm
-measures the wall-clock time, user CPU time, system/kernel CPU time, used by
-.Ar command
-and outputs it to stderr.
-.Sh OPTIONS
-.Fl p
-is ignored for compatibility reasons, the output is always in the POSIX format.
-.Sh EXIT STATUS
-If
-.Ar command
-is invoked, the exit status should be the one given by
-.Ar command .
-Otherwise, it will exit with the following values:
-.Bl -tag -width 1-125
-.It 1-125
-An error occured in
-.Ar command
-.It 126
-.Ar command
-was found but couldn't be invoked
-.It 127
-.Ar command
-could not be found
-.El
-.Sh SEE ALSO
-.Xr times 3
-.Sh STANDARDS
-.Nm
-is compliant with the
-.St -p1003.1-2008
-specification.
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/time.c b/cmd/time.c
@@ -1,112 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L
-#include <errno.h> // errno
-#include <stdio.h> // perror, fprintf
-#include <stdlib.h> // abort
-#include <sys/times.h> // times
-#include <sys/wait.h> // waitpid
-#include <unistd.h> // sysconf, fork, execvp, getopt
-
-void
-usage()
-{
- fprintf(stderr, "Usage: time command [argument ...]\n");
-}
-
-int
-main(int argc, char *argv[])
-{
- struct tms tms;
- int ret = 0;
-
- if(argc <= 1)
- {
- usage();
- return 0;
- }
-
- int c = -1;
- while((c = getopt(argc, argv, ":p")) != -1)
- {
- switch(c)
- {
- case 'p': // POSIX format (default)
- break;
- case ':':
- fprintf(stderr, "time: Error: Missing operand for option: '-%c'\n", optopt);
- usage();
- return 1;
- case '?':
- fprintf(stderr, "time: Error: Unrecognised option: '-%c'\n", optopt);
- usage();
- return 1;
- }
- }
-
- argc -= optind;
- argv += optind;
- (void)argc;
-
- long ticks = sysconf(_SC_CLK_TCK);
- if(ticks <= 0)
- {
- perror("time: sysconf(_SC_CLK_TCK)");
- return 1;
- }
-
- clock_t t0 = times(&tms);
- if(t0 == (clock_t)-1)
- {
- perror("time: times");
- return 1;
- }
-
- // Note: posix_spawnp seems to lack enough error information
- pid_t pid = fork();
- switch(pid)
- {
- case -1:
- perror("time: fork");
- return 1;
- case 0:
- /* flawfinder: ignore. No restrictions on commands is intended */
- execvp(argv[0], argv);
- ret = 126 + (errno == ENOENT);
- perror("time: execvp");
- return ret;
- default:
- break;
- }
-
- int status = 0;
- waitpid(pid, &status, 0);
-
- int t1 = times(&tms);
- if(t1 == (clock_t)-1)
- {
- perror("time: times");
- return 1;
- }
-
- if(WIFSIGNALED(status))
- {
- fprintf(stderr, "time: Command terminated by signal %d\n", WTERMSIG(status));
- ret = 128 + WTERMSIG(status);
- }
-
- fprintf(stderr,
- "real %f\nuser %f\nsys %f\n",
- (t1 - t0) / (double)ticks,
- tms.tms_cutime / (double)ticks,
- tms.tms_cstime / (double)ticks);
-
- if(WIFEXITED(status))
- {
- ret = WEXITSTATUS(status);
- }
-
- return ret;
-}
diff --git a/cmd/touch.1 b/cmd/touch.1
@@ -1,85 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2023-06-03
-.Dt TOUCH 1
-.Os
-.Sh NAME
-.Nm touch
-.Nd change file access and modification times
-.Sh SYNOPSIS
-.Nm
-.Op Fl achm
-.Op Fl d Ar datetime | Fl r Ar ref_file
-.Ar file...
-.Sh DESCRIPTION
-.Nm
-changes the date modification and access times on each
-.Ar file
-it is given.
-.Bl -tag -width Ds
-.It Fl a
-Change the access time, no changes to modification time unless
-.Fl m
-is also given.
-.It Fl c
-Do not create
-.Ar file .
-.It Fl h
-Do not follow symlinks.
-.It Fl m
-Change the modification time, no changes to access time unless
-.Fl a
-is also given.
-.It Fl d Ar datetime
-Use the specified
-.Ar datetime
-instead of the current time.
-Should be formatted as
-.Ql YYYY-MM-DDThh:mm:SS[frac][Z] ,
-where:
-.Bl -tag -width Ds
-.It Ql YYYY-MM-DD
-Corresponds to %Y-%m-%d of
-.Xr strptime 3 .
-.It Ql T
-Is either
-.Ql T
-or a space.
-.It Ql [frac]
-Is either empty, or fractional seconds starting with either a comma
-.Pq \&,
-or a period
-.Pq \&. .
-.It Ql [Z]
-Is either empty, signifying local time, or the letter
-.Qq Z ,
-signifying UTC.
-.El
-.Pp
-For example:
-.Ql 2003-06-02T13:37:42.713Z
-.It Fl r Ar ref_file
-Use the corresponding times of the file at
-.Ar ref_file
-instead of the current time.
-.El
-.Sh EXIT STATUS
-.Ex -std
-Note: Will exit with failure when
-.Fl c
-is given but the file doesn't exists.
-.Sh SEE ALSO
-.Xr stat 1 ,
-.Xr futimens 3
-.Sh STANDARDS
-.Nm
-is mostly compliant with the
-.St -p1003.1-2008
-specification.
-.Fl t
-is intentionally missing.
-.Fl h
-is an extension.
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/touch.c b/cmd/touch.c
@@ -1,136 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#define _POSIX_C_SOURCE 200809L // O_NOFOLLOW
-
-#include "../lib/iso_parse.h" /* iso_parse */
-
-#include <errno.h> /* errno */
-#include <fcntl.h> /* open */
-#include <stdbool.h> /* bool */
-#include <stdio.h> /* perror */
-#include <sys/stat.h> /* futimens, stat, utimensat */
-#include <unistd.h> /* getopt, opt*, close */
-
-int
-main(int argc, char *argv[])
-{
- bool ch_atime = false, ch_mtime = false;
- char *ref_file = NULL;
- struct timespec times[2] = {
- {.tv_sec = 0, .tv_nsec = UTIME_OMIT}, // access
- {.tv_sec = 0, .tv_nsec = UTIME_OMIT} // modification
- };
- struct timespec target = {0, UTIME_NOW};
- int open_flags = O_WRONLY | O_CREAT | O_NOCTTY;
- int utimensat_flags = 0;
-
- int c = 0;
- while((c = getopt(argc, argv, ":achmr:t:d:")) != -1)
- {
- switch(c)
- {
- case 'a':
- ch_atime = true;
- break;
- case 'c':
- open_flags ^= O_CREAT;
- break;
- case 'h':
- open_flags |= O_NOFOLLOW;
- utimensat_flags |= AT_SYMLINK_NOFOLLOW;
- break;
- case 'm':
- ch_mtime = true;
- break;
- case 'r':
- ref_file = optarg;
- break;
- case 't': // [[CC]YY]MMDDhhmm[.SS]
- // Too legacy of a format, too annoying to parse
- fprintf(stderr, "touch: Option -d not supported, use -t\n");
- return 1;
- break;
- case 'd':
- target = iso_parse(optarg);
- break;
- case ':':
- fprintf(stderr, "touch: Error: Missing operand for option: '-%c'\n", optopt);
- return 1;
- case '?':
- fprintf(stderr, "touch: Error: Unrecognised option: '-%c'\n", optopt);
- return 1;
- }
- }
-
- argc -= optind;
- argv += optind;
-
- // When neither -a nor -m are specified, change both
- if(!ch_atime && !ch_mtime)
- {
- ch_atime = true;
- ch_mtime = true;
- }
-
- if(ref_file == NULL)
- {
- if(ch_atime) times[0] = target;
- if(ch_mtime) times[1] = target;
- }
- else
- {
- struct stat ref;
-
- if(stat(ref_file, &ref) != 0)
- {
- perror("touch: stat");
- return 1;
- }
-
- if(ch_atime)
- {
- times[0] = ref.st_atim;
- }
- if(ch_mtime)
- {
- times[1] = ref.st_mtim;
- }
- }
-
- for(int i = 0; i < argc; i++)
- {
- int fd = open(argv[i], open_flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
- if(fd == -1)
- {
- if(errno == EISDIR)
- {
- if(utimensat(AT_FDCWD, argv[i], times, utimensat_flags) != 0)
- {
- perror("touch: utimensat");
- return 1;
- }
-
- return 0;
- }
- if(errno != ENOENT) perror("touch: open");
-
- return 1;
- }
-
- if(futimens(fd, times) != 0)
- {
- perror("touch: futimens");
- return 1;
- }
-
- if(close(fd) != 0)
- {
- perror("touch: close");
- return 1;
- }
- }
-
- return 0;
-}
diff --git a/cmd/true.1 b/cmd/true.1
@@ -1,18 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2022-02-13
-.Dt TRUE 1
-.Os
-.Sh NAME
-.Nm true
-.Nd do nothing, successfully
-.Sh SYNOPSIS
-.Nm
-.Sh DESCRIPTION
-.Nm
-will return with exit status code zero.
-.Sh EXIT STATUS
-Zero.
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/true.c b/cmd/true.c
@@ -1,8 +0,0 @@
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-int
-main(void)
-{
- return 0;
-}
diff --git a/cmd/tty.c b/cmd/tty.c
@@ -1,38 +0,0 @@
-// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#include <errno.h> // errno
-#include <stdio.h> // puts()
-#include <unistd.h> // ttyname()
-
-int
-main(void)
-{
- char *name = ttyname(STDIN_FILENO);
-
- if(!name)
- {
- if(errno == ENOTTY)
- {
- if(puts("not a tty") < 0)
- {
- return 2;
- }
-
- return 1;
- }
- else
- {
- perror("ttyname");
- return 2;
- }
- }
-
- if(puts(name) < 0)
- {
- return 2;
- }
-
- return 0;
-}
diff --git a/cmd/unlink.1 b/cmd/unlink.1
@@ -1,25 +0,0 @@
-.\" Collection of Unix tools, comparable to coreutils
-.\" Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2022-05-10
-.Dt UNLINK 1
-.Os
-.Sh NAME
-.Nm unlink
-.Nd unlink filenames and possibly the referred files
-.Sh SYNOPSIS
-.Nm
-.Op Ar files ...
-.Sh DESCRIPTION
-.Nm
-deletes a filename from the filesystem.
-If that filename was the last link to a file and no processes have the file open, the file is deleted.
-.Sh EXIT STATUS
-.Ex -std
-.Sh STANDARDS
-.Nm
-is compliant with the
-.St -p1003.1-2008
-specification.
-.Sh AUTHORS
-.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/unlink.c b/cmd/unlink.c
@@ -1,22 +0,0 @@
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-// SPDX-License-Identifier: MPL-2.0
-
-#include <errno.h> // errno
-#include <stdio.h> // fprintf
-#include <string.h> // strerror
-#include <unistd.h> // unlink
-
-int
-main(int argc, char *argv[])
-{
- for(int i = 1; i < argc; i++)
- {
- if(unlink(argv[i]) != 0)
- {
- fprintf(stderr, "unlink: Cannot unlink ‘%s’: %s\n", argv[i], strerror(errno));
- return 1;
- }
- }
-
- return 0;
-}
diff --git a/test-cmd/Kyuafile b/test-cmd/Kyuafile
@@ -9,30 +9,12 @@ basedir = fs.dirname(fs.dirname(current_kyuafile()))
-- 9,$|LC_ALL=C.UTF-8 sort
-- atf_test_program{name="pat", required_files=basedir.."/cmd/pat", timeout=1}
atf_test_program{name="args", required_files=basedir.."/cmd/args", timeout=1}
-atf_test_program{name="base64", required_files=basedir.."/cmd/base64", timeout=1}
-atf_test_program{name="basename", required_files=basedir.."/cmd/basename", timeout=1}
-atf_test_program{name="cat", required_files=basedir.."/cmd/cat", timeout=1}
-atf_test_program{name="date", required_files=basedir.."/cmd/date", timeout=1}
atf_test_program{name="del", required_files=basedir.."/cmd/del", timeout=1}
-atf_test_program{name="dirname", required_files=basedir.."/cmd/dirname", timeout=1}
-atf_test_program{name="echo", required_files=basedir.."/cmd/echo", timeout=1}
-atf_test_program{name="env", required_files=basedir.."/cmd/env", timeout=1}
atf_test_program{name="errno", required_files=basedir.."/cmd/errno", timeout=1}
-atf_test_program{name="false", required_files=basedir.."/cmd/false", timeout=1}
atf_test_program{name="humanize", required_files=basedir.."/cmd/humanize", timeout=1}
-atf_test_program{name="id", required_files=basedir.."/cmd/id", timeout=1}
-atf_test_program{name="link", required_files=basedir.."/cmd/link", timeout=1}
atf_test_program{name="lolcat", required_files=basedir.."/cmd/lolcat", timeout=1}
atf_test_program{name="mdate", required_files=basedir.."/cmd/mdate", timeout=1}
atf_test_program{name="memsys", required_files=basedir.."/cmd/memsys", timeout=1}
-atf_test_program{name="pwd", required_files=basedir.."/cmd/pwd", timeout=1}
-atf_test_program{name="seq", required_files=basedir.."/cmd/seq", timeout=1}
atf_test_program{name="sizeof", required_files=basedir.."/cmd/sizeof", timeout=1}
atf_test_program{name="sname", required_files=basedir.."/cmd/sname", timeout=1}
-atf_test_program{name="strings", required_files=basedir.."/cmd/strings", timeout=1}
-atf_test_program{name="tee", required_files=basedir.."/cmd/tee", timeout=1}
-atf_test_program{name="touch", required_files=basedir.."/cmd/touch", timeout=3}
-atf_test_program{name="true", required_files=basedir.."/cmd/true", timeout=1}
-atf_test_program{name="tty", required_files=basedir.."/cmd/tty", timeout=1}
-atf_test_program{name="unlink", required_files=basedir.."/cmd/unlink", timeout=1}
atf_test_program{name="xcd", required_files=basedir.."/cmd/xcd", timeout=1}
diff --git a/test-cmd/base64 b/test-cmd/base64
@@ -1,80 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case allbytes
-allbytes_body() {
- atf_check -o file:outputs/base64/all_bytes ../cmd/base64 inputs/all_bytes
- atf_check -o file:outputs/base64/all_bytes ../cmd/base64 <inputs/all_bytes
- atf_check -o file:outputs/base64/all_bytes ../cmd/base64 - <inputs/all_bytes
-}
-
-atf_test_case devnull
-devnull_body() {
- atf_check ../cmd/base64 /dev/null
- atf_check ../cmd/base64 </dev/null
- atf_check ../cmd/base64 - </dev/null
-}
-
-atf_test_case rfc4648
-rfc4648_body() {
- # Test vectors from RFC4648
- atf_check -o 'inline:' sh -c 'printf "" | ../cmd/base64'
- atf_check -o 'inline:Zg==\n' sh -c 'printf "f" | ../cmd/base64'
- atf_check -o 'inline:Zm8=\n' sh -c 'printf "fo" | ../cmd/base64'
- atf_check -o 'inline:Zm9v\n' sh -c 'printf "foo" | ../cmd/base64'
- atf_check -o 'inline:Zm9vYg==\n' sh -c 'printf "foob" | ../cmd/base64'
- atf_check -o 'inline:Zm9vYmE=\n' sh -c 'printf "fooba" | ../cmd/base64'
- atf_check -o 'inline:Zm9vYmFy\n' sh -c 'printf "foobar" | ../cmd/base64'
-}
-
-atf_test_case noperm cleanup
-noperm_body() {
- touch inputs/chmod_000 || atf_fail "touching chmod_000"
- chmod 0000 inputs/chmod_000 || atf_fail "chmod 0000 chmod_000"
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:base64: Error opening ‘inputs/chmod_000’: Permission denied\n' ../cmd/base64 inputs/chmod_000
-}
-noperm_cleanup() {
- chmod 0600 inputs/chmod_000 || atf_fail "chmod 0600 chmod_000"
- rm inputs/chmod_000 || atf_fail "rm chmod_000"
-}
-
-atf_test_case devfull
-devfull_body() {
- atf_check -s exit:1 -e 'inline:base64: Error writing: No space left on device\n' sh -c '../cmd/base64 inputs/all_bytes >/dev/full'
- atf_check -s exit:1 -e 'inline:base64: Error writing: No space left on device\n' sh -c '../cmd/base64 <inputs/all_bytes >/dev/full'
- atf_check -s exit:1 -e 'inline:base64: Error writing: No space left on device\n' sh -c '../cmd/base64 - <inputs/all_bytes >/dev/full'
-}
-
-atf_test_case readslash
-readslash_body() {
- [ "$(uname -s)" = "NetBSD" ] && atf_skip "NetBSD allows to read directories"
-
- atf_check -s exit:1 -e "inline:base64: Error reading ‘/’: Is a directory\n" ../cmd/base64 /
-}
-
-atf_test_case enoent
-enoent_body() {
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:base64: Error opening ‘/var/empty/e/no/ent’: No such file or directory\n' ../cmd/base64 /var/empty/e/no/ent
-}
-
-atf_test_case doubledash
-doubledash_body() {
- atf_check -o file:outputs/base64/all_bytes -- ../cmd/base64 -- inputs/all_bytes
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e "inline:base64: Error: Unrecognised option: ‘--’\n" -o empty -- ../cmd/base64 --- inputs/all_bytes
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- atf_add_test_case rfc4648
- atf_add_test_case allbytes
- atf_add_test_case devnull
- atf_add_test_case noperm
- atf_add_test_case devfull
- atf_add_test_case readslash
- atf_add_test_case enoent
- atf_add_test_case doubledash
-}
diff --git a/test-cmd/basename b/test-cmd/basename
@@ -1,80 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-
-atf_test_case noargs
-noargs_body() {
- atf_check -o "inline:.\n" ../cmd/basename
-}
-
-atf_test_case one_slash
-one_slash_body() {
- atf_check -o "inline:bin\n" ../cmd/basename "/usr/bin"
-}
-
-atf_test_case two_slash
-two_slash_body() {
- atf_check -o "inline:bin\n" ../cmd/basename "/usr//bin"
-}
-
-atf_test_case two_dash
-two_dash_body() {
- atf_check -o "inline:bin\n" ../cmd/basename -- "/usr//bin"
-}
-
-atf_test_case testopt
-testopt_body() {
- atf_check -o "inline:bin\n" ../cmd/basename "/usr//bin-test" "-test"
-}
-
-atf_test_case usage
-usage_body() {
- atf_check -s exit:1 -e "inline:usage: basename string [suffix]\n" ../cmd/basename 1 2 3
-}
-
-atf_test_case devfull
-devfull_body() {
- has_glibc && atf_expect_fail "glibc ignoring write errors for puts()"
- [ "$(uname -s)" = "NetBSD" ] && atf_expect_fail "NetBSD ignoring write errors for puts()"
- [ "$(uname -s)" = "FreeBSD" ] && atf_expect_fail "FreeBSD ignoring write errors for puts()"
-
- atf_check -s exit:1 -e 'inline:basename: puts: No space left on device\n' sh -c '../cmd/basename >/dev/full'
- atf_check -s exit:1 -e 'inline:basename: puts: No space left on device\n' sh -c '../cmd/basename "/usr/bin" >/dev/full'
- atf_check -s exit:1 -e 'inline:basename: puts: No space left on device\n' sh -c '../cmd/basename "/usr//bin-test" "-test" >/dev/full'
-}
-
-atf_test_case nullarg
-nullarg_body() {
- atf_check -e "inline:.\n" ../cmd/basename "$(printf '\x00')"
- atf_check -e "inline:bin\n" ../cmd/basename "/usr/bin" "$(printf '\x00')"
-}
-
-atf_test_case doubledash
-doubledash_body() {
- atf_check -o 'inline:-\n' -- ../cmd/basename '-'
- atf_check -o 'inline:.\n' -- ../cmd/basename '--'
- atf_check -o 'inline:--\n' -- ../cmd/basename --a a
- atf_check -o 'inline:---\n' -- ../cmd/basename '---'
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
-
- . ../test_functions.sh
-
- atf_add_test_case noargs
- atf_add_test_case one_slash
- atf_add_test_case two_slash
- atf_add_test_case two_dash
- atf_add_test_case testopt
- atf_add_test_case usage
-
- # puts in glibc doesn't returns -1 on failure
- atf_add_test_case devfull
-
- # Broken behavior in ATF, might be caused by stripping out \x00
- #atf_add_test_case nullarg
-
- atf_add_test_case doubledash
-}
diff --git a/test-cmd/cat b/test-cmd/cat
@@ -1,78 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case allfile
-allfile_body() {
- atf_check -o file:inputs/all_bytes ../cmd/cat inputs/all_bytes
-}
-
-atf_test_case allinput
-allinput_body() {
- atf_check -o file:inputs/all_bytes ../cmd/cat <inputs/all_bytes
-}
-
-atf_test_case alldashinput
-alldashinput_body() {
- atf_check -o file:inputs/all_bytes ../cmd/cat - <inputs/all_bytes
-}
-
-atf_test_case devnull
-devnull_body() {
- atf_check ../cmd/cat /dev/null
- atf_check ../cmd/cat </dev/null
- atf_check ../cmd/cat - </dev/null
-}
-
-atf_test_case noperm cleanup
-noperm_body() {
- touch inputs/chmod_000 || atf_fail "touching chmod_000"
- chmod 0000 inputs/chmod_000 || atf_fail "chmod 0000 chmod_000"
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:cat: Error opening ‘inputs/chmod_000’: Permission denied\n' ../cmd/cat inputs/chmod_000
-}
-noperm_cleanup() {
- chmod 0600 inputs/chmod_000 || atf_fail "chmod 0600 chmod_000"
- rm inputs/chmod_000 || atf_fail "rm chmod_000"
-}
-
-atf_test_case devfull
-devfull_body() {
- atf_check -s exit:1 -e 'inline:cat: Error writing: No space left on device\n' sh -c '../cmd/cat inputs/all_bytes >/dev/full'
- atf_check -s exit:1 -e 'inline:cat: Error writing: No space left on device\n' sh -c '../cmd/cat <inputs/all_bytes >/dev/full'
- atf_check -s exit:1 -e 'inline:cat: Error writing: No space left on device\n' sh -c '../cmd/cat - <inputs/all_bytes >/dev/full'
-}
-
-atf_test_case readslash
-readslash_body() {
- [ "$(uname -s)" = "NetBSD" ] && atf_skip "NetBSD allows to read directories"
-
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:cat: Error reading ‘/’: Is a directory\n' ../cmd/cat /
-}
-
-atf_test_case enoent
-enoent_body() {
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:cat: Error opening ‘/var/empty/e/no/ent’: No such file or directory\n' ../cmd/cat /var/empty/e/no/ent
-}
-
-atf_test_case doubledash
-doubledash_body() {
- atf_check -o file:inputs/all_bytes -- ../cmd/cat -- inputs/all_bytes
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:cat: Error opening ‘---’: No such file or directory\n' -o empty -- ../cmd/cat --- inputs/all_bytes
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- atf_add_test_case allfile
- atf_add_test_case allinput
- atf_add_test_case alldashinput
- atf_add_test_case devnull
- atf_add_test_case noperm
- atf_add_test_case devfull
- atf_add_test_case readslash
- atf_add_test_case enoent
- atf_add_test_case doubledash
-}
diff --git a/test-cmd/date b/test-cmd/date
@@ -1,93 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case noargs
-noargs_body() {
- atf_check -o not-empty ../cmd/date
-}
-
-atf_test_case badarg
-badarg_body() {
- atf_check -s 'exit:1' -e "inline:date: Error: Unrecognised option: '-x'\ndate [-uR][-d datetime] [+format]\n" ../cmd/date -x
-}
-
-atf_test_case epoch
-epoch_body() {
- atf_check -o "match:^[0-9]+$" ../cmd/date '+%s'
- atf_check -o "inline:1155544496\n" ../cmd/date -uR -d @1155544496 '+%s'
-}
-
-atf_test_case rfc3339
-rfc3339_body() {
- atf_check -o "match:^[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\+[0-9]{4}$" ../cmd/date '+%FT%T%z'
- atf_check -o "match:^2006\-08\-14T08:34:56[+\-]00:?00$" ../cmd/date -uR -d @1155544496 '+%FT%T%z'
-}
-
-atf_test_case rfc5322
-rfc5322_body() {
- atf_check -o "match:^Mon, 14 Aug 2006 08:34:56 [+\-]00:?00$" ../cmd/date -uR -d @1155544496
-}
-
-atf_test_case empty
-empty_body() {
- atf_check -o 'inline:\n' ../cmd/date '+'
-}
-
-atf_test_case echolike
-echolike_body() {
- atf_check -o 'inline:hello world\n' ../cmd/date '+hello world'
-}
-
-atf_test_case devfull
-devfull_body() {
- has_glibc && atf_expect_fail "glibc ignoring write errors for puts()"
- [ "$(uname -s)" = "NetBSD" ] && atf_expect_fail "NetBSD ignoring write errors for puts()"
- [ "$(uname -s)" = "FreeBSD" ] && atf_expect_fail "FreeBSD ignoring write errors for puts()"
-
- atf_check -s exit:1 -e 'inline:date: puts: No space left on device\n' sh -c '../cmd/date >/dev/full'
-}
-
-atf_test_case utc
-utc_body() {
- atf_check -o "match:^[0-9]+$" ../cmd/date -u '+%s'
-}
-
-atf_test_case timestamp
-timestamp_body() {
- atf_check -o "inline:1970-01-01T00:00:00\n" ../cmd/date -u -d @0 '+%FT%T'
- atf_check -o "inline:1970-01-01T00:01:09\n" ../cmd/date -u -d @69 '+%FT%T'
- atf_check -o "inline:1969-12-31T23:58:51\n" ../cmd/date -u -d @-69 '+%FT%T'
-
- atf_check -s 'exit:1' -e "inline:date: Error: Missing operand for option: '-d'\ndate [-uR][-d datetime] [+format]\n" ../cmd/date -u -d
-
- # 36893488147419103232 = 2^65
- atf_check -s 'exit:1' -e not-empty ../cmd/date -u -d @36893488147419103232
-}
-
-atf_test_case isodate
-isodate_body() {
- atf_check -o "inline:0\n" ../cmd/date -u -d "1970-01-01T00:00:00Z" '+%s'
- atf_check -o "inline:69\n" ../cmd/date -u -d "1970-01-01T00:01:09Z" '+%s'
- atf_check -o "inline:-69\n" ../cmd/date -u -d "1969-12-31T23:58:51Z" '+%s'
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
-
- . ../test_functions.sh
-
- atf_add_test_case noargs
- atf_add_test_case badarg
- atf_add_test_case empty
- atf_add_test_case echolike
- atf_add_test_case devfull
-
- atf_add_test_case epoch
- atf_add_test_case rfc3339
- atf_add_test_case rfc5322
- atf_add_test_case utc
-
- atf_add_test_case timestamp
- atf_add_test_case isodate
-}
diff --git a/test-cmd/dirname b/test-cmd/dirname
@@ -1,38 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-
-atf_test_case noargs
-noargs_body() {
- atf_check -e "inline:usage: dirname string\n" -s exit:1 ../cmd/dirname
-}
-
-atf_test_case one_slash
-one_slash_body() {
- atf_check -o "inline:/usr\n" ../cmd/dirname "/usr/bin"
-}
-
-atf_test_case two_slash
-two_slash_body() {
- atf_check -o "inline:/usr\n" ../cmd/dirname "/usr//bin"
-}
-
-atf_test_case two_dash
-two_dash_body() {
- atf_check -o "inline:/usr\n" ../cmd/dirname -- "/usr//bin"
-}
-
-atf_test_case badarg
-badarg_body() {
- atf_check -s exit:1 -e "inline:usage: dirname string\n" ../cmd/dirname -a "/usr//bin"
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- atf_add_test_case noargs
- atf_add_test_case badarg
- atf_add_test_case one_slash
- atf_add_test_case two_slash
- atf_add_test_case two_dash
-}
diff --git a/test-cmd/echo b/test-cmd/echo
@@ -1,40 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case empty
-empty_body() {
- atf_check -o "inline:\n" ../cmd/echo
-}
-
-atf_test_case hello
-hello_body() {
- atf_check -o "inline:hello world\n" ../cmd/echo hello world
-}
-
-atf_test_case doubledash
-doubledash_body() {
- atf_check -o "inline:-- hello\n" ../cmd/echo -- hello
-}
-
-atf_test_case devfull
-devfull_body() {
- atf_check -s exit:1 -e 'inline:echo: write(1, buffer, arg_len): No space left on device\n' sh -c '../cmd/echo hello world >/dev/full'
-}
-
-atf_test_case opt_n
-opt_n_body() {
- atf_check -o "inline:" ../cmd/echo -n
- atf_check -o "inline:foo" ../cmd/echo -n foo
- atf_check -o "inline:foo bar" ../cmd/echo -n foo bar
- atf_check -o "inline:-- foo" ../cmd/echo -n -- foo
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- atf_add_test_case empty
- atf_add_test_case hello
- atf_add_test_case doubledash
- atf_add_test_case devfull
- atf_add_test_case opt_n
-}
diff --git a/test-cmd/env b/test-cmd/env
@@ -1,73 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case noargs
-noargs_body() {
- [ "${LD_PRELOAD}" = "libsandbox.so" ] && atf_expect_fail "sandbox (gentoo) interferes with the environment"
-
- atf_check -o "inline:FOO=BAR\n" env -i FOO=BAR ../cmd/env
-}
-
-atf_test_case badarg
-badarg_body() {
- atf_check -s not-exit:0 -e "inline:env: Error: Unrecognised option: '-f'\nenv [-i] [-u key | --unset=key] [key=value ...] [command [args]]\n" ../cmd/env -f
-}
-
-atf_test_case iflag
-iflag_body() {
- [ "${LD_PRELOAD}" = "libsandbox.so" ] && atf_expect_fail "sandbox (gentoo) interferes with the environment"
-
- atf_check -o "inline:FOO=BAR\n" ../cmd/env -i FOO=BAR ../cmd/env
- atf_check -o "inline:FOO=BAR\n" ../cmd/env -i FOO=BAR
- atf_check -o "not-inline:FOO=BAR\n" ../cmd/env FOO=BAR ../cmd/env
- atf_check -o "not-inline:FOO=BAR\n" ../cmd/env FOO=BAR
-}
-
-atf_test_case uflag
-uflag_body() {
- [ "${LD_PRELOAD}" = "libsandbox.so" ] && atf_expect_fail "sandbox (gentoo) interferes with the environment"
-
- atf_check -o "inline:FOO=BAR\n" ../cmd/env -i FOO=BAR BAR=FOO ../cmd/env -u BAR
-
- atf_check -s not-exit:0 -e "inline:env: Error: Missing operand for option: '-u'\nenv [-i] [-u key | --unset=key] [key=value ...] [command [args]]\n" ../cmd/env -u
-}
-
-atf_test_case unsetflag
-unsetflag_body() {
- [ "${LD_PRELOAD}" = "libsandbox.so" ] && atf_expect_fail "sandbox (gentoo) interferes with the environment"
-
- atf_check -o "inline:FOO=BAR\n" ../cmd/env -i FOO=BAR BAR=FOO ../cmd/env --unset=BAR
-
- atf_check -s not-exit:0 -e "inline:env: Error: Missing operand for option: '-u'\nenv [-i] [-u key | --unset=key] [key=value ...] [command [args]]\n" ../cmd/env -u
-}
-
-atf_test_case devfull
-devfull_body() {
- [ "$(uname -s)" = "NetBSD" ] && atf_expect_fail "NetBSD ignoring write errors for puts()"
- [ "$(uname -s)" = "FreeBSD" ] && atf_expect_fail "FreeBSD ignoring write errors for puts()"
-
- atf_check -s exit:1 -e 'inline:env: puts(environ[i]): No space left on device\n' sh -c '../cmd/env >/dev/full'
-}
-
-atf_test_case noutil
-noutil_body() {
- atf_check -s exit:127 -e 'inline:env: execvp("/var/empty/e/no/ent", ...): No such file or directory\n' ../cmd/env /var/empty/e/no/ent
-}
-
-atf_test_case false
-false_body() {
- atf_check -s exit:1 ../cmd/env false
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- atf_add_test_case noargs
- atf_add_test_case badarg
- atf_add_test_case iflag
- atf_add_test_case uflag
- atf_add_test_case unsetflag
- atf_add_test_case devfull
- atf_add_test_case noutil
- atf_add_test_case false
-}
diff --git a/test-cmd/false b/test-cmd/false
@@ -1,25 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case basic
-basic_body() {
- atf_check -s exit:1 ../cmd/false
-}
-
-atf_test_case nohelp
-nohelp_body() {
- atf_check -s exit:1 ../cmd/false --help
-}
-
-atf_test_case devfull
-devfull_body() {
- atf_check -s exit:1 ../cmd/false --help >/dev/full
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- atf_add_test_case basic
- atf_add_test_case nohelp
- atf_add_test_case devfull
-}
diff --git a/test-cmd/id b/test-cmd/id
@@ -1,164 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case noargs cleanup
-noargs_body() {
- atf_check -o save:noargs.out ../cmd/id
- atf_check grep -q "uid=$(id -u)($(id -un)) gid=$(id -g)($(id -gn)) groups=" noargs.out
-}
-noargs_cleanup() {
- rm -f noargs.out
-}
-
-atf_test_case names cleanup
-names_body() {
- atf_check -o save:names.out ../cmd/id -n
- atf_check grep -q "uid=$(id -un) gid=$(id -gn) groups=" names.out
-}
-names_cleanup() {
- rm -f names.out
-}
-
-atf_test_case devfull
-devfull_body() {
- has_glibc && atf_expect_fail "glibc ignoring write errors for puts()"
- [ "$(uname -s)" = "NetBSD" ] && atf_expect_fail "NetBSD ignoring write errors for puts()"
- [ "$(uname -s)" = "FreeBSD" ] && atf_expect_fail "FreeBSD ignoring write errors for puts()"
-
- atf_check -s exit:1 sh -c '../cmd/id >/dev/full'
- atf_check -s exit:1 sh -c '../cmd/id -n >/dev/full'
- atf_check -s exit:1 sh -c '../cmd/id -u >/dev/full'
- atf_check -s exit:1 sh -c '../cmd/id -g >/dev/full'
-}
-
-atf_test_case group
-group_body() {
- atf_check -o "inline:$(id -g)\n" ../cmd/id -g
- atf_check -o "inline:$(id -gr)\n" ../cmd/id -gr
- atf_check -o "inline:$(id -gn)\n" ../cmd/id -gn
- atf_check -o "inline:$(id -gnr)\n" ../cmd/id -gnr
-}
-
-atf_test_case user
-user_body() {
- atf_check -o "inline:$(id -u)\n" ../cmd/id -u
- atf_check -o "inline:$(id -ur)\n" ../cmd/id -ur
- atf_check -o "inline:$(id -un)\n" ../cmd/id -un
- atf_check -o "inline:$(id -unr)\n" ../cmd/id -unr
-}
-
-atf_test_case groups
-groups_body() {
- # sadly GNU coreutils' id(1) sorts it's grouplist
-
- atf_check -o not-empty ../cmd/id -G
- atf_check -o not-empty ../cmd/id -Gr
- atf_check -o not-empty ../cmd/id -Gn
- atf_check -o not-empty ../cmd/id -Gnr
-}
-
-# Make sure the correct list is returned for different users
-# Previously it would only return the runtime list of the current user
-atf_test_case regression_groups
-regression_groups() {
- atf_check -o "not-inline:$(../cmd/id -G root)" ../cmd/id -G nobody
- atf_check -o "not-inline:$(../cmd/id -Gr root)" ../cmd/id -Gr nobody
- atf_check -o "not-inline:$(../cmd/id -Gn root)" ../cmd/id -Gn nobody
- atf_check -o "not-inline:$(../cmd/id -Gnr root)" ../cmd/id -Gnr nobody
-}
-
-atf_test_case noetc
-noetc_body() {
- bwrap_args="--bind / / --bind /var/empty /etc"
-
- command -v "${BWRAP:-bwrap}" >/dev/null 2>/dev/null || atf_skip "${BWRAP:-bwrap} command not found"
- [ -n "${NO_BWRAP}" ] && atf_skip "'NO_BWRAP' set"
-
- set -f
-
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -u
- # shellcheck disable=SC2086
- atf_check -s exit:1 -e "inline:id: cannot find name for user ID $(id -u)\n" -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -un
-
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -g
- # shellcheck disable=SC2086
- atf_check -s exit:1 -e "inline:id: cannot find name for group ID $(id -g)\n" -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -gn
-}
-
-atf_test_case nopasswd
-nopasswd_body() {
- bwrap_args="--bind / / --bind /dev/null /etc/passwd"
-
- command -v "${BWRAP:-bwrap}" >/dev/null 2>/dev/null || atf_skip "${BWRAP:-bwrap} command not found"
- [ -n "${NO_BWRAP}" ] && atf_skip "'NO_BWRAP' set"
-
- set -f
-
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -u
- # shellcheck disable=SC2086
- atf_check -s exit:1 -e "inline:id: cannot find name for user ID $(id -u)\n" -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -un
-
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -g
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -gn)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -gn
-}
-
-atf_test_case nogroup
-nogroup_body() {
- bwrap_args="--bind / / --bind /dev/null /etc/group"
-
- command -v "${BWRAP:-bwrap}" >/dev/null 2>/dev/null || atf_skip "${BWRAP:-bwrap} command not found"
- [ -n "${NO_BWRAP}" ] && atf_skip "'NO_BWRAP' set"
-
- set -f
-
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -u
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -un)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -un
-
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -g
- # shellcheck disable=SC2086
- atf_check -s exit:1 -e "inline:id: cannot find name for group ID $(id -g)\n" -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -gn
-}
-
-atf_test_case badarg
-badarg_body() {
- atf_check -s exit:1 -e 'inline:Usage: id [-Ggu] [-nr] [user]\n' ../cmd/id -a
-}
-
-atf_test_case root cleanup
-root_body() {
- atf_check -o save:root.out ../cmd/id root
- atf_check grep -q "uid=$(id -u root)($(id -un root)) gid=$(id -g root)($(id -gn root)) groups=" root.out
-}
-root_cleanup() {
- rm -f root.out
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
-
- . ../test_functions.sh
-
- atf_add_test_case devfull
- atf_add_test_case badarg
-
- atf_add_test_case noargs
- atf_add_test_case names
- atf_add_test_case group
- atf_add_test_case user
- atf_add_test_case groups
-
- atf_add_test_case noetc
- atf_add_test_case nogroup
- atf_add_test_case nopasswd
-
- atf_add_test_case root
-}
diff --git a/test-cmd/link b/test-cmd/link
@@ -1,35 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case empty
-empty_body() {
- atf_check -s exit:1 -e "inline:usage: link <reference> <destination>\n" ../cmd/link
-}
-
-atf_test_case basic cleanup
-basic_body() {
- touch foo
-
- atf_check ../cmd/link foo bar
- atf_check cmp foo bar
-
- echo hello >> foo
- atf_check cmp foo bar
-}
-basic_cleanup() {
- atf_check rm -f foo bar
-}
-
-atf_test_case enoent
-enoent_body() {
- atf_check -s exit:1 -e 'inline:link: No such file or directory\n' ../cmd/link /var/empty/e/no/ent enoent_foo
- atf_check -s exit:1 -e 'inline:link: No such file or directory\n' ../cmd/link enoent_bar /var/empty/e/no/ent
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- atf_add_test_case empty
- atf_add_test_case basic
- atf_add_test_case enoent
-}
diff --git a/test-cmd/pwd b/test-cmd/pwd
@@ -1,49 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case simple
-simple_body() {
- atf_check -o "inline:${PWD}\n" ../cmd/pwd
- atf_check -o "inline:$(atf_get_srcdir)\n" ../cmd/pwd
-}
-
-atf_test_case args
-args_body() {
- atf_check -s exit:1 -e "inline:usage: pwd\n" ../cmd/pwd -H
-}
-
-atf_test_case enoent cleanup
-enoent_body() {
- mkdir -p "$(atf_get_srcdir)/remove-me" || exit 1
- cd "$(atf_get_srcdir)/remove-me" || exit 1
-
- atf_check -o "inline:$(atf_get_srcdir)/remove-me\n" "$(atf_get_srcdir)/../cmd/pwd"
-
- rm -fr "$(atf_get_srcdir)/remove-me" || exit 1
-
- atf_check -s exit:1 -e 'inline:pwd: getcwd: No such file or directory\n' "$(atf_get_srcdir)/../cmd/pwd"
-}
-enoent_cleanup() {
- rm -fr "$(atf_get_srcdir)/remove-me"
-}
-
-atf_test_case devfull
-devfull_body() {
- has_glibc && atf_expect_fail "glibc ignoring write errors for puts()"
- [ "$(uname -s)" = "NetBSD" ] && atf_expect_fail "NetBSD ignoring write errors for puts()"
- [ "$(uname -s)" = "FreeBSD" ] && atf_expect_fail "FreeBSD ignoring write errors for puts()"
-
- atf_check -s exit:1 sh -c '../cmd/pwd >/dev/full'
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
-
- . ../test_functions.sh
-
- atf_add_test_case simple
- atf_add_test_case args
- atf_add_test_case enoent
- atf_add_test_case devfull
-}
diff --git a/test-cmd/seq b/test-cmd/seq
@@ -1,75 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case one
-one_body() {
- atf_check -o "inline:1\n" ../cmd/seq 1
- atf_check -o "inline:1\n2\n3\n4\n5\n" ../cmd/seq 5
-
- atf_check -o "inline:1\n0\n-1\n" -- ../cmd/seq -- -1
- atf_check -o "inline:1\n0\n-1\n-2\n-3\n-4\n-5\n" -- ../cmd/seq -- -5
-}
-
-atf_test_case two
-two_body() {
- atf_check -o "inline:0\n1\n" ../cmd/seq 0 1
- atf_check -o "inline:0\n1\n2\n3\n4\n5\n" ../cmd/seq 0 5
- atf_check -o "inline:10\n11\n12\n13\n14\n15\n" ../cmd/seq 10 15
-
- atf_check -o "inline:0\n-1\n" -- ../cmd/seq 0 -1
- atf_check -o "inline:0\n-1\n-2\n-3\n-4\n-5\n" -- ../cmd/seq 0 -5
- atf_check -o "inline:-10\n-11\n-12\n-13\n-14\n-15\n" -- ../cmd/seq -- -10 -15
- atf_check -o "inline:2\n1\n0\n-1\n-2\n" -- ../cmd/seq 2 -2
- atf_check -o "inline:-2\n-1\n0\n1\n2\n" -- ../cmd/seq -- -2 2
-}
-
-atf_test_case three
-three_body() {
- atf_check -o "inline:0\n1\n" ../cmd/seq 0 1 1
- atf_check -o "inline:0\n1\n2\n3\n4\n5\n" ../cmd/seq 0 1 5
- atf_check -o "inline:10\n11\n12\n13\n14\n15\n" ../cmd/seq 10 1 15
-
- atf_check -o "inline:0\n2\n4\n" ../cmd/seq 0 2 5
- atf_check -o "inline:10\n12\n14\n" ../cmd/seq 10 2 15
-
- atf_check -o "inline:0\n-1\n" -- ../cmd/seq 0 1 -1
- atf_check -o "inline:0\n-1\n-2\n-3\n-4\n-5\n" -- ../cmd/seq 0 1 -5
- atf_check -o "inline:-10\n-11\n-12\n-13\n-14\n-15\n" -- ../cmd/seq -- -10 1 -15
-
- atf_check -o "inline:0\n-1\n" -- ../cmd/seq 0 -1 -1
-
- atf_check -o "inline:0\n-2\n-4\n" -- ../cmd/seq 0 2 -5
- atf_check -o "inline:-10\n-12\n-14\n" -- ../cmd/seq -- -10 2 -15
-}
-
-atf_test_case noarg
-noarg_body() {
- atf_check -s exit:1 -e 'inline:usage: seq [-w] [-s separator] [first [step]] last\n' ../cmd/seq
-}
-
-atf_test_case badflag
-badflag_body() {
- atf_check -s exit:1 -e 'inline:usage: seq [-w] [-s separator] [first [step]] last\n' ../cmd/seq -f
-}
-
-atf_test_case limits
-limits_body() {
- atf_check ../cmd/seq 9223372036854775806 9223372036854775807
- atf_check -s exit:1 ../cmd/seq 9223372036854775806 9223372036854775808
-
- atf_check ../cmd/seq -- -9223372036854775807 -9223372036854775808
- atf_check -s exit:1 ../cmd/seq -- -9223372036854775807 -9223372036854775809
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
-
- atf_add_test_case one
- atf_add_test_case two
- atf_add_test_case three
-
- atf_add_test_case noarg
- atf_add_test_case badflag
- #atf_add_test_case limits
-}
diff --git a/test-cmd/sleep.t b/test-cmd/sleep.t
@@ -1,23 +0,0 @@
-#!/usr/bin/env cram
-# SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
- $ cd $TESTDIR/../cmd
-
- $ ./sleep
- sleep: Got a duration of 0
- [1]
-
- $ ./sleep -f
- (sleep: Not a number: -f|sleep: sscanf: Invalid argument) (re)
- [1]
-
- $ ./sleep 0
- sleep: Got a duration of 0
- [1]
-
- $ ./sleep 1
-
- $ ./sleep .1
-
- $ ./sleep 1.
diff --git a/test-cmd/strings b/test-cmd/strings
@@ -1,123 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case allbytes
-allbytes_body() {
- atf_check -o file:outputs/strings/all_bytes ../cmd/strings inputs/all_bytes
- atf_check -o file:outputs/strings/all_bytes ../cmd/strings <inputs/all_bytes
- atf_check -o file:outputs/strings/all_bytes ../cmd/strings - <inputs/all_bytes
-}
-
-atf_test_case trueelf
-trueelf_body() {
- atf_check -o file:outputs/strings/true ../cmd/strings inputs/strings/true
- atf_check -o file:outputs/strings/true ../cmd/strings <inputs/strings/true
- atf_check -o file:outputs/strings/true ../cmd/strings - <inputs/strings/true
-}
-
-atf_test_case true8elf
-true8elf_body() {
- atf_check -o file:outputs/strings/true_8 ../cmd/strings -n 8 inputs/strings/true
- atf_check -o file:outputs/strings/true_8 ../cmd/strings -n 8 <inputs/strings/true
- atf_check -o file:outputs/strings/true_8 ../cmd/strings -n 8 - <inputs/strings/true
-}
-
-atf_test_case devnull
-devnull_body() {
- atf_check ../cmd/strings /dev/null
- atf_check ../cmd/strings </dev/null
- atf_check ../cmd/strings - </dev/null
-}
-
-atf_test_case devfull
-devfull_body() {
- has_glibc && atf_expect_fail "glibc ignoring write errors for puts()"
- [ "$(uname -s)" = "NetBSD" ] && atf_expect_fail "NetBSD ignoring write errors for puts()"
- [ "$(uname -s)" = "FreeBSD" ] && atf_expect_fail "FreeBSD ignoring write errors for puts()"
-
- atf_check -s exit:1 -e 'inline:strings: Error writing: No space left on device\n' sh -c '../cmd/strings inputs/strings/true >/dev/full'
-}
-
-atf_test_case noperm cleanup
-noperm_body() {
- touch inputs/chmod_000 || atf_fail "touching chmod_000"
- chmod 0000 inputs/chmod_000 || atf_fail "chmod 0000 chmod_000"
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:strings: Error opening ‘inputs/chmod_000’: Permission denied\n' ../cmd/strings inputs/chmod_000
-}
-noperm_cleanup() {
- chmod 0600 inputs/chmod_000 || atf_fail "chmod 0600 chmod_000"
- rm inputs/chmod_000 || atf_fail "rm chmod_000"
-}
-
-atf_test_case octalformat
-octalformat_body() {
- atf_check -o file:outputs/strings/all_bytes_to ../cmd/strings -to inputs/all_bytes
- atf_check ../cmd/strings -to /dev/null
- atf_check -o file:outputs/strings/true_to ../cmd/strings -to inputs/strings/true
- atf_check -o file:outputs/strings/true_8_to ../cmd/strings -to -n 8 inputs/strings/true
-}
-
-atf_test_case hexformat
-hexformat_body() {
- atf_check -o file:outputs/strings/all_bytes_tx ../cmd/strings -tx inputs/all_bytes
- atf_check ../cmd/strings -tx /dev/null
- atf_check -o file:outputs/strings/true_tx ../cmd/strings -tx inputs/strings/true
- atf_check -o file:outputs/strings/true_8_tx ../cmd/strings -tx -n 8 inputs/strings/true
-}
-
-atf_test_case decformat
-decformat_body() {
- atf_check -o file:outputs/strings/all_bytes_td ../cmd/strings -td inputs/all_bytes
- atf_check ../cmd/strings -td /dev/null
- atf_check -o file:outputs/strings/true_td ../cmd/strings -td inputs/strings/true
- atf_check -o file:outputs/strings/true_8_td ../cmd/strings -td -n 8 inputs/strings/true
-}
-
-atf_test_case badformat
-badformat_body() {
- usage="strings: [-a] [-t format] [-n number] [file...]\n"
-
- atf_check -s exit:1 -e "inline:strings: Unknown format: t\n${usage}" ../cmd/strings -tt inputs/all_bytes
- atf_check -s exit:1 -e "inline:strings: Unknown format: t\n${usage}" ../cmd/strings -tt /dev/null
- atf_check -s exit:1 -e "inline:strings: Unknown format: t\n${usage}" ../cmd/strings -tt inputs/strings/true
- atf_check -s exit:1 -e "inline:strings: Unknown format: t\n${usage}" ../cmd/strings -tt -n 8 inputs/strings/true
-}
-
-atf_test_case erange_n
-erange_n_body() {
- usage="strings: [-a] [-t format] [-n number] [file...]\n"
-
- atf_check -s exit:1 -e "inline:strings: Option \`-n 0\` is too small\n${usage}" -- ../cmd/strings -n 0 inputs/all_bytes
- atf_check -s exit:1 -e "inline:strings: Option \`-n 4097\` is too large\n${usage}" -- ../cmd/strings -n 4097 inputs/all_bytes
- atf_check -s exit:1 -e "inline:strings: Option \`-n f\`: Invalid argument\n${usage}" -- ../cmd/strings -n f inputs/all_bytes
- atf_check -s exit:1 -e "inline:strings: Option \`-n 42f\`: Invalid argument\n${usage}" -- ../cmd/strings -n 42f inputs/all_bytes
-}
-
-atf_test_case usage
-usage_body() {
- atf_check -s exit:1 -e 'inline:strings: [-a] [-t format] [-n number] [file...]\n' ../cmd/strings -t aa inputs/all_bytes
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
-
- . ../test_functions.sh
-
- atf_add_test_case allbytes
- atf_add_test_case trueelf
- atf_add_test_case true8elf
- atf_add_test_case devnull
-
- atf_add_test_case devfull
- atf_add_test_case noperm
- atf_add_test_case erange_n
-
- atf_add_test_case octalformat
- atf_add_test_case hexformat
- atf_add_test_case decformat
- atf_add_test_case badformat
-
- atf_add_test_case usage
-}
diff --git a/test-cmd/tee b/test-cmd/tee
@@ -1,92 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case allinput
-allinput_body() {
- atf_check -o file:inputs/all_bytes ../cmd/tee <inputs/all_bytes
-}
-
-atf_test_case writefile cleanup
-writefile_body() {
- echo 'hello' > tmp_tee.log
- atf_check -o file:inputs/all_bytes ../cmd/tee tmp_tee.log <inputs/all_bytes
- atf_check -o empty -s exit:1 grep hello tmp_tee.log
-}
-writefile_cleanup() {
- rm tmp_tee.log
-}
-
-atf_test_case appendfile cleanup
-appendfile_body() {
- echo 'hello' > tmp_tee.log
- atf_check -o file:inputs/all_bytes ../cmd/tee -a tmp_tee.log <inputs/all_bytes
- atf_check -o file:outputs/tee/hello_all_bytes cat tmp_tee.log
-}
-appendfile_cleanup() {
- rm tmp_tee.log
-}
-
-atf_test_case noperm cleanup
-noperm_body() {
- touch inputs/chmod_000 || atf_fail "touching chmod_000"
- chmod 0000 inputs/chmod_000 || atf_fail "chmod 0000 chmod_000"
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:tee: Error opening ‘inputs/chmod_000’: Permission denied\n' ../cmd/tee inputs/chmod_000 </dev/null
-}
-noperm_cleanup() {
- chmod 0600 inputs/chmod_000 || atf_fail "chmod 0600 chmod_000"
- rm inputs/chmod_000 || atf_fail "rm chmod_000"
-}
-
-atf_test_case devfull
-devfull_body() {
- has_glibc && atf_expect_fail "glibc ignoring write errors for fputs()"
- [ "$(uname -s)" = "NetBSD" ] && atf_expect_fail "NetBSD ignoring write errors for fputs()"
- [ "$(uname -s)" = "FreeBSD" ] && atf_expect_fail "FreeBSD ignoring write errors for fputs()"
-
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:tee: Error writing ‘<stdout>’: No space left on device\n' sh -c '../cmd/tee <inputs/all_bytes >/dev/full'
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:tee: Error writing ‘<stdout>’: No space left on device\n' sh -c '../cmd/tee - <inputs/all_bytes >/dev/full'
-}
-
-atf_test_case nullinput
-nullinput_body() {
- atf_check ../cmd/tee </dev/null
-}
-
-atf_test_case writeslash
-writeslash_body() {
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:tee: Error opening ‘./’: Is a directory\n' ../cmd/tee ./ <inputs/all_bytes
-}
-
-atf_test_case enoent
-enoent_body() {
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:tee: Error opening ‘/var/empty/e/no/ent’: No such file or directory\n' ../cmd/tee /var/empty/e/no/ent <inputs/all_bytes
-}
-
-atf_test_case doubledash
-doubledash_body() {
- atf_check -o file:inputs/all_bytes -- ../cmd/tee -- <inputs/all_bytes
- #atf_check -s exit:1 -e 'inline:tee: Error opening ‘---’: No such file or directory\n' -o empty -- ../cmd/tee --- <inputs/all_bytes
-}
-
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
-
- . ../test_functions.sh
-
- atf_add_test_case allinput
- atf_add_test_case writefile
- atf_add_test_case appendfile
- atf_add_test_case noperm
- atf_add_test_case devfull
- atf_add_test_case nullinput
- atf_add_test_case writeslash
- atf_add_test_case enoent
- atf_add_test_case doubledash
-}
diff --git a/test-cmd/time.t b/test-cmd/time.t
@@ -1,31 +0,0 @@
-#!/usr/bin/env cram
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
- $ cd $TESTDIR/../cmd
-
- $ ./time
- Usage: time command [argument ...]
-
- $ ./time -f
- time: Error: Unrecognised option: '-f'
- Usage: time command [argument ...]
- [1]
-
- $ ./time /var/empty/e/no/ent
- time: execvp: No such file or directory
- real 0.[0-9]* (re)
- user 0.[0-9]* (re)
- sys 0.[0-9]* (re)
- [127]
-
- $ ./time false
- real 0.[0-9]* (re)
- user 0.[0-9]* (re)
- sys 0.[0-9]* (re)
- [1]
-
- $ ./time true
- real 0.[0-9]* (re)
- user 0.[0-9]* (re)
- sys 0.[0-9]* (re)
diff --git a/test-cmd/touch b/test-cmd/touch
@@ -1,205 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case noargs
-noargs_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
-
- atf_check ../cmd/touch ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
-}
-
-atf_test_case ref_noargs
-ref_noargs_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
-
- atf_check ../cmd/touch -r ../cmd/touch ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- atf_check -o "inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
- atf_check -o "inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
-}
-
-atf_test_case mtime
-mtime_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
-
- atf_check ../cmd/touch -m ./foo
- atf_check -o "inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
-}
-
-atf_test_case ref_mtime
-ref_mtime_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
-
- atf_check ../cmd/touch -m -r ../cmd/touch ./foo
- atf_check -o "inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- atf_check -o "not-inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
- atf_check -o "inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
-}
-
-atf_test_case atime
-atime_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
-
- atf_check ../cmd/touch -a ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "inline:${mtime}\n" ./stat_mtime ./foo
-}
-
-atf_test_case ref_atime
-ref_atime_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
-
- atf_check ../cmd/touch -a -r ../cmd/touch ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "inline:${mtime}\n" ./stat_mtime ./foo
- atf_check -o "inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
- atf_check -o "not-inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
-}
-
-atf_test_case amtime
-amtime_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
-
- atf_check ../cmd/touch -a -m ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
-}
-
-atf_test_case ref_amtime
-ref_amtime_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
-
- atf_check ../cmd/touch -a -m -r ../cmd/touch ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- atf_check -o "inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
- atf_check -o "inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
-}
-
-atf_test_case dir
-dir_body() {
- mkdir -p ./foo.d
- atime="$(./stat_atime ./foo.d)"
- mtime="$(./stat_mtime ./foo.d)"
-
- maybe_sleep
- atf_check ../cmd/touch ./foo.d
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo.d
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo.d
-}
-
-atf_test_case optd
-optd_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
-
- unset TZ
-
- atf_check ../cmd/touch -d 2003-06-02T13:37:42Z ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42(\.0+)? ?(Z|[\+\-]00:?00)$' ./stat_atime ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42(\.0+)? ?(Z|[\+\-]00:?00)$' ./stat_mtime ./foo
-}
-
-atf_test_case optd_frac
-optd_frac_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
-
- unset TZ
-
- atf_check ../cmd/touch -d 2003-06-02T13:37:42.713Z ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42.7130+ ?(Z|[\+\-]00:?00)$' ./stat_atime ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42.7130+ ?(Z|[\+\-]00:?00)$' ./stat_mtime ./foo
-
- atf_check ../cmd/touch -d 2003-06-02T13:37:42.123456789Z ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42.123456789 ?(Z|[\+\-]00:?00)$' ./stat_atime ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42.123456789 ?(Z|[\+\-]00:?00)$' ./stat_mtime ./foo
-
- atf_check -s 'exit:1' ../cmd/touch -d 2003-06-02T13:37:42.1234567890Z ./foo
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
-
- atf_add_test_case noargs
- atf_add_test_case atime
- atf_add_test_case mtime
- atf_add_test_case amtime
-
- atf_add_test_case ref_noargs
- atf_add_test_case ref_atime
- atf_add_test_case ref_mtime
- atf_add_test_case ref_amtime
-
- atf_add_test_case dir
-
- atf_add_test_case optd
-
- # No support for displaying fractional seconds on FreeBSD stat(1)
- if uname -s | grep -iq FreeBSD; then
- # shellcheck disable=SC2317
- maybe_sleep() { sleep 1; }
- else
- atf_add_test_case optd_frac
- # shellcheck disable=SC2317
- maybe_sleep() { sleep .1; }
- fi
-}
diff --git a/test-cmd/true b/test-cmd/true
@@ -1,25 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case basic
-basic_body() {
- atf_check ../cmd/true
-}
-
-atf_test_case nohelp
-nohelp_body() {
- atf_check ../cmd/true --help
-}
-
-atf_test_case devfull
-devfull_body() {
- atf_check ../cmd/true --help >/dev/full
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- atf_add_test_case basic
- atf_add_test_case nohelp
- atf_add_test_case devfull
-}
diff --git a/test-cmd/tty b/test-cmd/tty
@@ -1,43 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case basic
-basic_body() {
- tty >/dev/null || atf_expect_fail "test environment not in a tty"
-
- atf_check -o 'not-inline:not a tty\n' ../cmd/tty
-
-}
-
-atf_test_case in_devnull
-in_devnull_body() {
- atf_check -s 'exit:1' -o 'inline:not a tty\n' ../cmd/tty </dev/null
-}
-
-atf_test_case out_devnull
-out_devnull_body() {
- tty >/dev/null || atf_expect_fail "test environment not in a tty"
-
- atf_check -o 'not-inline:not a tty\n' sh -c '../cmd/tty >/dev/null'
-}
-
-atf_test_case out_devfull
-out_devfull_body() {
- has_glibc && atf_expect_fail "glibc ignoring write errors for puts()"
- [ "$(uname -s)" = "NetBSD" ] && atf_expect_fail "NetBSD ignoring write errors for puts()"
- [ "$(uname -s)" = "FreeBSD" ] && atf_expect_fail "FreeBSD ignoring write errors for puts()"
-
- atf_check -s "exit:2" sh -c '../cmd/tty >/dev/full'
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
-
- . ../test_functions.sh
-
- atf_add_test_case basic
- atf_add_test_case in_devnull
- atf_add_test_case out_devnull
- atf_add_test_case out_devfull
-}
diff --git a/test-cmd/unlink b/test-cmd/unlink
@@ -1,24 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case basic cleanup
-basic_body() {
- touch inputs/unlink-this || atf_fail "touch inputs/unlink-this"
- atf_check ../cmd/unlink inputs/unlink-this
-}
-basic_cleanup() {
- rm -f inputs/unlink-this || atf_fail "rm -f inputs/unlink-this"
-}
-
-atf_test_case enoent
-enoent_body() {
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:unlink: Cannot unlink ‘/var/empty/e/no/ent’: No such file or directory\n' ../cmd/unlink /var/empty/e/no/ent
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- atf_add_test_case basic
- atf_add_test_case enoent
-}