Tuesday, April 17, 2007

Well since Windows lacks fgetln() but as far as I can tell the BSD and GNU C Libraries have it.

/* The origenal, basically the same as one of the functions in FreeBSDs head(1) implementation.

static void
read_top( FILE *fp, int lncnt ) {

        char *cp;
        size_t error, rlen;
        while ( lncnt && (cp = fgetln( fp, &rlen )) != NULL ) {
                error = fwrite( cp, sizeof(char), rlen, stdout );
                if ( error != rlen )
                        err( 1, "stdout" );
                lncnt--;
        }
}

/* A new version of read_top that *should* be a bit more standard compilent and simple to read */

static void
read_top( FILE *fp, int lncnt ) {

        int c = 0;
        int f = 0;

        while ( (c < lncnt) && (f != EOF ) ) {
                f = fgetc( fp );
                printf( "%c", f );
                if ( (f == '\n') ) {
                        c++;
                }
        }
}

The biggest problem with rf.c is the use of err()/errx(), a nice pair of Macros that expand to a fairly compatible meaning would be nice for portability. *cough* Windows *cough* and I don't even want to know how groff works on WIN32 if at all....

No comments:

Post a Comment