|
| 1 | +/* tail.c |
| 2 | +
|
| 3 | + Unix-like tail for CP/M. |
| 4 | +
|
| 5 | + Print last lines from files. |
| 6 | +
|
| 7 | + Copyright (c) Miguel I. Garcia Lopez. |
| 8 | +
|
| 9 | + Usage: |
| 10 | +
|
| 11 | + tail [-nNUMBER] [file...] |
| 12 | +
|
| 13 | + Example: |
| 14 | +
|
| 15 | + tail -n6 myletter.doc resume.txt |
| 16 | + tail calendar.txt |
| 17 | +
|
| 18 | + Revisions: |
| 19 | +
|
| 20 | + 07 Sep 2015 : v1.00 : Version for CP/M. |
| 21 | + 07 Sep 2015 : v1.00 : Version for SamaruX as external command. |
| 22 | + 29 May 2016 : v1.01 : Reworked. |
| 23 | +*/ |
| 24 | + |
| 25 | +/* Defines for MESCC libraries |
| 26 | + --------------------------- |
| 27 | +*/ |
| 28 | +#define CC_STDIO // Support for stdin, stdout, stderr |
| 29 | +#define CC_REDIR // Support for CP/M command line redirection |
| 30 | + |
| 31 | +#define CC_FCX // Support for user number in filenames. |
| 32 | + |
| 33 | +#define CC_NO_SWITCH // Exclude support for SWITCH. |
| 34 | + |
| 35 | +/* Standard MESCC library |
| 36 | + ---------------------- |
| 37 | +*/ |
| 38 | +#include <mescc.h> |
| 39 | + |
| 40 | +/* Standard MESCC libraries |
| 41 | + ------------------------ |
| 42 | +*/ |
| 43 | +#include <conio.h> |
| 44 | +#include <fileio.h> |
| 45 | +#include <printf.h> |
| 46 | +#include <fprintf.h> |
| 47 | +#include <redir.h> |
| 48 | + |
| 49 | +/* Project defs. |
| 50 | + ------------- |
| 51 | +*/ |
| 52 | +#define APP_NAME "tail" |
| 53 | +#define APP_VERSION "v1.01 / 29 May 2016" |
| 54 | +#define APP_COPYRGT "(c) 2015-2016 FloppySoftware" |
| 55 | +#define APP_USAGE "tail [-nNUMBER] [file...]" |
| 56 | + |
| 57 | +#define MAX_LINES 16 |
| 58 | +#define MAX_LNBUF 80 |
| 59 | + |
| 60 | +/* The program |
| 61 | + ----------- |
| 62 | +*/ |
| 63 | +main(argc, argv) |
| 64 | +int argc, argv[]; |
| 65 | +{ |
| 66 | + char *pch, *fn; |
| 67 | + WORD *linarr; /* char *linarr[] */ |
| 68 | + int lines, i, retcode, argx; |
| 69 | + |
| 70 | + /* No command line arguments: show usage and exit */ |
| 71 | + |
| 72 | + if(argc == 1) |
| 73 | + { |
| 74 | + fprintf(stderr, "%s %s\n\n", APP_NAME, APP_VERSION); |
| 75 | + fprintf(stderr, "%s\n\n", APP_COPYRGT); |
| 76 | + fprintf(stderr, "Usage: %s\n", APP_USAGE); |
| 77 | + |
| 78 | + return 0; |
| 79 | + } |
| 80 | + |
| 81 | + /* Default values */ |
| 82 | + |
| 83 | + lines = 10; /* Print # lines */ |
| 84 | + fn = NULL; /* Filename */ |
| 85 | + |
| 86 | + /* Parse command line arguments */ |
| 87 | + |
| 88 | + for(i = 1; i < argc; ++i) |
| 89 | + { |
| 90 | + pch = argv[i]; |
| 91 | + |
| 92 | + if(*pch == '-') /* Look for -options */ |
| 93 | + { |
| 94 | + if(*(++pch) == 'N') |
| 95 | + { |
| 96 | + lines = atoi(++pch); |
| 97 | + |
| 98 | + if(lines < 1 || lines > MAX_LINES) |
| 99 | + error("Bad # of lines"); |
| 100 | + } |
| 101 | + else |
| 102 | + error("Bad option"); |
| 103 | + } |
| 104 | + else /* Look for files */ |
| 105 | + break; |
| 106 | + } |
| 107 | + |
| 108 | + argx = i; |
| 109 | + |
| 110 | + /* Get memory for lines */ |
| 111 | + |
| 112 | + if((linarr = malloc(lines + lines)) == NULL) |
| 113 | + error("No memory"); |
| 114 | + |
| 115 | + for(i = 0; i < lines; ++i) |
| 116 | + { |
| 117 | + if((linarr[i] = malloc(MAX_LNBUF)) == NULL) |
| 118 | + error("No memory"); |
| 119 | + } |
| 120 | + |
| 121 | + /* Process files */ |
| 122 | + |
| 123 | + if(argx == argc) |
| 124 | + retcode = do_tail("-", lines, linarr); |
| 125 | + else |
| 126 | + { |
| 127 | + while(argx < argc) |
| 128 | + { |
| 129 | + if((retcode = do_tail(argv[argx++], lines, linarr))) |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /* Success or failure */ |
| 135 | + |
| 136 | + if(retcode) |
| 137 | + error("Can't open file"); |
| 138 | + |
| 139 | + return 0; |
| 140 | +} |
| 141 | + |
| 142 | +/* Do tail on a file |
| 143 | + ----------------- |
| 144 | +*/ |
| 145 | +do_tail(fn, lines, arr) |
| 146 | +char *fn; int lines; WORD *arr; /* char *arr[] */ |
| 147 | +{ |
| 148 | + FILE *fp; |
| 149 | + int i, k, over; |
| 150 | + |
| 151 | + /* Open the file */ |
| 152 | + |
| 153 | + if(fn[0] == '-' && fn[1] == 0) |
| 154 | + fp = stdin; |
| 155 | + else if((fp = fopen(fn, "r")) == NULL) |
| 156 | + return -1; |
| 157 | + |
| 158 | + /* Read the file */ |
| 159 | + |
| 160 | + i = over = 0; |
| 161 | + |
| 162 | + while(fgets(arr[i], MAX_LNBUF, fp) != NULL) |
| 163 | + { |
| 164 | + if(++i == lines) |
| 165 | + { |
| 166 | + i = 0; |
| 167 | + over = 1; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /* Print the lines */ |
| 172 | + |
| 173 | + if(!over) |
| 174 | + { |
| 175 | + lines = i; |
| 176 | + i = 0; |
| 177 | + } |
| 178 | + |
| 179 | + k = lines; |
| 180 | + |
| 181 | + while(k--) |
| 182 | + { |
| 183 | + putstr(arr[i++]); /* The string already has a \n -- well, maybe not */ |
| 184 | + |
| 185 | + if(over && i == lines) |
| 186 | + i = 0; |
| 187 | + } |
| 188 | + |
| 189 | + /* Close the file */ |
| 190 | + |
| 191 | + if(fp != stdin) |
| 192 | + fclose(fp); |
| 193 | + |
| 194 | + /* Success */ |
| 195 | + |
| 196 | + return 0; |
| 197 | +} |
| 198 | + |
| 199 | +/* Print error and exit |
| 200 | + -------------------- |
| 201 | +*/ |
| 202 | +error(msg) |
| 203 | +char *msg; |
| 204 | +{ |
| 205 | + fprintf(stderr, "%s: %s.\n", APP_NAME, msg); |
| 206 | + exit(-1); |
| 207 | +} |
0 commit comments