home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / UUCODE / UUPC / TEST / UPC12ES4.ZIP / UTIL / fmt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-24  |  5.2 KB  |  161 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    Program:    fmt.c          14 August 1990                       */
  3. /*    Author:     Andrew H. Derbyshire                                */
  4. /*                108 Decatur St, Apt 9                               */
  5. /*                Arlington, MA 02174                                 */
  6. /*    Function:   Format lines into user specified width rows         */
  7. /*    Arguments:  width, input file, output file.                     */
  8. /*--------------------------------------------------------------------*/
  9.  
  10. /*--------------------------------------------------------------------*/
  11. /*                          RCS Information                           */
  12. /*--------------------------------------------------------------------*/
  13.  
  14. /*
  15.  *    $Id: fmt.c 1.3 1993/10/24 20:58:55 rhg Exp $
  16.  *
  17.  *    Revision history:
  18.  *    $Log: fmt.c $
  19.  * Revision 1.3  1993/10/24  20:58:55  rhg
  20.  * Clean up for MS C 7.0
  21.  *
  22.  * Revision 1.2  1993/04/11  00:33:54  ahd
  23.  * Global edits for year, TEXT, etc.
  24.  *
  25.  * Revision 1.1  1992/11/15  04:29:22  ahd
  26.  * Initial revision
  27.  *
  28.  * Revision 1.2  1992/04/27  00:41:11  ahd
  29.  * Add RCS Information
  30.  *
  31.  */
  32.  
  33. static char rcsid[] = "$Id: fmt.c 1.3 1993/10/24 20:58:55 rhg Exp $";
  34.  
  35. #include <stdlib.h>
  36. #include <ctype.h>
  37. #include <string.h>
  38. #include <stdio.h>
  39.  
  40. #include "lib.h"
  41. #include "timestmp.h"
  42.  
  43. /*--------------------------------------------------------------------*/
  44. /*    m a i n                                                         */
  45. /*                                                                    */
  46. /*    main program                                                    */
  47. /*--------------------------------------------------------------------*/
  48.  
  49.  void main( int argc, char *argv[] )
  50.  {
  51.    int width = 0;          /* Width of current line                  */
  52.    int maxwidth = 72;      /* Max width of line allowed              */
  53.    char buf[BUFSIZ];       /* Our I/O buffer                         */
  54.    int punct = 0;          /* Last character in last word was punct  */
  55.    int argx = 1;           /* Current argument being processed       */
  56.    FILE *input;
  57.    FILE *output;
  58.  
  59. /*--------------------------------------------------------------------*/
  60. /*                        Announce our version                        */
  61. /*--------------------------------------------------------------------*/
  62.  
  63.    banner( argv );
  64.  
  65. /*--------------------------------------------------------------------*/
  66. /*                            Handle help                             */
  67. /*--------------------------------------------------------------------*/
  68.  
  69.    if (( argc > 1 ) && equal(argv[1],"-?"))
  70.    {
  71.       printf("Usage:\tfmt\t[-#] infile outfile\n");
  72.       exit(1);
  73.    }
  74.  
  75. /*--------------------------------------------------------------------*/
  76. /*       Get the line width if the user specified it                  */
  77. /*--------------------------------------------------------------------*/
  78.  
  79.     if ((argx < argc) && (*argv[argx] == '-'))
  80.       maxwidth = atoi( argv[argx++] );
  81.  
  82. /*--------------------------------------------------------------------*/
  83. /*                  Get the input file name, if any                   */
  84. /*--------------------------------------------------------------------*/
  85.  
  86.     if (argx == argc)
  87.       input = stdin;
  88.     else {
  89.       input = fopen(argv[argx++],"r");
  90.       if (input == NULL)
  91.       {
  92.          perror(argv[--argx]);
  93.          exit (100);
  94.       }
  95.     }
  96.  
  97. /*--------------------------------------------------------------------*/
  98. /*                  Get the output file name, if any                  */
  99. /*--------------------------------------------------------------------*/
  100.  
  101.     if (argx == argc )
  102.       output = stdout;
  103.     else {
  104.       output = fopen(argv[argx++],"w");
  105.       if (output == NULL)
  106.       {
  107.          perror(argv[--argx]);
  108.          exit( 200 );
  109.       }
  110.     }
  111.  
  112. /*--------------------------------------------------------------------*/
  113. /*                          Process the file                          */
  114. /*--------------------------------------------------------------------*/
  115.  
  116.     while( fgets(buf, BUFSIZ, input) != NULL )
  117.     {
  118.       char *token = strtok(buf, " \t\n");
  119.       if (token == NULL )
  120.       {
  121.          fputc('\n',output);
  122.          width = punct = 0;
  123.       }
  124.       else while(token != NULL)
  125.       {
  126.          register int toklen = (int) strlen(token);
  127.          width = toklen + width + 1 + punct;
  128.          if (width > max(maxwidth, toklen + 1))
  129.          {
  130.             fputc('\n',output);
  131.             width = toklen;
  132.             punct = 0;
  133.          }
  134.          else {
  135.             if (width > (toklen + 1))
  136.             {
  137.                fputc(' ',output);
  138.                if (punct)
  139.                   fputc(' ',output);
  140.             }
  141.             else
  142.                width = toklen;
  143.             punct = ispunct( token[toklen - 1] ) ? 1 : 0;
  144.          } /* else */
  145.          fputs(token, output);
  146.          token = strtok(NULL, " \t\n");
  147.       } /* else while */
  148.    } /* while */
  149.  
  150.    if (ferror(input))
  151.    {
  152.       perror(argv[1]);
  153.       clearerr(input);
  154.    }
  155.  
  156.    fclose(input);
  157.    fclose(output);
  158.  
  159.    exit (0);
  160.  } /* main */
  161.