warnerr.c revision 78527
1139749Simp/*
259743Sgroudier * Copyright (c) 1995 - 2001 Kungliga Tekniska H�gskolan
359743Sgroudier * (Royal Institute of Technology, Stockholm, Sweden).
459743Sgroudier * All rights reserved.
586266Sgroudier *
659743Sgroudier * Redistribution and use in source and binary forms, with or without
759743Sgroudier * modification, are permitted provided that the following conditions
859743Sgroudier * are met:
959743Sgroudier *
1059743Sgroudier * 1. Redistributions of source code must retain the above copyright
1159743Sgroudier *    notice, this list of conditions and the following disclaimer.
1259743Sgroudier *
1359743Sgroudier * 2. Redistributions in binary form must reproduce the above copyright
1459743Sgroudier *    notice, this list of conditions and the following disclaimer in the
1559743Sgroudier *    documentation and/or other materials provided with the distribution.
1659743Sgroudier *
1759743Sgroudier * 3. Neither the name of the Institute nor the names of its contributors
1859743Sgroudier *    may be used to endorse or promote products derived from this software
1959743Sgroudier *    without specific prior written permission.
2059743Sgroudier *
2159743Sgroudier * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2259743Sgroudier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2359743Sgroudier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2459743Sgroudier * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2559743Sgroudier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2659743Sgroudier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2759743Sgroudier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2859743Sgroudier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2959743Sgroudier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3059743Sgroudier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3159743Sgroudier * SUCH DAMAGE.
3259743Sgroudier */
3359743Sgroudier
3459743Sgroudier#ifdef HAVE_CONFIG_H
3559743Sgroudier#include <config.h>
3659743SgroudierRCSID("$Id: warnerr.c,v 1.13 2001/05/16 23:54:19 assar Exp $");
3759743Sgroudier#endif
3859743Sgroudier
3959743Sgroudier#include "roken.h"
4059743Sgroudier#include "err.h"
4159743Sgroudier
4259743Sgroudier#ifndef HAVE___PROGNAME
4359743Sgroudierconst char *__progname;
4459743Sgroudier#endif
4559743Sgroudier
4659743Sgroudier#ifndef HAVE_GETPROGNAME
4759743Sgroudierconst char *
4859743Sgroudiergetprogname(void)
4959743Sgroudier{
5059743Sgroudier    return __progname;
5159743Sgroudier}
5259743Sgroudier#endif
5359743Sgroudier
5459743Sgroudier#ifndef HAVE_SETPROGNAME
5559743Sgroudiervoid
5659743Sgroudiersetprogname(const char *argv0)
5759743Sgroudier{
5859743Sgroudier#ifndef HAVE___PROGNAME
5959743Sgroudier    char *p;
6059743Sgroudier    if(argv0 == NULL)
6159743Sgroudier	return;
6259743Sgroudier    p = strrchr(argv0, '/');
6359743Sgroudier    if(p == NULL)
6459743Sgroudier	p = argv0;
6559743Sgroudier    else
6659743Sgroudier	p++;
6759743Sgroudier    __progname = p;
6859743Sgroudier#endif
6959743Sgroudier}
7059743Sgroudier#endif /* HAVE_SETPROGNAME */
7159743Sgroudier
7259743Sgroudiervoid
7359743Sgroudierset_progname(char *argv0)
7459743Sgroudier{
7559743Sgroudier    setprogname ((const char *)argv0);
7659743Sgroudier}
7759743Sgroudier
7859743Sgroudierconst char *
7959743Sgroudierget_progname (void)
8059743Sgroudier{
8159743Sgroudier    return getprogname ();
8259743Sgroudier}
8359743Sgroudier
8459743Sgroudiervoid
8559743Sgroudierwarnerr(int doerrno, const char *fmt, va_list ap)
8659743Sgroudier{
8759743Sgroudier    int sverrno = errno;
8859743Sgroudier    const char *progname = getprogname();
8959743Sgroudier
9059743Sgroudier    if(progname != NULL){
9159743Sgroudier	fprintf(stderr, "%s", progname);
9259743Sgroudier	if(fmt != NULL || doerrno)
9359743Sgroudier	    fprintf(stderr, ": ");
9459743Sgroudier    }
9559743Sgroudier    if (fmt != NULL){
9659743Sgroudier	vfprintf(stderr, fmt, ap);
9759743Sgroudier	if(doerrno)
9859743Sgroudier	    fprintf(stderr, ": ");
9959743Sgroudier    }
10059743Sgroudier    if(doerrno)
10159743Sgroudier	fprintf(stderr, "%s", strerror(sverrno));
10259743Sgroudier    fprintf(stderr, "\n");
10359743Sgroudier}
10459743Sgroudier