diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index f7a3b2a33703..0380b925e64c 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -1,1305 +1,1307 @@ # # Please see the file src/etc/mtree/README before making changes to this file. # /set type=dir uname=root gname=wheel mode=0755 tags=package=tests . atf_python sys net .. netlink .. netpfil ipfw .. .. .. .. bin cat .. chflags .. chmod .. cp .. date .. dd .. echo .. expr .. hostname .. ln .. ls .. mkdir .. mv .. pax .. pkill .. pwait .. rm .. rmdir .. sh builtins .. errors .. execution .. expansion .. invocation .. parameters .. parser .. set-e .. .. sleep .. test .. timeout .. .. cddl lib libtpool .. .. sbin .. usr.bin ctfconvert .. ztest .. .. usr.sbin dtrace amd64 arrays .. .. common aggs .. arithmetic .. arrays .. assocs .. begin .. bitfields .. buffering .. builtinvar .. cg .. clauses .. cpc .. decls .. drops .. dtraceUtil .. end .. enum .. env .. error .. exit .. fbtprovider .. funcs .. grammar .. include .. inline .. io .. ip .. java_api .. json .. kinst .. lexer .. llquantize .. mdb .. mib .. misc .. multiaggs .. offsetof .. oformat .. operators .. pid .. plockstat .. pointers .. pragma .. predicates .. preprocessor .. print .. printa .. printf .. privs .. probes .. proc .. profile-n .. providers .. raise .. rates .. safety .. scalars .. sched .. scripting .. sdt .. sizeof .. speculation .. stability .. stack .. stackdepth .. stop .. strlen .. strtoll .. struct .. sugar .. syscall .. sysevent .. tick-n .. trace .. tracemem .. translators .. typedef .. types .. uctf .. union .. usdt .. ustack .. vars .. version .. .. i386 arrays .. funcs .. pid .. ustack .. .. .. zfsd .. .. .. etc rc.d .. .. examples .. games .. gnu lib .. usr.bin diff .. .. .. include .. lib atf libatf-c detail .. .. libatf-c++ detail .. .. test-programs .. .. csu dynamic .. dynamiclib .. dynamicpie .. errno .. static .. .. googletest gmock .. gmock_main .. gtest .. gtest_main .. .. libarchive .. libbe .. libbsnmp .. libc c063 .. db .. gen execve .. posix_spawn .. .. hash data .. .. iconv .. inet .. locale .. net getaddrinfo data .. .. .. nss .. regex data .. .. resolv .. rpc .. secure .. setjmp .. ssp .. stdio .. stdlib .. stdtime .. string .. sys .. termios .. time .. tls dso .. .. ttyio .. .. libcam .. libcasper services cap_dns .. cap_fileargs .. cap_grp .. cap_net .. cap_netdb .. cap_pwd .. cap_sysctl .. .. .. libcrypt .. libdevdctl .. libdiff .. libexecinfo .. libkvm .. libmd .. libmp .. libnv .. libproc .. libregex data .. .. librt .. libsbuf .. libsysdecode .. libthr dlopen .. .. libutil .. libxo .. msun .. .. libexec atf atf-check .. atf-pytest-wrapper .. atf-sh .. .. nuageinit .. rc .. rtld-elf rtld_deepbind .. .. tftpd .. .. sbin bectl .. devd .. dhclient .. growfs .. ifconfig .. ipfw .. md5 .. mdconfig .. newfs_msdos .. nvmecontrol .. pfctl files .. .. ping .. route .. savecore .. swapon .. sysctl .. .. secure lib libcrypto .. .. libexec .. usr.bin .. usr.sbin .. .. share examples tests atf .. googletest .. plain .. tap .. .. .. zoneinfo .. .. sys acl .. aio .. audit .. auditpipe .. cam ctl .. .. capsicum .. cddl zfs bin .. include .. tests acl cifs .. nontrivial .. trivial .. .. atime .. bootfs .. cache .. cachefile .. clean_mirror .. cli_root zdb .. zfs .. zfs_clone .. zfs_copies .. zfs_create .. zfs_destroy .. zfs_diff .. zfs_get .. zfs_inherit .. zfs_mount .. zfs_promote .. zfs_property .. zfs_receive .. zfs_rename .. zfs_reservation .. zfs_rollback .. zfs_send .. zfs_set .. zfs_share .. zfs_snapshot .. zfs_unmount .. zfs_unshare .. zfs_upgrade .. zpool .. zpool_add .. zpool_attach .. zpool_clear .. zpool_create .. zpool_destroy .. zpool_detach .. zpool_expand .. zpool_export .. zpool_get .. zpool_history .. zpool_import blockfiles .. .. zpool_offline .. zpool_online .. zpool_remove .. zpool_replace .. zpool_scrub .. zpool_set .. zpool_status .. zpool_upgrade blockfiles .. .. .. cli_user misc .. zfs_list .. zpool_iostat .. zpool_list .. .. compression .. ctime .. delegate .. devices .. exec .. grow_pool .. grow_replicas .. history .. hotplug .. hotspare .. inheritance .. interop .. inuse .. iscsi .. large_files .. largest_pool .. link_count .. migration .. mmap .. mount .. mv_files .. nestedfs .. no_space .. online_offline .. pool_names .. poolversion .. quota .. redundancy .. refquota .. refreserv .. rename_dirs .. replacement .. reservation .. rootpool .. rsend .. scrub_mirror .. slog .. snapshot .. snapused .. sparse .. threadsappend .. truncate .. txg_integrity .. userquota .. utils_test .. write_dirs .. xattr .. zfsd .. zil .. zinject .. zones .. zvol zvol_ENOSPC .. zvol_cli .. zvol_misc .. zvol_swap .. .. zvol_thrash .. .. .. .. common .. compat32 .. devrandom .. dtrace .. fifo .. file .. fs fusefs .. tarfs .. tmpfs .. .. geom class concat .. eli .. gate .. gpt .. label .. mirror .. multipath .. nop .. part .. raid3 .. shsec .. stripe .. union .. uzip etalon .. .. virstor .. .. .. kern acct .. execve .. pipe .. tty .. .. kqueue libkqueue .. .. mac bsdextended .. ipacl .. portacl .. .. mqueue .. net bpf .. if_ovpn ccd .. .. routing .. .. netgraph .. netinet libalias .. .. netinet6 frag6 .. .. netipsec tunnel .. .. netlink .. netmap .. netpfil common .. ipfw .. pf ioctl .. .. .. opencrypto .. pjdfstest chflags .. chmod .. chown .. ftruncate .. granular .. link .. mkdir .. mkfifo .. mknod .. open .. rename .. rmdir .. symlink .. truncate .. unlink .. utimensat .. .. posixshm .. ses .. sound .. sys .. vfs .. vm stack .. .. vmm .. .. usr.bin apply .. asa .. awk bugs-fixed .. netbsd .. .. basename .. bintrans .. bmake archives fmt_44bsd .. fmt_44bsd_mod .. fmt_oldbsd .. .. basic t0 .. t1 .. t2 .. t3 .. .. execution ellipsis .. empty .. joberr .. plus .. .. shell builtin .. meta .. path .. path_select .. replace .. select .. .. suffixes basic .. src_wild1 .. src_wild2 .. .. syntax directive-t0 .. enl .. funny-targets .. semi .. .. sysmk t0 2 1 .. .. mk .. .. t1 2 1 .. .. mk .. .. t2 2 1 .. .. mk .. .. .. variables modifier_M .. modifier_t .. opt_V .. t0 .. .. .. bsdcat .. calendar .. cmp .. col .. column .. comm .. compress .. cpio .. csplit .. cut .. diff .. diff3 .. dirname .. du .. env .. factor .. file .. file2c .. find .. fold .. getconf .. gh-bc scripts .. tests bc errors .. scripts .. .. dc errors .. scripts .. .. .. .. grep .. gzip .. head .. hexdump .. ident .. indent .. join .. jot .. + lam + .. lastcomm .. limits .. locale .. lockf .. lorder .. m4 .. mail .. mkimg .. mktemp .. ncal .. opensm .. patch .. pr .. printenv .. printf .. procstat .. renice .. rs .. sdiff .. sed regress.multitest.out .. .. seq .. sockstat .. soelim .. sort .. split .. stat .. tail .. tar .. tee .. tftp .. touch .. tr .. truncate .. tsort .. unifdef .. uniq .. units .. unzip .. vmstat .. wc .. xargs .. xinstall .. xo .. yacc yacc .. .. .. usr.sbin certctl .. chown .. ctladm .. daemon .. etcupdate .. extattr .. fstyp .. jail .. makefs .. mixer .. newsyslog .. nmtree .. praudit .. pw .. quot .. rpcbind .. sa .. syslogd .. sysrc .. traceroute .. .. .. # vim: set expandtab ts=4 sw=4: diff --git a/usr.bin/lam/Makefile b/usr.bin/lam/Makefile index e47ea0a98eaa..faad910f2202 100644 --- a/usr.bin/lam/Makefile +++ b/usr.bin/lam/Makefile @@ -1,3 +1,8 @@ +.include + PROG= lam +HAS_TESTS= +SUBDIR.${MK_TESTS}+= tests + .include diff --git a/usr.bin/lam/lam.c b/usr.bin/lam/lam.c index 2194ae7ab596..c1221ca92d0e 100644 --- a/usr.bin/lam/lam.c +++ b/usr.bin/lam/lam.c @@ -1,238 +1,235 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * lam - laminate files * Author: John Kunze, UCB */ #include #include #include #include #include #include #include #include #include #include #define MAXOFILES 20 #define BIGBUFSIZ 5 * BUFSIZ static struct openfile { /* open file structure */ FILE *fp; /* file pointer */ short eof; /* eof flag */ short pad; /* pad flag for missing columns */ char eol; /* end of line character */ const char *sepstring; /* string to print before each line */ const char *format; /* printf(3) style string spec. */ } input[MAXOFILES]; static int morefiles; /* set by getargs(), changed by gatherline() */ static int nofinalnl; /* normally append \n to each output line */ static char line[BIGBUFSIZ]; static char *linep; static char *gatherline(struct openfile *); static void getargs(char *[]); static char *pad(struct openfile *); static void usage(void); int main(int argc, char *argv[]) { struct openfile *ip; if (argc == 1) usage(); if (caph_limit_stdio() == -1) err(1, "unable to limit stdio"); getargs(argv); if (!morefiles) usage(); /* * Cache NLS data, for strerror, for err(3), before entering capability * mode. */ caph_cache_catpages(); if (caph_enter() < 0) err(1, "unable to enter capability mode"); for (;;) { linep = line; for (ip = input; ip->fp != NULL; ip++) linep = gatherline(ip); if (!morefiles) exit(0); fputs(line, stdout); fputs(ip->sepstring, stdout); if (!nofinalnl) putchar('\n'); } } static void getargs(char *av[]) { struct openfile *ip = input; char *p, *c; static char fmtbuf[BUFSIZ]; char *fmtp = fmtbuf; int P, S, F, T; cap_rights_t rights_ro; cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT); P = S = F = T = 0; /* capitalized options */ while ((p = *++av) != NULL) { if (*p != '-' || !p[1]) { if (++morefiles >= MAXOFILES) errx(1, "too many input files"); if (*p == '-') ip->fp = stdin; else if ((ip->fp = fopen(p, "r")) == NULL) { err(1, "%s", p); } if (caph_rights_limit(fileno(ip->fp), &rights_ro) < 0) err(1, "unable to limit rights on: %s", p); ip->pad = P; if (!ip->sepstring) ip->sepstring = (S ? (ip-1)->sepstring : ""); if (!ip->format) ip->format = ((P || F) ? (ip-1)->format : "%s"); if (!ip->eol) ip->eol = (T ? (ip-1)->eol : '\n'); ip++; continue; } c = ++p; switch (tolower((unsigned char)*c)) { case 's': if (*++p || (p = *++av)) ip->sepstring = p; else usage(); S = (*c == 'S' ? 1 : 0); break; case 't': if (*++p || (p = *++av)) ip->eol = *p; else usage(); T = (*c == 'T' ? 1 : 0); nofinalnl = 1; break; case 'p': ip->pad = 1; P = (*c == 'P' ? 1 : 0); /* FALLTHROUGH */ case 'f': F = (*c == 'F' ? 1 : 0); if (*++p || (p = *++av)) { fmtp += strlen(fmtp) + 1; if (fmtp >= fmtbuf + sizeof(fmtbuf)) errx(1, "no more format space"); /* restrict format string to only valid width formatters */ if (strspn(p, "-.0123456789") != strlen(p)) errx(1, "invalid format string `%s'", p); if (snprintf(fmtp, fmtbuf + sizeof(fmtbuf) - fmtp, "%%%ss", p) >= fmtbuf + sizeof(fmtbuf) - fmtp) errx(1, "no more format space"); ip->format = fmtp; } else usage(); break; default: usage(); } } ip->fp = NULL; if (!ip->sepstring) ip->sepstring = ""; } static char * pad(struct openfile *ip) { char *lp = linep; strlcpy(lp, ip->sepstring, line + sizeof(line) - lp); lp += strlen(lp); if (ip->pad) { snprintf(lp, line + sizeof(line) - lp, ip->format, ""); lp += strlen(lp); } return (lp); } static char * gatherline(struct openfile *ip) { char s[BUFSIZ]; int c; char *p; char *lp = linep; char *end = s + sizeof(s) - 1; if (ip->eof) return (pad(ip)); for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++) if ((*p = c) == ip->eol) break; *p = '\0'; if (c == EOF) { ip->eof = 1; - if (ferror(ip->fp)) { + if (ferror(ip->fp)) err(EX_IOERR, NULL); - } - if (ip->fp == stdin) - fclose(stdin); morefiles--; return (pad(ip)); } strlcpy(lp, ip->sepstring, line + sizeof(line) - lp); lp += strlen(lp); snprintf(lp, line + sizeof(line) - lp, ip->format, s); lp += strlen(lp); return (lp); } static void usage(void) { fprintf(stderr, "%s\n%s\n", "usage: lam [ -f min.max ] [ -s sepstring ] [ -t c ] file ...", " lam [ -p min.max ] [ -s sepstring ] [ -t c ] file ..."); exit(1); } diff --git a/usr.bin/lam/tests/Makefile b/usr.bin/lam/tests/Makefile new file mode 100644 index 000000000000..8d41af5e6e09 --- /dev/null +++ b/usr.bin/lam/tests/Makefile @@ -0,0 +1,5 @@ +PACKAGE= tests + +ATF_TESTS_SH= lam_test + +.include diff --git a/usr.bin/lam/tests/lam_test.sh b/usr.bin/lam/tests/lam_test.sh new file mode 100755 index 000000000000..bf3998a42d11 --- /dev/null +++ b/usr.bin/lam/tests/lam_test.sh @@ -0,0 +1,59 @@ +# +# Copyright (c) 2025 Klara, Inc. +# +# SPDX-License-Identifier: BSD-2-Clause +# + +atf_test_case basic +basic_head() +{ + atf_set "descr" "Test basic lam(1) functionality" +} +basic_body() +{ + printf '1\n2\n3\n' > a + printf '4\n5\n6\n' > b + + atf_check -o inline:"14\n25\n36\n" lam a b +} + +atf_test_case sep +sep_head() +{ + atf_set "descr" "Test lam(1) -s and -S options" +} +sep_body() +{ + printf "1\n" > a + printf "0\n" > b + + atf_check -o inline:"x1x0\n" lam -S x a b + atf_check -o inline:"1x0\n" lam a -S x b + atf_check -o inline:"x10\n" lam -S x a -s '' b + + atf_check -o inline:"x10\n" lam -s x a b + atf_check -o inline:"x1y0\n" lam -s x a -s y b + atf_check -o inline:"1x0\n" lam a -s x b +} + +atf_test_case stdin +stdin_head() +{ + atf_set "descr" "Test lam(1) using stdin" +} +stdin_body() +{ + printf '1\n2\n3\n4\n' > a + + atf_check -o inline:"11\n22\n33\n44\n" lam a - < a + atf_check -o inline:"11\n22\n33\n44\n" lam - a < a + + atf_check -o inline:"12\n34\n" lam - - < a +} + +atf_init_test_cases() +{ + atf_add_test_case basic + atf_add_test_case sep + atf_add_test_case stdin +}