4703614d649d87a6edbfc340ebac1f88da00c784
[tprouty/samba.git] / source3 / smbwrapper / wrapped.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4    SMB wrapper functions
5    Copyright (C) Andrew Tridgell 1998
6    
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.
11    
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.
16    
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.
20 */
21
22 /* NOTE: This file WILL produce compiler warnings. They are unavoidable 
23
24    Do not try and get rid of them by including other include files or
25    by including includes.h or proto.h or you will break portability. 
26   */
27
28 #include "config.h"
29 #include <sys/types.h>
30 #include <errno.h>
31 #include "realcalls.h"
32
33  int open(char *name, int flags, mode_t mode)
34 {
35         if (smbw_path(name)) {
36                 return smbw_open(name, flags, mode);
37         }
38
39         return real_open(name, flags, mode);
40 }
41
42 #ifdef HAVE__OPEN
43  int _open(char *name, int flags, mode_t mode) 
44 {
45         return open(name, flags, mode);
46 }
47 #elif HAVE___OPEN
48  int __open(char *name, int flags, mode_t mode) 
49 {
50         return open(name, flags, mode);
51 }
52 #endif
53
54
55 #ifdef HAVE_OPEN64
56  int open64(char *name, int flags, mode_t mode)
57 {
58         if (smbw_path(name)) {
59                 return smbw_open(name, flags, mode);
60         }
61
62         return real_open64(name, flags, mode);
63 }
64 #endif
65
66 #ifndef NO_OPEN64_ALIAS
67 #ifdef HAVE__OPEN64
68  int _open64(char *name, int flags, mode_t mode) 
69 {
70         return open64(name, flags, mode);
71 }
72 #elif HAVE___OPEN64
73  int __open64(char *name, int flags, mode_t mode) 
74 {
75         return open64(name, flags, mode);
76 }
77 #endif
78 #endif
79
80 #ifdef HAVE_PREAD
81  ssize_t pread(int fd, void *buf, size_t size, off_t ofs)
82 {
83         if (smbw_fd(fd)) {
84                 return smbw_pread(fd, buf, size, ofs);
85         }
86
87         return real_pread(fd, buf, size, ofs);
88 }
89 #endif
90
91 #ifdef HAVE_PREAD64
92  ssize_t pread64(int fd, void *buf, size_t size, off64_t ofs)
93 {
94         if (smbw_fd(fd)) {
95                 return smbw_pread(fd, buf, size, ofs);
96         }
97
98         return real_pread64(fd, buf, size, ofs);
99 }
100 #endif
101
102 #ifdef HAVE_PWRITE
103  ssize_t pwrite(int fd, void *buf, size_t size, off_t ofs)
104 {
105         if (smbw_fd(fd)) {
106                 return smbw_pwrite(fd, buf, size, ofs);
107         }
108
109         return real_pwrite(fd, buf, size, ofs);
110 }
111 #endif
112
113 #ifdef HAVE_PWRITE64
114  ssize_t pwrite64(int fd, void *buf, size_t size, off64_t ofs)
115 {
116         if (smbw_fd(fd)) {
117                 return smbw_pwrite(fd, buf, size, ofs);
118         }
119
120         return real_pwrite64(fd, buf, size, ofs);
121 }
122 #endif
123
124
125  int chdir(char *name)
126 {
127         return smbw_chdir(name);
128 }
129
130 #ifdef HAVE___CHDIR
131  int __chdir(char *name)
132 {
133         return chdir(name);
134 }
135 #elif HAVE__CHDIR
136  int _chdir(char *name)
137 {
138         return chdir(name);
139 }
140 #endif
141
142
143  int close(int fd)
144 {
145         if (smbw_fd(fd)) {
146                 return smbw_close(fd);
147         }
148         if (smbw_local_fd(fd)) {
149                 errno = EBADF;
150                 return -1;
151         }
152
153         return real_close(fd);
154 }
155
156 #ifdef HAVE___CLOSE
157  int __close(int fd)
158 {
159         return close(fd);
160 }
161 #elif HAVE__CLOSE
162  int _close(int fd)
163 {
164         return close(fd);
165 }
166 #endif
167
168
169  int fchdir(int fd)
170 {
171         return smbw_fchdir(fd);
172 }
173
174 #ifdef HAVE___FCHDIR
175  int __fchdir(int fd)
176 {
177         return fchdir(fd);
178 }
179 #elif HAVE__FCHDIR
180  int _fchdir(int fd)
181 {
182         return fchdir(fd);
183 }
184 #endif
185
186
187  int fcntl(int fd, int cmd, long arg)
188 {
189         if (smbw_fd(fd)) {
190                 return smbw_fcntl(fd, cmd, arg);
191         }
192
193         return real_fcntl(fd, cmd, arg);
194 }
195
196
197 #ifdef HAVE___FCNTL
198  int __fcntl(int fd, int cmd, long arg)
199 {
200         return fcntl(fd, cmd, arg);
201 }
202 #elif HAVE__FCNTL
203  int _fcntl(int fd, int cmd, long arg)
204 {
205         return fcntl(fd, cmd, arg);
206 }
207 #endif
208
209
210
211 #ifdef real_getdents
212  int getdents(int fd, void *dirp, unsigned int count)
213 {
214         if (smbw_fd(fd)) {
215                 return smbw_getdents(fd, dirp, count);
216         }
217
218         return real_getdents(fd, dirp, count);
219 }
220 #endif
221
222 #ifdef HAVE___GETDENTS
223  int __getdents(int fd, void *dirp, unsigned int count)
224 {
225         return getdents(fd, dirp, count);
226 }
227 #elif HAVE__GETDENTS
228  int _getdents(int fd, void *dirp, unsigned int count)
229 {
230         return getdents(fd, dirp, count);
231 }
232 #endif
233
234
235  off_t lseek(int fd, off_t offset, int whence)
236 {
237         if (smbw_fd(fd)) {
238                 return smbw_lseek(fd, offset, whence);
239         }
240
241         return real_lseek(fd, offset, whence);
242 }
243
244 #ifdef HAVE___LSEEK
245  off_t __lseek(int fd, off_t offset, int whence)
246 {
247         return lseek(fd, offset, whence);
248 }
249 #elif HAVE__LSEEK
250  off_t _lseek(int fd, off_t offset, int whence)
251 {
252         return lseek(fd, offset, whence);
253 }
254 #endif
255
256
257  ssize_t read(int fd, void *buf, size_t count)
258 {
259         if (smbw_fd(fd)) {
260                 return smbw_read(fd, buf, count);
261         }
262
263         return real_read(fd, buf, count);
264 }
265
266 #ifdef HAVE___READ
267  ssize_t __read(int fd, void *buf, size_t count)
268 {
269         return read(fd, buf, count);
270 }
271 #elif HAVE__READ
272  ssize_t _read(int fd, void *buf, size_t count)
273 {
274         return read(fd, buf, count);
275 }
276 #endif
277
278
279  ssize_t write(int fd, void *buf, size_t count)
280 {
281         if (smbw_fd(fd)) {
282                 return smbw_write(fd, buf, count);
283         }
284
285         return real_write(fd, buf, count);
286 }
287
288 #ifdef HAVE___WRITE
289  ssize_t __write(int fd, void *buf, size_t count)
290 {
291         return write(fd, buf, count);
292 }
293 #elif HAVE__WRITE
294  ssize_t _write(int fd, void *buf, size_t count)
295 {
296         return write(fd, buf, count);
297 }
298 #endif
299
300
301
302  int access(char *name, int mode)
303 {
304         if (smbw_path(name)) {
305                 return smbw_access(name, mode);
306         }
307
308         return real_access(name, mode);
309 }
310
311
312
313  int chmod(char *name,mode_t mode)
314 {
315         if (smbw_path(name)) {
316                 return smbw_chmod(name, mode);
317         }
318
319         return real_chmod(name, mode);
320 }
321
322
323
324  int chown(char *name,uid_t owner, gid_t group)
325 {
326         if (smbw_path(name)) {
327                 return smbw_chown(name, owner, group);
328         }
329
330         return real_chown(name, owner, group);
331 }
332
333
334  char *getcwd(char *buf, size_t size)
335 {
336         return (char *)smbw_getcwd(buf, size);
337 }
338
339
340
341
342  int mkdir(char *name, mode_t mode)
343 {
344         if (smbw_path(name)) {
345                 return smbw_mkdir(name, mode);
346         }
347
348         return real_mkdir(name, mode);
349 }
350
351
352 #if HAVE___FXSTAT
353  int __fxstat(int vers, int fd, void *st)
354 {
355         double xx[32];
356         int ret;
357
358         if (smbw_fd(fd)) {
359                 return smbw_fstat(fd, st);
360         }
361
362         ret = real_fstat(fd, xx);
363         xstat_convert(vers, st, xx);
364         return ret;
365 }
366 #endif
367
368 #if HAVE___XSTAT
369  int __xstat(int vers, char *name, void *st)
370 {
371         double xx[32];
372         int ret;
373
374         if (smbw_path(name)) {
375                 return smbw_stat(name, st);
376         }
377
378         ret = real_stat(name, xx);
379         xstat_convert(vers, st, xx);
380         return ret;
381 }
382 #endif
383
384
385 #if HAVE___LXSTAT
386  int __lxstat(int vers, char *name, void *st)
387 {
388         double xx[32];
389         int ret;
390
391         if (smbw_path(name)) {
392                 return smbw_stat(name, st);
393         }
394
395         ret = real_lstat(name, xx);
396         xstat_convert(vers, st, xx);
397         return ret;
398 }
399 #endif
400
401
402  int stat(char *name, void *st)
403 {
404 #if HAVE___XSTAT
405         return __xstat(0, name, st);
406 #else
407         if (smbw_path(name)) {
408                 return smbw_stat(name, st);
409         }
410         return real_stat(name, st);
411 #endif
412 }
413
414  int lstat(char *name, void *st)
415 {
416 #if HAVE___LXSTAT
417         return __lxstat(0, name, st);
418 #else
419         if (smbw_path(name)) {
420                 return smbw_stat(name, st);
421         }
422         return real_lstat(name, st);
423 #endif
424 }
425
426  int fstat(int fd, void *st)
427 {
428 #if HAVE___LXSTAT
429         return __fxstat(0, fd, st);
430 #else
431         if (smbw_fd(fd)) {
432                 return smbw_fstat(fd, st);
433         }
434         return real_fstat(fd, st);
435 #endif
436 }
437
438
439  int unlink(char *name)
440 {
441         if (smbw_path(name)) {
442                 return smbw_unlink(name);
443         }
444
445         return real_unlink(name);
446 }
447
448
449 #ifdef HAVE_UTIME
450  int utime(char *name,void *tvp)
451 {
452         if (smbw_path(name)) {
453                 return smbw_utime(name, tvp);
454         }
455
456         return real_utime(name, tvp);
457 }
458 #endif
459
460 #ifdef HAVE_UTIMES
461  int utimes(char *name,void *tvp)
462 {
463         if (smbw_path(name)) {
464                 return smbw_utimes(name, tvp);
465         }
466
467         return real_utimes(name, tvp);
468 }
469 #endif
470
471  int readlink(char *path, char *buf, size_t bufsize)
472 {
473         if (smbw_path(path)) {
474                 return smbw_readlink(path, buf, bufsize);
475         }
476
477         return real_readlink(path, buf, bufsize);
478 }
479
480
481  int rename(char *oldname,char *newname)
482 {
483         int p1, p2;
484         p1 = smbw_path(oldname); 
485         p2 = smbw_path(newname); 
486         if (p1 ^ p2) {
487                 /* can't cross filesystem boundaries */
488                 errno = EXDEV;
489                 return -1;
490         }
491         if (p1 && p2) {
492                 return smbw_rename(oldname, newname);
493         }
494
495         return real_rename(oldname, newname);
496 }
497
498  int rmdir(char *name)
499 {
500         if (smbw_path(name)) {
501                 return smbw_rmdir(name);
502         }
503
504         return real_rmdir(name);
505 }
506
507
508  int symlink(char *topath,char *frompath)
509 {
510         int p1, p2;
511         p1 = smbw_path(topath); 
512         p2 = smbw_path(frompath); 
513         if (p1 || p2) {
514                 /* can't handle symlinks */
515                 errno = EPERM;
516                 return -1;
517         }
518
519         return real_symlink(topath, frompath);
520 }
521
522  int dup(int fd)
523 {
524         if (smbw_fd(fd)) {
525                 return smbw_dup(fd);
526         }
527
528         return real_dup(fd);
529 }
530
531  int dup2(int oldfd, int newfd)
532 {
533         if (smbw_fd(newfd)) {
534                 close(newfd);
535         }
536
537         if (smbw_fd(oldfd)) {
538                 return smbw_dup2(oldfd, newfd);
539         }
540
541         return real_dup2(oldfd, newfd);
542 }
543
544 #ifdef real_opendir
545  void *opendir(char *name)
546 {
547         if (smbw_path(name)) {
548                 return (void *)smbw_opendir(name);
549         }
550
551         return (void *)real_opendir(name);
552 }
553 #endif
554
555 #ifdef real_readdir
556  void *readdir(void *dir)
557 {
558         if (smbw_dirp(dir)) {
559                 return (void *)smbw_readdir(dir);
560         }
561
562         return (void *)real_readdir(dir);
563 }
564 #endif
565
566 #ifdef real_closedir
567  int closedir(void *dir)
568 {
569         if (smbw_dirp(dir)) {
570                 return smbw_closedir(dir);
571         }
572
573         return real_closedir(dir);
574 }
575 #endif
576
577 #ifdef real_telldir
578  off_t telldir(void *dir)
579 {
580         if (smbw_dirp(dir)) {
581                 return smbw_telldir(dir);
582         }
583
584         return real_telldir(dir);
585 }
586 #endif
587
588 #ifdef real_seekdir
589  int seekdir(void *dir, off_t offset)
590 {
591         if (smbw_dirp(dir)) {
592                 smbw_seekdir(dir, offset);
593                 return 0;
594         }
595
596         real_seekdir(dir, offset);
597         return 0;
598 }
599 #endif
600
601
602 #ifndef NO_ACL_WRAPPER
603  int  acl(char  *pathp,  int  cmd,  int  nentries, void *aclbufp)
604 {
605         if (smbw_path(pathp)) {
606                 return smbw_acl(pathp, cmd, nentries, aclbufp);
607         }
608
609         return real_acl(pathp, cmd, nentries, aclbufp);
610 }
611 #endif
612
613 #ifndef NO_FACL_WRAPPER
614  int  facl(int fd,  int  cmd,  int  nentries, void *aclbufp)
615 {
616         if (smbw_fd(fd)) {
617                 return smbw_facl(fd, cmd, nentries, aclbufp);
618         }
619
620         return real_facl(fd, cmd, nentries, aclbufp);
621 }
622 #endif
623
624  int creat(char *path, mode_t mode)
625 {
626         extern int creat_bits;
627         return open(path, creat_bits, mode);
628 }
629
630 #ifdef HAVE_CREAT64
631  int creat64(char *path, mode_t mode)
632 {
633         extern int creat_bits;
634         return open64(path, creat_bits, mode);
635 }
636 #endif
637
638 #ifdef HAVE_STAT64
639   int stat64(char *name, void *st64)
640 {
641         if (smbw_path(name)) {
642                 double xx[32];
643                 int ret = stat(name, xx);
644                 stat64_convert(xx, st64);
645                 return ret;
646         }
647         return real_stat64(name, st64);
648 }
649
650   int fstat64(int fd, void *st64)
651 {
652         if (smbw_fd(fd)) {
653                 double xx[32];
654                 int ret = fstat(fd, xx);
655                 stat64_convert(xx, st64);
656                 return ret;
657         }
658         return real_fstat64(fd, st64);
659 }
660
661   int lstat64(char *name, void *st64)
662 {
663         if (smbw_path(name)) {
664                 double xx[32];
665                 int ret = lstat(name, xx);
666                 stat64_convert(xx, st64);
667                 return ret;
668         }
669         return real_lstat64(name, st64);
670 }
671 #endif
672
673 #ifdef HAVE_LLSEEK
674   offset_t llseek(int fd, offset_t ofs, int whence)
675 {
676         if (smbw_fd(fd)) {
677                 return lseek(fd, ofs, whence);
678         }
679         return real_llseek(fd, ofs, whence);
680 }
681 #endif
682
683 #ifdef HAVE_READDIR64
684  void *readdir64(void *dir)
685 {
686         if (smbw_dirp(dir)) {
687                 static double xx[70];
688                 void *d;
689                 d = (void *)readdir(dir);
690                 if (!d) return (void *)0;
691                 dirent64_convert(d, xx);
692                 return xx;
693         }
694         return (void *)real_readdir64(dir);
695 }
696 #endif
697
698  int fork(void)
699 {
700         return smbw_fork();
701 }