perror(): Print unknown errors too with errno

This commit is contained in:
2025-06-26 10:33:33 +02:00
parent 232d50576f
commit 43b5367790
2 changed files with 16 additions and 1 deletions

View File

@ -11,6 +11,7 @@ section .rodata
; Errors (perror) ; Errors (perror)
perrorMsg db "%s: %s",10,0 perrorMsg db "%s: %s",10,0
perrorInvalid db "%s: Unknown error (errno %d)",10,0
errorMsgs dq em0,em1,em2,em3,em4,em5,em6,em7,em8,em9,em10,em11,em12,em13,em14,em15,em16,em17,em18,em19,em20,em21,em22,em23,em24,em25,em26,em27,em28,em29,em30,em31,em32,em33,em34 errorMsgs dq em0,em1,em2,em3,em4,em5,em6,em7,em8,em9,em10,em11,em12,em13,em14,em15,em16,em17,em18,em19,em20,em21,em22,em23,em24,em25,em26,em27,em28,em29,em30,em31,em32,em33,em34
em0 db "No error",0 em0 db "No error",0
em1 db "Operation not permitted",0 em1 db "Operation not permitted",0
@ -263,12 +264,18 @@ perror:
jge .quit jge .quit
neg rax neg rax
cmp rax, 34 cmp rax, 34
jg .quit jg .unknown
mov rsi, rdi mov rsi, rdi
lea rdi, [rel perrorMsg] lea rdi, [rel perrorMsg]
lea rdx, [errorMsgs + rax*8] lea rdx, [errorMsgs + rax*8]
mov rdx, [rdx] mov rdx, [rdx]
call printf call printf
jmp .quit
.unknown:
mov rsi, rdi
lea rdi, [rel perrorInvalid]
mov rdx, rax
call printf
.quit: .quit:
pop rax pop rax
ret ret

View File

@ -91,6 +91,7 @@ section .rodata
msgPerror1 db "TEST perror(",34,"ErrTest",34,") (rax=0):",NL,EOS msgPerror1 db "TEST perror(",34,"ErrTest",34,") (rax=0):",NL,EOS
msgPerror2 db "TEST perror(",34,"ErrTest",34,") (rax=-22):",NL,EOS msgPerror2 db "TEST perror(",34,"ErrTest",34,") (rax=-22):",NL,EOS
msgPerror3 db "TEST perror(",34,"ErrTest",34,") (rax=7):",NL,EOS msgPerror3 db "TEST perror(",34,"ErrTest",34,") (rax=7):",NL,EOS
msgPerror4 db "TEST perror(",34,"ErrTest",34,") (rax=-50)",NL,EOS
perrorStr db "ErrTest",EOS perrorStr db "ErrTest",EOS
; min() / minu() ; min() / minu()
msgMin db NL,"# min() / minu()",EOS msgMin db NL,"# min() / minu()",EOS
@ -282,6 +283,13 @@ _start:
mov rax, 7 mov rax, 7
lea rdi, [rel perrorStr] lea rdi, [rel perrorStr]
call perror call perror
; TEST: perror (err=-50)
lea rdi, [rel msgPerror4]
call print
mov rax, -50
lea rdi, [rel perrorStr]
call perror
%ENDIF %ENDIF
%IF TEST_min %IF TEST_min
lea rdi, [rel msgMin] lea rdi, [rel msgMin]