xref: /titanic_51/usr/src/lib/libc/port/gen/addsev.c (revision a31148363f598def767ac48c5d82e1572e44b935)
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 #pragma	weak _addsev = addsev
33 
34 #include "lint.h"
35 #include "mtlib.h"
36 #include "libc.h"
37 #include <stdlib.h>
38 #include <pfmt.h>
39 #include <thread.h>
40 #include "pfmt_data.h"
41 #include <sys/types.h>
42 #include <string.h>
43 #include <synch.h>
44 
45 int
46 addsev(int severity, const char *string)
47 {
48 	int i, firstfree;
49 	void *new;
50 
51 	/* Cannot redefine standard severity */
52 	if ((severity <= 4) || (severity > 255))
53 		return (-1);
54 
55 	/* Locate severity in table */
56 	lrw_wrlock(&_rw_pfmt_sev_tab);
57 	for (i = 0, firstfree = -1; i < __pfmt_nsev; i++) {
58 		if (__pfmt_sev_tab[i].severity == 0 && firstfree == -1)
59 			firstfree = i;
60 		if (__pfmt_sev_tab[i].severity == severity)
61 			break;
62 	}
63 
64 	if (i == __pfmt_nsev) {
65 		if (string == NULL)	/* Removing non-existing severity */
66 			return (0);
67 		if (firstfree != -1)	/* Re-use old entry */
68 			i = firstfree;
69 		else {
70 			/* Allocate new entry */
71 			new = libc_realloc(__pfmt_sev_tab,
72 			    sizeof (struct sev_tab) * (__pfmt_nsev + 1));
73 			if (new == NULL) {
74 				lrw_unlock(&_rw_pfmt_sev_tab);
75 				return (-1);
76 			}
77 			__pfmt_nsev++;
78 			__pfmt_sev_tab = new;
79 		}
80 	}
81 	if (string == NULL) {
82 		if (__pfmt_sev_tab[i].string)
83 			libc_free(__pfmt_sev_tab[i].string);
84 		__pfmt_sev_tab[i].severity = 0;
85 		__pfmt_sev_tab[i].string = NULL;
86 		lrw_unlock(&_rw_pfmt_sev_tab);
87 		return (0);
88 	}
89 	new = libc_realloc(__pfmt_sev_tab[i].string, strlen(string) + 1);
90 	if (new == NULL) {
91 		lrw_unlock(&_rw_pfmt_sev_tab);
92 		return (-1);
93 	}
94 	__pfmt_sev_tab[i].severity = severity;
95 	__pfmt_sev_tab[i].string = new;
96 	(void) strcpy(__pfmt_sev_tab[i].string, string);
97 	lrw_unlock(&_rw_pfmt_sev_tab);
98 	return (0);
99 }
100