xref: /aosp_15_r20/external/ltp/testcases/kernel/sched/clisrv/readline.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2001
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
5*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
6*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
7*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
10*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
16*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker /*****************************************************************************
20*49cdfc7eSAndroid Build Coastguard Worker  *  readline.c
21*49cdfc7eSAndroid Build Coastguard Worker  ******************************************************************************/
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker /* Read line from a descriptor, byte at a time, return number of
26*49cdfc7eSAndroid Build Coastguard Worker    characters up to the null */
27*49cdfc7eSAndroid Build Coastguard Worker 
readline(int fd,char * ptr,int maxlen)28*49cdfc7eSAndroid Build Coastguard Worker int readline(int fd, char *ptr, int maxlen)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	int n, rc;
31*49cdfc7eSAndroid Build Coastguard Worker 	char c;
32*49cdfc7eSAndroid Build Coastguard Worker 	/*
33*49cdfc7eSAndroid Build Coastguard Worker 	   printf("readline: fd = %d\n", fd);
34*49cdfc7eSAndroid Build Coastguard Worker 	 */
35*49cdfc7eSAndroid Build Coastguard Worker 	for (n = 1; n < maxlen; n++) {
36*49cdfc7eSAndroid Build Coastguard Worker 		if ((rc = read(fd, &c, 1)) == 1) {
37*49cdfc7eSAndroid Build Coastguard Worker 			*ptr++ = c;
38*49cdfc7eSAndroid Build Coastguard Worker 			if (c == '\n')
39*49cdfc7eSAndroid Build Coastguard Worker 				break;
40*49cdfc7eSAndroid Build Coastguard Worker 		} else if (rc == 0) {
41*49cdfc7eSAndroid Build Coastguard Worker 			if (n == 1)
42*49cdfc7eSAndroid Build Coastguard Worker 				return 0;	/* EOF no data read */
43*49cdfc7eSAndroid Build Coastguard Worker 			else
44*49cdfc7eSAndroid Build Coastguard Worker 				break;	/* EOF, some data read */
45*49cdfc7eSAndroid Build Coastguard Worker 		} else
46*49cdfc7eSAndroid Build Coastguard Worker 			return (-1);	/* error */
47*49cdfc7eSAndroid Build Coastguard Worker 	}
48*49cdfc7eSAndroid Build Coastguard Worker 	*ptr = 0;
49*49cdfc7eSAndroid Build Coastguard Worker 	return (n);
50*49cdfc7eSAndroid Build Coastguard Worker }
51