Browse Source

Restoring stdin fcntl flags before exit

Yann Weber 6 years ago
parent
commit
58ea0f523d
1 changed files with 24 additions and 3 deletions
  1. 24
    3
      wtfstopw.asm

+ 24
- 3
wtfstopw.asm View File

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

Loading…
Cancel
Save