xref: /titanic_51/usr/src/lib/libc/port/gen/str2sig.c (revision 89b43686db1fe9681d80a7cf5662730cb9378cae)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include "lint.h"
31 #include <string.h>
32 #include <stdlib.h>
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <errno.h>
36 
37 typedef struct signame {
38 	const char *sigstr;
39 	const int   signum;
40 } signame_t;
41 
42 static signame_t signames[] = {
43 	{ "EXIT",	0 },
44 	{ "HUP",	SIGHUP },
45 	{ "INT",	SIGINT },
46 	{ "QUIT",	SIGQUIT },
47 	{ "ILL",	SIGILL },
48 	{ "TRAP",	SIGTRAP },
49 	{ "ABRT",	SIGABRT },
50 	{ "IOT",	SIGIOT },
51 	{ "EMT",	SIGEMT },
52 	{ "FPE",	SIGFPE },
53 	{ "KILL",	SIGKILL },
54 	{ "BUS",	SIGBUS },
55 	{ "SEGV",	SIGSEGV },
56 	{ "SYS",	SIGSYS },
57 	{ "PIPE",	SIGPIPE },
58 	{ "ALRM",	SIGALRM },
59 	{ "TERM",	SIGTERM },
60 	{ "USR1",	SIGUSR1 },
61 	{ "USR2",	SIGUSR2 },
62 	{ "CLD",	SIGCLD },
63 	{ "CHLD",	SIGCHLD },
64 	{ "PWR",	SIGPWR },
65 	{ "WINCH",	SIGWINCH },
66 	{ "URG",	SIGURG },
67 	{ "POLL",	SIGPOLL },
68 	{ "IO",		SIGPOLL },
69 	{ "STOP",	SIGSTOP },
70 	{ "TSTP",	SIGTSTP },
71 	{ "CONT",	SIGCONT },
72 	{ "TTIN",	SIGTTIN },
73 	{ "TTOU",	SIGTTOU },
74 	{ "VTALRM",	SIGVTALRM },
75 	{ "PROF",	SIGPROF },
76 	{ "XCPU",	SIGXCPU },
77 	{ "XFSZ",	SIGXFSZ },
78 	{ "WAITING",	SIGWAITING },
79 	{ "LWP",	SIGLWP },
80 	{ "FREEZE",	SIGFREEZE },
81 	{ "THAW",	SIGTHAW },
82 	{ "CANCEL",	SIGCANCEL },
83 	{ "LOST",	SIGLOST },
84 	{ "XRES", 	SIGXRES },
85 	{ "JVM1",	SIGJVM1 },
86 	{ "JVM2",	SIGJVM2 },
87 	{ "RTMIN",	_SIGRTMIN },
88 	{ "RTMIN+1",	_SIGRTMIN+1 },
89 	{ "RTMIN+2",	_SIGRTMIN+2 },
90 	{ "RTMIN+3",	_SIGRTMIN+3 },
91 	{ "RTMIN+4",	_SIGRTMIN+4 },
92 	{ "RTMIN+5",	_SIGRTMIN+5 },
93 	{ "RTMIN+6",	_SIGRTMIN+6 },
94 	{ "RTMIN+7",	_SIGRTMIN+7 },
95 	{ "RTMIN+8",	_SIGRTMIN+8 },
96 	{ "RTMIN+9",	_SIGRTMIN+9 },
97 	{ "RTMIN+10",	_SIGRTMIN+10 },
98 	{ "RTMIN+11",	_SIGRTMIN+11 },
99 	{ "RTMIN+12",	_SIGRTMIN+12 },
100 	{ "RTMIN+13",	_SIGRTMIN+13 },
101 	{ "RTMIN+14",	_SIGRTMIN+14 },
102 	{ "RTMIN+15",	_SIGRTMIN+15 },
103 	{ "RTMAX-15",	_SIGRTMAX-15 },
104 	{ "RTMAX-14",	_SIGRTMAX-14 },
105 	{ "RTMAX-13",	_SIGRTMAX-13 },
106 	{ "RTMAX-12",	_SIGRTMAX-12 },
107 	{ "RTMAX-11",	_SIGRTMAX-11 },
108 	{ "RTMAX-10",	_SIGRTMAX-10 },
109 	{ "RTMAX-9",	_SIGRTMAX-9 },
110 	{ "RTMAX-8",	_SIGRTMAX-8 },
111 	{ "RTMAX-7",	_SIGRTMAX-7 },
112 	{ "RTMAX-6",	_SIGRTMAX-6 },
113 	{ "RTMAX-5",	_SIGRTMAX-5 },
114 	{ "RTMAX-4",	_SIGRTMAX-4 },
115 	{ "RTMAX-3",	_SIGRTMAX-3 },
116 	{ "RTMAX-2",	_SIGRTMAX-2 },
117 	{ "RTMAX-1",	_SIGRTMAX-1 },
118 	{ "RTMAX",	_SIGRTMAX },
119 };
120 
121 #define	SIGCNT	(sizeof (signames) / sizeof (struct signame))
122 
123 static int	str2long(const char *, long *);
124 
125 static int
126 str2long(const char *p, long *val)
127 {
128 	char *q;
129 	int error;
130 	int saved_errno = errno;
131 
132 	errno = 0;
133 	*val = strtol(p, &q, 10);
134 
135 	error = ((errno != 0 || q == p || *q != '\0') ? -1 : 0);
136 	errno = saved_errno;
137 
138 	return (error);
139 }
140 
141 int
142 str2sig(const char *s, int *sigp)
143 {
144 	const struct signame *sp;
145 
146 	if (*s >= '0' && *s <= '9') {
147 		long val;
148 
149 		if (str2long(s, &val) == -1)
150 			return (-1);
151 
152 		for (sp = signames; sp < &signames[SIGCNT]; sp++) {
153 			if (sp->signum == val) {
154 				*sigp = sp->signum;
155 				return (0);
156 			}
157 		}
158 		return (-1);
159 	} else {
160 		for (sp = signames; sp < &signames[SIGCNT]; sp++) {
161 			if (strcmp(sp->sigstr, s) == 0) {
162 				*sigp = sp->signum;
163 				return (0);
164 			}
165 		}
166 		return (-1);
167 	}
168 }
169 
170 int
171 sig2str(int i, char *s)
172 {
173 	const struct signame *sp;
174 
175 	for (sp = signames; sp < &signames[SIGCNT]; sp++) {
176 		if (sp->signum == i) {
177 			(void) strcpy(s, sp->sigstr);
178 			return (0);
179 		}
180 	}
181 	return (-1);
182 }
183