home *** CD-ROM | disk | FTP | other *** search
- /*
- * usage: lines file_name[s]
- *
- * LINES Provides a quick count of the number of lines in each of one
- * or more files.
- *
- * input: Any file[s], but most useful if ASCII text.
- *
- * output: A one line report on the screen of the number of lines in
- * each input file.
- *
- * writeup: MIR TUTORIAL ONE, topic 6
- *
- * written: Douglas Lowry Mar 07 92
- * Copyright (C) 1992 Marpex Inc.
- *
- * The MIR (Mass Indexing and Retrieval) Tutorials explain detailed
- * usage and co-ordination of the MIR family of programs to analyze,
- * prepare and index databases (small through gigabyte size), and
- * how to build integrated retrieval software around the MIR search
- * engine. The fifth of the five MIR tutorial series explains how
- * to extend indexing capability into leading edge search-related
- * technologies. For more information, GO IBMPRO on CompuServe;
- * MIR files are in the DBMS library. The same files are on the
- * Canada Remote Systems BBS. A diskette copy of the Introduction
- * is available by mail ($10 US... check, Visa or Mastercard);
- * diskettes with Introduction, Tutorial ONE software and the
- * shareware Tutorial ONE text cost $29. Shareware registration
- * for a tutorial is also $29.
- *
- * E-mail...
- * Compuserve 71431,1337
- * Internet doug.lowry%canrem.com
- * UUCP canrem!doug.lowry
- * Others: doug.lowry@canrem.uucp
- *
- * FAX... 416 963-5677
- *
- * "Snail mail"... Douglas Lowry, Ph.D.
- * Marpex Inc.
- * 5334 Yonge Street, #1102
- * North York, Ontario
- * Canada M2N 6M2
- *
- * Related database consultation and preparation services are
- * available through:
- * Innotech Inc., 2001 Sheppard Avenue E., Suite #118,
- * North York, Ontario Canada M2J 4Z7
- * Tel. 416 492-3838 FAX 416 492-3843
- *
- * This program is free software; you may redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, 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
- * (file 05LICENS) along with this program; if not, write to the
- * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
- * USA.
- */
-
- #include <stdio.h>
- #include <fcntl.h>
- #include <sys\types.h>
- #include <sys\stat.h>
- #include <io.h>
-
- #define repeat for(;;)
-
- typedef enum bool
- { FALSE = 0, TRUE = 1 } Bool ;
-
- #define INTAKE 2048 /* # of bytes in input buffer */
-
- void process(), Usage_();
- char *Cmdname_() { return( "lines" ) ; }
-
- /*
- * MAIN -
- */
-
- main( argc, argv )
- int argc;
- char **argv;
- {
- char c10 ;
- int fd, /* file descriptor */
- file;
- long int line_ct, /* accumulator within file */
- grand_total ;
-
- c10 = argv[1][0] ;
- if( argc == 1 || c10 == '-' || c10 == '/' || c10 == '?' )
- Usage_();
-
- grand_total = 0 ;
-
- for( file = 1 ; file < argc ; file++ )
- {
- if(( fd = open( argv[ file ], O_RDONLY | O_BINARY )) == -1 )
- {
- fprintf( stderr, "Can't open file %s\n", argv[ file ] );
- continue;
- }
-
- process( fd, &line_ct );
-
- printf( "\n%7ld lines in file %s\n", line_ct, argv[file] ) ;
- grand_total += line_ct ;
-
- if( close( fd ))
- fprintf( stderr, "Problem closing %s\n", argv[ file ] );
- }
-
- printf( "\n%7ld lines TOTAL\n", grand_total ) ;
-
- exit( 0 );
- }
- void
- Usage_()
- {
- fprintf( stderr, "\nusage: %s file_name[s]\n\n\
- Provides a quick count of the number of lines in each of one\n\
- or more files.\n\n\
- input: Any file[s], but most useful if ASCII text.\n\n\
- output: A one line report on the screen of the number of lines in\n\
- each input file.\n\n", Cmdname_() );
- fprintf( stderr, "writeup: MIR TUTORIAL ONE, topic 6\n\n" ) ;
-
- exit( 1 ) ;
- }
- /*
- * PROCESS
- */
- void
- process( fd, cum )
- int fd; /* file descriptor */
- long int *cum; /* count of line feeds */
- {
- unsigned char buf_in[ INTAKE ];
- register int buf_len,
- i;
- *cum = 0;
-
- repeat
- {
- if ( ( buf_len = read( fd, buf_in, INTAKE ) ) == 0 )
- break;
-
- for ( i= 0 ; i < buf_len ; i++ )
- {
- if( buf_in[ i ] == '\n' )
- *cum += 1 ;
- }
- }
-
- return;
- }