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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
; ============================================================
; fenv_unified.asm — GNU-compatible 'env' command
; Builds with: nasm -f bin fenv_unified.asm -o fenv
;
; env: Run a program in a modified environment.
;
; Register allocation:
;   r14d = argc, r15 = argv
;   r12  = envp base pointer
;   ebx  = flags (bit 0 = -i/--ignore-environment)
;   r13  = current arg index
; ============================================================

BITS 64
ORG 0x400000

%define SYS_READ        0
%define SYS_WRITE       1
%define SYS_OPEN        2
%define SYS_CLOSE       3
%define SYS_EXECVE     59
%define SYS_EXIT       60

%define STDOUT          1
%define STDERR          2

%define BUF_SIZE    65536
%define MAX_ENV      1024    ; max env vars we can track

; --- ELF Header (64 bytes) ---
ehdr:
    db 0x7F, 'E','L','F'
    db 2, 1, 1, 0
    dq 0
    dw 2, 0x3E
    dd 1
    dq _start
    dq phdr - $$
    dq 0
    dd 0
    dw ehdr_size, phdr_size, 2, 64, 0, 0
ehdr_size equ $ - ehdr

; --- Program Headers ---
phdr:
    ; PT_LOAD
    dd 1, 7
    dq 0, $$, $$, file_size, file_size + bss_size, 0x200000
phdr_size equ $ - phdr

    ; PT_GNU_STACK (NX)
    dd 0x6474e551, 6
    dq 0, 0, 0, 0, 0, 0x10

; ============================================================
; Code
; ============================================================
_start:
    ; Save argc/argv
    mov     r14d, [rsp]         ; argc
    lea     r15, [rsp + 8]      ; argv

    ; Compute envp: skip past argv[] and its NULL terminator
    mov     eax, r14d
    inc     eax
    lea     r12, [r15 + rax*8]  ; r12 = envp

    ; Initialize flags
    xor     ebx, ebx            ; flags: bit 0 = -i
    mov     r13d, 1             ; arg index (skip argv[0])

    ; -- Copy envp pointers into our env_ptrs array --
    ; Count envp entries
    xor     ecx, ecx
.count_env:
    cmp     qword [r12 + rcx*8], 0
    je      .count_env_done
    inc     ecx
    jmp     .count_env
.count_env_done:
    mov     [env_count], ecx
    ; Copy all pointers
    xor     edx, edx
.copy_env:
    cmp     edx, ecx
    jge     .copy_env_done
    mov     rax, [r12 + rdx*8]
    mov     [env_ptrs + rdx*8], rax
    inc     edx
    jmp     .copy_env
.copy_env_done:
    mov     qword [env_ptrs + rdx*8], 0  ; NULL terminate

    ; Parse options
.parse_opts:
    cmp     r13d, r14d
    jge     .no_command
    mov     rdi, [r15 + r13*8]
    cmp     byte [rdi], '-'
    jne     .check_assignment     ; not an option, could be VAR=VALUE or command
    cmp     byte [rdi + 1], 0
    je      .check_assignment     ; bare "-" is not an option

    ; Check for --
    cmp     byte [rdi + 1], '-'
    jne     .short_opt

    ; Long option
    cmp     byte [rdi + 2], 0
    je      .double_dash

    ; Check --help
    mov     rsi, str_help_flag
    call    _strcmp
    test    eax, eax
    jz      .show_help

    ; Check --version
    mov     rdi, [r15 + r13*8]
    mov     rsi, str_version_flag
    call    _strcmp
    test    eax, eax
    jz      .show_version

    ; Check --ignore-environment
    mov     rdi, [r15 + r13*8]
    mov     rsi, str_ignore_env_flag
    call    _strcmp
    test    eax, eax
    jz      .set_ignore_env

    ; Check --unset=VAR
    mov     rdi, [r15 + r13*8]
    mov     rsi, str_unset_eq_flag
    mov     ecx, 8              ; length of "--unset="
    call    _strncmp
    test    eax, eax
    jz      .unset_eq

    ; Unrecognized long option
    mov     rdi, STDERR
    mov     rsi, str_prefix
    call    _strlen_and_write
    mov     rdi, STDERR
    mov     rsi, str_unrecog
    call    _strlen_and_write
    mov     rdi, [r15 + r13*8]
    push    rdi
    call    _strlen
    mov     rdx, rax
    pop     rsi
    mov     rdi, STDERR
    call    _write
    mov     rdi, STDERR
    mov     rsi, str_sq_nl
    mov     rdx, 2
    call    _write
    mov     rdi, STDERR
    mov     rsi, str_try
    call    _strlen_and_write
    mov     rdi, 125
    jmp     _exit

