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