2005-04-21 16:53:53 +02:00
|
|
|
/* head - print the first few lines of a file Author: Andy Tanenbaum */
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define DEFAULT 10
|
|
|
|
|
2012-03-24 16:16:34 +01:00
|
|
|
int main(int argc, char **argv);
|
|
|
|
void do_file(int n, FILE *f);
|
|
|
|
void usage(void);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
int main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
FILE *f;
|
2009-10-11 00:36:46 +02:00
|
|
|
int legacy, n, k, nfiles;
|
2005-04-21 16:53:53 +02:00
|
|
|
char *ptr;
|
|
|
|
|
2009-10-11 00:36:46 +02:00
|
|
|
/* Check for flags. One can only specify how many lines to print. */
|
2005-04-21 16:53:53 +02:00
|
|
|
k = 1;
|
|
|
|
n = DEFAULT;
|
2009-10-11 00:36:46 +02:00
|
|
|
legacy = 0;
|
|
|
|
for (k = 1; k < argc && argv[k][0] == '-'; k++) {
|
|
|
|
ptr = &argv[k][1];
|
|
|
|
if (ptr[0] == 'n' && ptr[1] == 0) {
|
|
|
|
k++;
|
|
|
|
if (k >= argc) usage();
|
|
|
|
ptr = argv[k];
|
|
|
|
}
|
|
|
|
else if (ptr[0] == '-' && ptr[1] == 0) {
|
|
|
|
k++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (++legacy > 1) usage();
|
2005-04-21 16:53:53 +02:00
|
|
|
n = atoi(ptr);
|
|
|
|
if (n <= 0) usage();
|
|
|
|
}
|
|
|
|
nfiles = argc - k;
|
|
|
|
|
|
|
|
if (nfiles == 0) {
|
|
|
|
/* Print standard input only. */
|
|
|
|
do_file(n, stdin);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One or more files have been listed explicitly. */
|
|
|
|
while (k < argc) {
|
|
|
|
if (nfiles > 1) printf("==> %s <==\n", argv[k]);
|
|
|
|
if ((f = fopen(argv[k], "r")) == NULL)
|
|
|
|
fprintf(stderr, "%s: cannot open %s: %s\n",
|
|
|
|
argv[0], argv[k], strerror(errno));
|
|
|
|
else {
|
|
|
|
do_file(n, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
k++;
|
|
|
|
if (k < argc) printf("\n");
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void do_file(n, f)
|
|
|
|
int n;
|
|
|
|
FILE *f;
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
/* Print the first 'n' lines of a file. */
|
|
|
|
while (n) switch (c = getc(f)) {
|
|
|
|
case EOF:
|
|
|
|
return;
|
|
|
|
case '\n':
|
|
|
|
--n;
|
|
|
|
default: putc((char) c, stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void usage()
|
|
|
|
{
|
2009-10-11 00:36:46 +02:00
|
|
|
fprintf(stderr, "Usage: head [-lines | -n lines] [file ...]\n");
|
2005-04-21 16:53:53 +02:00
|
|
|
exit(1);
|
|
|
|
}
|