.short_opt:
    ; Short options: -i, -u, -0
    mov     rdi, [r15 + r13*8]
    inc     rdi                 ; skip '-'
.short_loop:
    movzx   eax, byte [rdi]
    test    al, al
    jz      .next_opt
    cmp     al, 'i'
    je      .set_ignore_env_short
    cmp     al, 'u'
    je      .unset_short
    cmp     al, '0'
    je      .set_null_short
    ; Invalid short option
    push    rdi
    mov     rdi, STDERR
    mov     rsi, str_prefix
    call    _strlen_and_write
    mov     rdi, STDERR
    mov     rsi, str_invalid
    call    _strlen_and_write
    pop     rdi
    mov     rsi, rdi
    mov     rdi, STDERR
    mov     rdx, 1
    call    _write
    mov     rdi, STDERR
    mov     rsi, str_sq_nl
    mov     rdx, 2
    call    _write
    mov     rdi, STDERR
    mov     rsi, str_try
    call    _strlen_and_write
    mov     rdi, 125
    jmp     _exit

.set_ignore_env_short:
    or      bl, 1
    ; Immediately clear env when -i is encountered
    mov     dword [env_count], 0
    mov     qword [env_ptrs], 0
    inc     rdi
    jmp     .short_loop

.set_null_short:
    or      bl, 2               ; bit 1 = null terminator
    inc     rdi
    jmp     .short_loop

.unset_short:
    ; -u requires next argument as var name
    inc     rdi
    ; If there are more chars in this arg, they are the var name
    cmp     byte [rdi], 0
    jne     .do_unset_inline
    ; Otherwise, next argv is the var name
    inc     r13d
    cmp     r13d, r14d
    jge     .missing_arg_u
    mov     rdi, [r15 + r13*8]
.do_unset_inline:
    push    rdi
    call    _unset_var
    pop     rdi
    inc     r13d
    jmp     .parse_opts

.missing_arg_u:
    mov     rdi, STDERR
    mov     rsi, str_prefix
    call    _strlen_and_write
    mov     rdi, STDERR
    mov     rsi, str_missing_u
    call    _strlen_and_write
    mov     rdi, 125
    jmp     _exit

.set_ignore_env:
    or      bl, 1
    ; Immediately clear env when --ignore-environment is encountered
    mov     dword [env_count], 0
    mov     qword [env_ptrs], 0
    jmp     .next_opt

.unset_eq:
    ; --unset=VAR: var name starts at offset 8 of current arg
    mov     rdi, [r15 + r13*8]
    add     rdi, 8
    call    _unset_var
    jmp     .next_opt

.double_dash:
    inc     r13d
    jmp     .done_opts

.next_opt:
    inc     r13d
    jmp     .parse_opts

.check_assignment:
    ; Check if current arg contains '=' (VAR=VALUE)
    mov     rdi, [r15 + r13*8]
    call    _has_equals
    test    eax, eax
    jz      .done_opts          ; no '=' means it's a command

    ; It's a VAR=VALUE assignment — add/replace in env
    mov     rdi, [r15 + r13*8]
    call    _set_env_var
    inc     r13d
    jmp     .parse_opts

