15 Commits

19 changed files with 101 additions and 71 deletions

View File

@@ -19,7 +19,7 @@ PREFIX ?= /usr/local
# for conditionally compiling OS features
OS != uname
OS_INCLUDE != test -e include/$(OS).mk && printf 'include/$(OS).mk\n' \
|| include/None.mk
|| printf '/dev/null\n'
# normalized prefix
PREFIX_N != dirname $(PREFIX)/.

View File

View File

@@ -170,12 +170,6 @@ usage(char *argv0) {
}
int main(int argc, char *argv[]) {
#ifdef __OpenBSD__
if (pledge("cpath rpath stdio unveil wpath", NULL) == -1) {
return oserr(NULL", errno);
}
#endif
int align; /* low 8b used, negative if no alignment is being done */
int count; /* -1 if dj(1) runs until no more reads are possible */
char *fmt; /* set to fmt_asv (default) or fmt_human (-H) */
@@ -183,6 +177,12 @@ int main(int argc, char *argv[]) {
bool retry; /* false if exits on partial reads or writes */
struct Io io[2 /* { in, out } */];
#ifdef __OpenBSD__
if (pledge("cpath rpath stdio unveil wpath", NULL) == -1) {
return oserr(NULL, errno);
}
#endif
/* Set defaults. */
align = -1;
count = -1;
@@ -217,13 +217,9 @@ int main(int argc, char *argv[]) {
break;
} else {
int fd;
#ifdef __OpenBSD__
char *perms = "wc";
/* modify perms in-place to read-only */
if (i == 0) { perms = "r"; }
if (unveil(optarg, perms) == -1) {
if (unveil(optarg, i == 0 ? "r" : "wc") == -1) {
return oserr(NULL, errno);
}
#endif
@@ -267,6 +263,7 @@ int main(int argc, char *argv[]) {
}
}
}
#ifdef __OpenBSD__
if (unveil(NULL, NULL) == -1) { return oserr(NULL, errno); }
#endif

View File

@@ -1,5 +1,6 @@
/*
* Copyright (c) 20232024 Emma Tebibyte <emma@tebibyte.media>
* Copyright (c) 2024 DTB <trinity@trinity.moe>
* SPDX-License-Identifier: CC0
*
* This work is marked with CC0 1.0. To view a copy of this license, visit
@@ -10,9 +11,9 @@
# include <unistd.h> /* pledge(2) */
#endif
int main() {
int main(void) {
#ifdef __OpenBSD__
pledge(NULL, NULL);
#endif
return 1;
return 1;
}

View File

@@ -39,7 +39,7 @@ fn main() {
let mut d = '\u{1E}'.to_string(); /* ASCII record separator */
let mut optind = 1;
if cfg!(target_os="openbsd") {
#[cfg(target_os="openbsd")] {
let promises = Promises::new("stdio proc exec");
if let Err(e) = pledge(Some(promises), None) {
eprintln!("{}: {}", argv[0], e.strerror());

View File

@@ -86,7 +86,7 @@ fn main() -> ExitCode {
return ExitCode::from(EX_USAGE as u8);
}
if cfg!(target_os="openbsd") {
#[cfg(target_os="openbsd")] {
let promises = Promises::new("stdio");
if let Err(e) = pledge(Some(promises), None) {
eprintln!("{}: {}", argv[0], e.strerror());

View File

@@ -34,7 +34,6 @@ use sysexits::EX_USAGE;
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge };
#[cfg(target_os="openbsd")] use strerror::StrError;
fn usage(s: &str) -> ExitCode {
eprintln!("Usage: {} [-egl] integer integer...", s);
ExitCode::from(EX_USAGE as u8)
@@ -43,7 +42,7 @@ fn usage(s: &str) -> ExitCode {
fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>();
if cfg!(target_os="openbsd") {
#[cfg(target_os="openbsd")] {
let promises = Promises::new("stdio");
if let Err(e) = pledge(Some(promises), None) {
eprintln!("{}: {}", argv[0], e.strerror());

View File

@@ -17,7 +17,7 @@
*/
use std::{
ffi::CString,
ffi::{ CString, c_char },
io::Error,
ptr::null,
};
@@ -33,13 +33,13 @@ mod openbsd {
}
}
pub struct Promises(*const i8);
pub struct Promises(*const c_char);
impl Promises {
pub fn new(promises: &str) -> Self {
let p = CString::new(promises).unwrap();
Promises(p.into_raw() as *const i8)
Promises(p.into_raw() as *const c_char)
}
}
@@ -81,10 +81,10 @@ pub fn unveil(
permissions: Option<UnveilPerms>,
) -> Result<(), Error> {
let path_c = path.map(CString::new).map(Result::unwrap);
let arg1 = path_c.map(|p| p.into_raw() as *const i8).unwrap_or(null());
let arg1 = path_c.map(|p| p.into_raw() as *const c_char).unwrap_or(null());
let arg2 = permissions
.map(|p| p.0.into_raw() as *const i8)
.map(|p| p.0.into_raw() as *const c_char)
.unwrap_or(null());
unsafe {

View File

@@ -51,8 +51,8 @@ fn main() -> ExitCode {
let argv = args().collect::<Vec<_>>();
let usage = format!("Usage: {} [-aetu] [-i input] [-o output]", argv[0]);
if cfg!(target_os="openbsd") {
let promises = Promises::new("rpath stdio unveil");
#[cfg(target_os="openbsd")] {
let promises = Promises::new("cpath rpath stdio unveil wpath");
if let Err(e) = pledge(Some(promises), None) {
eprintln!("{}: {}", argv[0], e.strerror());
return ExitCode::from(EX_OSERR as u8);
@@ -76,29 +76,11 @@ fn main() -> ExitCode {
Ok("t") => t = false,
Ok("i") => { /* add inputs */
let input = opt.arg().unwrap();
if cfg!(target_os="openbsd") {
let perms = UnveilPerms::new(vec!['r']);
if let Err(e) = unveil(Some(&input), Some(perms)) {
eprintln!("{}: {}", argv[0], e.strerror());
return ExitCode::from(EX_OSERR as u8);
}
}
ins.push(input);
mode = Some(In); /* latest argument == -i */
},
Ok("o") => { /* add output */
let output = opt.arg().unwrap();
if cfg!(target_os="openbsd") {
let perms = UnveilPerms::new(vec!['w', 'c']);
if let Err(e) = unveil(Some(&output), Some(perms)) {
eprintln!("{}: {}", argv[0], e.strerror());
return ExitCode::from(EX_OSERR as u8);
}
}
let output = opt.arg().unwrap();
outs.push(output);
mode = Some(Out); /* latest argument == -o */
},
@@ -124,7 +106,25 @@ fn main() -> ExitCode {
}
}
if cfg!(target_os="openbsd") {
#[cfg(target_os="openbsd")] {
for input in &ins {
let perms = UnveilPerms::new(vec!['r']);
if let Err(e) = unveil(Some(&input), Some(perms)) {
eprintln!("{}: {}", argv[0], e.strerror());
return ExitCode::from(EX_OSERR as u8);
}
}
for output in &outs {
let perms = UnveilPerms::new(vec!['c', 'w']);
if let Err(e) = unveil(Some(&output), Some(perms)) {
eprintln!("{}: {}", argv[0], e.strerror());
return ExitCode::from(EX_OSERR as u8);
}
}
if let Err(e) = unveil(None, None) {
eprintln!("{}: {}", argv[0], e.strerror());
return ExitCode::from(EX_OSERR as u8);

View File

@@ -39,22 +39,20 @@ usage(char *argv0) {
}
int main(int argc, char *argv[]) {
#ifdef __OpenBSD__
program_name = argv[0] == NULL ? program_name : argv[0];
if (pledge("stdio", NULL) == -1) {
perror(program_name);
return EX_OSERR;
}
#endif
int c;
char showend = 0; /* print a dollar sign before each newline */
char showtab = 0; /* prints tab characters in caret notation */
if (argc > 0) {
#ifndef __OpenBSD__
program_name = argv[0];
#ifdef __OpenBSD__
if (pledge("stdio", NULL) == -1) {
perror(argv[0] == NULL ? program_name : argv[0]);
return EX_OSERR;
}
#endif
if (argc > 0) {
program_name = argv[0];
while ((c = getopt(argc, argv, "et")) != -1) {
switch (c){
case 'e': showend = 1; break;

View File

@@ -198,7 +198,7 @@ fn round_precise(value: &f64, precision: usize) -> f64 {
fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>();
if cfg!(target_os="openbsd") {
#[cfg(target_os="openbsd")] {
let promises = Promises::new("stdio");
if let Err(e) = pledge(Some(promises), None) {
eprintln!("{}: {}", argv[0], e.strerror());

View File

@@ -17,6 +17,7 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
#include <assert.h> /* assert(3) */
#include <stdio.h> /* fprintf(3), stderr, NULL */
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
#include <string.h> /* memset(3), strchr(3) */
@@ -29,6 +30,7 @@
char *program_name = "scrut";
#define OPTS "bcdefgkprsuwxLS"
/* this is an array so main:sel's size can be known at compile time */
static char opts[] = OPTS;
static int
@@ -58,7 +60,10 @@ int main(int argc, char *argv[]) {
memset(sel, '\0', sizeof sel);
for (int c; (c = getopt(argc, argv, opts)) != -1;) {
if ((p = strchr(opts, c)) == NULL) { return usage(argv[0]); }
else { sel[p - opts] = c; }
else {
assert(p - opts < sizeof sel / sizeof *sel); /* bounds check */
sel[p - opts] = c;
}
}
/* straighten out selections; permute out nulls */
@@ -73,7 +78,7 @@ int main(int argc, char *argv[]) {
if (optind == argc) { return usage(argv[0]); }
for (argv += optind ; *argv != NULL; ++argv) {
for (argv += optind ; *argv != NULL; argv = &argv[1]) {
struct stat buf;
#ifdef __OpenBSD__

View File

@@ -26,18 +26,22 @@
char *program_name = "strcmp";
int main(int argc, char *argv[]) {
program_name = argv[0] == NULL ? program_name : argv[0];
int i;
unsigned int i;
#ifdef __OpenBSD__
if (pledge("stdio", NULL) == -1) {
perror(program_name);
perror(argv[0] == NULL ? program_name : argv[0]);
return EX_OSERR;
}
#endif
if (argc < 3) {
(void)fprintf(stderr, "Usage: %s string string...\n", program_name);
(void)fprintf(
stderr,
"Usage: %s string string...\n",
argv[0] == NULL ? program_name : argv[0]
);
return EX_USAGE;
}

View File

@@ -54,7 +54,7 @@ fn usage(s: &str) -> ExitCode {
fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>();
if cfg!(target_os="openbsd") {
#[cfg(target_os="openbsd")] {
let promises = Promises::new("stdio");
if let Err(e) = pledge(Some(promises), None) {
return oserr(&argv[0], e);

View File

@@ -1,5 +1,6 @@
/*
* Copyright (c) 20232024 Emma Tebibyte <emma@tebibyte.media>
* Copyright (c) 2024 DTB <trinity@trinity.moe>
* SPDX-License-Identifier: CC0
*
* This work is marked with CC0 1.0. To view a copy of this license, visit
@@ -10,7 +11,7 @@
# include <unistd.h> /* pledge(2) */
#endif
int main() {
int main(void) {
#ifdef __OpenBSD__
pledge(NULL, NULL);
#endif

View File

@@ -19,7 +19,7 @@ dj_tests: dj_help dj_full dj_null # dj_skip_stdin
dj_full: $(BIN)/dj /dev/full
case "$$(uname)" in \
Linux) \
$(BIN)/dj -Hi /dev/zero -o /dev/full 2>&1 \
! $(BIN)/dj -Hi /dev/zero -o /dev/full 2>&1 \
| tee /dev/stderr \
| xargs -I out test '1+0 > 0+0; 1024 > 0' = out \
;; \

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2024 E$(NAME)a Tebibyte <e$(NAME)a@tebibyte.media>
# Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
# SPDX-License-Identifier: FSFAP
#
# Copying and distribution of this file, with or without modification, are
@@ -6,7 +6,7 @@
# notice are preserved. This file is offered as-is, without any warranty.
.PHONY: mm_tests
mm_tests: mm_args mm_help mm_stderr
mm_tests: mm_args mm_help mm_stderr mm_remaining
.PHONY: mm_none
mm_none: $(BIN)/mm
@@ -25,3 +25,10 @@ mm_help: $(BIN)/mm
# check if stderr is empty upon specifying -e
mm_stderr: $(BIN)/mm
test "$$(printf 'test\n' | $(BIN)/mm -e 2>&1 >/dev/null )" = "test"
.PHONY: mm_remaining
# check to make sure remaining arguments are used
mm_remaining: $(BIN)/mm
test "$$($(BIN)/mm -i README COPYING)" = "$$(cat README COPYING)"
$(BIN)/mm -i README -o /tmp/mm_test0 /tmp/mm_test1
diff /tmp/mm_test0 /tmp/mm_test1

View File

@@ -32,11 +32,29 @@ npc_ascii: npc_ascii_controls npc_ascii_symbols npc_ascii_uppers # \
.PHONY: npc_ascii_controls
# (control characters)
npc_ascii_controls:
# The following test prints the bytes 0x00 (inclusive) through 0x20
# (exclusive) and pipes them through npc(1). npc(1) should then replace all
# non-printing, non-space (in the isspace(3p) sense) characters with their
# graphical carat-char counterparts (see the npc(1) man page). The head(1p)
# invocation then strips off everything past the first line (or past the
# first newline byte, 0x0A) and xargs(1p) is used to test(1p) the output
# against the known good answer.
# Immediately before that newline, 0x09 is printed - in ASCII, the
# horizontal tab. If xargs' -I option is used, tr(1p) should used to delete
# that tab. If the tab is left as part of input, OpenBSD's xargs(1)
# implementation has been observed to strip it along with the other
# trailing whitespace (the newline), but Busybox's and GNU's xargs(1)
# implementations have been observed to leave the tab in. All three
# implementations strip off the trailing tab if `-I` is not used. The POSIX
# specification for `-I` is ambiguous as to which behavior is correct.
# This comment is the result of much bewilderment and debugging.
# ASCII 0x00 to 0x0a (before the newline, due to xargs(1p) issues)
awk 'BEGIN{ for (i = 0; i < 32; ++i) printf("%c", i); }' \
| $(BIN)/npc \
| head -n 1 \
| xargs -I out test "^@^A^B^C^D^E^F^G^H" = out
| xargs test "^@^A^B^C^D^E^F^G^H" =
# ASCII 0x0a (otherwise the head|tail sequence won't work) to 0x1f
awk 'BEGIN{ for (i = 0; i < 32; ++i) printf("%c", i); print }' \

View File

@@ -30,7 +30,7 @@ rpn_mul: $(BIN)/rpn
.PHONY: rpn_div
rpn_div: $(BIN)/rpn
test "$$($(BIN)/rpn 12 5 /)" = 2.4
test "$$($(BIN)/rpn 3 0 /)" -eq inf
test "$$($(BIN)/rpn 3 0 /)" = inf
.PHONY: rpn_mod
rpn_mod: $(BIN)/rpn