xref: /onnv-gate/usr/src/lib/libcmd/common/head.c (revision 4887)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1992-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                                                                      *
20*4887Schin ***********************************************************************/
21*4887Schin #pragma prototyped
22*4887Schin /*
23*4887Schin  * David Korn
24*4887Schin  * AT&T Bell Laboratories
25*4887Schin  *
26*4887Schin  * output the beginning portion of one or more files
27*4887Schin  */
28*4887Schin 
29*4887Schin static const char usage[] =
30*4887Schin "[-?\n@(#)$Id: head (AT&T Research) 2006-09-27 $\n]"
31*4887Schin USAGE_LICENSE
32*4887Schin "[+NAME?head - output beginning portion of one or more files ]"
33*4887Schin "[+DESCRIPTION?\bhead\b copies one or more input files to standard "
34*4887Schin     "output stopping at a designated point for each file or to the end of "
35*4887Schin     "the file whichever comes first. Copying ends at the point indicated by "
36*4887Schin     "the options. By default a header of the form \b==> \b\afilename\a\b "
37*4887Schin     "<==\b is output before all but the first file but this can be changed "
38*4887Schin     "with the \b-q\b and \b-v\b options.]"
39*4887Schin "[+?If no \afile\a is given, or if the \afile\a is \b-\b, \bhead\b "
40*4887Schin     "copies from standard input starting at the current location.]"
41*4887Schin "[+?The option argument for \b-c\b, and \b-s\b can optionally be "
42*4887Schin     "followed by one of the following characters to specify a different unit "
43*4887Schin     "other than a single byte:]"
44*4887Schin     "{"
45*4887Schin         "[+b?512 bytes.]"
46*4887Schin         "[+k?1-killobyte.]"
47*4887Schin         "[+m?1-megabyte.]"
48*4887Schin     "}"
49*4887Schin "[+?For backwards compatibility, \b-\b\anumber\a is equivalent to \b-n\b "
50*4887Schin     "\anumber\a.]"
51*4887Schin "[n:lines?Copy \alines\a lines from each file.]#[lines:=10]"
52*4887Schin "[c:bytes?Copy \achars\a bytes from each file.]#[chars]"
53*4887Schin "[q:quiet|silent?Never ouput filename headers.]"
54*4887Schin "[s:skip?Skip \askip\a characters or lines from each file before "
55*4887Schin     "copying.]#[skip]"
56*4887Schin "[v:verbose?Always ouput filename headers.]"
57*4887Schin     "\n\n"
58*4887Schin "[ file ... ]"
59*4887Schin     "\n\n"
60*4887Schin "[+EXIT STATUS?]"
61*4887Schin     "{"
62*4887Schin         "[+0?All files copied successfully.]"
63*4887Schin         "[+>0?One or more files did not copy.]"
64*4887Schin     "}"
65*4887Schin "[+SEE ALSO?\bcat\b(1), \btail\b(1)]"
66*4887Schin ;
67*4887Schin 
68*4887Schin #include <cmd.h>
69*4887Schin 
70*4887Schin int
71*4887Schin b_head(int argc, register char** argv, void* context)
72*4887Schin {
73*4887Schin 	static const char	header_fmt[] = "\n==> %s <==\n";
74*4887Schin 
75*4887Schin 	register Sfio_t*	fp;
76*4887Schin 	register char*		cp;
77*4887Schin 	register off_t		keep = 10;
78*4887Schin 	register off_t		skip = 0;
79*4887Schin 	register int		delim = '\n';
80*4887Schin 	int			header = 1;
81*4887Schin 	char*			format = (char*)header_fmt+1;
82*4887Schin 
83*4887Schin 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
84*4887Schin 	for (;;)
85*4887Schin 	{
86*4887Schin 		switch (optget(argv, usage))
87*4887Schin 		{
88*4887Schin 		case 'c':
89*4887Schin 			delim = -1;
90*4887Schin 			/*FALLTHROUGH*/
91*4887Schin 		case 'n':
92*4887Schin 			if (opt_info.offset && argv[opt_info.index][opt_info.offset] == 'c')
93*4887Schin 			{
94*4887Schin 				delim = -1;
95*4887Schin 				opt_info.offset++;
96*4887Schin 			}
97*4887Schin 			if ((keep = opt_info.number) <=0)
98*4887Schin 				error(2, "%s: %I*d: positive numeric option argument expected", opt_info.name, sizeof(keep), keep);
99*4887Schin 			continue;
100*4887Schin 		case 'q':
101*4887Schin 			header = argc;
102*4887Schin 			continue;
103*4887Schin 		case 'v':
104*4887Schin 			header = 0;
105*4887Schin 			continue;
106*4887Schin 		case 's':
107*4887Schin 			skip = opt_info.number;
108*4887Schin 			continue;
109*4887Schin 		case '?':
110*4887Schin 			error(ERROR_usage(2), "%s", opt_info.arg);
111*4887Schin 			continue;
112*4887Schin 		case ':':
113*4887Schin 			error(2, "%s", opt_info.arg);
114*4887Schin 			continue;
115*4887Schin 		}
116*4887Schin 		break;
117*4887Schin 	}
118*4887Schin 	argv += opt_info.index;
119*4887Schin 	argc -= opt_info.index;
120*4887Schin 	if (error_info.errors)
121*4887Schin 		error(ERROR_usage(2), "%s", optusage(NiL));
122*4887Schin 	if (cp = *argv)
123*4887Schin 		argv++;
124*4887Schin 	do
125*4887Schin 	{
126*4887Schin 		if (!cp || streq(cp, "-"))
127*4887Schin 		{
128*4887Schin 			cp = "/dev/stdin";
129*4887Schin 			fp = sfstdin;
130*4887Schin 			sfset(fp, SF_SHARE, 1);
131*4887Schin 		}
132*4887Schin 		else if (!(fp = sfopen(NiL, cp, "r")))
133*4887Schin 		{
134*4887Schin 			error(ERROR_system(0), "%s: cannot open", cp);
135*4887Schin 			continue;
136*4887Schin 		}
137*4887Schin 		if (argc > header)
138*4887Schin 			sfprintf(sfstdout, format, cp);
139*4887Schin 		format = (char*)header_fmt;
140*4887Schin 		if (skip > 0)
141*4887Schin 			sfmove(fp, NiL, skip, delim);
142*4887Schin 		if (sfmove(fp, sfstdout, keep, delim) < 0 && errno != EPIPE)
143*4887Schin 			error(ERROR_system(0), "%s: read error", cp);
144*4887Schin 		if (fp != sfstdin)
145*4887Schin 			sfclose(fp);
146*4887Schin 	} while (cp = *argv++);
147*4887Schin 	if (sfsync(sfstdout))
148*4887Schin 		error(ERROR_system(0), "write error");
149*4887Schin 	return error_info.errors != 0;
150*4887Schin }
151