xref: /titanic_41/usr/src/lib/libc/port/gen/str2sig.c (revision ba2e4443695ee6a6f420a35cd4fc3d3346d22932)
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 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 
34 #pragma weak str2sig = _str2sig
35 #pragma weak sig2str = _sig2str
36 
37 #include "synonyms.h"
38 
39 #include <string.h>
40 #include <stdlib.h>
41 #include <signal.h>
42 #include <sys/types.h>
43 #include <errno.h>
44 
45 typedef struct signame {
46 	const char *sigstr;
47 	const int   signum;
48 } signame_t;
49 
50 static signame_t signames[] = {
51 	{ "EXIT",	0 },
52 	{ "HUP",	SIGHUP },
53 	{ "INT",	SIGINT },
54 	{ "QUIT",	SIGQUIT },
55 	{ "ILL",	SIGILL },
56 	{ "TRAP",	SIGTRAP },
57 	{ "ABRT",	SIGABRT },
58 	{ "IOT",	SIGIOT },
59 	{ "EMT",	SIGEMT },
60 	{ "FPE",	SIGFPE },
61 	{ "KILL",	SIGKILL },
62 	{ "BUS",	SIGBUS },
63 	{ "SEGV",	SIGSEGV },
64 	{ "SYS",	SIGSYS },
65 	{ "PIPE",	SIGPIPE },
66 	{ "ALRM",	SIGALRM },
67 	{ "TERM",	SIGTERM },
68 	{ "USR1",	SIGUSR1 },
69 	{ "USR2",	SIGUSR2 },
70 	{ "CLD",	SIGCLD },
71 	{ "CHLD",	SIGCHLD },
72 	{ "PWR",	SIGPWR },
73 	{ "WINCH",	SIGWINCH },
74 	{ "URG",	SIGURG },
75 	{ "POLL",	SIGPOLL },
76 	{ "IO",		SIGPOLL },
77 	{ "STOP",	SIGSTOP },
78 	{ "TSTP",	SIGTSTP },
79 	{ "CONT",	SIGCONT },
80 	{ "TTIN",	SIGTTIN },
81 	{ "TTOU",	SIGTTOU },
82 	{ "VTALRM",	SIGVTALRM },
83 	{ "PROF",	SIGPROF },
84 	{ "XCPU",	SIGXCPU },
85 	{ "XFSZ",	SIGXFSZ },
86 	{ "WAITING",	SIGWAITING },
87 	{ "LWP",	SIGLWP },
88 	{ "FREEZE",	SIGFREEZE },
89 	{ "THAW",	SIGTHAW },
90 	{ "CANCEL",	SIGCANCEL },
91 	{ "LOST",	SIGLOST },
92 	{ "XRES", 	SIGXRES },
93 	{ "JVM1",	SIGJVM1 },
94 	{ "JVM2",	SIGJVM2 },
95 	{ "RTMIN",	_SIGRTMIN },
96 	{ "RTMIN+1",	_SIGRTMIN+1 },
97 	{ "RTMIN+2",	_SIGRTMIN+2 },
98 	{ "RTMIN+3",	_SIGRTMIN+3 },
99 	{ "RTMAX-3",	_SIGRTMAX-3 },
100 	{ "RTMAX-2",	_SIGRTMAX-2 },
101 	{ "RTMAX-1",	_SIGRTMAX-1 },
102 	{ "RTMAX",	_SIGRTMAX },
103 };
104 
105 #define	SIGCNT	(sizeof (signames) / sizeof (struct signame))
106 
107 static int	str2long(const char *, long *);
108 
109 static int
110 str2long(const char *p, long *val)
111 {
112 	char *q;
113 	int error;
114 	int saved_errno = errno;
115 
116 	errno = 0;
117 	*val = strtol(p, &q, 10);
118 
119 	error = ((errno != 0 || q == p || *q != '\0') ? -1 : 0);
120 	errno = saved_errno;
121 
122 	return (error);
123 }
124 
125 int
126 str2sig(const char *s, int *sigp)
127 {
128 	const struct signame *sp;
129 
130 	if (*s >= '0' && *s <= '9') {
131 		long val;
132 
133 		if (str2long(s, &val) == -1)
134 			return (-1);
135 
136 		for (sp = signames; sp < &signames[SIGCNT]; sp++) {
137 			if (sp->signum == val) {
138 				*sigp = sp->signum;
139 				return (0);
140 			}
141 		}
142 		return (-1);
143 	} else {
144 		for (sp = signames; sp < &signames[SIGCNT]; sp++) {
145 			if (strcmp(sp->sigstr, s) == 0) {
146 				*sigp = sp->signum;
147 				return (0);
148 			}
149 		}
150 		return (-1);
151 	}
152 }
153 
154 int
155 sig2str(int i, char *s)
156 {
157 	const struct signame *sp;
158 
159 	for (sp = signames; sp < &signames[SIGCNT]; sp++) {
160 		if (sp->signum == i) {
161 			(void) strcpy(s, sp->sigstr);
162 			return (0);
163 		}
164 	}
165 	return (-1);
166 }
167