mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Fix head(1) and tail(1) directory handling.
This commit is contained in:
parent
a306193824
commit
ec38222f9b
1 changed files with 30 additions and 9 deletions
|
@ -1,6 +1,6 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
|
|
||||||
Copyright(C) Jonas 'Sortie' Termansen 2012.
|
Copyright(C) Jonas 'Sortie' Termansen 2012, 2015.
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify it
|
This program is free software: you can redistribute it and/or modify it
|
||||||
under the terms of the GNU General Public License as published by the Free
|
under the terms of the GNU General Public License as published by the Free
|
||||||
|
@ -20,6 +20,8 @@
|
||||||
|
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -45,6 +47,13 @@ bool verbose = false;
|
||||||
|
|
||||||
bool processfp(const char* inputname, FILE* fp)
|
bool processfp(const char* inputname, FILE* fp)
|
||||||
{
|
{
|
||||||
|
struct stat st;
|
||||||
|
if ( fstat(fileno(fp), &st) == 0 && S_ISDIR(st.st_mode) )
|
||||||
|
{
|
||||||
|
error(0, EISDIR, "%s", inputname);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const bool tail = TAIL;
|
const bool tail = TAIL;
|
||||||
const bool head = !TAIL;
|
const bool head = !TAIL;
|
||||||
if ( !numlines ) { return true; }
|
if ( !numlines ) { return true; }
|
||||||
|
@ -69,7 +78,7 @@ bool processfp(const char* inputname, FILE* fp)
|
||||||
{
|
{
|
||||||
free(line);
|
free(line);
|
||||||
if ( feof(fp) ) { break; }
|
if ( feof(fp) ) { break; }
|
||||||
error(1, errno, "error reading line: %s", inputname);
|
error(1, errno, "%s", inputname);
|
||||||
}
|
}
|
||||||
if ( specialleading )
|
if ( specialleading )
|
||||||
{
|
{
|
||||||
|
@ -182,24 +191,36 @@ int main(int argc, char* argv[])
|
||||||
if ( !numfiles )
|
if ( !numfiles )
|
||||||
{
|
{
|
||||||
bool header = verbose;
|
bool header = verbose;
|
||||||
if ( header ) { printf("==> %s <==\n", STDINNAME); }
|
if ( header )
|
||||||
if ( !processfp(STDINNAME, stdin) ) { return 1; }
|
printf("==> %s <==\n", STDINNAME);
|
||||||
|
if ( !processfp(STDINNAME, stdin) )
|
||||||
|
return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
const char* prefix = "";
|
const char* prefix = "";
|
||||||
for ( int i = 1; i < argc; i++ )
|
for ( int i = 1; i < argc; i++ )
|
||||||
{
|
{
|
||||||
if ( !argv[i] ) { continue; }
|
if ( !argv[i] ) { continue; }
|
||||||
bool isstdin = strcmp(argv[i], "-") == 0;
|
bool isstdin = strcmp(argv[i], "-") == 0;
|
||||||
FILE* fp = isstdin ? stdin : fopen(argv[i], "r");
|
FILE* fp = isstdin ? stdin : fopen(argv[i], "r");
|
||||||
if ( !fp ) { error(1, errno, "error opening: %s", argv[i]); }
|
if ( !fp )
|
||||||
|
{
|
||||||
|
error(0, errno, "%s", argv[i]);
|
||||||
|
result = 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
bool header = !quiet && (verbose || 1 < numfiles);
|
bool header = !quiet && (verbose || 1 < numfiles);
|
||||||
if ( header ) { printf("%s==> %s <==\n", prefix, argv[i]); }
|
if ( header )
|
||||||
|
printf("%s==> %s <==\n", prefix, argv[i]);
|
||||||
prefix = "\n";
|
prefix = "\n";
|
||||||
if ( !processfp(isstdin ? STDINNAME : argv[i], fp) ) { return 1; }
|
if ( !processfp(isstdin ? STDINNAME : argv[i], fp) )
|
||||||
if ( !isstdin ) { fclose(fp); }
|
result = 1;
|
||||||
|
if ( !isstdin )
|
||||||
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue