scc

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

main.c (1110B)


      1 static char sccsid[] = "@(#) ./ar/main.c";
      2 
      3 #include <errno.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <string.h>
      7 
      8 #include <stat.h>
      9 
     10 #include "../inc/scc.h"
     11 #include "../inc/ar.h"
     12 
     13 int
     14 main(int argc, char *argv[])
     15 {
     16 	int c;
     17 	size_t n;
     18 	FILE *fp, *arfile;
     19 	char *fname, *arname = "lib.a";
     20 	struct stat st;
     21 	struct arhdr hdr;
     22 
     23 	if ((arfile = fopen(arname, "wb")) == NULL) {
     24 		perror("ar:error opening library file");
     25 		exit(1);
     26 	}
     27 
     28 	fputs(ARMAGIC, arfile);
     29 	while ((fname = *++argv) != NULL) {
     30 		if ((n = strlen(fname)) > ARNAME_SIZ)
     31 			die("ar: %s: too long file name", fname);
     32 		if (stat(fname, &st) < 0)
     33 			goto member_error;
     34 
     35 		strcpy(hdr.name, fname);
     36 		hdr.time = st.st_atime;
     37 		hdr.uid = st.st_uid;
     38 		hdr.gid = st.st_gid;
     39 		hdr.mode = st.st_mode;
     40 		hdr.size = st.st_mode;
     41 
     42 		if (wrarhdr(arfile, &hdr) < 0)
     43 			goto member_error;
     44 		if (wrarfile(arfile, &hdr) < 0)
     45 			goto member_error;
     46 	}
     47 
     48 	if (fclose(arfile)) {
     49 		fprintf(stderr,
     50 		        "ar:error writing to output file '%s':%s\n",
     51 		        arname, strerror(errno));
     52 	}
     53 
     54 	return 0;
     55 
     56 member_error:
     57 	die("ar: %s: %s", fname, strerror(errno));
     58 }