Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/uu/stdbuf/src/libstdbuf/src/libstdbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
// file that was distributed with this source code.
// spell-checker:ignore (ToDO) getreent reent IOFBF IOLBF IONBF setvbuf stderrp stdinp stdoutp

#![feature(stdout_switchable_buffering)]

use ctor::ctor;
use libc::{_IOFBF, _IOLBF, _IONBF, FILE, c_char, c_int, fileno, size_t};
use std::env;
use std::io::{self, BufferMode};
use std::ptr;

fn value_to_buffer_mode(value: &str) -> BufferMode {
match value {
"0" => BufferMode::Immediate,
"L" => BufferMode::Line,
_ => BufferMode::Block,
}
}

// This runs automatically when the library is loaded via LD_PRELOAD
#[ctor]
fn init() {
Expand Down Expand Up @@ -224,6 +235,9 @@ pub unsafe extern "C" fn __stdbuf() {
set_buffer(unsafe { __stdbuf_get_stdin() }, &val);
}
if let Ok(val) = env::var("_STDBUF_O") {
io::stdout()
.lock()
.set_buffer_mode(value_to_buffer_mode(&val));
set_buffer(unsafe { __stdbuf_get_stdout() }, &val);
}
}
5 changes: 1 addition & 4 deletions src/uu/uniq/src/uniq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,9 +843,6 @@ fn open_output_file(out_file_name: Option<&OsStr>) -> UResult<Box<dyn Write>> {
)?;
Box::new(BufWriter::with_capacity(OUTPUT_BUFFER_CAPACITY, out_file))
}
_ => Box::new(BufWriter::with_capacity(
OUTPUT_BUFFER_CAPACITY,
stdout().lock(),
)),
_ => Box::new(stdout().lock()),
})
}
56 changes: 56 additions & 0 deletions test-stdbuf-poc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
# requires stage1 rustc built from https://github.com/victor-prokhorov/rust/commit/a9d5767288d132bc3688799ad45c4647b7043f7d
# which is a clone of https://github.com/rust-lang/rust/pull/78515
set -euo pipefail

COREUTILS_DIR=/home/victorprokhorov/coreutils

Check failure on line 6 in test-stdbuf-poc.sh

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: `cspell`: Unknown word 'victorprokhorov' (file:'test-stdbuf-poc.sh', line:6)
RUST_DIR=/home/victorprokhorov/rust-lang/rust

Check failure on line 7 in test-stdbuf-poc.sh

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: `cspell`: Unknown word 'victorprokhorov' (file:'test-stdbuf-poc.sh', line:7)
GNU_DIR=/home/victorprokhorov/gnu

Check failure on line 8 in test-stdbuf-poc.sh

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: `cspell`: Unknown word 'victorprokhorov' (file:'test-stdbuf-poc.sh', line:8)
STAGE1_SYSROOT_LIB="$RUST_DIR/build/aarch64-unknown-linux-gnu/stage1/lib/rustlib/aarch64-unknown-linux-gnu/lib"

Check failure on line 9 in test-stdbuf-poc.sh

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: `cspell`: Unknown word 'rustlib' (file:'test-stdbuf-poc.sh', line:9)
STAGE1_RUSTC="$RUST_DIR/build/aarch64-unknown-linux-gnu/stage1/bin/rustc"

ok() { echo " OK $*"; }
fail() { echo "FAIL $*"; exit 1; }
step() { echo; echo "$*"; }

step "build stdlib"

Check failure on line 16 in test-stdbuf-poc.sh

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: `cspell`: Unknown word 'stdlib' (file:'test-stdbuf-poc.sh', line:16)
cd "$RUST_DIR"
python3 x.py build library
ok "stdlib built"

Check failure on line 19 in test-stdbuf-poc.sh

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: `cspell`: Unknown word 'stdlib' (file:'test-stdbuf-poc.sh', line:19)

step "toolchain check"
rustup run stage1 rustc --version | grep -q 'rustc' \
|| fail "stage1 toolchain not found"
ok "$(rustup run stage1 rustc --version)"
rustup run stage1 rustc --edition 2021 -C prefer-dynamic \
-o /tmp/poc_api_check - <<'RUST' \
|| fail "stdout_switchable_buffering or set_buffer_mode not found in stage1 stdlib"

Check failure on line 27 in test-stdbuf-poc.sh

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: `cspell`: Unknown word 'stdlib' (file:'test-stdbuf-poc.sh', line:27)
#![feature(stdout_switchable_buffering)]
use std::io::{self, BufferMode};
fn main() { io::stdout().lock().set_buffer_mode(BufferMode::Line); }
RUST
ok "stage1 stdlib has stdout_switchable_buffering and set_buffer_mode"

Check failure on line 32 in test-stdbuf-poc.sh

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: `cspell`: Unknown word 'stdlib' (file:'test-stdbuf-poc.sh', line:32)

step "cargo +stage1 build"
cd "$COREUTILS_DIR"
RUSTC="$STAGE1_RUSTC" cargo build -p uu_stdbuf_libstdbuf -p uu_stdbuf -p uu_uniq

step "block buffering test all lines must appear together at the end"
rustup run stage1 rustc --edition 2021 -C prefer-dynamic \
-o /tmp/poc_block_test - <<'RUST' || fail "compile poc_block_test"
#![feature(stdout_switchable_buffering)]
use std::io::{self, BufferMode};
fn main() {
io::stdout().lock().set_buffer_mode(BufferMode::Block);
for line in ["1", "2", "3", "soleil"] {

Check failure on line 45 in test-stdbuf-poc.sh

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: `cspell`: Unknown word 'soleil' (file:'test-stdbuf-poc.sh', line:45)
println!("{line}");
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
RUST
LD_LIBRARY_PATH="$STAGE1_SYSROOT_LIB" /tmp/poc_block_test

step "GNU compliance tests/misc/stdbuf.sh"
cd "$COREUTILS_DIR"
path_UUTILS="$COREUTILS_DIR" path_GNU="$GNU_DIR" PROFILE=debug \
util/run-gnu-test.sh tests/misc/stdbuf.sh
Loading