shithub: scc

Download patch

ref: b8e7849bb565bd5195f8232318c14a6d8fb3b599
parent: 4b78a1bebc78fa724444f23265e7c2d90e098024
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Sep 29 10:43:38 EDT 2017

[ar] Write a draft of an ar tool

This ar only writes an ar file, without checking anything,
without supporting anything and it is not portable because
it uses stat.

diff: cannot open b/ar//null: file does not exist: 'b/ar//null'
--- /dev/null
+++ b/ar/Makefile
@@ -1,0 +1,8 @@
+
+all: ar
+
+dep:
+clean:
+	rm -f ar
+
+distclean: clean
--- /dev/null
+++ b/ar/ar.c
@@ -1,0 +1,69 @@
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int
+main(int argc, char *argv[])
+{
+	int c;
+	size_t n;
+	FILE *fp, *arfile;
+	char *fname, *arname = "lib.a";
+	struct stat st;
+
+	if ((arfile = fopen(arname, "wb")) == NULL) {
+		perror("ar:error opening library file");
+		exit(1);
+	}
+
+	fputs("!<arch>\n", arfile);
+	while ((fname = *++argv) != NULL) {
+		if ((n = strlen(fname)) > 16) {
+			fprintf(stderr, "ar:too long file name '%s'\n", fname);
+			exit(3);
+		}
+		if (stat(fname, &st) < 0) {
+			fprintf(stderr,
+			        "ar:error opening object file '%s':%s\n",
+			        fname, strerror(errno));
+			exit(2);
+		}
+		fprintf(arfile,
+		        "%-16s%-12llu%-6u%-6u%-8o%-10llu`\n",
+		        fname,
+		        (unsigned long long) st.st_atime,
+		        (int) st.st_uid, (int) st.st_gid,
+		        (int) st.st_mode,
+		        (unsigned long long) st.st_size);
+		if ((fp = fopen(fname, "rb")) == NULL) {
+			fprintf(stderr,
+			        "ar: error opening file '%s':%s\n",
+			        fname, strerror(errno));
+			exit(3);
+		}
+		while ((c = getc(fp)) != EOF)
+			putc(c, arfile);
+		if (st.st_size & 1)
+			putc('\n', arfile);
+		if (fclose(fp)) {
+			fprintf(stderr,
+			        "ar:error reading from input file '%s':%s\n",
+			        fname, strerror(errno));
+			exit(4);
+		}
+	}
+
+	if (fclose(arfile)) {
+		fprintf(stderr,
+		        "ar:error writing to output file '%s':%s\n",
+		        arname, strerror(errno));
+	}
+
+	return 0;
+}