scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 564b8fa720db69f222aa8eb2dec7e0b4c95696da
parent 36ae36d877ddd8e3ecd6ec6934747d0ff205e3d7
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 29 Sep 2017 14:43:38 +0100

[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.

Diffstat:
ar/Makefile | 8++++++++
ar/ar.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 77 insertions(+), 0 deletions(-)

diff --git a/ar/Makefile b/ar/Makefile @@ -0,0 +1,8 @@ + +all: ar + +dep: +clean: + rm -f ar + +distclean: clean diff --git a/ar/ar.c b/ar/ar.c @@ -0,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; +}