coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2020-06-26 15:37:26
Commit998882e59a3dbe5c7c8512ffc95c194f860c9a9b
Parent7291d8604e1896b13deddfd68b3c135c2bb3f4f1

head: Add POSIX head

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
A man/head.1 | 42 ++++++++++++++++++++++++++++++++++++++++++
A src/head.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4 files changed, 174 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7d7f951..6845312 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,6 +18,7 @@ ADD_EXECUTABLE(dirname  ${CMAKE_SOURCE_DIR}/src/dirname.c)
 ADD_EXECUTABLE(echo     ${CMAKE_SOURCE_DIR}/src/echo.c)
 ADD_EXECUTABLE(env      ${CMAKE_SOURCE_DIR}/src/env.c)
 ADD_EXECUTABLE(false    ${CMAKE_SOURCE_DIR}/src/false.c)
+ADD_EXECUTABLE(head     ${CMAKE_SOURCE_DIR}/src/head.c)
 ADD_EXECUTABLE(link     ${CMAKE_SOURCE_DIR}/src/link.c)
 ADD_EXECUTABLE(logname  ${CMAKE_SOURCE_DIR}/src/logname.c)
 ADD_EXECUTABLE(mkdir    ${CMAKE_SOURCE_DIR}/src/mkdir.c)
diff --git a/README.md b/README.md
index 461ea25..0976edf 100644
--- a/README.md
+++ b/README.md
@@ -17,6 +17,7 @@ UNIX-like systems.
 | echo             | Write arguments to standard output       | POSIX    |
 | env              | Execute with an altered enviroment       | POSIX    |
 | false            | Return false value                       | POSIX    |
+| head             | Output the first part of files           | POSIX    |
 | link             | Create a link to a file                  | POSIX    |
 | logname          | Return the user's login name             | POSIX    |
 | mkdir            | Make directories                         | POSIX    |
diff --git a/man/head.1 b/man/head.1
new file mode 100644
index 0000000..f432772
--- /dev/null
+++ b/man/head.1
@@ -0,0 +1,42 @@
+.TH HEAD 1 2020-06-27 "OMKOV coreutils" "General Commands Manual"
+.SH NAME
+head \(em output the first part of files
+.SH SYNOPSYS
+\fBhead\fR [-n \fInumber\fR] [\fIfile\fR...]
+.SH DESCRIPTION
+Output the first \fInumber\fR of lines from each \fIfile\fR to standard output.
+If no \fInumber\fR is specified, 10 lines will be output.
+.SH OPTIONS
+The following options are supported:
+.TP
+.B -n \fInumber\fR
+Number of lines to output for each \fIfile\fR.
+.TP
+.B --help
+Display help information.
+.TP
+.B --version
+Display version information.
+.SH OPERANDS
+The following operand is supported:
+.TP
+.I file
+A pathname of an input file. If no \fIfile\fR operands are specified, or
+\fIfile\fR is a '\fB-\fR', \fIhead\fR will read from standard input.
+.SH EXIT STATUS
+The following exit values will be returned:
+.TP
+\ 0
+All files were processed successfully.
+.TP
+>0
+An error occurred.
+.SH STANDARDS
+The \fIhead\fR utility is compliant with the IEEE Std 1003.2-1992 ("POSIX.2")
+specification.
+.SH COPYRIGHT
+.nf
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+OMKOV Permissive Licence (https://www.omkov.net/OLPE)
+.fi
diff --git a/src/head.c b/src/head.c
new file mode 100644
index 0000000..7341a71
--- /dev/null
+++ b/src/head.c
@@ -0,0 +1,130 @@
+// head.c, version 1.0.1
+// OMKOV coreutils implementation of POSIX head
+// Copyright (C) 2020, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
+#include "error.h"
+#include "optget.h"
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+static char stdbuf[BUFSIZ * 16];
+
+static uintmax_t limit = 10;
+static bool      label;
+
+static inline int head(const char *path);
+
+static void help(void);
+static void version(void);
+
+int main(int argc, char *argv[]) {
+	lop_t lops[] = {
+		{ "help",    ARG_NUL, 256 },
+		{ "version", ARG_NUL, 257 },
+		{ NULL, 0, 0 }
+	};
+	
+	opt_t opt = OPTGET_INIT; opt.str = "n:"; opt.lops = lops; int o;
+	while ((o = optget(&opt, argv, 1)) != -1) switch (o) {
+	case 'n': {
+		register uintmax_t d; register char *p = opt.arg;
+		for (limit = 0; *p >= '0' && *p <= '9'; ++p) {
+			d = (uintmax_t)*p - '0';
+			if (limit > (UINTMAX_MAX - d) / 10) { break; }
+			limit = limit * 10 + d;
+		}
+		
+		if (*p) { error(1, "%s: %s: invalid line count", argv[0], opt.arg); }
+		break;
+	}
+	case 256: { help(); return 0; }
+	case 257: { version(); return 0; }
+	default: { return 1; }
+	}
+	
+	bool warned = false;
+	
+	if (opt.ind == argc) { head("-"); return 0; }
+	if (opt.ind + 1 != argc) { label = true; }
+	for (char **p = &argv[opt.ind]; *p; ++p) if (head(*p)) {
+		warn("%s: %s: %s", argv[0], *p, serrno); warned = true;
+	}
+	
+	return warned;
+}
+
+static inline int head(const char *file) {
+	FILE *fi; register uintmax_t l = 0;
+	
+	if (file[0] == '-' && file[1] == 0) { fi = stdin; }
+	else if (!(fi = fopen(file, "r"))) { return 1; }
+	
+	if (label) { static bool spc = false;
+		if (spc) { fputc('\n', stdout); } else { spc = true; }
+		printf("==> %s <==\n", file);
+	}
+	
+	if (limit == 0) { goto end; }
+	for (size_t c; (c = fread(stdbuf, 1, sizeof (stdbuf), fi));) {
+		for (register size_t i = 0; i != c; ++i) {
+			fputc(stdbuf[i], stdout);
+			if (stdbuf[i] == '\n' && ++l == limit) { goto end; }
+		}
+	}
+	
+end:
+	if (fi != stdin) { fclose(fi); } return 0;
+}
+
+static void help(void) {
+	puts("head - output the first part of files\n");
+	puts("usage: head [-n number] [file...]\n");
+	puts("options:");
+	puts("  -n number  Number of lines to output for each file");
+	puts("  --help     Display help information");
+	puts("  --version  Display version information");
+	return;
+}
+
+static void version(void) {
+	puts("OMKOV coreutils head, version 1.0.1");
+	puts("Copyright (C) 2020, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}