r14683: Get rid of hardcoded output file. With no arg, print to stdout,
[Samba.git] / source / libsmb / libsmb_compat.c
blob5699e153bbd2c59ba092e4cfaa687a169062b5f5
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 Copyright (C) Derrell Lipman 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
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 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "includes.h"
28 #include "include/libsmb_internal.h"
30 struct smbc_compat_fdlist {
31 SMBCFILE * file;
32 int fd;
33 struct smbc_compat_fdlist *next, *prev;
36 static SMBCCTX * statcont = NULL;
37 static int smbc_compat_initialized = 0;
38 static int smbc_compat_nextfd = 0;
39 static struct smbc_compat_fdlist * smbc_compat_fd_in_use = NULL;
40 static struct smbc_compat_fdlist * smbc_compat_fd_avail = NULL;
42 /* Find an fd and return the SMBCFILE * or NULL on failure */
43 static SMBCFILE * find_fd(int fd)
45 struct smbc_compat_fdlist * f = smbc_compat_fd_in_use;
46 while (f) {
47 if (f->fd == fd)
48 return f->file;
49 f = f->next;
51 return NULL;
54 /* Add an fd, returns 0 on success, -1 on error with errno set */
55 static int add_fd(SMBCFILE * file)
57 struct smbc_compat_fdlist * f = smbc_compat_fd_avail;
59 if (f) {
60 /* We found one that's available */
61 DLIST_REMOVE(smbc_compat_fd_avail, f);
63 } else {
65 * None were available, so allocate one. Keep the number of
66 * file descriptors determinate. This allows the application
67 * to allocate bitmaps or mapping of file descriptors based on
68 * a known maximum number of file descriptors that will ever
69 * be returned.
71 if (smbc_compat_nextfd >= FD_SETSIZE) {
72 errno = EMFILE;
73 return -1;
76 f = SMB_MALLOC_P(struct smbc_compat_fdlist);
77 if (!f) {
78 errno = ENOMEM;
79 return -1;
82 f->fd = SMBC_BASE_FD + smbc_compat_nextfd++;
85 f->file = file;
86 DLIST_ADD(smbc_compat_fd_in_use, f);
88 return f->fd;
93 /* Delete an fd, returns 0 on success */
94 static int del_fd(int fd)
96 struct smbc_compat_fdlist * f = smbc_compat_fd_in_use;
98 while (f) {
99 if (f->fd == fd)
100 break;
101 f = f->next;
104 if (f) {
105 /* found */
106 DLIST_REMOVE(smbc_compat_fd_in_use, f);
107 f->file = NULL;
108 DLIST_ADD(smbc_compat_fd_avail, f);
109 return 0;
111 return 1;
116 int smbc_init(smbc_get_auth_data_fn fn, int debug)
118 if (!smbc_compat_initialized) {
119 statcont = smbc_new_context();
120 if (!statcont)
121 return -1;
123 statcont->debug = debug;
124 statcont->callbacks.auth_fn = fn;
126 if (!smbc_init_context(statcont)) {
127 smbc_free_context(statcont, False);
128 return -1;
131 smbc_compat_initialized = 1;
133 return 0;
135 return 0;
139 SMBCCTX *smbc_set_context(SMBCCTX * context)
141 SMBCCTX *old_context = statcont;
143 if (context) {
144 /* Save provided context. It must have been initialized! */
145 statcont = context;
147 /* You'd better know what you're doing. We won't help you. */
148 smbc_compat_initialized = 1;
151 return old_context;
155 int smbc_open(const char *furl, int flags, mode_t mode)
157 SMBCFILE * file;
158 int fd;
160 file = statcont->open(statcont, furl, flags, mode);
161 if (!file)
162 return -1;
164 fd = add_fd(file);
165 if (fd == -1)
166 statcont->close_fn(statcont, file);
167 return fd;
171 int smbc_creat(const char *furl, mode_t mode)
173 SMBCFILE * file;
174 int fd;
176 file = statcont->creat(statcont, furl, mode);
177 if (!file)
178 return -1;
180 fd = add_fd(file);
181 if (fd == -1) {
182 /* Hmm... should we delete the file too ? I guess we could try */
183 statcont->close_fn(statcont, file);
184 statcont->unlink(statcont, furl);
186 return fd;
190 ssize_t smbc_read(int fd, void *buf, size_t bufsize)
192 SMBCFILE * file = find_fd(fd);
193 return statcont->read(statcont, file, buf, bufsize);
196 ssize_t smbc_write(int fd, void *buf, size_t bufsize)
198 SMBCFILE * file = find_fd(fd);
199 return statcont->write(statcont, file, buf, bufsize);
202 off_t smbc_lseek(int fd, off_t offset, int whence)
204 SMBCFILE * file = find_fd(fd);
205 return statcont->lseek(statcont, file, offset, whence);
208 int smbc_close(int fd)
210 SMBCFILE * file = find_fd(fd);
211 del_fd(fd);
212 return statcont->close_fn(statcont, file);
215 int smbc_unlink(const char *fname)
217 return statcont->unlink(statcont, fname);
220 int smbc_rename(const char *ourl, const char *nurl)
222 return statcont->rename(statcont, ourl, statcont, nurl);
225 int smbc_opendir(const char *durl)
227 SMBCFILE * file;
228 int fd;
230 file = statcont->opendir(statcont, durl);
231 if (!file)
232 return -1;
234 fd = add_fd(file);
235 if (fd == -1)
236 statcont->closedir(statcont, file);
238 return fd;
241 int smbc_closedir(int dh)
243 SMBCFILE * file = find_fd(dh);
244 del_fd(dh);
245 return statcont->closedir(statcont, file);
248 int smbc_getdents(unsigned int dh, struct smbc_dirent *dirp, int count)
250 SMBCFILE * file = find_fd(dh);
251 return statcont->getdents(statcont, file,dirp, count);
254 struct smbc_dirent* smbc_readdir(unsigned int dh)
256 SMBCFILE * file = find_fd(dh);
257 return statcont->readdir(statcont, file);
260 off_t smbc_telldir(int dh)
262 SMBCFILE * file = find_fd(dh);
263 return statcont->telldir(statcont, file);
266 int smbc_lseekdir(int fd, off_t offset)
268 SMBCFILE * file = find_fd(fd);
269 return statcont->lseekdir(statcont, file, offset);
272 int smbc_mkdir(const char *durl, mode_t mode)
274 return statcont->mkdir(statcont, durl, mode);
277 int smbc_rmdir(const char *durl)
279 return statcont->rmdir(statcont, durl);
282 int smbc_stat(const char *url, struct stat *st)
284 return statcont->stat(statcont, url, st);
287 int smbc_fstat(int fd, struct stat *st)
289 SMBCFILE * file = find_fd(fd);
290 return statcont->fstat(statcont, file, st);
293 int smbc_chmod(const char *url, mode_t mode)
295 return statcont->chmod(statcont, url, mode);
298 int smbc_utimes(const char *fname, struct timeval *tbuf)
300 return statcont->utimes(statcont, fname, tbuf);
303 #ifdef HAVE_UTIME_H
304 int smbc_utime(const char *fname, struct utimbuf *utbuf)
306 struct timeval tv[2];
308 if (utbuf == NULL)
309 return statcont->utimes(statcont, fname, NULL);
311 tv[0].tv_sec = utbuf->actime;
312 tv[1].tv_sec = utbuf->modtime;
313 tv[0].tv_usec = tv[1].tv_usec = 0;
315 return statcont->utimes(statcont, fname, tv);
317 #endif
319 int smbc_setxattr(const char *fname,
320 const char *name,
321 const void *value,
322 size_t size,
323 int flags)
325 return statcont->setxattr(statcont, fname, name, value, size, flags);
328 int smbc_lsetxattr(const char *fname,
329 const char *name,
330 const void *value,
331 size_t size,
332 int flags)
334 return statcont->setxattr(statcont, fname, name, value, size, flags);
337 int smbc_fsetxattr(int fd,
338 const char *name,
339 const void *value,
340 size_t size,
341 int flags)
343 SMBCFILE * file = find_fd(fd);
344 return statcont->setxattr(statcont, file->fname,
345 name, value, size, flags);
348 int smbc_getxattr(const char *fname,
349 const char *name,
350 const void *value,
351 size_t size)
353 return statcont->getxattr(statcont, fname, name, value, size);
356 int smbc_lgetxattr(const char *fname,
357 const char *name,
358 const void *value,
359 size_t size)
361 return statcont->getxattr(statcont, fname, name, value, size);
364 int smbc_fgetxattr(int fd,
365 const char *name,
366 const void *value,
367 size_t size)
369 SMBCFILE * file = find_fd(fd);
370 return statcont->getxattr(statcont, file->fname, name, value, size);
373 int smbc_removexattr(const char *fname,
374 const char *name)
376 return statcont->removexattr(statcont, fname, name);
379 int smbc_lremovexattr(const char *fname,
380 const char *name)
382 return statcont->removexattr(statcont, fname, name);
385 int smbc_fremovexattr(int fd,
386 const char *name)
388 SMBCFILE * file = find_fd(fd);
389 return statcont->removexattr(statcont, file->fname, name);
392 int smbc_listxattr(const char *fname,
393 char *list,
394 size_t size)
396 return statcont->listxattr(statcont, fname, list, size);
399 int smbc_llistxattr(const char *fname,
400 char *list,
401 size_t size)
403 return statcont->listxattr(statcont, fname, list, size);
406 int smbc_flistxattr(int fd,
407 char *list,
408 size_t size)
410 SMBCFILE * file = find_fd(fd);
411 return statcont->listxattr(statcont, file->fname, list, size);
414 int smbc_print_file(const char *fname, const char *printq)
416 return statcont->print_file(statcont, fname, statcont, printq);
419 int smbc_open_print_job(const char *fname)
421 SMBCFILE * file = statcont->open_print_job(statcont, fname);
422 if (!file) return -1;
423 return file->cli_fd;
426 int smbc_list_print_jobs(const char *purl, smbc_list_print_job_fn fn)
428 return statcont->list_print_jobs(statcont, purl, fn);
431 int smbc_unlink_print_job(const char *purl, int id)
433 return statcont->unlink_print_job(statcont, purl, id);