-
Notifications
You must be signed in to change notification settings - Fork 22
/
mcdb_error.c
80 lines (67 loc) · 2.34 KB
/
mcdb_error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* mcdb_error - mcdb error codes and messages
*
* Copyright (c) 2010, Glue Logic LLC. All rights reserved. code()gluelogic.com
*
* This file is part of mcdb.
*
* mcdb is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* mcdb 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mcdb. If not, see <http://www.gnu.org/licenses/>.
*
*
* mcdb is originally based upon the Public Domain cdb-0.75 by Dan Bernstein
*/
#ifndef _POSIX_C_SOURCE /* 200112L for XSI-compliant sterror_r() */
#define _POSIX_C_SOURCE 200112L
#endif
#ifdef __hpux
#ifndef _XOPEN_SOURCE /* strerror_r() */
#define _XOPEN_SOURCE 600
#endif
#endif
#include "mcdb_error.h"
#include <errno.h>
#include <stdio.h> /* fprintf() */
#include <string.h> /* strerror_r() */
#include <stdlib.h> /* EXIT_SUCCESS */
int
mcdb_error (const int rv, const char * const restrict prefix,
const char * const restrict mcdb_usage)
{
char errstr[128];
switch (rv) {
case EXIT_SUCCESS:
return EXIT_SUCCESS;
case MCDB_ERROR_READFORMAT:
fprintf(stderr, "%s: read input: bad format\n", prefix);
return 111;
case MCDB_ERROR_READ:
if (strerror_r(errno, errstr, sizeof(errstr)) != 0) errstr[0] = '\0';
fprintf(stderr, "%s: read input: %s\n", prefix, errstr);
return 111;
case MCDB_ERROR_WRITE:
if (strerror_r(errno, errstr, sizeof(errstr)) != 0) errstr[0] = '\0';
fprintf(stderr, "%s: write output: %s\n", prefix, errstr);
return 111;
case MCDB_ERROR_MALLOC:
if (strerror_r(errno, errstr, sizeof(errstr)) != 0) errstr[0] = '\0';
fprintf(stderr, "%s: malloc: %s\n", prefix, errstr);
return 111;
case MCDB_ERROR_USAGE:
fprintf(stderr, "%s: %s", prefix, mcdb_usage);
return 101;
default:
fprintf(stderr, "%s: (unknown error)\n", prefix);
return 111;
}
}