2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <sys/types.h>
30 #include <stuff/symbol_list.h>
31 #include <stuff/allocate.h>
32 #include <stuff/errors.h>
34 static int cmp_qsort_name(
35 const struct symbol_list
*sym1
,
36 const struct symbol_list
*sym2
);
39 * This is called to setup a symbol list from a file. It reads the file with
40 * the strings in it and places them in an array of symbol_list structures and
41 * then sorts them by name.
43 * The file that contains the symbol names must have symbol names one per line,
44 * leading and trailing white space is removed and lines starting with a '#'
45 * and lines with only white space are ignored.
51 struct symbol_list
**list
,
55 uint32_t i
, j
, len
, strings_size
;
57 char *strings
, *p
, *line
;
59 if((fd
= open(file
, O_RDONLY
)) < 0){
60 system_error("can't open: %s", file
);
63 if(fstat(fd
, &stat_buf
) == -1){
64 system_error("can't stat: %s", file
);
68 strings_size
= stat_buf
.st_size
;
69 strings
= (char *)allocate(strings_size
+ 2);
70 strings
[strings_size
] = '\n';
71 strings
[strings_size
+ 1] = '\0';
72 if(read(fd
, strings
, strings_size
) != (int)strings_size
){
73 system_error("can't read: %s", file
);
78 * Change the newlines to '\0' and count the number of lines with
79 * symbol names. Lines starting with '#' are comments and lines
80 * contain all space characters do not contain symbol names.
84 for(i
= 0; i
< strings_size
+ 1; i
++){
85 if(*p
== '\n' || *p
== '\r'){
88 while(*line
!= '\0' && isspace(*line
))
100 *list
= (struct symbol_list
*)
101 allocate((*size
) * sizeof(struct symbol_list
));
104 * Place the strings in the list trimming leading and trailing spaces
105 * from the lines with symbol names.
109 for(i
= 0; i
< (*size
); ){
111 if(*line
!= '#' && *line
!= '\0'){
112 while(*line
!= '\0' && isspace(*line
))
115 (*list
)[i
].name
= line
;
116 (*list
)[i
].seen
= FALSE
;
120 while(j
> 0 && isspace(line
[j
])){
123 if(j
> 0 && j
+ 1 < len
&& isspace(line
[j
+1]))
130 qsort(*list
, *size
, sizeof(struct symbol_list
),
131 (int (*)(const void *, const void *))cmp_qsort_name
);
133 /* remove duplicates on the list */
134 for(i
= 0; i
< (*size
); i
++){
136 if(strcmp((*list
)[i
].name
, (*list
)[i
+1].name
) == 0){
137 for(j
= 1; j
< ((*size
) - i
- 1); j
++){
138 (*list
)[i
+ j
].name
= (*list
)[i
+ j
+ 1].name
;
142 * Since there may be more than two of the same name
143 * check this one again against the next one in the
144 * list before moving on.
152 printf("symbol list:\n");
153 for(i
= 0; i
< (*size
); i
++){
154 printf("0x%x name = %s\n", &((*list
)[i
]),(*list
)[i
].name
);
160 * Function for qsort for comparing symbol list names.
165 const struct symbol_list
*sym1
,
166 const struct symbol_list
*sym2
)
168 return(strcmp(sym1
->name
, sym2
->name
));
172 * Function for bsearch for finding a symbol name.
178 const struct symbol_list
*sym
)
180 return(strcmp(name
, sym
->name
));
182 #endif /* !defined(RLD) */