xref: /onnv-gate/usr/src/lib/libcmd/common/head.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1992-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                                                                      *
204887Schin ***********************************************************************/
214887Schin #pragma prototyped
224887Schin /*
234887Schin  * David Korn
244887Schin  * AT&T Bell Laboratories
254887Schin  *
264887Schin  * output the beginning portion of one or more files
274887Schin  */
284887Schin 
294887Schin static const char usage[] =
308462SApril.Chin@Sun.COM "[-n?\n@(#)$Id: head (AT&T Research) 2006-09-27 $\n]"
314887Schin USAGE_LICENSE
324887Schin "[+NAME?head - output beginning portion of one or more files ]"
334887Schin "[+DESCRIPTION?\bhead\b copies one or more input files to standard "
344887Schin     "output stopping at a designated point for each file or to the end of "
354887Schin     "the file whichever comes first. Copying ends at the point indicated by "
364887Schin     "the options. By default a header of the form \b==> \b\afilename\a\b "
374887Schin     "<==\b is output before all but the first file but this can be changed "
384887Schin     "with the \b-q\b and \b-v\b options.]"
394887Schin "[+?If no \afile\a is given, or if the \afile\a is \b-\b, \bhead\b "
404887Schin     "copies from standard input starting at the current location.]"
414887Schin "[+?The option argument for \b-c\b, and \b-s\b can optionally be "
424887Schin     "followed by one of the following characters to specify a different unit "
434887Schin     "other than a single byte:]"
444887Schin     "{"
454887Schin         "[+b?512 bytes.]"
464887Schin         "[+k?1-killobyte.]"
474887Schin         "[+m?1-megabyte.]"
484887Schin     "}"
494887Schin "[+?For backwards compatibility, \b-\b\anumber\a is equivalent to \b-n\b "
504887Schin     "\anumber\a.]"
514887Schin "[n:lines?Copy \alines\a lines from each file.]#[lines:=10]"
524887Schin "[c:bytes?Copy \achars\a bytes from each file.]#[chars]"
534887Schin "[q:quiet|silent?Never ouput filename headers.]"
544887Schin "[s:skip?Skip \askip\a characters or lines from each file before "
554887Schin     "copying.]#[skip]"
564887Schin "[v:verbose?Always ouput filename headers.]"
574887Schin     "\n\n"
584887Schin "[ file ... ]"
594887Schin     "\n\n"
604887Schin "[+EXIT STATUS?]"
614887Schin     "{"
624887Schin         "[+0?All files copied successfully.]"
634887Schin         "[+>0?One or more files did not copy.]"
644887Schin     "}"
654887Schin "[+SEE ALSO?\bcat\b(1), \btail\b(1)]"
664887Schin ;
674887Schin 
684887Schin #include <cmd.h>
694887Schin 
704887Schin int
b_head(int argc,register char ** argv,void * context)714887Schin b_head(int argc, register char** argv, void* context)
724887Schin {
734887Schin 	static const char	header_fmt[] = "\n==> %s <==\n";
744887Schin 
754887Schin 	register Sfio_t*	fp;
764887Schin 	register char*		cp;
774887Schin 	register off_t		keep = 10;
784887Schin 	register off_t		skip = 0;
794887Schin 	register int		delim = '\n';
804887Schin 	int			header = 1;
814887Schin 	char*			format = (char*)header_fmt+1;
824887Schin 
834887Schin 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
844887Schin 	for (;;)
854887Schin 	{
864887Schin 		switch (optget(argv, usage))
874887Schin 		{
884887Schin 		case 'c':
894887Schin 			delim = -1;
904887Schin 			/*FALLTHROUGH*/
914887Schin 		case 'n':
924887Schin 			if (opt_info.offset && argv[opt_info.index][opt_info.offset] == 'c')
934887Schin 			{
944887Schin 				delim = -1;
954887Schin 				opt_info.offset++;
964887Schin 			}
974887Schin 			if ((keep = opt_info.number) <=0)
984887Schin 				error(2, "%s: %I*d: positive numeric option argument expected", opt_info.name, sizeof(keep), keep);
994887Schin 			continue;
1004887Schin 		case 'q':
1014887Schin 			header = argc;
1024887Schin 			continue;
1034887Schin 		case 'v':
1044887Schin 			header = 0;
1054887Schin 			continue;
1064887Schin 		case 's':
1074887Schin 			skip = opt_info.number;
1084887Schin 			continue;
1094887Schin 		case '?':
1104887Schin 			error(ERROR_usage(2), "%s", opt_info.arg);
1114887Schin 			continue;
1124887Schin 		case ':':
1134887Schin 			error(2, "%s", opt_info.arg);
1144887Schin 			continue;
1154887Schin 		}
1164887Schin 		break;
1174887Schin 	}
1184887Schin 	argv += opt_info.index;
1194887Schin 	argc -= opt_info.index;
1204887Schin 	if (error_info.errors)
1214887Schin 		error(ERROR_usage(2), "%s", optusage(NiL));
1224887Schin 	if (cp = *argv)
1234887Schin 		argv++;
1244887Schin 	do
1254887Schin 	{
1264887Schin 		if (!cp || streq(cp, "-"))
1274887Schin 		{
1284887Schin 			cp = "/dev/stdin";
1294887Schin 			fp = sfstdin;
1304887Schin 			sfset(fp, SF_SHARE, 1);
1314887Schin 		}
1324887Schin 		else if (!(fp = sfopen(NiL, cp, "r")))
1334887Schin 		{
1344887Schin 			error(ERROR_system(0), "%s: cannot open", cp);
1354887Schin 			continue;
1364887Schin 		}
1374887Schin 		if (argc > header)
1384887Schin 			sfprintf(sfstdout, format, cp);
1394887Schin 		format = (char*)header_fmt;
1404887Schin 		if (skip > 0)
1414887Schin 			sfmove(fp, NiL, skip, delim);
1424887Schin 		if (sfmove(fp, sfstdout, keep, delim) < 0 && errno != EPIPE)
1434887Schin 			error(ERROR_system(0), "%s: read error", cp);
1444887Schin 		if (fp != sfstdin)
1454887Schin 			sfclose(fp);
1464887Schin 	} while (cp = *argv++);
1474887Schin 	if (sfsync(sfstdout))
1484887Schin 		error(ERROR_system(0), "write error");
1494887Schin 	return error_info.errors != 0;
1504887Schin }
151