]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/berrno.c
Fix header file includes.
[bacula/bacula] / bacula / src / lib / berrno.c
1 /*
2  *  Bacula errno handler
3  *
4  *    berrno is a simplistic errno handler that works for
5  *      Unix, Win32, and Bacula bpipes.
6  *
7  *    See berrno.h for how to use berrno.
8  *
9  *   Kern Sibbald, July MMIV
10  *
11  *   Version $Id$
12  *
13  */
14 /*
15    Copyright (C) 2004-2005 Kern Sibbald
16
17    This program is free software; you can redistribute it and/or
18    modify it under the terms of the GNU General Public License
19    version 2 as amended with additional clauses defined in the
20    file LICENSE in the main source directory.
21
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
25    the file LICENSE for additional details.
26
27  */
28
29 #include "bacula.h"
30
31 #ifndef HAVE_WIN32
32 extern const char *get_signal_name(int sig);
33 extern int num_execvp_errors;
34 extern int execvp_errors[];
35 #endif
36
37 const char *berrno::strerror()
38 {
39 #ifdef HAVE_WIN32
40    if (berrno_ & b_errno_win32) {
41       return (const char *)buf_;
42    }
43 #else
44    int stat = 0;
45
46    if (berrno_ & b_errno_exit) {
47       stat = (berrno_ & ~b_errno_exit);       /* remove bit */
48       if (stat == 0) {
49          return _("Child exited normally.");    /* this really shouldn't happen */
50       } else {
51          /* Maybe an execvp failure */
52          if (stat >= 200) {
53             if (stat < 200 + num_execvp_errors) {
54                berrno_ = execvp_errors[stat - 200];
55             } else {
56                return _("Unknown error during program execvp");
57             }
58          } else {
59             Mmsg(&buf_, _("Child exited with code %d"), stat);
60             return buf_;
61          }
62          /* If we drop out here, berrno_ is set to an execvp errno */
63       }
64    }
65    if (berrno_ & b_errno_signal) {
66       stat = (berrno_ & ~b_errno_signal);        /* remove bit */
67       Mmsg(&buf_, _("Child died from signal %d: %s"), stat, get_signal_name(stat));
68       return buf_;
69    }
70 #endif
71    /* Normal errno */
72    if (bstrerror(berrno_, buf_, 1024) < 0) {
73       return _("Invalid errno. No error message possible.");
74    }
75    return buf_;
76 }
77
78 void berrno::format_win32_message()
79 {
80 #ifdef HAVE_WIN32
81    LPVOID msg;
82    if (berrno_ & b_errno_win32) {
83       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
84           FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
85           NULL,
86           GetLastError(),
87           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
88           (LPTSTR)&msg,
89           0,
90           NULL);
91
92       pm_strcpy(&buf_, (const char *)msg);
93       LocalFree(msg);
94    }
95 #endif
96 }
97
98 #ifdef TEST_PROGRAM
99
100 int main()
101 {
102 }
103 #endif