i finally found wc.c in the textutils-1.22 package. I've attached a copy
of my function
int LineCount( FILE *fp );
which uses the relevant code from wc.c.
Novak.
/* util.c : A collection of utility functions to make life easier. */
#include <stdio.h>
#include <string.h>
#include "util.h"
/* Size of atomic reads. */
#define BUFFER_SIZE (16 * 1024)
/************************************************************************/
/* LineCount() */
/*
*/
/* Count the number of lines of text in a file with file ptr fp */
/************************************************************************/
int LineCount(FILE *fp)
{
fpos_t nFilePosition;
char buf[BUFFER_SIZE + 1];
register int bytes_read = 0;
register unsigned long lines = 0;
/* -------------------------------------------------------------------- */
/* Ensure the file pointer is valid before using it.
*/
/* -------------------------------------------------------------------- */
if( fp == (FILE *)NULL )
return( -1 );
/* -------------------------------------------------------------------- */
/* Store the current file position.
*/
/* -------------------------------------------------------------------- */
if( (fgetpos( fp, &nFilePosition )) != 0)
return( -1 );
/* -------------------------------------------------------------------- */
/* Reset the file position indicator to the beginning of the
*/
/* file and clear end-of-file and error indicators.
*/
/* -------------------------------------------------------------------- */
rewind(fp);
/* -------------------------------------------------------------------- */
/* Calculate the number of lines in the file.
*/
/* -------------------------------------------------------------------- */
while ((bytes_read = fread(buf, sizeof(char), BUFFER_SIZE, fp)) > 0)
{
register char *p = buf;
while ( (p = memchr(p, '\n', (buf + bytes_read) - p)) )
{
++p;
++lines;
}
}
/* -------------------------------------------------------------------- */
/* Check for error on reading from file.
*/
/* -------------------------------------------------------------------- */
if (bytes_read < 0)
return( -1 );
/* -------------------------------------------------------------------- */
/* Restore the original file position.
*/
/* -------------------------------------------------------------------- */
if( (fsetpos( fp, &nFilePosition )) != 0)
return( -1 );
return( lines );
}