1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1992-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
20 ***********************************************************************/
24 * AT&T Bell Laboratories
26 * output the beginning portion of one or more files
29 static const char usage
[] =
30 "[-n?\n@(#)$Id: head (AT&T Research) 2006-09-27 $\n]"
32 "[+NAME?head - output beginning portion of one or more files ]"
33 "[+DESCRIPTION?\bhead\b copies one or more input files to standard "
34 "output stopping at a designated point for each file or to the end of "
35 "the file whichever comes first. Copying ends at the point indicated by "
36 "the options. By default a header of the form \b==> \b\afilename\a\b "
37 "<==\b is output before all but the first file but this can be changed "
38 "with the \b-q\b and \b-v\b options.]"
39 "[+?If no \afile\a is given, or if the \afile\a is \b-\b, \bhead\b "
40 "copies from standard input starting at the current location.]"
41 "[+?The option argument for \b-c\b, and \b-s\b can optionally be "
42 "followed by one of the following characters to specify a different unit "
43 "other than a single byte:]"
49 "[+?For backwards compatibility, \b-\b\anumber\a is equivalent to \b-n\b "
51 "[n:lines?Copy \alines\a lines from each file.]#[lines:=10]"
52 "[c:bytes?Copy \achars\a bytes from each file.]#[chars]"
53 "[q:quiet|silent?Never ouput filename headers.]"
54 "[s:skip?Skip \askip\a characters or lines from each file before "
56 "[v:verbose?Always ouput filename headers.]"
62 "[+0?All files copied successfully.]"
63 "[+>0?One or more files did not copy.]"
65 "[+SEE ALSO?\bcat\b(1), \btail\b(1)]"
71 b_head(int argc
, register char** argv
, void* context
)
73 static const char header_fmt
[] = "\n==> %s <==\n";
77 register off_t keep
= 10;
78 register off_t skip
= 0;
79 register int delim
= '\n';
81 char* format
= (char*)header_fmt
+1;
83 cmdinit(argc
, argv
, context
, ERROR_CATALOG
, 0);
86 switch (optget(argv
, usage
))
92 if (opt_info
.offset
&& argv
[opt_info
.index
][opt_info
.offset
] == 'c')
97 if ((keep
= opt_info
.number
) <=0)
98 error(2, "%s: %I*d: positive numeric option argument expected", opt_info
.name
, sizeof(keep
), keep
);
107 skip
= opt_info
.number
;
110 error(ERROR_usage(2), "%s", opt_info
.arg
);
113 error(2, "%s", opt_info
.arg
);
118 argv
+= opt_info
.index
;
119 argc
-= opt_info
.index
;
120 if (error_info
.errors
)
121 error(ERROR_usage(2), "%s", optusage(NiL
));
126 if (!cp
|| streq(cp
, "-"))
130 sfset(fp
, SF_SHARE
, 1);
132 else if (!(fp
= sfopen(NiL
, cp
, "r")))
134 error(ERROR_system(0), "%s: cannot open", cp
);
138 sfprintf(sfstdout
, format
, cp
);
139 format
= (char*)header_fmt
;
141 sfmove(fp
, NiL
, skip
, delim
);
142 if (sfmove(fp
, sfstdout
, keep
, delim
) < 0 && errno
!= EPIPE
)
143 error(ERROR_system(0), "%s: read error", cp
);
146 } while (cp
= *argv
++);
147 if (sfsync(sfstdout
))
148 error(ERROR_system(0), "write error");
149 return error_info
.errors
!= 0;