Files
wmaker/WINGs/error.c
T

106 lines
3.1 KiB
C
Raw Normal View History

1998-09-29 22:36:29 +00:00
/*
1998-10-21 14:43:47 +00:00
* Window Maker miscelaneous function library
2004-10-12 21:28:27 +00:00
*
2003-01-16 23:30:45 +00:00
* Copyright (c) 1997-2003 Alfredo K. Kojima
2004-10-12 21:28:27 +00:00
*
1998-09-29 22:36:29 +00:00
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
2001-07-23 20:31:32 +00:00
#include "wconfig.h"
1998-09-29 22:36:29 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
2010-03-17 04:04:18 +01:00
#include <WUtil.h>
1998-09-29 22:36:29 +00:00
extern char *_WINGS_progname;
#define MAXLINE 1024
/*********************************************************************
* Returns the system error message associated with error code 'errnum'
*********************************************************************/
2009-08-20 00:59:40 +02:00
char *wstrerror(int errnum)
{
#if defined(HAVE_STRERROR)
2009-08-20 00:59:40 +02:00
return strerror(errnum);
#elif !defined(HAVE_STRERROR) && defined(BSD)
2009-08-20 00:59:40 +02:00
extern int errno, sys_nerr;
# ifndef __DECC
2009-08-20 00:59:40 +02:00
extern char *sys_errlist[];
# endif
2009-08-20 00:59:40 +02:00
static char buf[] = "Unknown error 12345678901234567890";
2009-08-20 00:59:40 +02:00
if (errno < sys_nerr)
return sys_errlist[errnum];
2009-08-20 00:59:40 +02:00
snprintf(buf, sizeof(buf), _("Unknown error %d"), errnum);
return buf;
#else /* no strerror() and no sys_errlist[] */
static char buf[] = "Error 12345678901234567890";
2009-08-20 00:59:40 +02:00
snprintf(buf, sizeof(buf), _("Error %d"), errnum);
return buf;
#endif
}
2010-03-17 04:04:18 +01:00
void __wmessage(int type, void *extra, const char *msg, ...)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
va_list args;
char buf[MAXLINE];
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
fflush(stdout);
2010-03-17 04:04:18 +01:00
/* message format: <wings_progname>: <qualifier>: <message>[: <extra info>]"\n" */
snprintf(buf, sizeof(buf), "%s: ", _WINGS_progname ? _WINGS_progname : "WINGs");
2009-08-20 00:59:40 +02:00
va_start(args, msg);
2010-03-17 04:04:18 +01:00
switch (type) {
case WMESSAGE_TYPE_WARNING:
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
_("warning: "));
vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
msg, args);
break;
case WMESSAGE_TYPE_FATAL:
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
_("fatal error: "));
vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
msg, args);
break;
case WMESSAGE_TYPE_WSYSERROR:
case WMESSAGE_TYPE_WSYSERRORWITHCODE:
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
_("error: "));
vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
msg, args);
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
": %s", type == WMESSAGE_TYPE_WSYSERROR ?
wstrerror(errno) : wstrerror(*(int *)extra));
break;
case WMESSAGE_TYPE_MESSAGE:
/* FALLTHROUGH */
default:
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ": ");
vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), msg, args);
break;
}
2009-08-20 00:59:40 +02:00
va_end(args);
2010-03-17 04:04:18 +01:00
strncat(buf + strlen(buf), "\n", sizeof(buf) - strlen(buf) - 1);
2009-08-20 00:59:40 +02:00
fputs(buf, stderr);
1998-09-29 22:36:29 +00:00
}