diff --git a/src/file.S b/src/file.S index eabfa18..b8da07d 100644 --- a/src/file.S +++ b/src/file.S @@ -66,8 +66,15 @@ cat_file: # Cat out stdin to stdout # @param[in] a0: Not used. -# @param[out] a0: result code. +# @param[out] a0: Not used. cat_stdin: + addi sp, sp, -16 + sd ra, 0(sp) + sd s0, 8(sp) + + la t1, show_ends + lb s0, 0(t1) # store if we show ends into s1 + .LReadStdinBlock: li a0, 0 la a1, cat_buf @@ -75,8 +82,15 @@ cat_stdin: li a7, 63 ecall blez a0, .LCatStdinRet # if we haven't read any bytes, EOF, close FD - mv a2, a0 # move bytes read into bytes needed to write + beqz s0, .LNoEndingsStdinWrite + mv a1, a0 + la a0, cat_buf + call _stdout_add_endings + j .LReadStdinBlock + + .LNoEndingsStdinWrite: + mv a2, a0 # move bytes read into bytes needed to write li a0, 1 la a1, cat_buf li a7, 64 @@ -84,6 +98,9 @@ cat_stdin: j .LReadStdinBlock .LCatStdinRet: + ld ra, 0(sp) + ld s0, 8(sp) + addi sp, sp, 16 ret # Open fd diff --git a/src/main.S b/src/main.S index 39d1b23..3540ebb 100644 --- a/src/main.S +++ b/src/main.S @@ -46,11 +46,12 @@ _start: call fetch_argc mv s0, a1 # store argc li s1, 1 + li s2, 0 # processed files call _check_flags li t0, 1 - ble s0, t0, .LProcessStdin # use stdin if no commands + ble s0, t0, .LProcessStdin # use stdin if no commands, although this is checked later, if no flags are used, we can go ahead and jump .LProcessFile: mv a0, s1 @@ -64,10 +65,12 @@ _start: ld a0, 0(sp) addi sp, sp, 8 call cat_file + addi s2, s2, 1 bnez a0, .LReturnFileErr # error if failed .LSkipArg: addi s1, s1, 1 blt s1, s0, .LProcessFile + beqz s2, .LProcessStdin # if we reach the end of the file queue, and no files have actually been processed, that means they were all flags, switch to stdin j .LMainReturnOk .LProcessStdin: