fcoreutils 0.22.0

High-performance GNU coreutils replacement with SIMD and parallelism
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
use std::io::{self, Read, Write};
use std::path::Path;

use memchr::{memchr_iter, memrchr_iter};

use crate::common::io::{FileData, read_file, read_stdin};

/// Mode for head operation
#[derive(Clone, Debug)]
pub enum HeadMode {
    /// First N lines (default: 10)
    Lines(u64),
    /// All but last N lines
    LinesFromEnd(u64),
    /// First N bytes
    Bytes(u64),
    /// All but last N bytes
    BytesFromEnd(u64),
}

/// Configuration for head
#[derive(Clone, Debug)]
pub struct HeadConfig {
    pub mode: HeadMode,
    pub zero_terminated: bool,
}

impl Default for HeadConfig {
    fn default() -> Self {
        Self {
            mode: HeadMode::Lines(10),
            zero_terminated: false,
        }
    }
}

/// Parse a numeric argument with optional suffix (K, M, G, etc.)
/// Supports: b(512), kB(1000), K(1024), MB(1e6), M(1048576), GB(1e9), G(1<<30),
/// TB, T, PB, P, EB, E, ZB, Z, YB, Y
pub fn parse_size(s: &str) -> Result<u64, String> {
    let s = s.trim();
    if s.is_empty() {
        return Err("empty size".to_string());
    }

    // Find where the numeric part ends
    let mut num_end = 0;
    for (i, c) in s.char_indices() {
        if c.is_ascii_digit() || (i == 0 && (c == '+' || c == '-')) {
            num_end = i + c.len_utf8();
        } else {
            break;
        }
    }

    if num_end == 0 {
        return Err(format!("invalid number: '{}'", s));
    }

    let num_str = &s[..num_end];
    let suffix = &s[num_end..];

    let num: u64 = match num_str.parse() {
        Ok(n) => n,
        Err(_) => {
            // If the string is valid digits but overflows u64, clamp to u64::MAX
            // like GNU coreutils does for huge counts
            let digits = num_str
                .strip_prefix('+')
                .or_else(|| num_str.strip_prefix('-'))
                .unwrap_or(num_str);
            if !digits.is_empty() && digits.chars().all(|c| c.is_ascii_digit()) {
                u64::MAX
            } else {
                return Err(format!("invalid number: '{}'", num_str));
            }
        }
    };

    let multiplier: u64 = match suffix {
        "" => 1,
        "b" => 512,
        "kB" => 1000,
        "k" | "K" | "KiB" => 1024,
        "MB" => 1_000_000,
        "M" | "MiB" => 1_048_576,
        "GB" => 1_000_000_000,
        "G" | "GiB" => 1_073_741_824,
        "TB" => 1_000_000_000_000,
        "T" | "TiB" => 1_099_511_627_776,
        "PB" => 1_000_000_000_000_000,
        "P" | "PiB" => 1_125_899_906_842_624,
        "EB" => 1_000_000_000_000_000_000,
        "E" | "EiB" => 1_152_921_504_606_846_976,
        // ZB/Z/YB/Y would overflow u64, treat as max
        "ZB" | "Z" | "ZiB" | "YB" | "Y" | "YiB" => {
            if num > 0 {
                return Ok(u64::MAX);
            }
            return Ok(0);
        }
        _ => return Err(format!("invalid suffix in '{}'", s)),
    };

    num.checked_mul(multiplier)
        .ok_or_else(|| format!("number too large: '{}'", s))
}

/// Output first N lines from data
pub fn head_lines(data: &[u8], n: u64, delimiter: u8, out: &mut impl Write) -> io::Result<()> {
    if n == 0 || data.is_empty() {
        return Ok(());
    }

    let mut count = 0u64;
    for pos in memchr_iter(delimiter, data) {
        count += 1;
        if count == n {
            return out.write_all(&data[..=pos]);
        }
    }

    // Fewer than N lines — output everything
    out.write_all(data)
}

/// Output all but last N lines from data.
/// Uses reverse scanning (memrchr_iter) for single-pass O(n) instead of 2-pass.
pub fn head_lines_from_end(
    data: &[u8],
    n: u64,
    delimiter: u8,
    out: &mut impl Write,
) -> io::Result<()> {
    if n == 0 {
        return out.write_all(data);
    }
    if data.is_empty() {
        return Ok(());
    }

    // Scan backward: skip N delimiters (= N lines), then the next delimiter
    // marks the end of the last line to keep.
    // If the data does not end with a delimiter, the unterminated last "line"
    // counts as one line to skip.
    let mut count = if !data.is_empty() && *data.last().unwrap() != delimiter {
        1u64
    } else {
        0u64
    };
    for pos in memrchr_iter(delimiter, data) {
        count += 1;
        if count > n {
            return out.write_all(&data[..=pos]);
        }
    }

    // Fewer than N+1 lines → N >= total lines → output nothing
    Ok(())
}

