Now that I am running config.developer, I decided to get rif of some warnings:
[Samba/gbeck.git] / source3 / libsmb / libsmb_compat.c
blob27b274953ab388dc69ece6791dde8d137e4b2f70
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
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.
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.
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.
25 #include "includes.h"
27 #include "../include/libsmb_internal.h"
29 struct smbc_compat_fdlist {
30 SMBCFILE * file;
31 int fd;
32 struct smbc_compat_fdlist *next, *prev;
35 static SMBCCTX * statcont = NULL;
36 static int smbc_compat_initialized = 0;
37 static int smbc_currentfd = 10000;
38 static struct smbc_compat_fdlist * smbc_compat_fdlist = NULL;
41 /* Find an fd and return the SMBCFILE * or NULL on failure */
42 static SMBCFILE * find_fd(int fd)
44 struct smbc_compat_fdlist * f = smbc_compat_fdlist;
45 while (f) {
46 if (f->fd == fd)
47 return f->file;
48 f = f->next;
50 return NULL;
53 /* Add an fd, returns 0 on success, -1 on error with errno set */
54 static int add_fd(SMBCFILE * file)
56 struct smbc_compat_fdlist * f = malloc(sizeof(struct smbc_compat_fdlist));
57 if (!f) {
58 errno = ENOMEM;
59 return -1;
62 f->fd = smbc_currentfd++;
63 f->file = file;
65 DLIST_ADD(smbc_compat_fdlist, f);
67 return f->fd;
72 /* Delete an fd, returns 0 on success */
73 static int del_fd(int fd)
75 struct smbc_compat_fdlist * f = smbc_compat_fdlist;
76 while (f) {
77 if (f->fd == fd)
78 break;
79 f = f->next;
81 if (f) {
82 /* found */
83 DLIST_REMOVE(smbc_compat_fdlist, f);
84 SAFE_FREE(f);
85 return 0;
87 return 1;
92 int smbc_init(smbc_get_auth_data_fn fn, int debug)
94 if (!smbc_compat_initialized) {
95 statcont = smbc_new_context();
96 if (!statcont)
97 return -1;
99 statcont->debug = debug;
100 statcont->callbacks.auth_fn = fn;
102 if (!smbc_init_context(statcont)) {
103 smbc_free_context(statcont, False);
104 return -1;
107 smbc_compat_initialized = 1;
109 return 0;
111 return 0;
115 int smbc_open(const char *furl, int flags, mode_t mode)
117 SMBCFILE * file;
118 int fd;
120 file = statcont->open(statcont, furl, flags, mode);
121 if (!file)
122 return -1;
124 fd = add_fd(file);
125 if (fd == -1)
126 statcont->close(statcont, file);
127 return fd;
131 int smbc_creat(const char *furl, mode_t mode)
133 SMBCFILE * file;
134 int fd;
136 file = statcont->creat(statcont, furl, mode);
137 if (!file)
138 return -1;
140 fd = add_fd(file);
141 if (fd == -1) {
142 /* Hmm... should we delete the file too ? I guess we could try */
143 statcont->close(statcont, file);
144 statcont->unlink(statcont, furl);
146 return fd;
150 ssize_t smbc_read(int fd, void *buf, size_t bufsize)
152 SMBCFILE * file = find_fd(fd);
153 return statcont->read(statcont, file, buf, bufsize);
156 ssize_t smbc_write(int fd, void *buf, size_t bufsize)
158 SMBCFILE * file = find_fd(fd);
159 return statcont->write(statcont, file, buf, bufsize);
162 off_t smbc_lseek(int fd, off_t offset, int whence)
164 SMBCFILE * file = find_fd(fd);
165 return statcont->lseek(statcont, file, offset, whence);
168 int smbc_close(int fd)
170 SMBCFILE * file = find_fd(fd);
171 del_fd(fd);
172 return statcont->close(statcont, file);
175 int smbc_unlink(const char *fname)
177 return statcont->unlink(statcont, fname);
180 int smbc_rename(const char *ourl, const char *nurl)
182 return statcont->rename(statcont, ourl, statcont, nurl);
185 int smbc_opendir(const char *durl)
187 SMBCFILE * file;
188 int fd;
190 file = statcont->opendir(statcont, durl);
191 if (!file)
192 return -1;
194 fd = add_fd(file);
195 if (fd == -1)
196 statcont->closedir(statcont, file);
198 return fd;
201 int smbc_closedir(int dh)
203 SMBCFILE * file = find_fd(dh);
204 del_fd(dh);
205 return statcont->closedir(statcont, file);
208 int smbc_getdents(unsigned int dh, struct smbc_dirent *dirp, int count)
210 SMBCFILE * file = find_fd(dh);
211 return statcont->getdents(statcont, file,dirp, count);
214 struct smbc_dirent* smbc_readdir(unsigned int dh)
216 SMBCFILE * file = find_fd(dh);
217 return statcont->readdir(statcont, file);
220 off_t smbc_telldir(int dh)
222 SMBCFILE * file = find_fd(dh);
223 return statcont->telldir(statcont, file);
226 int smbc_lseekdir(int fd, off_t offset)
228 SMBCFILE * file = find_fd(fd);
229 return statcont->lseekdir(statcont, file, offset);
232 int smbc_mkdir(const char *durl, mode_t mode)
234 return statcont->mkdir(statcont, durl, mode);
237 int smbc_rmdir(const char *durl)
239 return statcont->rmdir(statcont, durl);
242 int smbc_stat(const char *url, struct stat *st)
244 return statcont->stat(statcont, url, st);
247 int smbc_fstat(int fd, struct stat *st)
249 SMBCFILE * file = find_fd(fd);
250 return statcont->fstat(statcont, file, st);
253 int smbc_chmod(const char *url, mode_t mode)
255 /* NOT IMPLEMENTED IN LIBSMBCLIENT YET */
256 return -1;
259 int smbc_print_file(const char *fname, const char *printq)
261 return statcont->print_file(statcont, fname, statcont, printq);
264 int smbc_open_print_job(const char *fname)
266 SMBCFILE * file = statcont->open_print_job(statcont, fname);
267 if (!file) return -1;
268 return (int) file;
271 int smbc_list_print_jobs(const char *purl, smbc_list_print_job_fn fn)
273 return statcont->list_print_jobs(statcont, purl, fn);
276 int smbc_unlink_print_job(const char *purl, int id)
278 return statcont->unlink_print_job(statcont, purl, id);