Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012, 2015 Jonas 'Sortie' Termansen.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* tail.c
|
|
|
|
* Output the last part of files.
|
|
|
|
*/
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2015-08-11 06:19:01 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2016-02-28 18:40:20 -05:00
|
|
|
#include <ctype.h>
|
2012-03-02 18:29:24 -05:00
|
|
|
#include <errno.h>
|
|
|
|
#include <error.h>
|
2016-02-28 18:40:20 -05:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-03-02 18:29:24 -05:00
|
|
|
|
|
|
|
#ifdef HEAD
|
|
|
|
#define TAIL false
|
|
|
|
#else
|
|
|
|
#define TAIL true
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const char* STDINNAME = "standard input";
|
|
|
|
|
|
|
|
long numlines = 10;
|
|
|
|
bool quiet = false;
|
|
|
|
bool verbose = false;
|
|
|
|
|
|
|
|
bool processfp(const char* inputname, FILE* fp)
|
|
|
|
{
|
2015-08-11 06:19:01 -04:00
|
|
|
struct stat st;
|
|
|
|
if ( fstat(fileno(fp), &st) == 0 && S_ISDIR(st.st_mode) )
|
|
|
|
{
|
|
|
|
error(0, EISDIR, "%s", inputname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-02 18:29:24 -05:00
|
|
|
const bool tail = TAIL;
|
|
|
|
const bool head = !TAIL;
|
|
|
|
if ( !numlines ) { return true; }
|
|
|
|
bool specialleading = (head && 0 < numlines) || (tail && numlines < 0);
|
|
|
|
bool specialtrailing = !specialleading;
|
|
|
|
long abslines = (numlines < 0) ? -numlines : numlines;
|
|
|
|
|
|
|
|
char** buffer = NULL;
|
|
|
|
if ( specialtrailing )
|
2016-02-28 18:40:20 -05:00
|
|
|
buffer = (char**) calloc(1, sizeof(char*) * abslines);
|
2012-03-02 18:29:24 -05:00
|
|
|
|
|
|
|
long linenum;
|
|
|
|
for ( linenum = 0; true; linenum++ )
|
|
|
|
{
|
|
|
|
char* line = NULL;
|
|
|
|
size_t linesize;
|
|
|
|
ssize_t linelen = getline(&line, &linesize, fp);
|
|
|
|
if ( linelen < 0 )
|
|
|
|
{
|
2016-05-15 10:32:38 -04:00
|
|
|
if ( ferror(fp) )
|
|
|
|
error(1, errno, "%s", inputname);
|
2015-05-11 09:49:54 -04:00
|
|
|
free(line);
|
2016-05-15 10:32:38 -04:00
|
|
|
break;
|
2012-03-02 18:29:24 -05:00
|
|
|
}
|
|
|
|
if ( specialleading )
|
|
|
|
{
|
|
|
|
bool doprint = false;
|
|
|
|
if ( head && linenum < abslines ) { doprint = true; }
|
|
|
|
else if ( tail && abslines <= linenum ) { doprint = true; }
|
|
|
|
bool done = head && abslines <= linenum+1;
|
|
|
|
if ( doprint ) { printf("%s", line); }
|
|
|
|
free(line);
|
|
|
|
if ( done ) { return true; }
|
|
|
|
}
|
|
|
|
if ( specialtrailing )
|
|
|
|
{
|
|
|
|
long index = linenum % abslines;
|
|
|
|
if ( buffer[index] )
|
|
|
|
{
|
|
|
|
char* bufline = buffer[index];
|
|
|
|
if ( head ) { printf("%s", bufline); }
|
|
|
|
free(bufline);
|
|
|
|
}
|
|
|
|
buffer[index] = line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( specialtrailing && tail )
|
|
|
|
{
|
|
|
|
long numtoprint = linenum < abslines ? linenum : abslines;
|
|
|
|
for ( long i = 0; i < numtoprint; i++ )
|
|
|
|
{
|
|
|
|
long index = (linenum - numtoprint + i) % abslines;
|
|
|
|
printf("%s", buffer[index]);
|
|
|
|
free(buffer[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 18:40:20 -05:00
|
|
|
free(buffer);
|
2012-03-02 18:29:24 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
static void compact_arguments(int* argc, char*** argv)
|
2012-03-02 18:29:24 -05:00
|
|
|
{
|
2015-07-29 19:08:49 -04:00
|
|
|
for ( int i = 0; i < *argc; i++ )
|
|
|
|
{
|
|
|
|
while ( i < *argc && !(*argv)[i] )
|
|
|
|
{
|
|
|
|
for ( int n = i; n < *argc; n++ )
|
|
|
|
(*argv)[n] = (*argv)[n+1];
|
|
|
|
(*argc)--;
|
|
|
|
}
|
|
|
|
}
|
2012-03-02 18:29:24 -05:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
static void help(FILE* fp, const char* argv0)
|
2012-03-02 18:29:24 -05:00
|
|
|
{
|
2015-07-29 19:08:49 -04:00
|
|
|
fprintf(fp, "Usage: %s [OPTION]... [FILE]...\n", argv0);
|
2012-03-02 18:29:24 -05:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
static void version(FILE* fp, const char* argv0)
|
2012-03-02 18:29:24 -05:00
|
|
|
{
|
2013-11-04 07:41:22 -05:00
|
|
|
fprintf(fp, "%s (Sortix) %s\n", argv0, VERSIONSTR);
|
2012-03-02 18:29:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2015-07-29 19:08:49 -04:00
|
|
|
const char* nlinesstr = NULL;
|
2012-03-02 18:29:24 -05:00
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
const char* argv0 = argv[0];
|
2012-03-02 18:29:24 -05:00
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
const char* arg = argv[i];
|
2015-07-29 19:08:49 -04:00
|
|
|
if ( arg[0] != '-' || !arg[1] )
|
2012-03-02 18:29:24 -05:00
|
|
|
continue;
|
2015-07-29 19:08:49 -04:00
|
|
|
argv[i] = NULL;
|
|
|
|
if ( !strcmp(arg, "--") )
|
|
|
|
break;
|
|
|
|
if ( isdigit((unsigned char) arg[1]) )
|
|
|
|
nlinesstr = arg + 1;
|
|
|
|
else if ( arg[1] != '-' )
|
2012-03-02 18:29:24 -05:00
|
|
|
{
|
2016-02-28 18:40:20 -05:00
|
|
|
char c;
|
|
|
|
while ( (c = *++arg) ) switch ( c )
|
2012-03-02 18:29:24 -05:00
|
|
|
{
|
2015-07-29 19:08:49 -04:00
|
|
|
case 'n':
|
|
|
|
if ( !*(nlinesstr = arg + 1) )
|
|
|
|
{
|
|
|
|
if ( i + 1 == argc )
|
|
|
|
{
|
|
|
|
error(0, 0, "option requires an argument -- 'n'");
|
|
|
|
fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
|
|
|
|
exit(125);
|
|
|
|
}
|
|
|
|
nlinesstr = argv[i+1];
|
|
|
|
argv[++i] = NULL;
|
|
|
|
}
|
|
|
|
arg = "n";
|
|
|
|
break;
|
|
|
|
case 'q': quiet = true; verbose = false; break;
|
|
|
|
case 'v': quiet = false; verbose = true; break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);
|
|
|
|
help(stderr, argv0);
|
|
|
|
exit(1);
|
2012-03-02 18:29:24 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-29 19:08:49 -04:00
|
|
|
else if ( !strcmp(arg, "--help") )
|
|
|
|
help(stdout, argv0), exit(0);
|
|
|
|
else if ( !strcmp(arg, "--version") )
|
|
|
|
version(stdout, argv0), exit(0);
|
|
|
|
else if ( !strcmp(arg, "--quiet") || !strcmp(arg, "--silent") )
|
|
|
|
quiet = true, verbose = false;
|
|
|
|
else if ( !strcmp(arg, "--verbose") )
|
|
|
|
quiet = false, verbose = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: unknown option: %s\n", argv0, arg);
|
|
|
|
help(stderr, argv0);
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-03-02 18:29:24 -05:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
compact_arguments(&argc, &argv);
|
|
|
|
|
|
|
|
if ( nlinesstr )
|
|
|
|
{
|
|
|
|
char* nlinesstrend;
|
|
|
|
long nlines = strtol(nlinesstr, &nlinesstrend, 10);
|
|
|
|
if ( *nlinesstrend )
|
|
|
|
error(1, 0, "Bad number of lines: %s", nlinesstr);
|
|
|
|
numlines = nlines;
|
|
|
|
}
|
2012-03-02 18:29:24 -05:00
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
if ( argc < 2 )
|
2012-03-02 18:29:24 -05:00
|
|
|
{
|
|
|
|
bool header = verbose;
|
2015-08-11 06:19:01 -04:00
|
|
|
if ( header )
|
|
|
|
printf("==> %s <==\n", STDINNAME);
|
|
|
|
if ( !processfp(STDINNAME, stdin) )
|
|
|
|
return 1;
|
2012-03-02 18:29:24 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-11 06:19:01 -04:00
|
|
|
int result = 0;
|
|
|
|
|
2012-03-02 18:29:24 -05:00
|
|
|
const char* prefix = "";
|
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
bool isstdin = strcmp(argv[i], "-") == 0;
|
|
|
|
FILE* fp = isstdin ? stdin : fopen(argv[i], "r");
|
2015-08-11 06:19:01 -04:00
|
|
|
if ( !fp )
|
|
|
|
{
|
|
|
|
error(0, errno, "%s", argv[i]);
|
|
|
|
result = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-29 19:08:49 -04:00
|
|
|
bool header = !quiet && (verbose || 2 < argc);
|
2015-08-11 06:19:01 -04:00
|
|
|
if ( header )
|
|
|
|
printf("%s==> %s <==\n", prefix, argv[i]);
|
2012-03-02 18:29:24 -05:00
|
|
|
prefix = "\n";
|
2015-08-11 06:19:01 -04:00
|
|
|
if ( !processfp(isstdin ? STDINNAME : argv[i], fp) )
|
|
|
|
result = 1;
|
|
|
|
if ( !isstdin )
|
|
|
|
fclose(fp);
|
2012-03-02 18:29:24 -05:00
|
|
|
}
|
|
|
|
|
2015-08-11 06:19:01 -04:00
|
|
|
return result;
|
2012-03-02 18:29:24 -05:00
|
|
|
}
|