/// Output first N bytes from data
pub fn head_bytes(data: &[u8], n: u64, out: &mut impl Write) -> io::Result<()> {
    let n = n.min(data.len() as u64) as usize;
    if n > 0 {
        out.write_all(&data[..n])?;
    }
    Ok(())
}

/// Output all but last N bytes from data
pub fn head_bytes_from_end(data: &[u8], n: u64, out: &mut impl Write) -> io::Result<()> {
    if n >= data.len() as u64 {
        return Ok(());
    }
    let end = data.len() - n as usize;
    if end > 0 {
        out.write_all(&data[..end])?;
    }
    Ok(())
}

/// Raw write(2) to stdout, bypassing all Rust I/O layers.
/// Avoids stdout.lock(), BufWriter allocation, and Write trait overhead.
#[cfg(target_os = "linux")]
fn write_all_raw(mut data: &[u8]) -> io::Result<()> {
    while !data.is_empty() {
        let ret = unsafe { libc::write(1, data.as_ptr() as *const libc::c_void, data.len()) };
        if ret > 0 {
            data = &data[ret as usize..];
        } else if ret == 0 {
            return Err(io::Error::new(io::ErrorKind::WriteZero, "write returned 0"));
        } else {
            let err = io::Error::last_os_error();
            if err.kind() == io::ErrorKind::Interrupted {
                continue;
            }
            return Err(err);
        }
    }
    Ok(())
}

/// Ultra-fast direct path: single file, positive line count, writes directly
/// to stdout fd without BufWriter overhead. Uses raw write(2) on Linux;
/// on other platforms uses a small stack-buffered stdout.
/// Returns Ok(true) on success, Ok(false) on file error (already printed).
pub fn head_file_direct(filename: &str, n: u64, delimiter: u8) -> io::Result<bool> {
    if n == 0 {
        return Ok(true);
    }

    let path = Path::new(filename);

    #[cfg(target_os = "linux")]
    {
        use std::os::unix::fs::OpenOptionsExt;
        let file = std::fs::OpenOptions::new()
            .read(true)
            .custom_flags(libc::O_NOATIME)
            .open(path)
            .or_else(|_| std::fs::File::open(path));
        let mut file = match file {
            Ok(f) => f,
            Err(e) => {
                eprintln!(
                    "head: cannot open '{}' for reading: {}",
                    filename,
                    crate::common::io_error_msg(&e)
                );
                return Ok(false);
            }
        };

        // Hint sequential readahead for better throughput on large-N line counts.
        {
            use std::os::unix::io::AsRawFd;
            unsafe {
                libc::posix_fadvise(file.as_raw_fd(), 0, 0, libc::POSIX_FADV_SEQUENTIAL);
            }
        }

        let mut buf = [0u8; 65536];
        let mut count = 0u64;

        loop {
            let bytes_read = match file.read(&mut buf) {
                Ok(0) => break,
                Ok(n) => n,
                Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
                Err(e) => return Err(e),
            };

            let chunk = &buf[..bytes_read];

            for pos in memchr_iter(delimiter, chunk) {
                count += 1;
                if count == n {
                    write_all_raw(&chunk[..=pos])?;
                    return Ok(true);
                }
            }

            write_all_raw(chunk)?;
        }

        return Ok(true);
    }

    #[cfg(not(target_os = "linux"))]
    {
        let stdout = io::stdout();
        let mut out = io::BufWriter::with_capacity(8192, stdout.lock());
        match head_lines_streaming_file(path, n, delimiter, &mut out) {
            Ok(true) => {
                out.flush()?;
                Ok(true)
            }
            Ok(false) => Ok(false),
            Err(e) => {
                eprintln!(
                    "head: cannot open '{}' for reading: {}",
                    filename,
                    crate::common::io_error_msg(&e)
                );
                Ok(false)
            }
        }
    }
}

