addsev.c revision 2552:04b007b7b43f
133965Sjdp/*
233965Sjdp * CDDL HEADER START
3218822Sdim *
489857Sobrien * The contents of this file are subject to the terms of the
589857Sobrien * Common Development and Distribution License (the "License").
689857Sobrien * You may not use this file except in compliance with the License.
789857Sobrien *
889857Sobrien * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
960484Sobrien * or http://www.opensolaris.org/os/licensing.
1060484Sobrien * See the License for the specific language governing permissions
1160484Sobrien * and limitations under the License.
1233965Sjdp *
13218822Sdim * When distributing Covered Code, include this CDDL HEADER in each
14218822Sdim * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1533965Sjdp * If applicable, add the following below this CDDL HEADER, with the
16130561Sobrien * fields enclosed by brackets "[]" replaced with your own identifying
17130561Sobrien * information: Portions Copyright [yyyy] [name of copyright owner]
18130561Sobrien *
19130561Sobrien * CDDL HEADER END
2033965Sjdp */
21130561Sobrien/*
22130561Sobrien * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23130561Sobrien * Use is subject to license terms.
24130561Sobrien */
2533965Sjdp
26130561Sobrien#pragma ident	"%Z%%M%	%I%	%E% SMI"
27130561Sobrien
28218822Sdim/*	Copyright (c) 1988 AT&T	*/
2933965Sjdp/*	  All Rights Reserved  	*/
3033965Sjdp
3133965Sjdp#pragma	weak addsev = _addsev
3233965Sjdp
3333965Sjdp#include "synonyms.h"
3433965Sjdp#include "mtlib.h"
3589857Sobrien#include "libc.h"
3689857Sobrien#include <stdlib.h>
3789857Sobrien#include <pfmt.h>
3889857Sobrien#include <thread.h>
3989857Sobrien#include "pfmt_data.h"
40218822Sdim#include <sys/types.h>
41218822Sdim#include <string.h>
42218822Sdim#include <synch.h>
43218822Sdim
44218822Sdimint
45218822Sdimaddsev(int severity, const char *string)
46218822Sdim{
47218822Sdim	int i, firstfree;
48218822Sdim	void *new;
49218822Sdim
5089857Sobrien	/* Cannot redefine standard severity */
51218822Sdim	if ((severity <= 4) || (severity > 255))
52218822Sdim		return (-1);
5333965Sjdp
5433965Sjdp	/* Locate severity in table */
5533965Sjdp	lrw_wrlock(&_rw_pfmt_sev_tab);
5633965Sjdp	for (i = 0, firstfree = -1; i < __pfmt_nsev; i++) {
5733965Sjdp		if (__pfmt_sev_tab[i].severity == 0 && firstfree == -1)
5833965Sjdp			firstfree = i;
5933965Sjdp		if (__pfmt_sev_tab[i].severity == severity)
6033965Sjdp			break;
6177298Sobrien	}
6233965Sjdp
63107492Sobrien	if (i == __pfmt_nsev) {
6433965Sjdp		if (string == NULL)	/* Removing non-existing severity */
6533965Sjdp			return (0);
6633965Sjdp		if (firstfree != -1)	/* Re-use old entry */
6733965Sjdp			i = firstfree;
6860484Sobrien		else {
69218822Sdim			/* Allocate new entry */
70218822Sdim			new = libc_realloc(__pfmt_sev_tab,
71218822Sdim			    sizeof (struct sev_tab) * (__pfmt_nsev + 1));
72218822Sdim			if (new == NULL) {
73218822Sdim				lrw_unlock(&_rw_pfmt_sev_tab);
7460484Sobrien				return (-1);
7560484Sobrien			}
7633965Sjdp			__pfmt_nsev++;
7733965Sjdp			__pfmt_sev_tab = new;
7860484Sobrien		}
7960484Sobrien	}
80130561Sobrien	if (string == NULL) {
8160484Sobrien		if (__pfmt_sev_tab[i].string)
8260484Sobrien			libc_free(__pfmt_sev_tab[i].string);
8360484Sobrien		__pfmt_sev_tab[i].severity = 0;
8460484Sobrien		__pfmt_sev_tab[i].string = NULL;
8560484Sobrien		lrw_unlock(&_rw_pfmt_sev_tab);
8660484Sobrien		return (0);
8760484Sobrien	}
8860484Sobrien	new = libc_realloc(__pfmt_sev_tab[i].string, strlen(string) + 1);
8960484Sobrien	if (new == NULL) {
9060484Sobrien		lrw_unlock(&_rw_pfmt_sev_tab);
91218822Sdim		return (-1);
92218822Sdim	}
93218822Sdim	__pfmt_sev_tab[i].severity = severity;
94218822Sdim	__pfmt_sev_tab[i].string = new;
95218822Sdim	(void) strcpy(__pfmt_sev_tab[i].string, string);
96218822Sdim	lrw_unlock(&_rw_pfmt_sev_tab);
9760484Sobrien	return (0);
9860484Sobrien}
9960484Sobrien