xref: /titanic_41/usr/src/lib/libc/port/gen/addsev.c (revision fd9cb95cbb2f626355a60efb9d02c5f0a33c10e6)
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 #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