xref: /titanic_52/usr/src/tools/ctf/cvt/util.c (revision 10db1377dafab8ba3feedef26db9c5d8539a5cd1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Utility functions
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <libelf.h>
37 #include <gelf.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 #include <pthread.h>
41 #include <unistd.h>
42 #include <sys/param.h>
43 
44 #include "ctftools.h"
45 #include "memory.h"
46 
47 static void (*terminate_cleanup)() = NULL;
48 
49 /* returns 1 if s1 == s2, 0 otherwise */
50 int
51 streq(char *s1, char *s2)
52 {
53 	if (s1 == NULL) {
54 		if (s2 != NULL)
55 			return (0);
56 	} else if (s2 == NULL)
57 		return (0);
58 	else if (strcmp(s1, s2) != 0)
59 		return (0);
60 
61 	return (1);
62 }
63 
64 int
65 findelfsecidx(Elf *elf, char *tofind)
66 {
67 	Elf_Scn *scn = NULL;
68 	GElf_Ehdr ehdr;
69 	GElf_Shdr shdr;
70 
71 	if (gelf_getehdr(elf, &ehdr) == NULL) {
72 		terminate("gelf_getehdr: %s\n", elf_errmsg(elf_errno()));
73 	}
74 
75 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
76 		char *name;
77 
78 		if (gelf_getshdr(scn, &shdr) == NULL ||
79 		    (name = elf_strptr(elf, ehdr.e_shstrndx,
80 		    (size_t)shdr.sh_name)) == NULL) {
81 			terminate("gelf_getehdr: %s\n",
82 			    elf_errmsg(elf_errno()));
83 		}
84 
85 		if (strcmp(name, tofind) == 0)
86 			return (elf_ndxscn(scn));
87 	}
88 
89 	return (-1);
90 }
91 
92 /*PRINTFLIKE2*/
93 static void
94 whine(char *type, char *format, va_list ap)
95 {
96 	int error = errno;
97 
98 	fprintf(stderr, "%s: %s: ", type, progname);
99 	vfprintf(stderr, format, ap);
100 
101 	if (format[strlen(format) - 1] != '\n')
102 		fprintf(stderr, ": %s\n", strerror(error));
103 }
104 
105 void
106 vaterminate(char *format, va_list ap)
107 {
108 	whine("ERROR", format, ap);
109 }
110 
111 void
112 set_terminate_cleanup(void (*cleanup)())
113 {
114 	terminate_cleanup = cleanup;
115 }
116 
117 /*PRINTFLIKE1*/
118 void
119 terminate(char *format, ...)
120 {
121 	if (format) {
122 		va_list ap;
123 
124 		va_start(ap, format);
125 		whine("ERROR", format, ap);
126 		va_end(ap);
127 	}
128 
129 	if (terminate_cleanup)
130 		terminate_cleanup();
131 
132 	exit(1);
133 }
134 
135 /*PRINTFLIKE1*/
136 void
137 warning(char *format, ...)
138 {
139 	va_list ap;
140 
141 	va_start(ap, format);
142 	whine("WARNING", format, ap);
143 	va_end(ap);
144 
145 	if (debug_level >= 3)
146 		terminate("Termination due to warning\n");
147 }
148 
149 /*PRINTFLIKE2*/
150 void
151 vadebug(int level, char *format, va_list ap)
152 {
153 	if (level > debug_level)
154 		return;
155 
156 	(void) fprintf(DEBUG_STREAM, "DEBUG: ");
157 	(void) vfprintf(DEBUG_STREAM, format, ap);
158 	fflush(DEBUG_STREAM);
159 }
160 
161 /*PRINTFLIKE2*/
162 void
163 debug(int level, char *format, ...)
164 {
165 	va_list ap;
166 
167 	if (level > debug_level)
168 		return;
169 
170 	va_start(ap, format);
171 	(void) vadebug(level, format, ap);
172 	va_end(ap);
173 }
174 
175 char *
176 mktmpname(const char *origname, const char *suffix)
177 {
178 	const char *dot;
179 	char *newname;
180 	size_t newlen;
181 
182 	if (!(dot = strrchr(origname, '.')))
183 		dot = origname + strlen(origname);
184 
185 	newlen = dot - origname + strlen(suffix) + 1;
186 	newname = xmalloc(newlen);
187 	snprintf(newname, newlen, "%*.*s%s", dot - origname, dot - origname,
188 	    origname, suffix);
189 
190 	return (newname);
191 }
192 
193 /*PRINTFLIKE2*/
194 void
195 elfterminate(const char *file, const char *fmt, ...)
196 {
197 	static char msgbuf[BUFSIZ];
198 	va_list ap;
199 
200 	va_start(ap, fmt);
201 	vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
202 	va_end(ap);
203 
204 	terminate("%s: %s: %s\n", file, msgbuf, elf_errmsg(elf_errno()));
205 }
206