Skip to content

Commit

Permalink
Add tail
Browse files Browse the repository at this point in the history
  • Loading branch information
vascocosta committed Mar 23, 2024
1 parent 7bcca5e commit 0ee6d92
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ Usage: %s [-hn min-len] filename
-n strings at least min-len long (default: 4)
```

### tail

```
Usage: %s [-hn] filename
-h show this help message
-n print the last n lines (default: 10)
```

### wc

```
Expand Down
6 changes: 6 additions & 0 deletions tail/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
obj/
src/gfx/*.c
src/gfx/*.h
src/gfx/*.8xv
.DS_Store
convimg.out
7 changes: 7 additions & 0 deletions tail/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The tail utility for MOS on the Agon Light computer

```
Usage: %s [-hn] filename
-h show this help message
-n print the last n lines (default: 10)
```
Binary file added tail/bin/tail.bin
Binary file not shown.
157 changes: 157 additions & 0 deletions tail/bin/tail.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
Section Base Top High Length
----------- ------ ------ ------ -------
.libs 0B0045 0B0045 0h
.init 0B0045 0B0079 34h
.fini 0B0079 0B00BF 46h
.init.args 0B00BF 0B00D5 16h
.init.bss 0B00D5 0B00E1 Ch
.text 0B00E1 0B336E 328Dh
.data 0B336E 0B33B3 45h
.rodata 0B33B3 0B35C0 20Dh
.init_array 000000 000000 0h
.ctors 000000 000000 0h
.dtors 000000 000000 0h
.fini_array 000000 000000 0h
.bss 080000 080031 09FFFF 31h
.header 0B0000 0B0045 45h

Label Value
---------------------- ------
___low_bss = 080000
___len_bss = 000031
___heaptop = 09FFFF
___heapbot = 080031
___init_array_count = 000000
___ctors_count = 000000
___dtors_count = 000000
___fini_array_count = 000000
__stack = 0AFFFF
__fcmp = 0B00E1
_inchar = 0B00FE
__setflag = 0B0105
___arg_processing = 0B0114
___parse_args = 0B0135
___check_redirect = 0B0218
___close_redir_files = 0B0409
_.str = 0B33B3
_.str.1 = 0B33B5
_.str.2 = 0B33B7
__frameset = 0B0452
__frameset0 = 0B0460
__iand = 0B046B
__idivu = 0B0484
__idvrms = 0B048C
__idvrmu = 0B04BA
__imulu = 0B04D2
__indcallhl = 0B0502
__ineg = 0B0503
__ior = 0B050C
__irems = 0B0525
__ishl = 0B052D
__ishrs = 0B0558
__ishru = 0B0553
__ixor = 0B0594
__ladd = 0B05AD
__land = 0B05B3
__lcmps = 0B05CE
__lcmpu = 0B05DB
__lcmpzero = 0B05E3
__lladd = 0B05EB
__lland = 0B060F
__llcmpu = 0B064C
__llcmpzero = 0B0676
__llctlz = 0B068B
__lldivs = 0B06A3
__lldivu = 0B06F3
__lldvrmu = 0B0707
__lldvrmu.hijack = 0B070E
__llmulu = 0B0765
__llneg = 0B07BA
__llshl = 0B07D3
__llshrs.hijack_llshru = 0B0801
__llshrs.finish = 0B0820
__llshru = 0B0824
__llsub = 0B083E
__lmulu = 0B0860
__lneg = 0B08BC
__lshrs.hijack_lshru = 0B08D5
__lshru = 0B08E4
_atoi = 0B08F4
_fclose = 0B0907
_fgetc = 0B094B
_fgets = 0B0A2A
__stdio_init = 0B0ABD
_stdin = 0B336E
_stdout = 0B3373
_stderr = 0B3378
__file_streams = 080000
_fopen = 0B0B0F
___fmodeflags = 0B0BD0
_.str = 0B33B9
_fputc = 0B0C37
_fput_char = 0B0CC7
_free = 0B0CEE
_fseek = 0B0DAB
_fwrite = 0B0E54
_malloc = 0B0F28
__alloc_base = 080028
___maptab = 0B33BD
_npf_vpprintf = 0B0FC7
JTI0_0 = 0B343D
JTI0_1 = 0B345B
JTI0_2 = 0B34A0
JTI0_3 = 0B34C1
JTI0_4 = 0B34D9
JTI0_5 = 0B34F1
_npf_utoa_rev = 0B2731
_printf = 0B27F9
_vprintf = 0B2818
_npf_putc_std = 0B2838
_.str = 0B3509
_.str.1 = 0B350D
_.str.2 = 0B3511
_strtol = 0B2848
_isspace = 0B2A41
_outchar = 0B2A58
_putchar = 0B2A5F
_puts = 0B2A8E
___null_literal_str = 0B3515
___end_of_line_str = 0B351C
_sbrk = 0B2ABA
__sbrkbase = 0B337D
_toupper = 0B2AD8
_strchr = 0B2AE5
_strcmp = 0B2B02
_strlen = 0B2B1F
_strncmp = 0B2B34
_errno = 08002E
_mos_fopen = 0B2DC7
_mos_fclose = 0B2DDF
_mos_fgetc = 0B2DF4
_mos_fputc = 0B2E09
_mos_fwrite = 0B2F0D
_mos_flseek = 0B2F29
_mos_getfil = 0B2F48
_freopen = 0B2FDE
_ungetc = 0B3028
__start = 0B0045
_exit = 0B007B
skip._abort = 0B00B3
_exit.sp = 0B00B4
_clear_bss = 0B00D5
_parse_params = 0B00BF
argv_ptrs = 0B3380
_atexit = 0B3065
exit_functions = 0B33B0
_show_usage = 0B3091
_show_lines = 0B30BB
___main_argc_argv = 0B321A
_.str = 0B351E
_.str.3 = 0B3539
_.str.4 = 0B353C
_.str.5 = 0B353F
_.str.6 = 0B3542
_.str.7 = 0B3567
_.str.8 = 0B3569
_str = 0B357C
_str.9 = 0B3597
17 changes: 17 additions & 0 deletions tail/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ----------------------------
# Makefile Options
# ----------------------------

NAME = tail
DESCRIPTION = "The tail utility for Agon"
COMPRESSED = NO
INIT_LOC = 0B0000
LDHAS_EXIT_HANDLER:=0
LDHAS_ARG_PROCESSING = 1

CFLAGS = -Wall -Wextra -Oz
CXXFLAGS = -Wall -Wextra -Oz

# ----------------------------

include $(shell cedev-config --makefile)
93 changes: 93 additions & 0 deletions tail/src/tail.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void show_usage(char *prog_name)
{
printf("Usage: %s [-hn] filename\r\n", prog_name);
printf("-h show this help message\r\n");
printf("-n print the last n lines (default: 10)\r\n");
}

void show_lines(FILE *file, int lines)
{
char **buf = malloc(sizeof(char *) * lines);

for (int i = 0; i < lines; i++)
buf[i] = malloc(sizeof(char) * 1024);

int lc = 0;

while (fgets(buf[lc % lines], 1024, file) != NULL)
{
lc++;
}

int start = lc > lines ? lc % lines : 0;
int count = lc > lines ? lines : lc;

for (int i = 0; i < count; i++)
{
printf("%s", buf[(start + i) % lines]);
}

for (int i = 0; i < lines; i++)
free(buf[i]);

free(buf);
}

int main(int argc, char *argv[])
{
FILE *file = NULL;
char *filename = NULL;
int parsed_lines = 0;
size_t lines = 10;

for (int i = 1; i != argc; i++)
{
if (strcmp(argv[i], "-h") == 0)
{
show_usage(argv[0]);
return 0;
}
else if (strncmp(argv[i], "-n", 2) == 0)
{
parsed_lines = atoi(argv[++i]);

if (parsed_lines <= 0)
{
fprintf(stderr, "The number of lines must be positive");
return 1;
}

lines = parsed_lines;
}
else if (!filename)
{
filename = argv[i];
}
else
{
show_usage(argv[0]);
return 0;
}
}

if (filename == NULL)
{
show_usage(argv[0]);
return 0;
}

if (!(file = fopen(filename, "r")))
{
fprintf(stderr, "Error opening file");
return 1;
}

show_lines(file, lines);
fclose(file);

return 0;
}

0 comments on commit 0ee6d92

Please sign in to comment.