xref: /illumos-gate/usr/src/cmd/line/line.c (revision 486c6fda)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  *	This program reads a single line from the standard input
32  *	and writes it on the standard output. It is probably most useful
33  *	in conjunction with the shell.
34  */
35 
36 #include <limits.h>
37 #include <unistd.h>
38 
39 #define	LSIZE	LINE_MAX		/* POSIX.2 */
40 
41 static char	readc(void);
42 
43 static int EOF;
44 static char nl = '\n';
45 
46 /*ARGSUSED*/
47 int
48 main(int argc, char **argv)
49 {
50 	char c;
51 	char line[LSIZE];
52 	char *linep, *linend;
53 
54 	EOF = 0;
55 	linep = line;
56 	linend = line + LSIZE;
57 
58 	while ((c = readc()) != nl) {
59 		if (linep == linend) {
60 			(void) write(1, line, LSIZE);
61 			linep = line;
62 		}
63 		*linep++ = c;
64 	}
65 
66 	/* LINTED  E_PTRDIFF_T_OVERFLOW */
67 	(void) write(1, line, linep-line);
68 	(void) write(1, &nl, 1);
69 	if (EOF == 1)
70 		return (1);
71 	return (0);
72 }
73 
74 static char
75 readc(void)
76 {
77 	char c;
78 
79 	if (read(0, &c, 1) != 1) {
80 		EOF = 1;
81 		return (nl);
82 	}
83 	else
84 		return (c);
85 }
86