.done_opts:
    ; -i flag already cleared env during option parsing (before assignments)
    ; No need to clear again here

    ; Check if there's a command to run
    cmp     r13d, r14d
    jge     .no_command

    ; --- Execute command ---
    ; argv[r13d] is the command, rest are args
    ; Build new argv: command + remaining args + NULL
    mov     rdi, [r15 + r13*8]  ; command path

    ; Build argv array on stack area (env_argv)
    xor     ecx, ecx            ; index into env_argv
    mov     eax, r13d           ; start from current arg
.build_argv:
    cmp     eax, r14d
    jge     .argv_done
    mov     rdx, [r15 + rax*8]
    mov     [env_argv + rcx*8], rdx
    inc     ecx
    inc     eax
    jmp     .build_argv
.argv_done:
    mov     qword [env_argv + rcx*8], 0  ; NULL terminate

    ; execve(command, argv, envp)
    mov     rdi, [r15 + r13*8]  ; command
    mov     rsi, env_argv       ; argv
    mov     rdx, env_ptrs       ; envp
    mov     eax, SYS_EXECVE
    syscall

    ; If execve returns, it failed
    ; Check errno
    neg     rax                 ; make positive
    cmp     rax, 2              ; ENOENT
    je      .exec_not_found
    cmp     rax, 13             ; EACCES
    je      .exec_no_permission

    ; Generic exec error
    mov     rdi, STDERR
    mov     rsi, str_prefix
    call    _strlen_and_write
    mov     rdi, [r15 + r13*8]
    push    rdi
    call    _strlen
    mov     rdx, rax
    pop     rsi
    mov     rdi, STDERR
    call    _write
    mov     rdi, STDERR
    mov     rsi, str_exec_err
    call    _strlen_and_write
    mov     rdi, 126
    jmp     _exit

.exec_not_found:
    mov     rdi, STDERR
    mov     rsi, str_prefix
    call    _strlen_and_write
    mov     rdi, [r15 + r13*8]
    push    rdi
    call    _strlen
    mov     rdx, rax
    pop     rsi
    mov     rdi, STDERR
    call    _write
    mov     rdi, STDERR
    mov     rsi, str_no_file
    call    _strlen_and_write
    mov     rdi, 127
    jmp     _exit

.exec_no_permission:
    mov     rdi, STDERR
    mov     rsi, str_prefix
    call    _strlen_and_write
    mov     rdi, [r15 + r13*8]
    push    rdi
    call    _strlen
    mov     rdx, rax
    pop     rsi
    mov     rdi, STDERR
    call    _write
    mov     rdi, STDERR
    mov     rsi, str_perm_denied
    call    _strlen_and_write
    mov     rdi, 126
    jmp     _exit

.no_command:
    ; No command: print environment
    ; -i flag already cleared env during parsing

    xor     r13d, r13d          ; env index
.print_env_loop:
    mov     rdi, [env_ptrs + r13*8]
    test    rdi, rdi
    jz      .print_env_done
    push    rdi
    call    _strlen
    pop     rdi
    mov     rdx, rax
    mov     rsi, rdi
    mov     rdi, STDOUT
    test    edx, edx
    jz      .print_env_term
    call    _write
.print_env_term:
    test    bl, 2
    jnz     .print_env_nul
    mov     rdi, STDOUT
    mov     rsi, str_newline
    mov     rdx, 1
    call    _write
    jmp     .print_env_next
.print_env_nul:
    mov     rdi, STDOUT
    mov     rsi, str_nul
    mov     rdx, 1
    call    _write
.print_env_next:
    inc     r13d
    jmp     .print_env_loop
.print_env_done:
    xor     edi, edi
    jmp     _exit

.show_help:
    mov     rdi, STDOUT
    mov     rsi, str_help
    mov     rdx, str_help_len
    call    _write
    xor     edi, edi
    jmp     _exit

.show_version:
    mov     rdi, STDOUT
    mov     rsi, str_version
    mov     rdx, str_version_len
    call    _write
    xor     edi, edi
    jmp     _exit

