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