/// Use sendfile for zero-copy byte output on Linux.
/// Falls back to read+write if sendfile fails (e.g., stdout is a terminal).
#[cfg(target_os = "linux")]
pub fn sendfile_bytes(path: &Path, n: u64, out_fd: i32) -> io::Result<bool> {
    use std::os::unix::fs::OpenOptionsExt;

    let file = std::fs::OpenOptions::new()
        .read(true)
        .custom_flags(libc::O_NOATIME)
        .open(path)
        .or_else(|_| std::fs::File::open(path))?;

    // Hint sequential readahead for sendfile throughput.
    {
        use std::os::unix::io::AsRawFd;
        unsafe {
            libc::posix_fadvise(file.as_raw_fd(), 0, 0, libc::POSIX_FADV_SEQUENTIAL);
        }
    }

    let metadata = file.metadata()?;
    let file_size = metadata.len();
    let to_send = n.min(file_size) as usize;

    if to_send == 0 {
        return Ok(true);
    }

    use std::os::unix::io::AsRawFd;
    let in_fd = file.as_raw_fd();
    let mut offset: libc::off_t = 0;
    let mut remaining = to_send;
    let total = to_send;

    while remaining > 0 {
        let chunk = remaining.min(0x7ffff000); // sendfile max per call
        let ret = unsafe { libc::sendfile(out_fd, in_fd, &mut offset, chunk) };
        if ret > 0 {
            remaining -= ret as usize;
        } else if ret == 0 {
            break;
        } else {
            let err = io::Error::last_os_error();
            if err.kind() == io::ErrorKind::Interrupted {
                continue;
            }
            // sendfile fails with EINVAL for terminal fds; fall back to read+write
            if err.raw_os_error() == Some(libc::EINVAL) && remaining == total {
                let mut file = file;
                let mut buf = [0u8; 65536];
                let mut left = to_send;
                while left > 0 {
                    let to_read = left.min(buf.len());
                    let nr = match file.read(&mut buf[..to_read]) {
                        Ok(0) => break,
                        Ok(nr) => nr,
                        Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
                        Err(e) => return Err(e),
                    };
                    write_all_raw(&buf[..nr])?;
                    left -= nr;
                }
                return Ok(true);
            }
            return Err(err);
        }
    }

    Ok(true)
}

/// Streaming head for positive line count on a regular file.
/// Reads small chunks from the start, never mmaps the whole file.
/// This is the critical fast path: `head -n 10` on a 100MB file
/// reads only a few KB instead of mapping all 100MB.
fn head_lines_streaming_file(
    path: &Path,
    n: u64,
    delimiter: u8,
    out: &mut impl Write,
) -> io::Result<bool> {
    if n == 0 {
        return Ok(true);
    }

    #[cfg(target_os = "linux")]
    let file = {
        use std::os::unix::fs::OpenOptionsExt;
        std::fs::OpenOptions::new()
            .read(true)
            .custom_flags(libc::O_NOATIME)
            .open(path)
            .or_else(|_| std::fs::File::open(path))?
    };
    #[cfg(not(target_os = "linux"))]
    let file = std::fs::File::open(path)?;

    let mut file = file;

    // Hint sequential readahead for better throughput on large-N line counts.
    #[cfg(target_os = "linux")]
    {
        use std::os::unix::io::AsRawFd;
        unsafe {
            libc::posix_fadvise(file.as_raw_fd(), 0, 0, libc::POSIX_FADV_SEQUENTIAL);
        }
    }

    let mut buf = [0u8; 65536];
    let mut count = 0u64;

    loop {
        let bytes_read = match file.read(&mut buf) {
            Ok(0) => break,
            Ok(n) => n,
            Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
            Err(e) => return Err(e),
        };

        let chunk = &buf[..bytes_read];

        for pos in memchr_iter(delimiter, chunk) {
            count += 1;
            if count == n {
                out.write_all(&chunk[..=pos])?;
                return Ok(true);
            }
        }

        out.write_all(chunk)?;
    }

    Ok(true)
}