; ============================================================
; _unset_var: remove VAR from env_ptrs
; Input: rdi = var name (NUL-terminated)
; ============================================================
_unset_var:
    push    rbx
    push    r12
    push    r13
    mov     r12, rdi            ; save var name
    ; Get length of var name
    call    _strlen
    mov     r13d, eax           ; r13d = name length

    xor     ecx, ecx            ; index
.uv_loop:
    mov     rsi, [env_ptrs + rcx*8]
    test    rsi, rsi
    jz      .uv_done
    ; Check if env entry starts with varname=
    xor     edx, edx
.uv_cmp:
    cmp     edx, r13d
    jge     .uv_check_eq
    movzx   eax, byte [r12 + rdx]
    movzx   ebx, byte [rsi + rdx]
    cmp     al, bl
    jne     .uv_next
    inc     edx
    jmp     .uv_cmp
.uv_check_eq:
    cmp     byte [rsi + rdx], '='
    jne     .uv_next
    ; Found! Remove by shifting rest down
    jmp     .uv_remove
.uv_next:
    inc     ecx
    jmp     .uv_loop
.uv_remove:
    mov     rax, [env_ptrs + rcx*8 + 8]
    mov     [env_ptrs + rcx*8], rax
    test    rax, rax
    jz      .uv_removed
    inc     ecx
    jmp     .uv_remove
.uv_removed:
    dec     dword [env_count]
.uv_done:
    pop     r13
    pop     r12
    pop     rbx
    ret

; ============================================================
; _set_env_var: add or replace VAR=VALUE in env_ptrs
; Input: rdi = "VAR=VALUE" string pointer
; ============================================================
_set_env_var:
    push    rbx
    push    r12
    push    r13
    mov     r12, rdi            ; save full string

    ; Find length of var name (up to '=')
    xor     ecx, ecx
.sev_find_eq:
    movzx   eax, byte [rdi + rcx]
    test    al, al
    jz      .sev_append         ; no '=' found, shouldn't happen
    cmp     al, '='
    je      .sev_got_len
    inc     ecx
    jmp     .sev_find_eq
.sev_got_len:
    mov     r13d, ecx           ; r13d = name length

    ; Search for existing entry with same var name
    xor     ecx, ecx
.sev_search:
    mov     rsi, [env_ptrs + rcx*8]
    test    rsi, rsi
    jz      .sev_append
    ; Compare first r13d bytes + check for '='
    xor     edx, edx
.sev_cmp:
    cmp     edx, r13d
    jge     .sev_check_eq
    movzx   eax, byte [r12 + rdx]
    movzx   ebx, byte [rsi + rdx]
    cmp     al, bl
    jne     .sev_next
    inc     edx
    jmp     .sev_cmp
.sev_check_eq:
    cmp     byte [rsi + rdx], '='
    jne     .sev_next
    ; Found existing entry, replace it
    mov     [env_ptrs + rcx*8], r12
    jmp     .sev_done
.sev_next:
    inc     ecx
    jmp     .sev_search

.sev_append:
    ; Add new entry at end
    mov     ecx, [env_count]
    cmp     ecx, MAX_ENV - 1
    jge     .sev_done           ; env full, silently ignore
    mov     [env_ptrs + rcx*8], r12
    inc     ecx
    mov     [env_count], ecx
    mov     qword [env_ptrs + rcx*8], 0  ; NULL terminate
.sev_done:
    pop     r13
    pop     r12
    pop     rbx
    ret

; ============================================================
; _has_equals: check if string contains '='
; Input: rdi = string
; Returns: eax = 1 if found, 0 if not
; ============================================================
_has_equals:
    xor     ecx, ecx
.he_loop:
    movzx   eax, byte [rdi + rcx]
    test    al, al
    jz      .he_no
    cmp     al, '='
    je      .he_yes
    inc     ecx
    jmp     .he_loop
.he_yes:
    mov     eax, 1
    ret
.he_no:
    xor     eax, eax
    ret

; ============================================================
; Utility functions
; ============================================================
_write:
    mov     eax, SYS_WRITE
    syscall
    cmp     rax, -4
    je      _write
    ret

