1#include <errno.h>2#include <stdio.h>3#include <stdlib.h>4#include <string.h>56#include <scc/ar.h>7#include <scc/mach.h>89static char *10getfname(struct ar_hdr *hdr, char *dst)11{12 char *p;13 int i;1415 memcpy(dst, hdr->ar_name, SARNAM);16 dst[SARNAM] = '\0';1718 for (i = SARNAM-1; i > 0; i--) {19 if (dst[i] != ' ' && dst[i] != '/')20 break;21 dst[i] = '\0';22 }23 return dst;24}2526long27armember(FILE *fp, char *member, struct ar_hdr *phdr)28{29 struct ar_hdr hdr;30 long siz;3132 if (fread(&hdr, sizeof(hdr), 1, fp) != 1)33 return (feof(fp)) ? 0 : -1;3435 if (strncmp(hdr.ar_fmag, ARFMAG, sizeof(hdr.ar_fmag))) {36 errno = ERANGE;37 return -1;38 }3940 siz = strtol(hdr.ar_size, NULL, 0);41 if (siz & 1)42 siz++;43 if (siz == 0) {44 errno = ERANGE;45 return -1;46 }4748 getfname(&hdr, member);4950 if (phdr)51 *phdr = hdr;5253 return sizeof(hdr) + siz;54}