Add pre-cat file existance check
This commit is contained in:
+20
-3
@@ -2,9 +2,9 @@
|
||||
cat_buf: .space 4096
|
||||
|
||||
.section .text
|
||||
.globl cat_file, open_fd
|
||||
.globl cat_file, open_fd, file_exists
|
||||
|
||||
# Fetch argument count
|
||||
# Cat out the given file to stdout
|
||||
# @param[in] a0: ptr to null-terminated file str.
|
||||
# @param[out] a0: result code.
|
||||
cat_file:
|
||||
@@ -46,7 +46,7 @@ cat_file:
|
||||
ret
|
||||
|
||||
|
||||
# Fetch argument count
|
||||
# Open fd
|
||||
# @param[in] a0: ptr to null-terminated file str.
|
||||
# @param[out] a0: result code. (0-exists 1-doesn't exist)
|
||||
# @param[out] a1: fd.
|
||||
@@ -67,3 +67,20 @@ open_fd:
|
||||
|
||||
2:
|
||||
ret
|
||||
|
||||
# Check if the given file exists without opening fd
|
||||
# @param[in] a0: ptr to null-terminated file str.
|
||||
# @param[out] a0: result code (0-exists 1-doesn't exist).
|
||||
file_exists:
|
||||
mv a1, a0
|
||||
li a0, -100 # relative/absolute path
|
||||
li a2, 0
|
||||
li a7, 48
|
||||
ecall
|
||||
beqz a0, 1f
|
||||
li a0, 1
|
||||
ret
|
||||
|
||||
1:
|
||||
li a0, 0
|
||||
ret
|
||||
|
||||
+13
-2
@@ -1,6 +1,6 @@
|
||||
.data
|
||||
argc: .space 8
|
||||
file_err: .asciz "\nfile not found: "
|
||||
file_err: .asciz "File not found or cannot be accessed: "
|
||||
|
||||
.equ FILE_ERR_LEN, . - file_err
|
||||
|
||||
@@ -18,8 +18,19 @@ _start:
|
||||
li t0, 1
|
||||
ble a1, t0, 1f # return 1 if no file given
|
||||
mv s0, a1 # store argc
|
||||
li s1, 1 # i=1 (skip program name)
|
||||
li s1, 1
|
||||
|
||||
5: #iterate through and check that all files exist first
|
||||
mv a0, s1
|
||||
call fetch_argv
|
||||
bnez a0, 1f # error if failed
|
||||
mv a0, a1
|
||||
call file_exists
|
||||
bnez a0, 4f
|
||||
addi s1, s1, 1
|
||||
blt s1, s0, 5b
|
||||
|
||||
li s1, 1 # reset counter now that we've verified all files exist, skip command
|
||||
|
||||
3:
|
||||
mv a0, s1
|
||||
|
||||
Reference in New Issue
Block a user