_exit:
    mov     eax, SYS_EXIT
    syscall

_strlen:
    xor     eax, eax
.sl_loop:
    cmp     byte [rdi + rax], 0
    je      .sl_done
    inc     eax
    jmp     .sl_loop
.sl_done:
    ret

_strcmp:
    xor     r8d, r8d
.se_loop:
    movzx   eax, byte [rdi + r8]
    movzx   edx, byte [rsi + r8]
    cmp     al, dl
    jne     .se_ne
    test    al, al
    jz      .se_eq
    inc     r8d
    jmp     .se_loop
.se_eq:
    xor     eax, eax
    ret
.se_ne:
    sub     eax, edx
    ret

; _strncmp: compare first ecx bytes
; Input: rdi = s1, rsi = s2, ecx = n
; Returns: eax = 0 if equal for first n bytes
_strncmp:
    xor     r8d, r8d
.sn_loop:
    cmp     r8d, ecx
    jge     .sn_eq
    movzx   eax, byte [rdi + r8]
    movzx   edx, byte [rsi + r8]
    cmp     al, dl
    jne     .sn_ne
    inc     r8d
    jmp     .sn_loop
.sn_eq:
    xor     eax, eax
    ret
.sn_ne:
    sub     eax, edx
    ret

_strlen_and_write:
    push    rdi
    mov     rdi, rsi
    push    rsi
    call    _strlen
    mov     rdx, rax
    pop     rsi
    pop     rdi
    call    _write
    ret

; ============================================================
; Data
; ============================================================
; @@DATA_START@@
str_help:
    db "Usage: env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]", 10
    db "Set each NAME to VALUE in the environment and run COMMAND.", 10, 10
    db "Mandatory arguments to long options are mandatory for short options too.", 10
    db "  -i, --ignore-environment  start with an empty environment", 10
    db "  -0, --null           end each output line with NUL, not newline", 10
    db "  -u, --unset=NAME     remove variable from the environment", 10
    db "      --help        display this help and exit", 10
    db "      --version     output version information and exit", 10, 10
    db "A mere - implies -i.  If no COMMAND, print the resulting environment.", 10, 10
    db "GNU coreutils online help: <https://www.gnu.org/software/coreutils/>", 10
    db "Full documentation <https://www.gnu.org/software/coreutils/env>", 10
    db "or available locally via: info '(coreutils) env invocation'", 10
str_help_len equ $ - str_help

str_version:
    db "env (GNU coreutils) 9.7", 10
    db "Copyright (C) 2025 Free Software Foundation, Inc.", 10
    db "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.", 10
    db "This is free software: you are free to change and redistribute it.", 10
    db "There is NO WARRANTY, to the extent permitted by law.", 10, 10
    db "Written by Richard Mlynarik and David MacKenzie.", 10
str_version_len equ $ - str_version

str_prefix:         db "env: ", 0
str_unrecog:        db "unrecognized option '", 0
str_invalid:        db "invalid option -- '", 0
str_sq_nl:          db "'", 10
str_try:            db "Try 'env --help' for more information.", 10, 0
str_missing_u:      db "option requires an argument -- 'u'", 10, "Try 'env --help' for more information.", 10, 0
str_exec_err:       db ": No such file or directory", 10, 0
str_no_file:        db ": No such file or directory", 10, 0
str_perm_denied:    db ": Permission denied", 10, 0
; @@DATA_END@@

str_newline:        db 10
str_nul:            db 0
str_help_flag:      db "--help", 0
str_version_flag:   db "--version", 0
str_ignore_env_flag: db "--ignore-environment", 0
str_unset_eq_flag:  db "--unset=", 0

file_size equ $ - $$

; BSS section
env_ptrs   equ $$ + file_size           ; MAX_ENV * 8 bytes for pointers
env_count  equ env_ptrs + MAX_ENV * 8   ; 4 bytes for count
env_argv   equ env_count + 8            ; MAX_ENV * 8 bytes for argv
bss_size   equ MAX_ENV * 8 + 8 + MAX_ENV * 8