Fixing path to libsmbclient.h so it never gets crossed with a system installed one.
[tprouty/samba.git] / source / libsmb / libsmb_compat.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB client library implementation (Old interface compatibility)
4    Copyright (C) Andrew Tridgell 1998
5    Copyright (C) Richard Sharpe 2000
6    Copyright (C) John Terpstra 2000
7    Copyright (C) Tom Jansen (Ninja ISD) 2002 
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24
25 #include "includes.h"
26
27 /*
28  * Define this to get the real SMBCFILE and SMBCSRV structures 
29  */
30 #define _SMBC_INTERNAL
31 #include "../include/libsmbclient.h"
32
33 struct smbc_compat_fdlist {
34         SMBCFILE * file;
35         int fd;
36         struct smbc_compat_fdlist *next, *prev;
37 };
38
39 static SMBCCTX * statcont = NULL;
40 static int smbc_compat_initialized = 0;
41 static int smbc_currentfd = 10000;
42 static struct smbc_compat_fdlist * smbc_compat_fdlist = NULL;
43
44
45 /* Find an fd and return the SMBCFILE * or NULL on failure */
46 static SMBCFILE * find_fd(int fd)
47 {
48         struct smbc_compat_fdlist * f = smbc_compat_fdlist;
49         while (f) {
50                 if (f->fd == fd) 
51                         return f->file;
52                 f = f->next;
53         }
54         return NULL;
55 }
56
57 /* Add an fd, returns 0 on success, -1 on error with errno set */
58 static int add_fd(SMBCFILE * file)
59 {
60         struct smbc_compat_fdlist * f = malloc(sizeof(struct smbc_compat_fdlist));
61         if (!f) {
62                 errno = ENOMEM;
63                 return -1;
64         }
65         
66         f->fd = smbc_currentfd++;
67         f->file = file;
68         
69         DLIST_ADD(smbc_compat_fdlist, f);
70
71         return f->fd;
72 }
73
74
75
76 /* Delete an fd, returns 0 on success */
77 static int del_fd(int fd)
78 {
79         struct smbc_compat_fdlist * f = smbc_compat_fdlist;
80         while (f) {
81                 if (f->fd == fd) 
82                         break;
83                 f = f->next;
84         }
85         if (f) {
86                 /* found */
87                 DLIST_REMOVE(smbc_compat_fdlist, f);
88                 SAFE_FREE(f);
89                 return 0;
90         }
91         return 1;
92 }
93  
94
95
96 int smbc_init(smbc_get_auth_data_fn fn, int debug)
97 {
98         if (!smbc_compat_initialized) {
99                 statcont = smbc_new_context();
100                 if (!statcont) 
101                         return -1;
102
103                 statcont->debug = debug;
104                 statcont->callbacks.auth_fn = fn;
105                 
106                 if (!smbc_init_context(statcont)) {
107                         smbc_free_context(statcont, False);
108                         return -1;
109                 }
110
111                 smbc_compat_initialized = 1;
112
113                 return 0;
114         }
115         return 0;
116 }
117
118
119 int smbc_open(const char *furl, int flags, mode_t mode)
120 {
121         SMBCFILE * file;
122         int fd;
123
124         file = statcont->open(statcont, furl, flags, mode);
125         if (!file)
126                 return -1;
127
128         fd = add_fd(file);
129         if (fd == -1) 
130                 statcont->close(statcont, file);
131         return fd;
132 }
133
134
135 int smbc_creat(const char *furl, mode_t mode)
136 {
137         SMBCFILE * file;
138         int fd;
139
140         file = statcont->creat(statcont, furl, mode);
141         if (!file)
142                 return -1;
143
144         fd = add_fd(file);
145         if (fd == -1) {
146                 /* Hmm... should we delete the file too ? I guess we could try */
147                 statcont->close(statcont, file);
148                 statcont->unlink(statcont, furl);
149         }
150         return fd;
151 }
152
153
154 ssize_t smbc_read(int fd, void *buf, size_t bufsize)
155 {
156         SMBCFILE * file = find_fd(fd);
157         return statcont->read(statcont, file, buf, bufsize);
158 }
159
160 ssize_t smbc_write(int fd, void *buf, size_t bufsize)
161 {
162         SMBCFILE * file = find_fd(fd);
163         return statcont->write(statcont, file, buf, bufsize);
164 }
165
166 off_t smbc_lseek(int fd, off_t offset, int whence)
167 {
168         SMBCFILE * file = find_fd(fd);
169         return statcont->lseek(statcont, file, offset, whence);
170 }
171
172 int smbc_close(int fd)
173 {
174         SMBCFILE * file = find_fd(fd);
175         del_fd(fd);
176         return statcont->close(statcont, file);
177 }
178
179 int smbc_unlink(const char *fname)
180 {
181         return statcont->unlink(statcont, fname);
182 }
183
184 int smbc_rename(const char *ourl, const char *nurl)
185 {
186         return statcont->rename(statcont, ourl, statcont, nurl);
187 }
188
189 int smbc_opendir(const char *durl)
190 {
191         SMBCFILE * file;
192         int fd;
193
194         file = statcont->opendir(statcont, durl);
195         if (!file)
196                 return -1;
197
198         fd = add_fd(file);
199         if (fd == -1) 
200                 statcont->closedir(statcont, file);
201
202         return fd;
203 }
204
205 int smbc_closedir(int dh) 
206 {
207         SMBCFILE * file = find_fd(dh);
208         del_fd(dh);
209         return statcont->closedir(statcont, file);
210 }
211
212 int smbc_getdents(unsigned int dh, struct smbc_dirent *dirp, int count)
213 {
214         SMBCFILE * file = find_fd(dh);
215         return statcont->getdents(statcont, file,dirp, count);
216 }
217
218 struct smbc_dirent* smbc_readdir(unsigned int dh)
219 {
220         SMBCFILE * file = find_fd(dh);
221         return statcont->readdir(statcont, file);
222 }
223
224 off_t smbc_telldir(int dh)
225 {
226         SMBCFILE * file = find_fd(dh);
227         return statcont->telldir(statcont, file);
228 }
229
230 int smbc_lseekdir(int fd, off_t offset)
231 {
232         SMBCFILE * file = find_fd(fd);
233         return statcont->lseekdir(statcont, file, offset);
234 }
235
236 int smbc_mkdir(const char *durl, mode_t mode)
237 {
238         return statcont->mkdir(statcont, durl, mode);
239 }
240
241 int smbc_rmdir(const char *durl)
242 {
243         return statcont->rmdir(statcont, durl);
244 }
245
246 int smbc_stat(const char *url, struct stat *st)
247 {
248         return statcont->stat(statcont, url, st);
249 }
250
251 int smbc_fstat(int fd, struct stat *st)
252 {
253         SMBCFILE * file = find_fd(fd);
254         return statcont->fstat(statcont, file, st);
255 }
256
257 int smbc_chmod(const char *url, mode_t mode)
258 {
259         /* NOT IMPLEMENTED IN LIBSMBCLIENT YET */
260         return -1;
261 }
262
263 int smbc_print_file(const char *fname, const char *printq)
264 {
265         return statcont->print_file(statcont, fname, statcont, printq);
266 }
267
268 int smbc_open_print_job(const char *fname)
269 {
270         SMBCFILE * file = statcont->open_print_job(statcont, fname);
271         if (!file) return -1;
272         return (int) file;
273 }
274
275 int smbc_list_print_jobs(const char *purl, smbc_get_print_job_info fn)
276 {
277         return statcont->list_print_jobs(statcont, purl, fn);
278 }
279
280 int smbc_unlink_print_job(const char *purl, int id)
281 {
282         return statcont->unlink_print_job(statcont, purl, id);
283 }
284
285