xref: /titanic_50/usr/src/lib/libc/port/gen/addsev.c (revision 67e3a03ed4a2813074d36330f062ed6e593a4937)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*	Copyright (c) 1988 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #pragma	weak addsev = _addsev
32 
33 #include "synonyms.h"
34 #include "mtlib.h"
35 #include "libc.h"
36 #include <stdlib.h>
37 #include <pfmt.h>
38 #include <thread.h>
39 #include "pfmt_data.h"
40 #include <sys/types.h>
41 #include <string.h>
42 #include <synch.h>
43 
44 int
45 addsev(int severity, const char *string)
46 {
47 	int i, firstfree;
48 	void *new;
49 
50 	/* Cannot redefine standard severity */
51 	if ((severity <= 4) || (severity > 255))
52 		return (-1);
53 
54 	/* Locate severity in table */
55 	lrw_wrlock(&_rw_pfmt_sev_tab);
56 	for (i = 0, firstfree = -1; i < __pfmt_nsev; i++) {
57 		if (__pfmt_sev_tab[i].severity == 0 && firstfree == -1)
58 			firstfree = i;
59 		if (__pfmt_sev_tab[i].severity == severity)
60 			break;
61 	}
62 
63 	if (i == __pfmt_nsev) {
64 		if (string == NULL)	/* Removing non-existing severity */
65 			return (0);
66 		if (firstfree != -1)	/* Re-use old entry */
67 			i = firstfree;
68 		else {
69 			/* Allocate new entry */
70 			new = libc_realloc(__pfmt_sev_tab,
71 			    sizeof (struct sev_tab) * (__pfmt_nsev + 1));
72 			if (new == NULL) {
73 				lrw_unlock(&_rw_pfmt_sev_tab);
74 				return (-1);
75 			}
76 			__pfmt_nsev++;
77 			__pfmt_sev_tab = new;
78 		}
79 	}
80 	if (string == NULL) {
81 		if (__pfmt_sev_tab[i].string)
82 			libc_free(__pfmt_sev_tab[i].string);
83 		__pfmt_sev_tab[i].severity = 0;
84 		__pfmt_sev_tab[i].string = NULL;
85 		lrw_unlock(&_rw_pfmt_sev_tab);
86 		return (0);
87 	}
88 	new = libc_realloc(__pfmt_sev_tab[i].string, strlen(string) + 1);
89 	if (new == NULL) {
90 		lrw_unlock(&_rw_pfmt_sev_tab);
91 		return (-1);
92 	}
93 	__pfmt_sev_tab[i].severity = severity;
94 	__pfmt_sev_tab[i].string = new;
95 	(void) strcpy(__pfmt_sev_tab[i].string, string);
96 	lrw_unlock(&_rw_pfmt_sev_tab);
97 	return (0);
98 }
99