• Antelope Release 5.9 Mac OS X 10.13.2 2019-05-01

 

NAME

gxline, gxopen, gxpipe, gxpipex, gxseek, gxtell, gxclose - read lines from a file

SYNOPSIS

#include "stock.h"

int gxopen(char *filename)
int gxpipe(char *cmd, char **env, int *cmdin );
int gxpipex(char *cmd, int *cmdin );
int gxclose(int fd)
int gxline(int fd, char *aline, int maxchars)
int gxseek(int fd, int p)
int gxtell(int fd)

DESCRIPTION

This set of routines is useful for reading lines from a file.

gxopen memory maps the file filename and returns a small integer identifier.

gxpipe runs the command cmd (no shell escapes allowed) with the specified environment env, and allowing the caller to write output to the command through the file descriptor cmdin. The first call to gxline causes this file descriptor to be closed, and gxline waits for the command to terminate.

gxpipe requires an explicit path to the command; gxpipex looks for the executable along the environment variable PATH, and fills in the appropriate path; also, it propagates the existing environment, rather than providing a new environment.

gxline returns successive lines from the file, with the terminating linefeed removed. If a line does not fit into the maxchars characters of aline, it is returned in two or more parts.

gxtell returns the current position in the file.

gxseek sets the current position.

gxclose unmaps the file.

EXAMPLE



fd = gxopen(filename) ;
n = 0 ;
if ( fd >= 0 )  {
    printf ( "\n%s\n", filename ) ;
    while ( gxline(fd, aline, sizeof(aline) )) {
        n++ ;
        if ( n == 9 ) {
            p = gxtell(fd) ;
        }
        printf ( "  %3d  %s\n", n, aline ) ;
    }
    gxseek(fd, p) ;
    if ( gxline(fd, aline, sizeof(aline) )) {
        printf ( "line #10:\n  %s\n", aline ) ;
    } else {
        complain ( 0, "gxseek failed" ) ;
    }
    result = gxclose(fd) ;
    if ( result ) {
        complain ( 1, "gxclose failed: %d", result ) ;
    }
} else {
    complain ( 1, "Can't open '%s'", filename ) ;
}

RETURN VALUES

gxopen returns a small positive integer identifier when successful, -1 if the file could not be mapped.

gxline returns 1 when returning a whole line, 2 when returning a line fragment, and 0 at the end of the file.

gxtell returns the current position in the file.

gxseek returns 0 for success, -1 if the new position p is outside the range for the file.

gxclose returns -1 if there is a problem unmapping the file.

LIBRARY

$(STOCKLIBS)

ATTRIBUTES

MT-Safe

SEE ALSO

fgets(3)
mmap(2)

AUTHOR

Daniel Quinlan
Printer icon