home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 8 Other
/
08-Other.zip
/
OS2MNX1.ZIP
/
HEAD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-12-27
|
3KB
|
130 lines
/*
* head - print the first few lines of a file Author: Andy
* Tanenbaum
*/
/* $Header: D:/RCS/RCS/head.c 1.1 89/12/27 02:12:35 RCA Exp $
* $Log: head.c $
* Revision 1.1 89/12/27 02:12:35 RCA
* Initial revision
*
*/
/* Someone should put wildcard capability in here */
/*
* change to use putc() instead of prints() -- Dean Long
* 3/7/87
*/
#include "stdio.h"
#define DEFAULT 10
char buff[BUFSIZ];
main(argc, argv)
int argc;
char *argv[];
{
int n, k, nfiles;
char *ptr;
if (argc == 1)
{
printf ("\n █▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█");
printf ("\n █ HEAD (Print first lines of file) $Author: RCA $ █");
printf ("\n █ $Date: 89/12/27 02:12:35 $ $Revision: 1.1 $ █");
printf ("\n █ Usage: HEAD [-n] filename █");
printf ("\n █ Purpose: Will print the first [n] lines of a file. █");
printf ("\n █ Option: -n: Print \"n\" lines of the file. █");
printf ("\n █ OS: Works under OS/2 or DOS. █");
printf ("\n █ Credits: A. Tannenbaum, D. Long █");
printf ("\n █▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█\n");
exit(1);
}
/*
* Check for flag. Only flag is -n, to say how many lines to
* print.
*/
setbuf(stdout, buff);
k = 1;
ptr = argv[1];
n = DEFAULT;
if (*ptr++ == '-')
{
k++;
n = atoi(ptr);
if (n <= 0)
usage();
}
nfiles = argc - k;
if (nfiles == 0)
{
/* Print standard input only. */
do_file(n);
fflush(stdout);
exit(0);
}
/* One or more files have been listed explicitly. */
while (k < argc)
{
fclose(stdin);
/*
* change if (nfiles > 1) prints("==> %s <==\n",
* argv[k]);
*/
if (nfiles > 1)
puts("==> %s <==\n");
if (fopen(argv[k], "r") == NULL)
/*
* change prints("head: cannot open %s\n", argv[k]);
*/
puts("head: cannot open %s\n");
else
{
do_file(n);
fflush(stdout);
}
k++;
/*
* change if (k < argc) prints("\n");
*/
if (k < argc)
puts("\n");
}
exit(0);
}
do_file(n)
int n;
{
int c;
/* Print the first 'n' lines of a file. */
while (n)
switch (c = getc(stdin))
{
case EOF:
return;
case '\n':
--n;
default:
putc((char) c, stdout);
}
}
usage()
{
/*
* change std_err("Usage: head [-n] [file ...]\n");
*/
printf("Usage: head [-n] [file ...]\n");
exit(1);
}