* Copyright (c) 1989 The Regents of the University of California.
* %sccs.include.redist.c%
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)nlist.c 5.10 (Berkeley) %G%";
#endif /* LIBC_SCCS and not lint */
fd
= open(name
, O_RDONLY
, 0);
#define ISLAST(p) (p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
register struct nlist
*list
;
register struct nlist
*p
, *s
;
register off_t stroff
, symoff
;
register int nent
, strsize
, cc
;
if (lseek(fd
, (off_t
)0, SEEK_SET
) == -1 ||
read(fd
, &exec
, sizeof(exec
)) != sizeof(exec
) ||
N_BADMAG(exec
) || fstat(fd
, &st
) < 0)
stroff
= symoff
+ symsize
;
strsize
= st
.st_size
- stroff
;
* Map string table into our address space. This gives us
* an easy way to randomly access all the strings, without
* making the memory allocation permanent as with malloc/free
* (i.e., munmap will return it to the system).
strtab
= mmap(0, strsize
, PROT_READ
, MAP_FILE
, fd
, stroff
);
if (strtab
== (char *)-1)
* clean out any left-over information for all valid entries.
* Type and value defined to be 0 if not found; historical
* versions cleared other and desc as well. Also figure out
* the largest string length so don't read any more of the
* string table than we have to.
* XXX clearing anything other than n_type and n_value violates
* the semantics given in the man page.
for (p
= list
; !ISLAST(p
); ++p
) {
if (lseek(fd
, symoff
, SEEK_SET
) == -1)
cc
= MIN(symsize
, sizeof(nbuf
));
if (read(fd
, nbuf
, cc
) != cc
)
for (s
= nbuf
; cc
> 0; ++s
, cc
-= sizeof(*s
)) {
register int soff
= s
->n_un
.n_strx
;
if (soff
== 0 || (s
->n_type
& N_STAB
) != 0)
for (p
= list
; !ISLAST(p
); p
++)
if (!strcmp(&strtab
[soff
], p
->n_un
.n_name
)) {