forked from bonsai/harakit
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
82a941eee3
|
|||
|
e674027b3e
|
|||
|
127192185f
|
|||
|
58245c9484
|
7
Makefile
7
Makefile
@@ -103,6 +103,13 @@ scrut: build/bin/scrut
|
||||
build/bin/scrut: src/scrut.c build
|
||||
$(CC) $(CFLAGS) -o $@ src/scrut.c
|
||||
|
||||
.PHONY: sleep
|
||||
sleep: build/bin/sleep
|
||||
build/bin/sleep: src/sleep.rs build
|
||||
$(RUSTC) $(RUSTFLAGS)
|
||||
--extern sysexits=build/o/libsysexits.rlib \
|
||||
-o $@ src/sleep.rs
|
||||
|
||||
.PHONY: str
|
||||
str: build/bin/str
|
||||
build/bin/str: src/str.c build
|
||||
|
||||
42
src/dj.c
42
src/dj.c
@@ -203,28 +203,28 @@ Io_fdseek(struct Io *io){
|
||||
|
||||
if(!fdisstd(io->fd) && lseek(io->fd, io->seek, SEEK_SET) != -1)
|
||||
return -1;
|
||||
else if(io->fl == write_flags){
|
||||
memset(io->buf, '\0', io->bs);
|
||||
/* This is a dirty trick; rather than testing conditions and operating
|
||||
* likewise, because the parameters to read or write are going to be
|
||||
* the same either way, just use a function pointer to keep track of
|
||||
* the intended operation. */
|
||||
op = (int (*)(int, void *, size_t))&write;
|
||||
/* Function pointer casts are risky; this works because the difference
|
||||
* is in the second parameter and only that write(2) makes the buffer
|
||||
* const whereas read(2) does not. To avoid even the slightest
|
||||
* undefined behavior comment out the cast, just be ready for a
|
||||
* -Wincompatible-function-pointer-types if your compiler notices it.
|
||||
*/
|
||||
}else
|
||||
op = &read;
|
||||
|
||||
/* We're going to cheat and use bufuse as the retval for write(2), which is
|
||||
* fine because it'll be zeroed as this function returns anyway. */
|
||||
do{ if( (io->bufuse = (*op)(io->fd, io->buf, MIN(io->bs, io->seek))) == 0)
|
||||
/* second chance */
|
||||
io->bufuse = (*op)(io->fd, io->buf, MIN(io->bs, io->seek));
|
||||
}while((io->seek -= io->bufuse) > 0 && io->bufuse != 0);
|
||||
/* repeated code to get the condition out of the loop */
|
||||
if(io->fl == write_flags){
|
||||
memset(io->buf, '\0', io->bs);
|
||||
/* We're going to cheat and use bufuse as the retval for write(2),
|
||||
* which is fine because it'll be zeroed as this function returns
|
||||
* anyway. */
|
||||
do{
|
||||
if((io->bufuse = write(io->fd, io->buf, MIN(io->bs, io->seek)))
|
||||
== 0)
|
||||
/* second chance */
|
||||
io->bufuse = write(io->fd, io->buf, MIN(io->bs, io->seek));
|
||||
}while((io->seek -= io->bufuse) > 0 && io->bufuse != 0);
|
||||
}else if(io->fl == read_flags){
|
||||
do{
|
||||
if((io->bufuse = read(io->fd, io->buf, MIN(io->bs, io->seek)))
|
||||
== 0)
|
||||
/* second chance */
|
||||
io->bufuse = read(io->fd, io->buf, MIN(io->bs, io->seek));
|
||||
}while((io->seek -= io->bufuse) > 0 && io->bufuse != 0);
|
||||
}else
|
||||
return EX_SOFTWARE;
|
||||
|
||||
io->bufuse = 0;
|
||||
|
||||
|
||||
47
src/sleep.rs
Normal file
47
src/sleep.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU Affero General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
use std::{
|
||||
env::args,
|
||||
process::ExitCode,
|
||||
thread::sleep,
|
||||
time::Duration
|
||||
};
|
||||
|
||||
extern crate sysexits;
|
||||
use sysexits::{EX_OK, EX_USAGE};
|
||||
|
||||
fn usage(s: &str) -> ExitCode {
|
||||
eprintln!("Usage: {} [seconds]", s);
|
||||
ExitCode::from(EX_USAGE as u8)
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
|
||||
if argv.len() == 2 {
|
||||
if let Ok(s) = argv[1].parse::<u64>() {
|
||||
sleep(Duration::from_secs(s));
|
||||
ExitCode::from(EX_OK as u8)
|
||||
} else {
|
||||
usage(&argv[0])
|
||||
}
|
||||
} else {
|
||||
usage(&argv[0])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user