xref: /onnv-gate/usr/src/lib/libc/port/gen/addsev.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52552Scraigm  * Common Development and Distribution License (the "License").
62552Scraigm  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*6812Sraf #pragma	weak _addsev = addsev
33*6812Sraf 
34*6812Sraf #include "lint.h"
350Sstevel@tonic-gate #include "mtlib.h"
360Sstevel@tonic-gate #include "libc.h"
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <pfmt.h>
390Sstevel@tonic-gate #include <thread.h>
400Sstevel@tonic-gate #include "pfmt_data.h"
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <string.h>
430Sstevel@tonic-gate #include <synch.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate int
addsev(int severity,const char * string)460Sstevel@tonic-gate addsev(int severity, const char *string)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	int i, firstfree;
490Sstevel@tonic-gate 	void *new;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	/* Cannot redefine standard severity */
520Sstevel@tonic-gate 	if ((severity <= 4) || (severity > 255))
530Sstevel@tonic-gate 		return (-1);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	/* Locate severity in table */
560Sstevel@tonic-gate 	lrw_wrlock(&_rw_pfmt_sev_tab);
570Sstevel@tonic-gate 	for (i = 0, firstfree = -1; i < __pfmt_nsev; i++) {
580Sstevel@tonic-gate 		if (__pfmt_sev_tab[i].severity == 0 && firstfree == -1)
590Sstevel@tonic-gate 			firstfree = i;
600Sstevel@tonic-gate 		if (__pfmt_sev_tab[i].severity == severity)
610Sstevel@tonic-gate 			break;
620Sstevel@tonic-gate 	}
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if (i == __pfmt_nsev) {
650Sstevel@tonic-gate 		if (string == NULL)	/* Removing non-existing severity */
660Sstevel@tonic-gate 			return (0);
670Sstevel@tonic-gate 		if (firstfree != -1)	/* Re-use old entry */
680Sstevel@tonic-gate 			i = firstfree;
690Sstevel@tonic-gate 		else {
700Sstevel@tonic-gate 			/* Allocate new entry */
710Sstevel@tonic-gate 			new = libc_realloc(__pfmt_sev_tab,
720Sstevel@tonic-gate 			    sizeof (struct sev_tab) * (__pfmt_nsev + 1));
730Sstevel@tonic-gate 			if (new == NULL) {
740Sstevel@tonic-gate 				lrw_unlock(&_rw_pfmt_sev_tab);
750Sstevel@tonic-gate 				return (-1);
760Sstevel@tonic-gate 			}
770Sstevel@tonic-gate 			__pfmt_nsev++;
780Sstevel@tonic-gate 			__pfmt_sev_tab = new;
790Sstevel@tonic-gate 		}
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 	if (string == NULL) {
820Sstevel@tonic-gate 		if (__pfmt_sev_tab[i].string)
830Sstevel@tonic-gate 			libc_free(__pfmt_sev_tab[i].string);
840Sstevel@tonic-gate 		__pfmt_sev_tab[i].severity = 0;
850Sstevel@tonic-gate 		__pfmt_sev_tab[i].string = NULL;
860Sstevel@tonic-gate 		lrw_unlock(&_rw_pfmt_sev_tab);
870Sstevel@tonic-gate 		return (0);
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 	new = libc_realloc(__pfmt_sev_tab[i].string, strlen(string) + 1);
900Sstevel@tonic-gate 	if (new == NULL) {
910Sstevel@tonic-gate 		lrw_unlock(&_rw_pfmt_sev_tab);
920Sstevel@tonic-gate 		return (-1);
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 	__pfmt_sev_tab[i].severity = severity;
950Sstevel@tonic-gate 	__pfmt_sev_tab[i].string = new;
960Sstevel@tonic-gate 	(void) strcpy(__pfmt_sev_tab[i].string, string);
970Sstevel@tonic-gate 	lrw_unlock(&_rw_pfmt_sev_tab);
980Sstevel@tonic-gate 	return (0);
990Sstevel@tonic-gate }
100