/// Process a single file/stdin for head
pub fn head_file(
    filename: &str,
    config: &HeadConfig,
    out: &mut impl Write,
    tool_name: &str,
) -> io::Result<bool> {
    let delimiter = if config.zero_terminated { b'\0' } else { b'\n' };

    if filename != "-" {
        let path = Path::new(filename);

        // Fast paths that avoid reading/mmapping the whole file
        match &config.mode {
            HeadMode::Lines(n) => {
                // Streaming: read small chunks, stop after N lines
                match head_lines_streaming_file(path, *n, delimiter, out) {
                    Ok(true) => return Ok(true),
                    Err(e) => {
                        eprintln!(
                            "{}: cannot open '{}' for reading: {}",
                            tool_name,
                            filename,
                            crate::common::io_error_msg(&e)
                        );
                        return Ok(false);
                    }
                    _ => {}
                }
            }
            HeadMode::Bytes(n) => {
                // sendfile: zero-copy, reads only N bytes
                #[cfg(target_os = "linux")]
                {
                    use std::os::unix::io::AsRawFd;
                    let stdout = io::stdout();
                    let out_fd = stdout.as_raw_fd();
                    if let Ok(true) = sendfile_bytes(path, *n, out_fd) {
                        return Ok(true);
                    }
                }
                // Non-Linux: still avoid full mmap
                #[cfg(not(target_os = "linux"))]
                {
                    if let Ok(true) = head_bytes_streaming_file(path, *n, out) {
                        return Ok(true);
                    }
                }
            }
            _ => {
                // LinesFromEnd and BytesFromEnd need the whole file — use mmap
            }
        }
    }

    // Fast path for stdin with positive line/byte counts — stream without buffering everything.
    if filename == "-" {
        match &config.mode {
            HeadMode::Lines(n) => {
                return match head_stdin_lines_streaming(*n, delimiter, out) {
                    Ok(()) => Ok(true),
                    Err(e) if e.kind() == io::ErrorKind::BrokenPipe => Ok(true),
                    Err(e) => {
                        eprintln!(
                            "{}: standard input: {}",
                            tool_name,
                            crate::common::io_error_msg(&e)
                        );
                        Ok(false)
                    }
                };
            }
            HeadMode::Bytes(n) => {
                return match head_stdin_bytes_streaming(*n, out) {
                    Ok(()) => Ok(true),
                    Err(e) if e.kind() == io::ErrorKind::BrokenPipe => Ok(true),
                    Err(e) => {
                        eprintln!(
                            "{}: standard input: {}",
                            tool_name,
                            crate::common::io_error_msg(&e)
                        );
                        Ok(false)
                    }
                };
            }
            _ => {} // LinesFromEnd/BytesFromEnd need full buffer
        }
    }

    // Slow path: read entire file (needed for -n -N, -c -N, or stdin from-end modes)
    let data: FileData = if filename == "-" {
        match read_stdin() {
            Ok(d) => FileData::Owned(d),
            Err(e) => {
                eprintln!(
                    "{}: standard input: {}",
                    tool_name,
                    crate::common::io_error_msg(&e)
                );
                return Ok(false);
            }
        }
    } else {
        match read_file(Path::new(filename)) {
            Ok(d) => d,
            Err(e) => {
                eprintln!(
                    "{}: cannot open '{}' for reading: {}",
                    tool_name,
                    filename,
                    crate::common::io_error_msg(&e)
                );
                return Ok(false);
            }
        }
    };

    match &config.mode {
        HeadMode::Lines(n) => head_lines(&data, *n, delimiter, out)?,
        HeadMode::LinesFromEnd(n) => head_lines_from_end(&data, *n, delimiter, out)?,
        HeadMode::Bytes(n) => head_bytes(&data, *n, out)?,
        HeadMode::BytesFromEnd(n) => head_bytes_from_end(&data, *n, out)?,
    }

    Ok(true)
}

/// Streaming head for positive byte count on non-Linux.
#[cfg(not(target_os = "linux"))]
fn head_bytes_streaming_file(path: &Path, n: u64, out: &mut impl Write) -> io::Result<bool> {
    let mut file = std::fs::File::open(path)?;
    let mut remaining = n as usize;
    let mut buf = [0u8; 65536];

    while remaining > 0 {
        let to_read = remaining.min(buf.len());
        let bytes_read = match file.read(&mut buf[..to_read]) {
            Ok(0) => break,
            Ok(n) => n,
            Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
            Err(e) => return Err(e),
        };
        out.write_all(&buf[..bytes_read])?;
        remaining -= bytes_read;
    }

    Ok(true)
}

/// Process head for stdin streaming (line mode, positive count)
/// Reads chunks and counts lines, stopping early once count reached.
pub fn head_stdin_lines_streaming(n: u64, delimiter: u8, out: &mut impl Write) -> io::Result<()> {
    if n == 0 {
        return Ok(());
    }

    let stdin = io::stdin();
    let mut reader = stdin.lock();
    let mut buf = [0u8; 262144];
    let mut count = 0u64;

    loop {
        let bytes_read = match reader.read(&mut buf) {
            Ok(0) => break,
            Ok(n) => n,
            Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
            Err(e) => return Err(e),
        };

        let chunk = &buf[..bytes_read];

        // Count delimiters in this chunk
        for pos in memchr_iter(delimiter, chunk) {
            count += 1;
            if count == n {
                out.write_all(&chunk[..=pos])?;
                return Ok(());
            }
        }

        // Haven't reached N lines yet, output entire chunk
        out.write_all(chunk)?;
    }

    Ok(())
}

/// Process head for stdin streaming (byte mode, positive count).
/// Reads chunks and outputs up to N bytes, stopping early.
fn head_stdin_bytes_streaming(n: u64, out: &mut impl Write) -> io::Result<()> {
    if n == 0 {
        return Ok(());
    }

    let stdin = io::stdin();
    let mut reader = stdin.lock();
    let mut buf = [0u8; 262144];
    let mut remaining = n;

    loop {
        let to_read = (remaining as usize).min(buf.len());
        let bytes_read = match reader.read(&mut buf[..to_read]) {
            Ok(0) => break,
            Ok(n) => n,
            Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
            Err(e) => return Err(e),
        };
        out.write_all(&buf[..bytes_read])?;
        remaining -= bytes_read as u64;
        if remaining == 0 {
            break;
        }
    }

    Ok(())
}