⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tail.c

📁 HLPDK V10.0+ System Extension Library
💻 C
📖 第 1 页 / 共 2 页
字号:
/* tail -- output last part of file(s)
   Copyright (C) 1989, 1990 Free Software Foundation, Inc.

   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 Software Foundation; either version 1, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
   This port is also distributed under the terms of the
   GNU General Public License as published by the
   Free Software Foundation.

   Please note that this file is not identical to the
   original GNU release, you should have received this
   code as patch to the official release.  */

#ifdef MSDOS
static char RCS_Id[] =
  "$Header: e:/gnu/fileutil/RCS/tail.c 1.4.0.4 90/09/19 12:09:20 tho Exp $";

static char Program_Id[] = "tail";
static char RCS_Revision[] = "$Revision: 1.4.0.4 $";

#define VERSION \
  "GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
  (sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__

#define COPYING \
  "This is free software, distributed under the terms of the\n" \
  "GNU General Public License.  For details, see the file COPYING.\n"
#endif /* MSDOS */

/* Can display any amount of data, unlike the Unix version, which uses
   a fixed size buffer and therefore can only deliver a limited number
   of lines.

   Usage: tail [-b [+]#] [-c [+]#] [-n [+]#] [-fqv] [+blocks [+]#]
          [+bytes [+]#] [+lines [+]#] [+follow] [+quiet] [+silent]
          [+verbose] [file...]

          tail [+/-#bcflqv] [file...]

   Options:
   -b, +blocks #	Tail by # 512-byte blocks.
   -c, +bytes #		Tail by # bytes.
   -f, +follow		Loop forever trying to read more characters at the
			end of the file, on the assumption that the file
			is growing.  Ignored if reading from a pipe.
			Cannot be used if more than one file is given.
   -l, -n, +lines #	Tail by # lines.
   -q, +quiet, +silent	Never print filename headers.
   -v, +verbose		Always print filename headers.

   If a number (#) starts with a `+', begin printing with the #th item
   from the start of each file, instead of from the end.

   Reads from standard input if no files are given or when a filename of
   ``-'' is encountered.
   By default, filename headers are printed only more than one file
   is given.
   By default, prints the last 10 lines (tail -n 10).

   Started by Paul Rubin <phr@ai.mit.edu>
   Finished by David MacKenzie <djm@ai.mit.edu> */

#include <stdio.h>
#include <getopt.h>
#include <ctype.h>
#include <sys/types.h>
#include "system.h"

#ifdef STDC_HEADERS
#include <errno.h>
#include <stdlib.h>
#define ISDIGIT(c) (isdigit ((unsigned char) (c)))
#else
#define ISDIGIT(c) (isascii (c) && isdigit (c))
char *malloc ();
void free ();

extern int errno;
#endif

#ifndef _POSIX_SOURCE
long lseek();
#endif

/* Number of items to tail. */
#define DEFAULT_NUMBER 10

/* The number of bytes in a block (-b option). */
#define BLOCKSIZE 512

/* Size of atomic reads. */
#define BUFSIZE (BLOCKSIZE * 8)

/* Masks for the operation mode.  If neither BYTES nor BLOCKS is set,
   tail operates by lines. */
#define BYTES 1			/* Tail by characters. */
#define BLOCKS 2		/* Tail by blocks. */
#define FOREVER 4		/* Read from end of file forever. */
#define START 8			/* Count from start of file instead of end. */
#define HEADERS 16		/* Print filename headers. */
#ifdef MSDOS
#define BINARY 32		/* Suppress crlf translation. */
#endif

/* When to print the filename banners. */
enum header_mode
{
  multiple_files, always, never
};

#ifdef MSDOS

#include <io.h>
#include <string.h>

extern  void main (int, char **);
extern  void write_header (char *);
extern  int tail (char *, int, int, long);
extern  int tail_file (char *, int, long);
extern  int tail_bytes (char *, int, int, long);
extern  int tail_lines (char *, int, int, long);
extern  int file_lines (char *, int, long, long);
extern  int pipe_lines (char *, int, long);
extern  int pipe_bytes (char *, int, long);
extern  int start_bytes (char *, int, long);
extern  int start_lines (char *, int, long);
extern  void dump_remainder (char *, int, int);
extern  void xwrite (int, char *, int);
extern  char *xmalloc (int);
extern  long atou (char *);
extern  char *basename (char *);
extern  void error (int status, int errnum, char *message, ...);
extern  void usage (void);

#else  /* not MSDOS */

char *xmalloc ();
int file_lines ();
int pipe_bytes ();
int pipe_lines ();
int start_bytes ();
int start_lines ();
int tail ();
int tail_bytes ();
int tail_file ();
int tail_lines ();
long atou();
void dump_remainder ();
void error ();
void usage ();
void write_header ();
void xwrite ();

#endif /* not MSDOS */

/* The name this program was run with. */
char *program_name;

struct option long_options[] =
{
#ifdef MSDOS
  {"binary", 1, NULL, 'B'},
  {"copying", 0, NULL, 30},
  {"version", 0, NULL, 31},
#endif
  {"blocks", 1, NULL, 'b'},
  {"bytes", 1, NULL, 'c'},
  {"follow", 0, NULL, 'f'},
  {"lines", 1, NULL, 'n'},
  {"quiet", 0, NULL, 'q'},
  {"silent", 0, NULL, 'q'},
  {"verbose", 0, NULL, 'v'},
  {NULL, 0, NULL, 0}
};

void
main (argc, argv)
     int argc;
     char **argv;
{
  enum header_mode header_mode = multiple_files;
  int errors = 0;		/* Exit status. */
  int mode = 0;			/* Flags. */
  /* In START mode, the number of items to skip before printing; otherwise,
     the number of items at the end of the file to print.  Initially, -1
     means the value has not been set. */
  long number = -1;
  int c;			/* Option character. */
  int longind;			/* Index in `long_options' of option found. */

  program_name = argv[0];

  if (argc > 1
      && ((argv[1][0] == '-' && ISDIGIT (argv[1][1]))
	  || (argv[1][0] == '+' && (ISDIGIT (argv[1][1]) || argv[1][1] == 0))))
    {
      /* Old option syntax: a dash or plus, one or more digits (zero digits
	 are acceptable with a plus), and one or more option letters. */
      if (argv[1][0] == '+')
	mode |= START;
      if (argv[1][1] != 0)
	{
	  for (number = 0, ++argv[1]; ISDIGIT (*argv[1]); ++argv[1])
	    number = number * 10 + *argv[1] - '0';
	  /* Parse any appended option letters. */
	  while (*argv[1])
	    {
	      switch (*argv[1])
		{
		case 'b':
		  mode |= BLOCKS;
		  mode &= ~BYTES;
		  break;

		case 'c':
		  mode |= BYTES;
		  mode &= ~BLOCKS;
		  break;

		case 'f':
		  mode |= FOREVER;
		  break;

		case 'l':
		  mode &= ~(BYTES | BLOCKS);
		  break;

		case 'q':
		  header_mode = never;
		  break;

		case 'v':
		  header_mode = always;
		  break;

		default:
		  error (0, 0, "unrecognized option `-%c'", *argv[1]);
		  usage ();
		}
	      ++argv[1];
	    }
	}
      /* Make the options we just parsed invisible to getopt. */
      argv[1] = argv[0];
      argv++;
      argc--;
    }

#ifdef MSDOS
  while ((c = getopt_long (argc, argv, "b:c:n:fqvB", long_options, &longind))
#else
  while ((c = getopt_long (argc, argv, "b:c:n:fqv", long_options, &longind))
#endif
	 != EOF)
    {
      switch (c)
	{
#ifdef MSDOS
	case 30:
	  fprintf (stderr, COPYING);
	  exit (0);
	  break;

	case 31:
	  fprintf (stderr, VERSION);
	  exit (0);
	  break;

	case 'B':
	  mode |= BINARY;
	  break;
#endif

	case 'b':
	  mode |= BLOCKS;
	  mode &= ~BYTES;
	  if (*optarg == '+')
	    {
	      mode |= START;
	      ++optarg;
	    }
	  else if (*optarg == '-')
	    ++optarg;
	  number = atou (optarg);
	  if (number == -1)
	    error (1, 0, "invalid number `%s'", optarg);
	  break;

	case 'c':
	  mode |= BYTES;
	  mode &= ~BLOCKS;
	  if (*optarg == '+')
	    {
	      mode |= START;
	      ++optarg;
	    }
	  else if (*optarg == '-')
	    ++optarg;
	  number = atou (optarg);
	  if (number == -1)
	    error (1, 0, "invalid number `%s'", optarg);
	  break;

	case 'f':
#ifndef MSDOS
  	  mode |= FOREVER;
#endif /* not MSDOS */
	  break;

	case 'n':
	  mode &= ~(BYTES | BLOCKS);
	  if (*optarg == '+')
	    {
	      mode |= START;
	      ++optarg;
	    }
	  else if (*optarg == '-')
	    ++optarg;
	  number = atou (optarg);
	  if (number == -1)
	    error (1, 0, "invalid number `%s'", optarg);
	  break;

	case 'q':
	  header_mode = never;
	  break;

	case 'v':
	  header_mode = always;
	  break;

	default:
	  usage ();
	}
    }

  if (number == -1)
    number = DEFAULT_NUMBER;

  /* To start printing with item `number' from the start of the file, skip
     `number' - 1 items.  `tail +0' is actually meaningless, but for Unix
     compatibility it's treated the same as `tail +1'. */
  if (mode & START)
    {
      if (number)
	--number;
    }

  if (mode & BLOCKS)
    number *= BLOCKSIZE;

  if (optind < argc - 1 && (mode & FOREVER))
    error (1, 0, "cannot follow the ends of multiple files");

  if (header_mode == always
      || header_mode == multiple_files && optind < argc - 1)
    mode |= HEADERS;

  if (optind == argc)
    errors |= tail_file ("-", mode, number);

  for (; optind < argc; ++optind)
    errors |= tail_file (argv[optind], mode, number);

  exit (errors);
}

/* Display the last `number' units of file `filename', controlled by
   the flags in `mode'.  "-" for `filename' means the standard input.
   Return 0 if successful, 1 if an error occurred. */

int
tail_file (filename, mode, number)
     char *filename;
     int mode;
     long number;
{
  int fd;

  if (!strcmp (filename, "-"))
    {
      filename = "standard input";
      if (mode & HEADERS)
	write_header (filename);
      return tail (filename, 0, mode, number);
    }
  else
    {
      fd = open (filename, O_RDONLY);
      if (fd == -1)
	{
	  error (0, errno, "%s", filename);
	  return 1;
	}
      else
	{
	  int errors;

	  if (mode & HEADERS)
	    write_header (filename);
	  errors = tail (filename, fd, mode, number);
	  close (fd);
	  return errors;
	}
    }
}

void
write_header (filename)
     char *filename;
{
  static int first_file = 1;

  if (first_file)
    {
      xwrite (1, "==> ", 4);
      first_file = 0;
    }
  else
    xwrite (1, "\n==> ", 5);
  xwrite (1, filename, strlen (filename));
  xwrite (1, " <==\n", 5);
}

/* Display the last `number' units of file `filename', open for reading
   in `fd', controlled by `mode'.
   Return 0 if successful, 1 if an error occurred. */

int
tail (filename, fd, mode, number)
     char *filename;
     int fd;
     int mode;
     long number;
{
#ifdef MSDOS
  int errors;

  if (mode & BINARY)
    {
      setmode (fileno (stdout), O_BINARY);
      setmode (fd, O_BINARY);
    }

  if (mode & (BYTES | BLOCKS))
    errors = tail_bytes (filename, fd, mode, number);
  else
    errors = tail_lines (filename, fd, mode, number);

  if (mode & BINARY)
    setmode (fileno (stdout), O_TEXT);

  return errors;

#else /* not MSDOS */

  if (mode & (BYTES | BLOCKS))
    return tail_bytes (filename, fd, mode, number);
  else
    return tail_lines (filename, fd, mode, number);

#endif /* not MSDOS */
}

/* Display the last part of file `filename', open for reading in`fd',
   using `number' characters, controlled by `mode'.
   Return 0 if successful, 1 if an error occurred. */

int
tail_bytes (filename, fd, mode, number)
     char *filename;
     int fd;
     int mode;
     long number;
{
  struct stat stats;

  /* Use fstat instead of checking for errno == ESPIPE because
     lseek doesn't work on some special files but doesn't return an
     error, either. */
  if (fstat (fd, &stats))
    {
      error (0, errno, "%s", filename);
      return 1;
    }

  if (mode & START)
    {
      if ((stats.st_mode & S_IFMT) == S_IFREG)
	lseek (fd, number, L_SET);
      else if (start_bytes (filename, fd, number))
	return 1;
      dump_remainder (filename, fd, mode);
    }
  else
    {
      if ((stats.st_mode & S_IFMT) == S_IFREG)
	{
	  if (lseek (fd, 0L, L_XTND) <= number)
	    /* The file is shorter than we want, or just the right size, so
	       print the whole file. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -