📄 tail.c
字号:
/*
* TAIL.C - Output the last 10 lines from a text file.
*
* PROGRAMMER: Martti Ylikoski
* CREATED: 5.12.1990
*/
static char *VERSION= "Version 1.1, Copyright(c) Martti Ylikoski, 1990-1992." ;
/*
*/
#include <stdio.h>
#include <time.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <param.h>
#include <paramstd.h>
#define INCL_VIO
#include <os2.h>
static char *progname ;
extern unsigned long pflags ;
ParamEntry pentry[12] = {
"P", &ParamSetPause, 0,
"F", &ParamSetFold, 0,
"V", &ParamSetVerbose, 0,
"R", &ParamSetReport, 0,
"S", &ParamSetSubDirs, 0,
"?", &ParamSetHelp, 1,
"H", &ParamSetHelp, 1,
"NOD", &ParamSetNoDefault, 0,
"TEST", &ParamSetTest, 0,
"Y", &ParamSetYes, 0,
"N", &ParamSetTest, 0,
"\0", NULL, 0
} ;
ParamBlock params = {
"/-", IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
pentry
} ;
static int count, cols, stdinassumed ;
/* local prototypes */
int tail( char *fname, char **lines ) ;
int main(int argc, char *argv[])
{
int i, j, nargc ;
char **lines, *ptmp;
VIOMODEINFO vioinfo ;
USHORT ret ;
progname = argv[0] ;
count = 10 ;
stdinassumed = FALSE ;
ParamHandle(¶ms, &argc, argv) ;
if (pflags & PA_HELP)
{
puts("TAIL - display the last lines from a text file.") ;
puts(VERSION) ;
puts("Usage: tail [/H | /?] [-count] [-] file") ;
return( 0 ) ;
}
nargc = argc ;
for (i = 1 ; i < argc ; i++)
if (argv[i][0] == '-' || argv[i][0] == '/')
{
if (strlen(argv[i]) == 1)
{
stdinassumed = TRUE ;
argv[i] = NULL ;
nargc --;
continue ;
}
count = atoi(&argv[i][1]) ;
argv[i] = NULL ;
nargc -- ;
}
vioinfo.cb = sizeof(vioinfo) ;
if (( ret = VioGetMode(&vioinfo, 0 )) != 0)
{
fprintf(stderr, "%s: error in VioGetMode...\nExiting ...\n", progname) ;
return( 1 ) ;
}
cols = vioinfo.col ; /* the number of colunms in current video mode */
lines = (char **) calloc(count, (size_t) sizeof(ptmp) ) ;
for(i = 0 ; i < count ; i++)
lines[i] = (char *) calloc( cols, (size_t) 1) ;
if (nargc == 1)
tail("-", lines) ;
else
{
for (i = 1 ; i < argc ; i++)
if (argv[i] != NULL)
tail(argv[i], lines) ;
if (stdinassumed == TRUE)
tail("-", lines) ;
}
return( 0 ) ;
}
int tail( char *fname, char **lines )
{
FILE *fptr ;
int i,j ;
for(i = 0 ; i < count ; i++)
memset( lines[i], (int) '\0', (size_t) cols ) ;
fprintf(stderr,"-----FILE : %s\n",fname) ;
/* open file */
if (strcmp(fname, "-") == 0)
fptr = stdin ;
else
if ((fptr = fopen(fname, "r")) == NULL)
{
printf("%s: unable to open file %s for reading...\n", progname, fname) ;
printf("Exiting...\n") ;
return( 1 ) ;
}
i = 0 ;
while (feof(fptr) == 0 && fgets(lines[i%count], cols-1, fptr) != NULL)
{
lines[i%count][cols-1] = '\0' ;
if (lines[i%count][strlen(lines[i%count])-1] != '\n') /* scan to the next line */
{
int c ;
while ( (c = fgetc(fptr)) != '\n' && c != EOF)
;
}
i++ ;
}
for (j = 0 ; j < count ; j++)
fputs(lines[(i+j)%count], stdout) ;
/* close file */
fclose ( fptr ) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -