add tail
This commit is contained in:
80
tail.c
Normal file
80
tail.c
Normal file
@@ -0,0 +1,80 @@
|
||||
/* See LICENSE file for copyright and license detaketails. */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
static void dropinit(FILE *, const char *, long);
|
||||
static void taketail(FILE *, const char *, long);
|
||||
|
||||
static void (*tail)(FILE *, const char *, long) = taketail;
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
char *end, c;
|
||||
long n = 10;
|
||||
FILE *fp;
|
||||
|
||||
while((c = getopt(argc, argv, "n:")) != -1)
|
||||
switch(c) {
|
||||
case 'n':
|
||||
n = abs(strtol(optarg, &end, 0));
|
||||
if(*end != '\0')
|
||||
eprintf("%s: not a number\n", optarg);
|
||||
if(optarg[0] == '+')
|
||||
tail = dropinit;
|
||||
break;
|
||||
default:
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if(optind == argc)
|
||||
tail(stdin, "<stdin>", n);
|
||||
else if(optind == argc-1) {
|
||||
if(!(fp = fopen(argv[optind], "r")))
|
||||
eprintf("fopen %s:", argv[optind]);
|
||||
tail(fp, argv[optind], n);
|
||||
fclose(fp);
|
||||
}
|
||||
else
|
||||
eprintf("usage: %s [-n lines] [file]\n", argv[0]);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void
|
||||
dropinit(FILE *fp, const char *str, long n)
|
||||
{
|
||||
char buf[BUFSIZ];
|
||||
long i = 0;
|
||||
|
||||
while(i < n && fgets(buf, sizeof buf, fp))
|
||||
if(buf[strlen(buf)-1] == '\n')
|
||||
i++;
|
||||
concat(fp, str, stdout, "<stdout>");
|
||||
}
|
||||
|
||||
void
|
||||
taketail(FILE *fp, const char *str, long n)
|
||||
{
|
||||
char **ring;
|
||||
long i, j;
|
||||
size_t *size;
|
||||
|
||||
if(!(ring = calloc(n, sizeof *ring)) || !(size = calloc(n, sizeof *size)))
|
||||
eprintf("calloc:");
|
||||
for(i = j = 0; afgets(&ring[i], &size[i], fp); i = j = (i+1)%n)
|
||||
;
|
||||
|
||||
do {
|
||||
if(ring[j])
|
||||
fputs(ring[j], stdout);
|
||||
} while((j = (j+1)%n) != i);
|
||||
free(ring);
|
||||
free(size);
|
||||
|
||||
if(ferror(fp))
|
||||
eprintf("%s: read error:", str);
|
||||
}
|
Reference in New Issue
Block a user