File: file_read.c

package info (click to toggle)
cti-ifhp 2.1.8-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 932 kB
  • ctags: 845
  • sloc: ansic: 6,036; sh: 1,524; makefile: 318
file content (43 lines) | stat: -rw-r--r-- 969 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**************************************************************************
 * LPRng IFHP Filter
 * Copyright 1994-1997 Patrick Powell, San Diego, CA <papowell@sdsu.edu>
 *
 * Based on the CTI printer filters.
 *  See COPYRIGHT for details.
 *
 * $Id: file_read.c,v 2.1 1997/01/05 18:41:39 papowell Exp $
 */

#include "portable.h"
#include "common.h"
#include "hp4.h"

/*
 * Read a line from a descriptor.  Read the line one byte at a time,
 * looking for a newline.  We store the newline in the buffer,
 * then follow it with a null (the same as fgets(3)).
 * We return the number of characters up to, but not including,
 * the null (the same as strlen(3)).
 */

int
file_readline(fd, ptr, maxlen)
	int    fd;
	char   *ptr;
	int    maxlen;
{
    int  n, rc = 0;

    for( n = 0; n < maxlen && (rc = read(fd,&ptr[n],1)) > 0; n++ ){
		if( ptr[n] == '\n' || ptr[n] == '\r' ){
			n++;
			break;
		}
	}
	ptr[n] = 0;
	if( n == 0 && rc < 0 ){
		n = rc;
	}
    return(n);
}