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