|
@@ -95,6 +95,8 @@ section .data
|
95
|
95
|
lapcountlen: equ $ - lapcount
|
96
|
96
|
laplen: dq 2
|
97
|
97
|
|
|
98
|
+ fcntl_flag: dq 0
|
|
99
|
+
|
98
|
100
|
section .text
|
99
|
101
|
global _start
|
100
|
102
|
_start:
|
|
@@ -105,6 +107,7 @@ xor rdi, rdi
|
105
|
107
|
mov rax, 72 ; fcntl
|
106
|
108
|
mov rsi, 3 ; F_GETFL
|
107
|
109
|
syscall
|
|
110
|
+mov [fcntl_flag], rax
|
108
|
111
|
mov rdx, rax
|
109
|
112
|
or rdx, 0x800 ; O_NONBLOCK
|
110
|
113
|
mov rax, 72 ; fcntl
|
|
@@ -175,9 +178,26 @@ flush_stdin:
|
175
|
178
|
je newline_exit
|
176
|
179
|
jg flush_stdin
|
177
|
180
|
|
|
181
|
+mov rdi, 0 ; EXIT OK
|
|
182
|
+;
|
|
183
|
+; Expect rdi to be the return code
|
|
184
|
+;
|
178
|
185
|
exit:
|
|
186
|
+ push rdi
|
|
187
|
+ ; restoring stdin state
|
|
188
|
+ mov rax, 72 ; fcntl
|
|
189
|
+ xor rdi, rdi
|
|
190
|
+ mov rsi, 4 ; F_SETFL
|
|
191
|
+ mov rdx, [fcntl_flag]
|
|
192
|
+ syscall
|
|
193
|
+ cmp rax, 0
|
|
194
|
+ je exit_end
|
|
195
|
+ pop rdi ; failed to restore
|
|
196
|
+ push 1 ; exit FAIL
|
|
197
|
+
|
|
198
|
+ exit_end:
|
179
|
199
|
mov rax, 60 ; sys_exit
|
180
|
|
- mov rdi, 0 ; OK
|
|
200
|
+ pop rdi ; return code
|
181
|
201
|
syscall
|
182
|
202
|
fault:
|
183
|
203
|
mov rax, 1 ; write
|
|
@@ -190,9 +210,8 @@ fault:
|
190
|
210
|
mov rdx, faultmsglen
|
191
|
211
|
syscall
|
192
|
212
|
|
193
|
|
- mov rax, 60 ; sys_exit
|
194
|
213
|
mov rdi, 1 ; failure
|
195
|
|
- syscall
|
|
214
|
+ jmp exit
|
196
|
215
|
|
197
|
216
|
newline_exit:
|
198
|
217
|
mov rax, 1
|
|
@@ -200,6 +219,8 @@ newline_exit:
|
200
|
219
|
mov rsi, nl
|
201
|
220
|
mov rdx, 1
|
202
|
221
|
syscall
|
|
222
|
+
|
|
223
|
+ mov rdi, 0 ; exit OK
|
203
|
224
|
jmp exit
|
204
|
225
|
|
205
|
226
|
;
|