Update.
[glibc.git] / sysdeps / unix / nlist.c
blob505de671b49f4b2e0a7e671000b80bbc1aa80d77
1 /* Copyright (C) 1991, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <errno.h>
20 #include <a.out.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
26 /* Search the executable FILE for symbols matching those in NL,
27 which is terminated by an element with a NULL `n_un.n_name' member,
28 and fill in the elements of NL. */
29 int
30 nlist (const char *file, struct nlist *nl)
32 FILE *f;
33 struct exec header;
34 size_t nsymbols;
35 struct nlist *symbols;
36 unsigned long int string_table_size;
37 char *string_table;
38 register size_t i;
40 if (nl == NULL)
42 __set_errno (EINVAL);
43 return -1;
46 f = fopen (file, "r");
47 if (f == NULL)
48 return -1;
50 if (fread ((void *) &header, sizeof (header), 1, f) != 1)
51 goto lose;
53 if (fseek (f, N_SYMOFF (header), SEEK_SET) != 0)
54 goto lose;
56 symbols = (struct nlist *) __alloca (header.a_syms);
57 nsymbols = header.a_syms / sizeof (symbols[0]);
59 if (fread ((void *) symbols, sizeof (symbols[0]), nsymbols, f) != nsymbols)
60 goto lose;
62 if (fread ((void *) &string_table_size, sizeof (string_table_size), 1, f)
63 != 1)
64 goto lose;
65 string_table_size -= sizeof (string_table_size);
67 string_table = (char *) __alloca (string_table_size);
68 if (fread ((void *) string_table, string_table_size, 1, f) != 1)
69 goto lose;
71 for (i = 0; i < nsymbols; ++i)
73 register struct nlist *nlp;
74 for (nlp = nl; nlp->n_un.n_name != NULL; ++nlp)
75 if (!strcmp (nlp->n_un.n_name,
76 &string_table[symbols[i].n_un.n_strx -
77 sizeof (string_table_size)]))
79 char *const name = nlp->n_un.n_name;
80 *nlp = symbols[i];
81 nlp->n_un.n_name = name;
85 (void) fclose (f);
86 return 0;
88 lose:;
89 (void) fclose (f);
90 return -1;