home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ohlutil / cat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  18.9 KB  |  793 lines

  1. /* cat -- concatenate files and print on the standard output.
  2.    Copyright (C) 1988, 1990 Free Software Foundation, Inc.
  3.    ChangeLog: changelog
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 1, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /*
  20.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  21.  *
  22.  * To this port, the same copying conditions apply as to the
  23.  * original release.
  24.  *
  25.  * IMPORTANT:
  26.  * This file is not identical to the original GNU release!
  27.  * You should have received this code as patch to the official
  28.  * GNU release.
  29.  *
  30.  * MORE IMPORTANT:
  31.  * This port comes with ABSOLUTELY NO WARRANTY.
  32.  *
  33.  * $Header: e:/gnu/fileutil/RCS/cat.c'v 1.3.0.2 90/06/29 00:46:30 tho Stable $
  34.  */
  35.  
  36. /* Differences from the Unix cat:
  37.    * Always unbuffered, -u is ignored.
  38.    * 100 times faster with -v -u.
  39.    * 20 times faster with -v.
  40.    * Warnings about nonexisting files are delayed until everything has
  41.      been written out.  This is nice if output is to a terminal.
  42.  
  43.    By tege@sics.se, Torbjorn Granlund, advised by rms, Richard Stallman. */
  44.  
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <sys/types.h>
  48. #ifndef MSDOS
  49. #ifndef _POSIX_SOURCE
  50. #include <sys/ioctl.h>
  51. #endif
  52. #endif /* not MSDOS */
  53. #include "system.h"
  54. #include "getopt.h"
  55.  
  56. #ifdef STDC_HEADERS
  57. #include <stdlib.h>
  58. #else
  59. char *malloc ();
  60. int free ();
  61.  
  62. extern int errno;
  63. #endif
  64.  
  65. #ifdef MSDOS
  66.  
  67. #include <string.h>
  68. #include <fcntl.h>
  69. #include <malloc.h>
  70. #include <io.h>
  71.  
  72. extern void main (int, char **);
  73. extern void error (int status, int errnum, char *message, ...);
  74. extern void usage (char *);
  75. extern void simple_cat (unsigned char *, int);
  76. extern void cat (unsigned char *, int, unsigned char *,\
  77.          int, int, int, int, int, int, int);
  78. extern void next_line_num (void);
  79. extern char *copystring (char *, char *);
  80.  
  81. #else /* not MSDOS */
  82.  
  83. #define max(h,i) ((h) > (i) ? (h) : (i))
  84.  
  85. #endif /* not MSDOS */
  86.  
  87. typedef unsigned char uchar;
  88.  
  89. char *copystring ();
  90. void cat ();
  91. void error ();
  92. void next_line_num ();
  93. void simple_cat ();
  94.  
  95. /* Name under which this program was invoked.  */
  96.  
  97. char *program_name;
  98.  
  99. /* Name of input file.  May be "-".  */
  100.  
  101. char *infile;
  102.  
  103. /* Descriptor on which input file is open.  */
  104.  
  105. int input_desc;
  106.  
  107. /* Descriptor on which output file is open.  Always is 1.  */
  108.  
  109. int output_desc;
  110.  
  111. /* Buffer for line numbers.  */
  112.  
  113. char line_buf[13] =
  114. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', '\t', '\0'};
  115.  
  116. /* Position in `line_buf' where printing starts.  This will not change
  117.    unless the number of lines are more than 999999.  */
  118.  
  119. char *line_num_print = line_buf + 5;
  120.  
  121. /* Position of the first digit in `line_buf'.  */
  122.  
  123. char *line_num_start = line_buf + 10;
  124.  
  125. /* Position of the last digit in `line_buf'.  */
  126.  
  127. char *line_num_end = line_buf + 10;
  128.  
  129. /* Preserves the `cat' function's local `newlines' between invocations.  */
  130.  
  131. int newlines2 = 0;
  132.  
  133. void
  134. usage (reason)
  135.      char *reason;
  136. {
  137.   if (reason != NULL)
  138.     fprintf (stderr, "%s: %s\n", program_name, reason);
  139.  
  140. #ifdef MSDOS
  141.   fprintf (stderr, "\
  142. Usage: %s [-bcenstuvABET] [+number] [+number-nonblank]\n\
  143.        [+squeeze-blank] [+show-nonprinting] [+show-ends] [+show-tabs]\n\
  144.        [+show-all] [+binary] [file...]\n", program_name);
  145. #else /* not MSDOS */
  146.   fprintf (stderr, "\
  147. Usage: %s [-bcenstuvABET] [+number] [+number-nonblank] [+squeeze-blank]\n\
  148.        [+show-nonprinting] [+show-ends] [+show-tabs] [+show-all] [file...]\n",
  149.        program_name);
  150. #endif /* not MSDOS */
  151.  
  152.   exit (2);
  153. }
  154.  
  155.  
  156. void
  157. main (argc, argv)
  158.      int argc;
  159.      char *argv[];
  160. {
  161.   /* Optimal size of i/o operations of output.  */
  162.   int outsize;
  163.  
  164.   /* Optimal size of i/o operations of input.  */
  165.   int insize;
  166.  
  167.   /* Pointer to the input buffer.  */
  168.   uchar *inbuf;
  169.  
  170.   /* Pointer to the output buffer.  */
  171.   uchar *outbuf;
  172.  
  173.   int c;
  174.  
  175.   /* Index in argv to processed argument.  */
  176.   int argind;
  177.  
  178.   /* Array of boolean values telling whether an input file exists and is
  179.      readable.  */
  180.   short *argbad;
  181.  
  182.   /* Device number of the output (file or whatever).  */
  183.   int out_dev;
  184.  
  185.   /* I-node number of the output.  */
  186.   int out_ino;
  187.  
  188.   /* Nonzero if the output file should not be the same as any input file. */
  189.   int check_redirection = 1;
  190.  
  191.   struct stat stat_buf;
  192.  
  193.   /* Count of non-fatal error conditions.  */
  194.   int exit_stat = 0;
  195.  
  196.   /* Variables that are set according to the specified options.  */
  197.   int numbers = 0;
  198.   int numbers_at_empty_lines = 1;
  199.   int squeeze_empty_lines = 0;
  200.   int mark_line_ends = 0;
  201.   int quote = 0;
  202.   int output_tabs = 1;
  203.   int options = 0;
  204.   int longind;
  205. #ifdef MSDOS
  206.   int binary = 0;
  207. #endif /* MSDOS */
  208.   static struct option long_options[] =
  209.   {
  210. #ifdef MSDOS
  211.     {"binary", 0, NULL, 'B'},
  212. #endif /* MSDOS */
  213.     {"number-nonblank", 0, NULL, 'b'},
  214.     {"number", 0, NULL, 'n'},
  215.     {"squeeze-blank", 0, NULL, 's'},
  216.     {"show-nonprinting", 0, NULL, 'v'},
  217.     {"show-ends", 0, NULL, 'E'},
  218.     {"show-tabs", 0, NULL, 'T'},
  219.     {"show-all", 0, NULL, 'A'},
  220.     {NULL, 0, NULL, 0}
  221.   };
  222.  
  223.   program_name = argv[0];
  224.  
  225.   /* Parse command line options.  */
  226.  
  227. #ifdef MSDOS
  228.   strlwr (program_name);
  229.   while ((c = getopt_long (argc, argv, "bcenstuvABET", long_options, &longind))
  230.      != EOF)
  231. #else /* not MSDOS */
  232.   while ((c = getopt_long (argc, argv, "bcenstuvAET", long_options, &longind))
  233.      != EOF)
  234. #endif /* not MSDOS */
  235.     {
  236.       options++;
  237.       if (c == 0)
  238.     c = long_options[longind].val;
  239.       switch (c)
  240.     {
  241.     case 'b':
  242.       numbers = 1;
  243.       numbers_at_empty_lines = 0;
  244.       break;
  245.  
  246.     case 'e':
  247.       mark_line_ends = 1;
  248.       quote = 1;
  249.       break;
  250.  
  251.     case 'n':
  252.       numbers = 1;
  253.       break;
  254.  
  255.     case 'c':        /* For POSIX. */
  256.     case 's':
  257.       squeeze_empty_lines = 1;
  258.       break;
  259.  
  260.     case 't':
  261.       output_tabs = 0;
  262.       quote = 1;
  263.       break;
  264.  
  265.     case 'u':
  266.       /* We provide the -u feature unconditionally.  */
  267.       options--;
  268.       break;
  269.  
  270.     case 'v':
  271.       quote = 1;
  272.       break;
  273.  
  274.     case 'A':
  275.       quote = 1;
  276.       mark_line_ends = 1;
  277.       output_tabs = 0;
  278.       break;
  279.  
  280. #ifdef MSDOS
  281.     case 'B':
  282.       binary++;
  283.       options--;    /* stdout will be binary iff no other option!  */
  284.       break;
  285. #endif /* MSDOS */
  286.  
  287.     case 'E':
  288.       mark_line_ends = 1;
  289.       break;
  290.  
  291.     case 'T':
  292.       output_tabs = 0;
  293.       break;
  294.  
  295.     default:
  296.       usage ((char *) 0);
  297.     }
  298.     }
  299.  
  300.   output_desc = fileno (stdout);
  301.  
  302.   /* Get device, i-node number, and optimal blocksize of output.  */
  303.  
  304.   if (fstat (output_desc, &stat_buf) < 0)
  305.     error (1, errno, "cannot fstat standard output");
  306.  
  307.   outsize = ST_BLKSIZE (stat_buf);
  308.   switch (stat_buf.st_mode & S_IFMT)
  309.     {
  310.       /* Input file can be output file for non-regular files.
  311.      fstat on pipes returns S_IFSOCK on some systems, S_IFIFO
  312.      on others, so the checking should not be done for those types,
  313.      and to allow things like cat < /dev/tty > /dev/tty, checking
  314.      is not done for device files either. */
  315.     case S_IFREG:
  316.       out_dev = stat_buf.st_dev;
  317.       out_ino = stat_buf.st_ino;
  318.       break;
  319.     default:
  320.       check_redirection = 0;
  321.       break;
  322.     }
  323.  
  324.   /* Check if any of the input files are the same as the output file.  */
  325.  
  326.   argbad = (short *) alloca ((argc + 1) * sizeof (short));
  327.  
  328.   /* Main loop.  */
  329.  
  330.   infile = "-";
  331.   argind = optind;
  332.  
  333.   do
  334.     {
  335.       argbad[argind] = 0;
  336.  
  337.       if (argind < argc)
  338.     infile = argv[argind];
  339.  
  340. #ifdef MSDOS
  341.       strlwr (infile);
  342. #endif /* MSDOS */
  343.  
  344.  
  345.       if (infile[0] == '-' && infile[1] == 0)
  346.     input_desc = fileno (stdin);
  347.       else
  348.     {
  349.       /* Open a file for input.  If the file cannot be opened, remember
  350.          the error number in argbad.  */
  351.  
  352.       input_desc = open (infile, O_RDONLY);
  353.       if (input_desc < 0)
  354.         {
  355.           argbad[argind] = errno;
  356.           exit_stat++;
  357.           continue;
  358.         }
  359.     }
  360.  
  361. #ifdef MSDOS
  362.       if (binary)
  363.     {
  364.       if (setmode (input_desc, O_BINARY) == -1)
  365.         error (1, errno, "%s: can't set to binary mode", infile);
  366.       if (options == 0 && setmode (output_desc, O_BINARY) == -1)
  367.         error (1, errno, "stdout: can't set to binary mode");
  368.     }
  369. #endif /* MSDOS */
  370.  
  371.       if (fstat (input_desc, &stat_buf) < 0)
  372.     error (1, errno, "%s", infile);
  373.       insize = ST_BLKSIZE (stat_buf);
  374.  
  375.       /* Compare the device and i-node numbers of this input file with
  376.      the corresponding values of the (output file associated with)
  377.      stdout, and skip this input file if they coincide.  Input
  378.      files cannot be redirected to themselves.  A warning will be
  379.      printed after all files have been processed.  This condition
  380.      is remembered in argbad.  */
  381.  
  382.       if (check_redirection
  383.       && stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino)
  384.     {
  385.       argbad[argind] = -1;
  386.       exit_stat++;
  387.       continue;
  388.     }
  389.  
  390.       /* Select which version of `cat' to use. If any options (more than -u)
  391.      were specified, use `cat', otherwise use `simple_cat'.  */
  392.  
  393.       if (options == 0)
  394.     {
  395.       insize = max (insize, outsize);
  396.       inbuf = (uchar *) malloc (insize);
  397.       if (inbuf == NULL)
  398.         error (1, 0, "virtual memory exhausted");
  399.  
  400.       simple_cat (inbuf, insize);
  401.     }
  402.       else
  403.     {
  404. #ifdef MSDOS            /* the user wants it slow, he can get it! */
  405.       insize = outsize = 0x1000;
  406. #endif /* MSDOS */
  407.       inbuf = (uchar *) malloc (insize + 1);
  408.       if (inbuf == NULL)
  409.         error (1, 0, "virtual memory exhausted");
  410.  
  411.       /* Why are (OUTSIZE  - 1 + INSIZE * 4 + 13) bytes allocated for
  412.          the output buffer?
  413.  
  414.          A test whether output needs to be written is done when the input
  415.          buffer empties or when a newline appears in the input.  After
  416.          output is written, at most (OUTSIZE - 1) bytes will remain in the
  417.          buffer.  Now INSIZE bytes of input is read.  Each input character
  418.          may grow by a factor of 4 (by the prepension of M-^).  If all
  419.          characters do, and no newlines appear in this block of input, we
  420.          will have at most (OUTSIZE - 1 + INSIZE) bytes in the buffer.  If
  421.          the last character in the preceeding block of input was a
  422.          newline, a line number may be written (according to the given
  423.          options) as the first thing in the output buffer. (Done after the
  424.          new input is read, but before processing of the input begins.)  A
  425.          line number requires seldom more than 13 positions.  */
  426.  
  427.       outbuf = (uchar *) malloc (outsize - 1 + insize * 4 + 13);
  428.       if (outbuf == NULL)
  429.         error (1, 0, "virtual memory exhausted");
  430.  
  431.       cat (inbuf, insize, outbuf, outsize, quote,
  432.            output_tabs, numbers, numbers_at_empty_lines, mark_line_ends,
  433.            squeeze_empty_lines);
  434.  
  435.       free (outbuf);
  436.     }
  437.  
  438.       free (inbuf);
  439.       if (input_desc)
  440.     close (input_desc);
  441.     }
  442.   while (++argind < argc);
  443.  
  444.   /* Print delayed warnings.  */
  445.  
  446.   infile = "-";
  447.   argind = optind;
  448.  
  449.   do
  450.     {
  451.       if (argind < argc)
  452.     infile = argv[argind];
  453.  
  454.       if (infile[0] == '-' && infile[1] == 0)
  455.     infile = "standard input";
  456.  
  457.       if (argbad[argind])
  458.     {
  459.       if (argbad[argind] < 0)
  460.         error (0, 0, "%s: input file is output file", infile);
  461.       else
  462.         error (0, argbad[argind], "%s", infile);
  463.     }
  464.     }
  465.   while (++argind < argc);
  466.  
  467.   exit (exit_stat != 0);
  468. }
  469.  
  470. /* Plain cat.  Copies the file behind `input_desc' to the file behind
  471.    `output_desc'.  */
  472.  
  473. void
  474. simple_cat (buf, bufsize)
  475.      /* Pointer to the buffer, used by reads and writes.  */
  476.      uchar *buf;
  477.  
  478.      /* Number of characters preferably read or written by each read and write
  479.         call.  */
  480.      int bufsize;
  481. {
  482.   /* Actual number of characters read, and therefore written.  */
  483.   int n_read;
  484.  
  485.   /* Loop until the end of the file.  */
  486.  
  487.   for (;;)
  488.     {
  489.       /* Read a block of input.  */
  490.  
  491.       n_read = read (input_desc, buf, bufsize);
  492.       if (n_read < 0)
  493.     error (1, errno, "%s", infile);
  494.  
  495.       /* End of this file?  */
  496.  
  497.       if (n_read == 0)
  498.     break;
  499.  
  500.       /* Write this block out.  */
  501.  
  502.       if (write (output_desc, buf, n_read) != n_read)
  503.     error (1, errno, "write error");
  504.     }
  505. }
  506.  
  507. /* Cat the file behind INPUT_DESC to the file behind OUTPUT_DESC.
  508.    Called if any option more than -u was specified.
  509.  
  510.    A newline character is always put at the end of the buffer, to make
  511.    an explicit test for buffer end unnecessary.  */
  512.  
  513. void
  514. cat (inbuf, insize, outbuf, outsize, quote,
  515.      output_tabs, numbers, numbers_at_empty_lines,
  516.      mark_line_ends, squeeze_empty_lines)
  517.  
  518.      /* Pointer to the beginning of the input buffer.  */
  519.      uchar *inbuf;
  520.  
  521.      /* Number of characters read in each read call.  */
  522.      int insize;
  523.  
  524.      /* Pointer to the beginning of the output buffer.  */
  525.      uchar *outbuf;
  526.  
  527.      /* Number of characters written by each write call.  */
  528.      int outsize;
  529.  
  530.      /* Variables that have values according to the specified options.  */
  531.      int quote;
  532.      int output_tabs;
  533.      int numbers;
  534.      int numbers_at_empty_lines;
  535.      int mark_line_ends;
  536.      int squeeze_empty_lines;
  537. {
  538.   /* Last character read from the input buffer.  */
  539.   uchar ch;
  540.  
  541.   /* Pointer to the next character in the input buffer.  */
  542.   uchar *bpin;
  543.  
  544.   /* Pointer to the first non-valid byte in the input buffer, i.e. the
  545.      current end of the buffer.  */
  546.   uchar *eob;
  547.  
  548.   /* Pointer to the position where the next character shall be written.  */
  549.   uchar *bpout;
  550.  
  551.   /* Number of characters read by the last read call.  */
  552.   int n_read;
  553.  
  554.   /* The variable NEWLINES determines how many consequtive newlines there have
  555.      been in the input.  0 newlines makes NEWLINES -1, 1 newline makes
  556.      NEWLINES 1, etc.  Initially 0 to indicate that we are at the beginning of
  557.      a new line.  The "state" of the procedure is determined by NEWLINES.  */
  558.   int newlines = newlines2;
  559.  
  560. #ifdef FIONREAD
  561.   /* If nonzero, use the FIONREAD ioctl, as an optimization.
  562.      (On Ultrix, it is not supported on NFS filesystems.)  */
  563.   int use_fionread = 1;
  564. #endif
  565.  
  566.   /* The inbuf pointers are initialized so that BPIN > EOB, and thereby input
  567.      is read immediately.  */
  568.  
  569.   eob = inbuf;
  570.   bpin = eob + 1;
  571.  
  572.   bpout = outbuf;
  573.  
  574.   for (;;)
  575.     {
  576.       do
  577.     {
  578.       /* Write if there are at least OUTSIZE bytes in OUTBUF.  */
  579.  
  580.       if (bpout - outbuf >= outsize)
  581.         {
  582.           uchar *wp = outbuf;
  583.           do
  584.         {
  585.           if (write (output_desc, wp, outsize) != outsize)
  586.             error (1, errno, "write error");
  587.           wp += outsize;
  588.         }
  589.           while (bpout - wp >= outsize);
  590.  
  591.           /* Move the remaining bytes to the beginning of the
  592.          buffer.  */
  593.  
  594.           bcopy (wp, outbuf, bpout - wp);
  595.           bpout = outbuf + (bpout - wp);
  596.         }
  597.  
  598.       /* Is INBUF empty?  */
  599.  
  600.       if (bpin > eob)
  601.         {
  602. #ifdef FIONREAD
  603.           int n_to_read = 0;
  604.  
  605.           /* Is there any input to read immediately?
  606.          If not, we are about to wait,
  607.          so write all buffered output before waiting.  */
  608.  
  609.           if (use_fionread
  610.           && ioctl (input_desc, FIONREAD, &n_to_read) < 0)
  611.         {
  612.           if (errno == EOPNOTSUPP)
  613.             use_fionread = 0;
  614.           else
  615.             error (1, errno, "cannot do ioctl on `%s'", infile);
  616.         }
  617.           if (n_to_read == 0)
  618. #endif
  619.         {
  620.           int n_write = bpout - outbuf;
  621.  
  622.           if (write (output_desc, outbuf, n_write) != n_write)
  623.             error (1, errno, "write error");
  624.           bpout = outbuf;
  625.         }
  626.  
  627.           /* Read more input into INBUF.  */
  628.  
  629.           n_read = read (input_desc, inbuf, insize);
  630.           if (n_read < 0)
  631.         error (1, errno, "%s", infile);
  632.           if (n_read == 0)
  633.         {
  634.           newlines2 = newlines;
  635.           return;
  636.         }
  637.  
  638.           /* Update the pointers and insert a sentinel at the buffer
  639.          end.  */
  640.  
  641.           bpin = inbuf;
  642.           eob = bpin + n_read;
  643.           *eob = '\n';
  644.         }
  645.       else
  646.         {
  647.           /* It was a real (not a sentinel) newline.  */
  648.  
  649.           /* Was the last line empty?
  650.          (i.e. have two or more consecutive newlines been read?)  */
  651.  
  652.           if (++newlines > 0)
  653.         {
  654.           /* Are multiple adjacent empty lines to be substituted by
  655.              single ditto (-s), and this was the second empty line?  */
  656.  
  657.           if (squeeze_empty_lines && newlines >= 2)
  658.             {
  659.               ch = *bpin++;
  660.               continue;
  661.             }
  662.  
  663.           /* Are line numbers to be written at empty lines (-n)?  */
  664.  
  665.           if (numbers && numbers_at_empty_lines)
  666.             {
  667.               next_line_num ();
  668.               bpout = (uchar *) copystring (bpout, line_num_print);
  669.             }
  670.         }
  671.  
  672.           /* Output a currency symbol if requested (-e).  */
  673.  
  674.           if (mark_line_ends)
  675.         *bpout++ = '$';
  676.  
  677.           /* Output the newline.  */
  678.  
  679.           *bpout++ = '\n';
  680.         }
  681.       ch = *bpin++;
  682.     }
  683.       while (ch == '\n');
  684.  
  685.       /* Are we at the beginning of a line, and line numbers are requested?  */
  686.  
  687.       if (newlines >= 0 && numbers)
  688.     {
  689.       next_line_num ();
  690.       bpout = (uchar *) copystring (bpout, line_num_print);
  691.     }
  692.  
  693.       /* Here CH cannot contain a newline character.  */
  694.  
  695.       /* The loops below continue until a newline character is found,
  696.      which means that the buffer is empty or that a proper newline
  697.      has been found.  */
  698.  
  699.       /* If quoting, i.e. at least one of -v, -e, or -t specified,
  700.      scan for chars that need conversion.  */
  701.       if (quote)
  702.     for (;;)
  703.       {
  704.         if (ch >= 32)
  705.           {
  706.         if (ch < 127)
  707.           *bpout++ = ch;
  708.         else if (ch == 127)
  709.           *bpout++ = '^',
  710.             *bpout++ = '?';
  711.         else
  712.           {
  713.             *bpout++ = 'M',
  714.               *bpout++ = '-';
  715.             if (ch >= 128 + 32)
  716.               if (ch < 128 + 127)
  717.             *bpout++ = ch - 128;
  718.               else
  719.             *bpout++ = '^',
  720.               *bpout++ = '?';
  721.             else
  722.               *bpout++ = '^',
  723.             *bpout++ = ch - 128 + 64;
  724.           }
  725.           }
  726.         else if (ch == '\t' && output_tabs)
  727.           *bpout++ = '\t';
  728.         else if (ch == '\n')
  729.           {
  730.         newlines = -1;
  731.         break;
  732.           }
  733.         else
  734.           *bpout++ = '^',
  735.         *bpout++ = ch + 64;
  736.  
  737.         ch = *bpin++;
  738.       }
  739.       else
  740.     /* Not quoting, neither of -v, -e, or -t specified.  */
  741.     for (;;)
  742.       {
  743.         if (ch == '\t' && !output_tabs)
  744.           *bpout++ = '^',
  745.         *bpout++ = ch + 64;
  746.         else if (ch != '\n')
  747.           *bpout++ = ch;
  748.         else
  749.           {
  750.         newlines = -1;
  751.         break;
  752.           }
  753.  
  754.         ch = *bpin++;
  755.       }
  756.     }
  757. }
  758.  
  759. /* Compute the next line number.  */
  760.  
  761. void
  762. next_line_num ()
  763. {
  764.   char *endp = line_num_end;
  765.   do
  766.     {
  767.       if ((*endp)++ < '9')
  768.     return;
  769.       *endp-- = '0';
  770.     }
  771.   while (endp >= line_num_start);
  772.   *--line_num_start = '1';
  773.   if (line_num_start < line_num_print)
  774.     line_num_print--;
  775. }
  776.  
  777. /* A less stupid strcpy.  Returns a pointer to the end of the destination
  778.    string, instead of to the beginning.  Doesn't null-terminate the
  779.    destination.  */
  780.  
  781. char *
  782. copystring (dst, src)
  783.      char *dst;
  784.      char *src;
  785. {
  786.   char c;
  787.  
  788.   while (c = *src++)
  789.     *dst++ = c;
  790.  
  791.   return dst;
  792. }
  793.