]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/berrno.h
Eliminate dependency on man2html.
[bacula/bacula] / bacula / src / lib / berrno.h
1 /*
2  *   Version $Id$
3  *
4  * Kern Sibbald, July MMIV
5  *
6  */
7 /*
8    Copyright (C) 2004-2006 Kern Sibbald
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License
12    version 2 as amended with additional clauses defined in the
13    file LICENSE in the main source directory.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
18    the file LICENSE for additional details.
19
20  */
21
22 /*
23  * Extra bits set to interpret errno value differently from errno
24  */
25 #ifdef HAVE_WIN32
26 #define b_errno_win32  (1<<29)        /* user reserved bit */
27 #endif
28 #define b_errno_exit   (1<<28)        /* child exited, exit code returned */
29 #define b_errno_signal (1<<27)        /* child died, signal code returned */
30
31 /*
32  * A more generalized way of handling errno that works with Unix, Windows,
33  *  and with Bacula bpipes.
34  *
35  * It works by picking up errno and creating a memory pool buffer
36  *  for editing the message. strerror() does the actual editing, and
37  *  it is thread safe.
38  *
39  * If bit 29 in berrno_ is set then it is a Win32 error, and we
40  *  must to a GetLastError() to get the error code for formatting.
41  * If bit 29 in berrno_ is not set, then it is a Unix errno.
42  *
43  */
44 class berrno : public SMARTALLOC {
45    POOLMEM *buf_;
46    int berrno_;
47    void format_win32_message();
48 public:
49    berrno(int pool=PM_EMSG);
50    ~berrno();
51    const char *strerror();
52    const char *strerror(int errnum);
53    void set_errno(int errnum);
54    int code() { return berrno_ & ~(b_errno_exit|b_errno_signal); }
55    int code(int stat) { return stat & ~(b_errno_exit|b_errno_signal); }
56 };
57
58 /* Constructor */
59 inline berrno::berrno(int pool)
60 {
61    berrno_ = errno;
62    buf_ = get_pool_memory(pool);
63 #ifdef HAVE_WIN32
64    format_win32_message();
65 #endif
66 }
67
68 inline berrno::~berrno()
69 {
70    free_pool_memory(buf_);
71 }
72
73 inline const char *berrno::strerror(int errnum)
74 {
75    berrno_ = errnum;
76    return berrno::strerror();
77 }
78
79
80 inline void berrno::set_errno(int errnum)
81 {
82    berrno_ = errnum;
83 }