]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/berrno.c
Apply fix from Martin Simmons to clear structure before
[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    int stat = 0;
40 #ifdef HAVE_WIN32
41    if (berrno_ & b_errno_win32) {
42       return (const char *)buf_;
43    }
44 #else
45    if (berrno_ & b_errno_exit) {
46       stat = (berrno_ & ~b_errno_exit);       /* remove bit */
47       if (stat == 0) {
48          return _("Child exited normally.");    /* this really shouldn't happen */
49       } else {
50          /* Maybe an execvp failure */
51          if (stat >= 200) {
52             if (stat < 200 + num_execvp_errors) {
53                berrno_ = execvp_errors[stat - 200];
54             } else {
55                return _("Unknown error during program execvp");
56             }
57          } else {
58             Mmsg(&buf_, _("Child exited with code %d"), stat);
59             return buf_;
60          }
61          /* If we drop out here, berrno_ is set to an execvp errno */
62       }
63    }
64    if (berrno_ & b_errno_signal) {
65       stat = (berrno_ & ~b_errno_signal);        /* remove bit */
66       Mmsg(&buf_, _("Child died from signal %d: %s"), stat, get_signal_name(stat));
67       return buf_;
68    }
69 #endif
70    /* Normal errno */
71    if (bstrerror(berrno_, buf_, 1024) < 0) {
72       return _("Invalid errno. No error message possible.");
73    }
74    return buf_;
75 }
76
77 void berrno::format_win32_message()
78 {
79 #ifdef HAVE_WIN32
80    LPVOID msg;
81    if (berrno_ & b_errno_win32) {
82       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
83           FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
84           NULL,
85           GetLastError(),
86           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
87           (LPTSTR)&msg,
88           0,
89           NULL);
90
91       pm_strcpy(&buf_, (const char *)msg);
92       LocalFree(msg);
93    }
94 #endif
95 }
96
97 #ifdef TEST_PROGRAM
98
99 int main()
100 {
101 }
102 #endif