shithub: scc

Download patch

ref: 7ce12b649649aa1f31cb4fb5007284a4128f29a6
parent: cc37574232c7a540c101aa91bd3ecdf89cb90627
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Feb 9 04:25:45 EST 2018

[ar] Begin to write the proper tool

nm has to be temporaly patched because it was using ar.h and the
library functions to cover with ar files.

--- a/ar/main.c
+++ b/ar/main.c
@@ -7,52 +7,91 @@
 
 #include <stat.h>
 
-#include "../inc/scc.h"
 #include "../inc/ar.h"
+#include "../inc/arg.h"
+#include "../inc/scc.h"
 
+char *argv0;
+
+int dflag, rflag, qflag, tflag, pflag, mflag, bflag,
+    iflag, xflag, vflag, cflag, lflag, uflag, aflag;
+    
+char *posname;
+
+static void
+usage(void)
+{
+	fputs("ar [-drqtpmx][posname] [-vuaibcl] [posname] afile name ...\n", stderr);
+	exit(1);
+}
+
 int
 main(int argc, char *argv[])
 {
-	int c;
-	size_t n;
-	FILE *fp, *arfile;
-	char *fname, *arname = "lib.a";
-	struct stat st;
-	struct arhdr hdr;
+	int key = 0, pos = 0;
 
-	if ((arfile = fopen(arname, "wb")) == NULL) {
-		perror("ar:error opening library file");
-		exit(1);
-	}
+	ARGBEGIN {
+	case 'd':
+		dflag = 1;
+		key++;
+		break;
+	case 'r':
+		rflag = 1;
+		key++;
+		break;
+	case 'q':
+		qflag = 1;
+		key++;
+		break;
+	case 't':
+		tflag = 1;
+		key++;
+		break;
+	case 'p':
+		pflag = 1;
+		key++;
+		break;
+	case 'm':
+		mflag = 1;
+		key++;
+		break;
+	case 'x':
+		xflag = 1;
+		key++;
+		break;
+	case 'a':
+		aflag = 1;
+		pos++;
+		posname = EARGF(usage());
+		break;
+	case 'b':
+		bflag = 1;
+		pos++;
+		posname = EARGF(usage());
+		break;
+	case 'i':
+		iflag = 1;
+		pos++;
+		posname = EARGF(usage());
+		break;
+	case 'v':
+		vflag = 1;
+		break;
+	case 'c':
+		cflag = 1;
+		break;
+	case 'l':
+		lflag = 1;
+		break;
+	case 'u':
+		uflag = 1;
+		break;
+	default:
+		usage();
+	} ARGEND
 
-	fputs(ARMAGIC, arfile);
-	while ((fname = *++argv) != NULL) {
-		if ((n = strlen(fname)) > ARNAME_SIZ)
-			die("ar: %s: too long file name", fname);
-		if (stat(fname, &st) < 0)
-			goto member_error;
+	if (key == 0 || key > 1 || pos > 1)
+		usage();
 
-		strcpy(hdr.name, fname);
-		hdr.time = st.st_atime;
-		hdr.uid = st.st_uid;
-		hdr.gid = st.st_gid;
-		hdr.mode = st.st_mode;
-		hdr.size = st.st_mode;
-
-		if (wrarhdr(arfile, &hdr) < 0)
-			goto member_error;
-		if (wrarfile(arfile, &hdr) < 0)
-			goto member_error;
-	}
-
-	if (fclose(arfile)) {
-		fprintf(stderr,
-		        "ar:error writing to output file '%s':%s\n",
-		        arname, strerror(errno));
-	}
-
 	return 0;
-
-member_error:
-	die("ar: %s: %s", fname, strerror(errno));
 }
--- a/inc/ar.h
+++ b/inc/ar.h
@@ -1,21 +1,14 @@
 
-#define ARMAGIC       "!<arch>\n"
-#define ARMAGIC_SIZ   8
+#define ARMAG      "!<arch>\n"     /* ar "magic number" */
+#define SARMAG     8               /* strlen(ARMAG); */
+#define ARFMAG     "`\n"
 
-struct arhdr {
-	char name[17];
-	unsigned long long time;
-	int uid;
-	int gid;
-	int mode;
-	unsigned long long size;
+struct ar_hdr {
+        char ar_name[16];               /* name */
+        char ar_date[12];               /* modification time */
+        char ar_uid[6];                 /* user id */
+        char ar_gid[6];                 /* group id */
+        char ar_mode[8];                /* octal file permissions */
+        char ar_size[10];               /* size in bytes */
+        char ar_fmag[2];                /* consistency check */
 };
-
-#define ARHDR_SIZ      60
-#define ARNAME_SIZ     16
-#define ARMAGIC        "!<arch>\n"
-#define ARMAGIC_SIZ    8
-
-extern int wrarhdr(FILE *fp, struct arhdr *hdr);
-extern int wrarfile(FILE *fp, struct arhdr *hdr);
-extern int rdarhdr(FILE *fp, struct arhdr *hdr);
--- a/nm/main.c
+++ b/nm/main.c
@@ -38,6 +38,7 @@
 	return 0;
 }
 
+#if 0
 static int
 archive(char *fname, FILE *fp)
 {
@@ -52,6 +53,7 @@
 		return 1;
 	return 0;
 }
+#endif
 
 static int
 cmp(const void *p1, const void *p2)
@@ -197,6 +199,7 @@
 	goto free_arrays;
 }
 
+#if 0
 static void
 ar(char *fname, FILE *fp)
 {
@@ -226,6 +229,7 @@
 		fseek(fp, pos, SEEK_SET);
 	}
 }
+#endif
 
 void
 doit(char *fname)
@@ -238,8 +242,8 @@
 
 	if (object(fname, fp))
 		nm(fname, fname, fp);
-	else if (archive(fname, fp))
-		ar(fname, fp);
+//	else if (archive(fname, fp))
+//		ar(fname, fp);
 	else
 		fprintf(stderr, "nm: %s: File format not recognized\n", fname);