test whether seekdir() returns void or not
[Samba/gbeck.git] / source / smbwrapper / wrapped.c
blob291da8301f9dbe70633e4a19e6c9bc454aacacc0
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 SMB wrapper functions
5 Copyright (C) Andrew Tridgell 1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* we don't want prototypes for this code */
24 #define NO_PROTO
26 #include "wrapper.h"
28 int open(const char *name, int flags, mode_t mode)
30 if (smbw_path(name)) {
31 return smbw_open(name, flags, mode);
34 return real_open(name, flags, mode);
37 #ifdef HAVE__OPEN
38 int _open(const char *name, int flags, mode_t mode)
40 return open(name, flags, mode);
42 #elif HAVE___OPEN
43 int __open(const char *name, int flags, mode_t mode)
45 return open(name, flags, mode);
47 #endif
50 #ifdef HAVE_OPEN64
51 int open64(const char *name, int flags, mode_t mode)
53 if (smbw_path(name)) {
54 return smbw_open(name, flags, mode);
57 return real_open64(name, flags, mode);
59 #endif
61 #ifndef NO_OPEN64_ALIAS
62 #ifdef HAVE__OPEN64
63 int _open64(const char *name, int flags, mode_t mode)
65 return open64(name, flags, mode);
67 #elif HAVE___OPEN64
68 int __open64(const char *name, int flags, mode_t mode)
70 return open64(name, flags, mode);
72 #endif
73 #endif
75 #ifdef HAVE_PREAD
76 ssize_t pread(int fd, void *buf, size_t size, off_t ofs)
78 if (smbw_fd(fd)) {
79 return smbw_pread(fd, buf, size, ofs);
82 return real_pread(fd, buf, size, ofs);
84 #endif
86 #ifdef HAVE_PREAD64
87 ssize_t pread64(int fd, void *buf, size_t size, off64_t ofs)
89 if (smbw_fd(fd)) {
90 return smbw_pread(fd, buf, size, ofs);
93 return real_pread64(fd, buf, size, ofs);
95 #endif
97 #ifdef HAVE_PWRITE
98 ssize_t pwrite(int fd, void *buf, size_t size, off_t ofs)
100 if (smbw_fd(fd)) {
101 return smbw_pwrite(fd, buf, size, ofs);
104 return real_pwrite(fd, buf, size, ofs);
106 #endif
108 #ifdef HAVE_PWRITE64
109 ssize_t pwrite64(int fd, void *buf, size_t size, off64_t ofs)
111 if (smbw_fd(fd)) {
112 return smbw_pwrite(fd, buf, size, ofs);
115 return real_pwrite64(fd, buf, size, ofs);
117 #endif
120 int chdir(const char *name)
122 return smbw_chdir(name);
125 #ifdef HAVE___CHDIR
126 int __chdir(const char *name)
128 return chdir(name);
130 #elif HAVE__CHDIR
131 int _chdir(const char *name)
133 return chdir(name);
135 #endif
138 int close(int fd)
140 if (smbw_fd(fd)) {
141 return smbw_close(fd);
144 return real_close(fd);
147 #ifdef HAVE___CLOSE
148 int __close(int fd)
150 return close(fd);
152 #elif HAVE__CLOSE
153 int _close(int fd)
155 return close(fd);
157 #endif
160 int fchdir(int fd)
162 if (smbw_fd(fd)) {
163 return smbw_fchdir(fd);
166 return real_fchdir(fd);
169 #ifdef HAVE___FCHDIR
170 int __fchdir(int fd)
172 return fchdir(fd);
174 #elif HAVE__FCHDIR
175 int _fchdir(int fd)
177 return fchdir(fd);
179 #endif
182 int fcntl(int fd, int cmd, long arg)
184 if (smbw_fd(fd)) {
185 return smbw_fcntl(fd, cmd, arg);
188 return real_fcntl(fd, cmd, arg);
192 #ifdef HAVE___FCNTL
193 int __fcntl(int fd, int cmd, long arg)
195 return fcntl(fd, cmd, arg);
197 #elif HAVE__FCNTL
198 int _fcntl(int fd, int cmd, long arg)
200 return fcntl(fd, cmd, arg);
202 #endif
206 #ifdef HAVE_GETDENTS
207 int getdents(int fd, struct dirent *dirp, unsigned int count)
209 if (smbw_fd(fd)) {
210 return smbw_getdents(fd, dirp, count);
213 return real_getdents(fd, dirp, count);
215 #endif
217 #ifdef HAVE___GETDENTS
218 int __getdents(int fd, struct dirent *dirp, unsigned int count)
220 return getdents(fd, dirp, count);
222 #elif HAVE__GETDENTS
223 int _getdents(int fd, struct dirent *dirp, unsigned int count)
225 return getdents(fd, dirp, count);
227 #endif
230 off_t lseek(int fd, off_t offset, int whence)
232 if (smbw_fd(fd)) {
233 return smbw_lseek(fd, offset, whence);
236 return real_lseek(fd, offset, whence);
239 #ifdef HAVE___LSEEK
240 off_t __lseek(int fd, off_t offset, int whence)
242 return lseek(fd, offset, whence);
244 #elif HAVE__LSEEK
245 off_t _lseek(int fd, off_t offset, int whence)
247 return lseek(fd, offset, whence);
249 #endif
252 ssize_t read(int fd, void *buf, size_t count)
254 if (smbw_fd(fd)) {
255 return smbw_read(fd, buf, count);
258 return real_read(fd, buf, count);
261 #ifdef HAVE___READ
262 ssize_t __read(int fd, void *buf, size_t count)
264 return read(fd, buf, count);
266 #elif HAVE__READ
267 ssize_t _read(int fd, void *buf, size_t count)
269 return read(fd, buf, count);
271 #endif
274 ssize_t write(int fd, void *buf, size_t count)
276 if (smbw_fd(fd)) {
277 return smbw_write(fd, buf, count);
280 return real_write(fd, buf, count);
283 #ifdef HAVE___WRITE
284 ssize_t __write(int fd, void *buf, size_t count)
286 return write(fd, buf, count);
288 #elif HAVE__WRITE
289 ssize_t _write(int fd, void *buf, size_t count)
291 return write(fd, buf, count);
293 #endif
297 int access(const char *name, int mode)
299 if (smbw_path(name)) {
300 return smbw_access(name, mode);
303 return real_access(name, mode);
308 int chmod(const char *name,mode_t mode)
310 if (smbw_path(name)) {
311 return smbw_chmod(name, mode);
314 return real_chmod(name, mode);
319 int chown(const char *name,uid_t owner, gid_t group)
321 if (smbw_path(name)) {
322 return smbw_chown(name, owner, group);
325 return real_chown(name, owner, group);
328 #ifdef LINUX
329 int __fxstat(int vers, int fd, struct stat *st)
331 struct kernel_stat kbuf;
332 int ret;
334 if (smbw_fd(fd)) {
335 return smbw_fstat(fd, st);
338 switch (vers) {
339 case _STAT_VER_LINUX_OLD:
340 /* Nothing to do. The struct is in the form the kernel expects
341 it to be. */
342 return real_fstat(fd, (struct kernel_stat *)st);
343 break;
345 case _STAT_VER_LINUX:
346 /* Do the system call. */
347 ret = real_fstat(fd, &kbuf);
349 st->st_dev = kbuf.st_dev;
350 #ifdef _HAVE___PAD1
351 st->__pad1 = 0;
352 #endif
353 st->st_ino = kbuf.st_ino;
354 st->st_mode = kbuf.st_mode;
355 st->st_nlink = kbuf.st_nlink;
356 st->st_uid = kbuf.st_uid;
357 st->st_gid = kbuf.st_gid;
358 st->st_rdev = kbuf.st_rdev;
359 #ifdef _HAVE___PAD2
360 st->__pad2 = 0;
361 #endif
362 st->st_size = kbuf.st_size;
363 st->st_blksize = kbuf.st_blksize;
364 st->st_blocks = kbuf.st_blocks;
365 st->st_atime = kbuf.st_atime;
366 #ifdef _HAVE___UNUSED1
367 st->__unused1 = 0;
368 #endif
369 st->st_mtime = kbuf.st_mtime;
370 #ifdef _HAVE___UNUSED2
371 st->__unused2 = 0;
372 #endif
373 st->st_ctime = kbuf.st_ctime;
374 #ifdef _HAVE___UNUSED3
375 st->__unused3 = 0;
376 #endif
377 #ifdef _HAVE___UNUSED4
378 st->__unused4 = 0;
379 #endif
380 #ifdef _HAVE___UNUSED5
381 st->__unused5 = 0;
382 #endif
383 return ret;
385 default:
386 errno = EINVAL;
387 return -1;
390 #endif
393 char *getcwd(char *buf, size_t size)
395 return smbw_getcwd(buf, size);
399 #ifdef LINUX
400 int __lxstat(int vers, const char *name, struct stat *st)
402 struct kernel_stat kbuf;
403 int ret;
405 if (smbw_path(name)) {
406 return smbw_stat(name, st);
409 switch (vers) {
410 case _STAT_VER_LINUX_OLD:
411 /* Nothing to do. The struct is in the form the kernel expects
412 it to be. */
413 return real_lstat(name, (struct kernel_stat *)st);
414 break;
416 case _STAT_VER_LINUX:
417 /* Do the system call. */
418 ret = real_lstat(name, &kbuf);
420 st->st_dev = kbuf.st_dev;
421 #ifdef _HAVE___PAD1
422 st->__pad1 = 0;
423 #endif
424 st->st_ino = kbuf.st_ino;
425 st->st_mode = kbuf.st_mode;
426 st->st_nlink = kbuf.st_nlink;
427 st->st_uid = kbuf.st_uid;
428 st->st_gid = kbuf.st_gid;
429 st->st_rdev = kbuf.st_rdev;
430 #ifdef _HAVE___PAD2
431 st->__pad2 = 0;
432 #endif
433 st->st_size = kbuf.st_size;
434 st->st_blksize = kbuf.st_blksize;
435 st->st_blocks = kbuf.st_blocks;
436 st->st_atime = kbuf.st_atime;
437 #ifdef _HAVE___UNUSED1
438 st->__unused1 = 0;
439 #endif
440 st->st_mtime = kbuf.st_mtime;
441 #ifdef _HAVE___UNUSED2
442 st->__unused2 = 0;
443 #endif
444 st->st_ctime = kbuf.st_ctime;
445 #ifdef _HAVE___UNUSED3
446 st->__unused3 = 0;
447 #endif
448 #ifdef _HAVE___UNUSED4
449 st->__unused4 = 0;
450 #endif
451 #ifdef _HAVE___UNUSED5
452 st->__unused5 = 0;
453 #endif
454 return ret;
456 default:
457 errno = EINVAL;
458 return -1;
461 #endif
464 int mkdir(const char *name, mode_t mode)
466 if (smbw_path(name)) {
467 return smbw_mkdir(name, mode);
470 return real_mkdir(name, mode);
474 #ifdef LINUX
475 int __xstat(int vers, const char *name, struct stat *st)
477 struct kernel_stat kbuf;
478 int ret;
480 if (smbw_path(name)) {
481 return smbw_stat(name, st);
484 switch (vers) {
485 case _STAT_VER_LINUX_OLD:
486 /* Nothing to do. The struct is in the form the kernel expects
487 it to be. */
488 return real_stat(name, (struct kernel_stat *)st);
489 break;
491 case _STAT_VER_LINUX:
492 /* Do the system call. */
493 ret = real_stat(name, &kbuf);
495 st->st_dev = kbuf.st_dev;
496 #ifdef _HAVE___PAD1
497 st->__pad1 = 0;
498 #endif
499 st->st_ino = kbuf.st_ino;
500 st->st_mode = kbuf.st_mode;
501 st->st_nlink = kbuf.st_nlink;
502 st->st_uid = kbuf.st_uid;
503 st->st_gid = kbuf.st_gid;
504 st->st_rdev = kbuf.st_rdev;
505 #ifdef _HAVE___PAD2
506 st->__pad2 = 0;
507 #endif
508 st->st_size = kbuf.st_size;
509 st->st_blksize = kbuf.st_blksize;
510 st->st_blocks = kbuf.st_blocks;
511 st->st_atime = kbuf.st_atime;
512 #ifdef _HAVE___UNUSED1
513 st->__unused1 = 0;
514 #endif
515 st->st_mtime = kbuf.st_mtime;
516 #ifdef _HAVE___UNUSED2
517 st->__unused2 = 0;
518 #endif
519 st->st_ctime = kbuf.st_ctime;
520 #ifdef _HAVE___UNUSED3
521 st->__unused3 = 0;
522 #endif
523 #ifdef _HAVE___UNUSED4
524 st->__unused4 = 0;
525 #endif
526 #ifdef _HAVE___UNUSED5
527 st->__unused5 = 0;
528 #endif
529 return ret;
531 default:
532 errno = EINVAL;
533 return -1;
536 #endif
538 int stat(const char *name, struct stat *st)
540 #if HAVE___XSTAT
541 return __xstat(_STAT_VER, name, st);
542 #else
543 if (smbw_path(name)) {
544 return smbw_stat(name, st);
546 return real_stat(name, st);
547 #endif
550 int lstat(const char *name, struct stat *st)
552 #if HAVE___LXSTAT
553 return __lxstat(_STAT_VER, name, st);
554 #else
555 if (smbw_path(name)) {
556 return smbw_stat(name, st);
558 return real_lstat(name, st);
559 #endif
562 int fstat(int fd, struct stat *st)
564 #if HAVE___LXSTAT
565 return __fxstat(_STAT_VER, fd, st);
566 #else
567 if (smbw_fd(fd)) {
568 return smbw_fstat(fd, st);
570 return real_fstat(fd, st);
571 #endif
575 int unlink(const char *name)
577 if (smbw_path(name)) {
578 return smbw_unlink(name);
581 return real_unlink(name);
585 #ifdef HAVE_UTIME
586 int utime(const char *name,void *tvp)
588 if (smbw_path(name)) {
589 return smbw_utime(name, tvp);
592 return real_utime(name, tvp);
594 #endif
596 #ifdef HAVE_UTIMES
597 int utimes(const char *name,const struct timeval tvp[2])
599 if (smbw_path(name)) {
600 return smbw_utimes(name, tvp);
603 return real_utimes(name, tvp);
605 #endif
607 int readlink(char *path, char *buf, size_t bufsize)
609 if (smbw_path(path)) {
610 return smbw_readlink(path, buf, bufsize);
613 return real_readlink(path, buf, bufsize);
617 int rename(const char *oldname,const char *newname)
619 int p1, p2;
620 p1 = smbw_path(oldname);
621 p2 = smbw_path(newname);
622 if (p1 ^ p2) {
623 /* can't cross filesystem boundaries */
624 errno = EXDEV;
625 return -1;
627 if (p1 && p2) {
628 return smbw_rename(oldname, newname);
631 return real_rename(oldname, newname);
634 int rmdir(const char *name)
636 if (smbw_path(name)) {
637 return smbw_rmdir(name);
640 return real_rmdir(name);
644 int symlink(const char *topath,const char *frompath)
646 int p1, p2;
647 p1 = smbw_path(topath);
648 p2 = smbw_path(frompath);
649 if (p1 || p2) {
650 /* can't handle symlinks */
651 errno = EPERM;
652 return -1;
655 return real_symlink(topath, frompath);
658 int dup(int fd)
660 if (smbw_fd(fd)) {
661 return smbw_dup(fd);
664 return real_dup(fd);
667 int dup2(int oldfd, int newfd)
669 if (smbw_fd(newfd)) {
670 close(newfd);
673 if (smbw_fd(oldfd)) {
674 return smbw_dup2(oldfd, newfd);
677 return real_dup2(oldfd, newfd);
680 #ifdef real_opendir
681 DIR *opendir(const char *name)
683 if (smbw_path(name)) {
684 return smbw_opendir(name);
687 return real_opendir(name);
689 #endif
691 #ifdef real_readdir
692 struct dirent *readdir(DIR *dir)
694 if (smbw_dirp(dir)) {
695 return smbw_readdir(dir);
698 return real_readdir(dir);
700 #endif
702 #ifdef real_closedir
703 int closedir(DIR *dir)
705 if (smbw_dirp(dir)) {
706 return smbw_closedir(dir);
709 return real_closedir(dir);
711 #endif
713 #ifdef real_telldir
714 off_t telldir(DIR *dir)
716 if (smbw_dirp(dir)) {
717 return smbw_telldir(dir);
720 return real_telldir(dir);
722 #endif
724 #ifdef real_seekdir
725 #if SEEKDIR_RETURNS_VOID
726 void
727 #else
729 #endif
730 seekdir(DIR *dir, off_t offset)
732 if (smbw_dirp(dir)) {
733 smbw_seekdir(dir, offset);
734 goto done;
737 real_seekdir(dir, offset);
738 done:
739 #ifndef SEEKDIR_RETURNS_VOID
740 return 0;
741 #endif
743 #endif
746 #ifndef NO_ACL_WRAPPER
747 int acl(const char *pathp, int cmd, int nentries, aclent_t *aclbufp)
749 if (smbw_path(pathp)) {
750 switch (cmd) {
751 case GETACL:
752 case GETACLCNT:
753 return 0;
754 default:
755 errno = ENOSYS;
756 return -1;
760 real_acl(pathp, cmd, nentries, aclbufp);
762 #endif
764 #ifndef NO_FACL_WRAPPER
765 int facl(int fd, int cmd, int nentries, aclent_t *aclbufp)
767 if (smbw_fd(fd)) {
768 switch (cmd) {
769 case GETACL:
770 case GETACLCNT:
771 return 0;
772 default:
773 errno = ENOSYS;
774 return -1;
778 real_facl(fd, cmd, nentries, aclbufp);
780 #endif
782 int creat(const char *path, mode_t mode)
784 return open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
787 #ifdef HAVE_CREAT64
788 int creat64(const char *path, mode_t mode)
790 return open64(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
792 #endif
794 #ifdef HAVE_STAT64
795 static void stat64_convert(struct stat *st, struct stat64 *st64)
797 st64->st_size = st->st_size;
798 st64->st_mode = st->st_mode;
799 st64->st_ino = st->st_ino;
800 st64->st_dev = st->st_dev;
801 st64->st_rdev = st->st_rdev;
802 st64->st_nlink = st->st_nlink;
803 st64->st_uid = st->st_uid;
804 st64->st_gid = st->st_gid;
805 st64->st_atime = st->st_atime;
806 st64->st_mtime = st->st_mtime;
807 st64->st_ctime = st->st_ctime;
808 st64->st_blksize = st->st_blksize;
809 st64->st_blocks = st->st_blocks;
812 int stat64(const char *name, struct stat64 *st64)
814 if (smbw_path(name)) {
815 struct stat st;
816 int ret = stat(name, &st);
817 stat64_convert(&st, st64);
818 return ret;
820 return real_stat64(name, st64);
823 int fstat64(int fd, struct stat64 *st64)
825 if (smbw_fd(fd)) {
826 struct stat st;
827 int ret = fstat(fd, &st);
828 stat64_convert(&st, st64);
829 return ret;
831 return real_fstat64(fd, st64);
834 int lstat64(const char *name, struct stat64 *st64)
836 if (smbw_path(name)) {
837 struct stat st;
838 int ret = lstat(name, &st);
839 stat64_convert(&st, st64);
840 return ret;
842 return real_lstat64(name, st64);
844 #endif
846 #ifdef HAVE_LLSEEK
847 offset_t llseek(int fd, offset_t ofs, int whence)
849 if (smbw_fd(fd)) {
850 return lseek(fd, ofs, whence);
852 return real_llseek(fd, ofs, whence);
854 #endif
856 #ifdef HAVE_READDIR64
857 static void dirent64_convert(struct dirent *d, struct dirent64 *d64)
859 d64->d_ino = d->d_ino;
860 d64->d_off = d->d_off;
861 d64->d_reclen = d->d_reclen;
862 strcpy(d64->d_name, d->d_name);
865 struct dirent64 *readdir64(DIR *dir)
867 if (smbw_dirp(dir)) {
868 struct dirent *d;
869 static union {
870 char buf[DIRP_SIZE];
871 struct dirent64 d64;
872 } dbuf;
873 d = readdir(dir);
874 if (!d) return NULL;
875 dirent64_convert(d, &dbuf.d64);
876 return &dbuf.d64;
878 return real_readdir64(dir);
880 #endif
882 int fork(void)
884 return smbw_fork();