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

📄 tac.c

📁 HLPDK V10.0+ System Extension Library
💻 C
📖 第 1 页 / 共 2 页
字号:
/* tac - concatenate and print files in reverse
   Copyright (C) 1988, 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.  */

/* Written by Jay Lepreau (lepreau@cs.utah.edu).
   GNU enhancements by David MacKenzie (djm@ai.mit.edu). */

/* 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/tac.c 1.4.0.3 90/09/19 12:09:16 tho Exp $";

static char Program_Id[] = "tac";
static char RCS_Revision[] = "$Revision: 1.4.0.3 $";

#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 */

/* Usage: tac [-br] [-s separator] [+before] [+regex] [+separator separator]
          [file...]

   Copy each FILE, or the standard input if none are given or when a
   FILE name of "-" is encountered, to the standard output with the
   order of the records reversed.  The records are separated by
   instances of a string, or a newline if none is given.  By default, the
   separator string is attached to the end of the record that it
   follows in the file.

   Options:
   -b, +before			The separator is attached to the beginning
				of the record that it precedes in the file.
   -r, +regex			The separator is a regular expression.
   -s, +separator separator	Use SEPARATOR as the record separator.

   To reverse a file byte by byte, use (in bash, ksh, or sh):
tac -r -s '.\|
' file */

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

#ifdef STDC_HEADERS
#include <stdlib.h>
#include <errno.h>
#else
char *malloc ();
char *realloc ();

extern int errno;
#endif

/* The number of bytes per atomic read. */
#define INITIAL_READSIZE 8192

/* The number of bytes per atomic write. */
#define WRITESIZE 8192

char *mktemp ();

#ifndef _POSIX_SOURCE
off_t lseek ();
#endif

#ifdef MSDOS

#include <gnulib.h>
#include <io.h>

/* We need a static malloc (with cleanup) here, so mask off
   the one from gnulib   */
#define xmalloc _xmalloc
#define xrealloc _xrealloc

static char *_xmalloc (unsigned int n);
static char *_xrealloc (char *p, unsigned int n);

extern void main (int argc, char **argv);
static int tac (int fd, char *file);
static int tac_file (char *file);
static int tac_stdin (void );
static void output (char *start, char *past_end);
static void save_stdin (void );
static void xwrite (int desc, char *buffer, int size);
static SIGTYPE cleanup (void );

#else /* not MSDOS */

SIGTYPE cleanup ();
int tac ();
int tac_file ();
int tac_stdin ();
char *xmalloc ();
char *xrealloc ();
void output ();
void error ();
void save_stdin ();
void xwrite ();
#endif /* not MSDOS */

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

/* The string that separates the records of the file. */
char *separator;

/* If nonzero, print `separator' along with the record preceding it
   in the file; otherwise with the record following it. */
int separator_ends_record;

/* 0 if `separator' is to be matched as a regular expression;
   otherwise, the length of `separator', used as a sentinel to
   stop the search. */
int sentinel_length;

/* The length of a match with `separator'.  If `sentinel_length' is 0,
   `match_length' is computed every time a match succeeds;
   otherwise, it is simply the length of `separator'. */
int match_length;

/* The input buffer. */
char *buffer;

/* The number of bytes to read at once into `buffer'. */
unsigned read_size;

/* The size of `buffer'.  This is read_size * 2 + sentinel_length + 2.
   The extra 2 bytes allow `past_end' to have a value beyond the
   end of `buffer' and `match_start' to run off the front of `buffer'. */
unsigned buffer_size;

/* The compiled regular expression representing `separator'. */
static struct re_pattern_buffer compiled_separator;

struct option longopts[] =
{
#ifdef MSDOS
  {"copying", 0, NULL, 30},
  {"version", 0, NULL, 31},
#endif
  {"before", 0, &separator_ends_record, 0},
  {"regex", 0, &sentinel_length, 0},
  {"separator", 1, NULL, 's'},
  {NULL, 0, NULL, 0}
};

