]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/berrno.c
Add new berrno files
[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 Kern Sibbald and John Walker
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 as
19    published by the Free Software Foundation; either version 2 of
20    the License, or (at your option) any later version.
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 GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public
28    License along with this program; if not, write to the Free
29    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30    MA 02111-1307, USA.
31
32  */
33
34 #include "bacula.h"
35
36 const char *berrno::strerror()
37 {
38 #ifdef HAVE_WIN32
39    LPVOID msg;
40
41    if (errnum && b_errno_win32) {
42       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
43           FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
44           NULL,
45           GetLastError(),
46           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
47           (LPTSTR)&msg;
48           0,
49           NULL);
50
51       pm_strcpy(&buf_, msg);
52       LocalFree(msg);
53       return (const char *)buf_;
54    }
55 #endif
56    if (bstrerror(berrno_, buf_, 1024) < 0) {
57       return "Invalid errno. No error message possible."; 
58    }
59    return (const char *)buf_;
60 }
61
62
63 #ifdef TEST_PROGRAM
64
65
66 struct FILESET {
67    alist mylist;
68 };
69
70 int main()
71 {
72    FILESET *fileset;
73    char buf[30];
74    alist *mlist;
75
76    fileset = (FILESET *)malloc(sizeof(FILESET));
77    memset(fileset, 0, sizeof(FILESET));
78    fileset->mylist.init();
79
80    printf("Manual allocation/destruction of list:\n");
81    
82    for (int i=0; i<20; i++) {
83       sprintf(buf, "This is item %d", i);
84       fileset->mylist.append(bstrdup(buf));
85    } 
86    for (int i=0; i< fileset->mylist.size(); i++) {
87       printf("Item %d = %s\n", i, (char *)fileset->mylist[i]);  
88    }
89    fileset->mylist.destroy();
90    free(fileset);
91
92    printf("Allocation/destruction using new delete\n");
93    mlist = new alist(10);
94
95    for (int i=0; i<20; i++) {
96       sprintf(buf, "This is item %d", i);
97       mlist->append(bstrdup(buf));
98    } 
99    for (int i=0; i< mlist->size(); i++) {
100       printf("Item %d = %s\n", i, (char *)mlist->get(i));  
101    }
102
103    delete mlist;
104
105
106    sm_dump(false);
107
108 }
109 #endif