scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

armember.c (877B)


      1 #include <errno.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 #include <scc/ar.h>
      7 #include <scc/mach.h>
      8 
      9 static char *
     10 getfname(struct ar_hdr *hdr, char *dst)
     11 {
     12         char *p;
     13         int i;
     14 
     15         memcpy(dst, hdr->ar_name, SARNAM);
     16         dst[SARNAM] = '\0';
     17 
     18         for (i = SARNAM-1; i > 0; i--) {
     19                 if (dst[i] != ' ' && dst[i] != '/')
     20                         break;
     21                 dst[i] = '\0';
     22         }
     23         return dst;
     24 }
     25 
     26 long
     27 armember(FILE *fp, char *member)
     28 {
     29 	struct ar_hdr hdr;
     30 	long siz;
     31 
     32 	if (fread(&hdr, sizeof(hdr), 1, fp) != 1)
     33 		return (feof(fp)) ? 0 : -1;
     34 
     35 	if (strncmp(hdr.ar_fmag, ARFMAG, sizeof(hdr.ar_fmag))) {
     36 		errno = ERANGE;
     37 		return -1;
     38 	}
     39 
     40 	siz = strtol(hdr.ar_size, NULL, 0);
     41 	if (siz & 1)
     42 		siz++;
     43 	if (siz == 0) {
     44 		errno = ERANGE;
     45 		return -1;
     46 	}
     47 
     48 	getfname(&hdr, member);
     49 
     50 	return sizeof(hdr) + siz;
     51 }