void
main (argc, argv)
     int argc;
     char **argv;
{
  char *error_message;		/* Return value from re_compile_pattern. */
  int optc, longind, errors;

  program_name = argv[0];
#ifdef MSDOS
  setmode (0, O_BINARY);
  setmode (1, O_BINARY);
#endif /* MSDOS */

  errors = 0;
  separator = "\n";
  sentinel_length = 1;
  separator_ends_record = 1;

  while ((optc = getopt_long (argc, argv, "brs:", longopts, &longind))
	 != EOF)
    {
      switch (optc)
	{
	case 0:
	  break;
	case 'b':
	  separator_ends_record = 0;
	  break;
	case 'r':
	  sentinel_length = 0;
	  break;
	case 's':
	  separator = optarg;
	  if (*separator == 0)
	    error (1, 0, "separator cannot be empty");
	  break;
#ifdef MSDOS
	case 30:
	  fprintf (stderr, COPYING);
	  exit (0);
	  break;
	case 31:
	  fprintf (stderr, VERSION);
	  exit (0);
	  break;
#endif
	default:
#ifdef MSDOS
	  fprintf (stderr, "\
Usage: %s [-br] [-s separator] [+before] [+regex] [+separator separator]\n\
       [+copying] [+version] [file...]\n",
#else /* not MSDOS */
	  fprintf (stderr, "\
Usage: %s [-br] [-s separator] [+before] [+regex] [+separator separator]\n\
       [file...]\n",
#endif /* not MSDOS */
		   program_name);
	  exit (1);
	}
    }

  if (sentinel_length == 0)
    {
      compiled_separator.allocated = 100;
#ifdef MSDOS
      compiled_separator.buffer
	= xmalloc ((size_t) compiled_separator.allocated);
#else /* not MSDOS */
      compiled_separator.buffer = xmalloc (compiled_separator.allocated);
#endif /* not MSDOS */
      compiled_separator.fastmap = xmalloc (256);
      compiled_separator.translate = 0;
      error_message = re_compile_pattern (separator, strlen (separator),
					  &compiled_separator);
      if (error_message)
	error (1, 0, "%s", error_message);
    }
  else
    match_length = sentinel_length = strlen (separator);

  read_size = INITIAL_READSIZE;
  /* A precaution that will probably never be needed. */
  while (sentinel_length * 2 >= read_size)
    read_size *= 2;
  buffer_size = read_size * 2 + sentinel_length + 2;
  buffer = xmalloc (buffer_size);
  if (sentinel_length)
    {
      strcpy (buffer, separator);
      buffer += sentinel_length;
    }
  else
    ++buffer;

  if (optind == argc)
    errors = tac_stdin ();
  else
    for (; optind < argc; ++optind)
      {
	if (strcmp (argv[optind], "-") == 0)
	  errors |= tac_stdin ();
	else
	  errors |= tac_file (argv[optind]);
      }

  /* Flush the output buffer. */
  output ((char *) NULL, (char *) NULL);
  exit (errors);
}

/* The name of a temporary file containing a copy of pipe input. */
char *tempfile;

/* Print the standard input in reverse, saving it to temporary
   file `tempfile' first if it is a pipe.
   Return 0 if ok, 1 if an error occurs. */

int
tac_stdin ()
{
  /* Previous values of signal handlers. */
  SIGTYPE (*sigint) (), (*sighup) (), (*sigterm) ();
  int errors;
  struct stat stats;

  /* No tempfile is needed for "tac < file".
     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 (0, &stats))
    {
      error (0, errno, "standard input");
      return 1;
    }
  if ((stats.st_mode & S_IFMT) == S_IFREG)
    return tac (0, "standard input");

  sigint = signal (SIGINT, SIG_IGN);
  if (sigint != SIG_IGN)
    signal (SIGINT, cleanup);
#ifdef SIGHUP
  sighup = signal (SIGHUP, SIG_IGN);
  if (sighup != SIG_IGN)
    signal (SIGHUP, cleanup);
#endif
  sigterm = signal (SIGTERM, SIG_IGN);
  if (sigterm != SIG_IGN)
    signal (SIGTERM, cleanup);
  save_stdin ();

  errors = tac_file (tempfile);

  unlink (tempfile);
  signal (SIGINT, sigint);
#ifdef SIGHUP
  signal (SIGHUP, sighup);
#endif
  signal (SIGTERM, sigterm);
  return errors;
}

#ifdef MSDOS
char template[] = "/tacXXXXXX";
char *workplate;
#else /* not MSDOS */
char template[] = "/tmp/tacXXXXXX";
char workplate[sizeof template];
#endif /* not MSDOS */

/* Make a copy of the standard input in `tempfile'. */

void
save_stdin ()
{
  int fd;

⌨️ 快捷键说明

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