scc

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

armember.c (812B)


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