d8a6a6fdac8f66612b7db34ad9728a7211d63b9c
[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
23 #include "wrapper.h"
24
25 #ifdef linux
26 __asm__(".globl __open; __open = open");
27 #endif
28
29  int open(const char *name, int flags, mode_t mode)
30 {
31         if (smbw_path(name)) {
32                 return smbw_open(name, flags, mode);
33         }
34
35         return real_open(name, flags, mode);
36 }
37
38
39 #ifdef linux
40 __asm__(".globl __chdir; __chdir = chdir");
41 #endif
42
43  int chdir(const char *name)
44 {
45         return smbw_chdir(name);
46 }
47
48
49
50 #ifdef linux
51 __asm__(".globl __close; __close = close");
52 #endif
53
54  ssize_t close(int fd)
55 {
56         if (smbw_fd(fd)) {
57                 return smbw_close(fd);
58         }
59
60         return real_close(fd);
61 }
62
63
64 #ifdef linux
65 __asm__(".globl __fchdir; __fchdir = fchdir");
66 #endif
67
68  int fchdir(int fd)
69 {
70         if (smbw_fd(fd)) {
71                 return smbw_fchdir(fd);
72         }
73
74         return real_fchdir(fd);
75 }
76
77
78 #ifdef linux
79 __asm__(".globl __fcntl; __fcntl = fcntl");
80 #endif
81
82  int fcntl(int fd, int cmd, long arg)
83 {
84         if (smbw_fd(fd)) {
85                 return smbw_fcntl(fd, cmd, arg);
86         }
87
88         return real_fcntl(fd, cmd, arg);
89 }
90
91
92
93 #ifdef linux
94 __asm__(".globl __getdents; __getdents = getdents");
95 #endif
96
97  int getdents(int fd, struct dirent *dirp, unsigned int count)
98 {
99         if (smbw_fd(fd)) {
100                 return smbw_getdents(fd, dirp, count);
101         }
102
103         return real_getdents(fd, dirp, count);
104 }
105
106
107 #ifdef linux
108 __asm__(".globl __lseek; __lseek = lseek");
109 #endif
110
111  ssize_t lseek(int fd, off_t offset, int whence)
112 {
113         if (smbw_fd(fd)) {
114                 return smbw_lseek(fd, offset, whence);
115         }
116
117         return real_lseek(fd, offset, whence);
118 }
119
120
121
122 #ifdef linux
123 __asm__(".globl __read; __read = read");
124 #endif
125
126  ssize_t read(int fd, void *buf, size_t count)
127 {
128         if (smbw_fd(fd)) {
129                 return smbw_read(fd, buf, count);
130         }
131
132         return real_read(fd, buf, count);
133 }
134
135
136 #ifdef linux
137 __asm__(".globl __write; __write = write");
138 #endif
139
140  ssize_t write(int fd, void *buf, size_t count)
141 {
142         if (smbw_fd(fd)) {
143                 return smbw_write(fd, buf, count);
144         }
145
146         return real_write(fd, buf, count);
147 }
148
149
150  int access(const char *name, int mode)
151 {
152         if (smbw_path(name)) {
153                 return smbw_access(name, mode);
154         }
155
156         return real_access(name, mode);
157 }
158
159
160
161  int chmod(const char *name,mode_t mode)
162 {
163         if (smbw_path(name)) {
164                 return smbw_chmod(name, mode);
165         }
166
167         return real_chmod(name, mode);
168 }
169
170
171
172  int chown(const char *name,uid_t owner, gid_t group)
173 {
174         if (smbw_path(name)) {
175                 return smbw_chown(name, owner, group);
176         }
177
178         return real_chown(name, owner, group);
179 }
180
181 #ifdef LINUX
182  int __fxstat(int vers, int fd, struct stat *st)
183 {
184         struct kernel_stat kbuf;
185         int ret;
186
187         if (smbw_fd(fd)) {
188                 return smbw_fstat(fd, st);
189         }
190
191         switch (vers) {
192         case _STAT_VER_LINUX_OLD:
193                 /* Nothing to do.  The struct is in the form the kernel expects
194                    it to be.  */
195                 return real_fstat(fd, (struct kernel_stat *)st);
196                 break;
197
198         case _STAT_VER_LINUX:
199                 /* Do the system call.  */
200                 ret = real_fstat(fd, &kbuf);
201
202                 st->st_dev = kbuf.st_dev;
203 #ifdef _HAVE___PAD1
204                 st->__pad1 = 0;
205 #endif
206                 st->st_ino = kbuf.st_ino;
207                 st->st_mode = kbuf.st_mode;
208                 st->st_nlink = kbuf.st_nlink;
209                 st->st_uid = kbuf.st_uid;
210                 st->st_gid = kbuf.st_gid;
211                 st->st_rdev = kbuf.st_rdev;
212 #ifdef _HAVE___PAD2
213                 st->__pad2 = 0;
214 #endif
215                 st->st_size = kbuf.st_size;
216                 st->st_blksize = kbuf.st_blksize;
217                 st->st_blocks = kbuf.st_blocks;
218                 st->st_atime = kbuf.st_atime;
219 #ifdef _HAVE___UNUSED1
220                 st->__unused1 = 0;
221 #endif
222                 st->st_mtime = kbuf.st_mtime;
223 #ifdef _HAVE___UNUSED2
224                 st->__unused2 = 0;
225 #endif
226                 st->st_ctime = kbuf.st_ctime;
227 #ifdef _HAVE___UNUSED3
228                 st->__unused3 = 0;
229 #endif
230 #ifdef _HAVE___UNUSED4
231                 st->__unused4 = 0;
232 #endif
233 #ifdef _HAVE___UNUSED5
234                 st->__unused5 = 0;
235 #endif
236                 return ret;
237
238         default:
239                 errno = EINVAL;
240                 return -1;
241         }
242 }
243 #endif
244
245
246  char *getcwd(char *buf, size_t size)
247 {
248         return smbw_getcwd(buf, size);
249 }
250
251
252 #ifdef LINUX
253  int __lxstat(int vers, const char *name, struct stat *st)
254 {
255         struct kernel_stat kbuf;
256         int ret;
257
258         if (smbw_path(name)) {
259                 return smbw_stat(name, st);
260         }
261
262         switch (vers) {
263         case _STAT_VER_LINUX_OLD:
264                 /* Nothing to do.  The struct is in the form the kernel expects
265                    it to be.  */
266                 return real_lstat(name, (struct kernel_stat *)st);
267                 break;
268
269         case _STAT_VER_LINUX:
270                 /* Do the system call.  */
271                 ret = real_lstat(name, &kbuf);
272
273                 st->st_dev = kbuf.st_dev;
274 #ifdef _HAVE___PAD1
275                 st->__pad1 = 0;
276 #endif
277                 st->st_ino = kbuf.st_ino;
278                 st->st_mode = kbuf.st_mode;
279                 st->st_nlink = kbuf.st_nlink;
280                 st->st_uid = kbuf.st_uid;
281                 st->st_gid = kbuf.st_gid;
282                 st->st_rdev = kbuf.st_rdev;
283 #ifdef _HAVE___PAD2
284                 st->__pad2 = 0;
285 #endif
286                 st->st_size = kbuf.st_size;
287                 st->st_blksize = kbuf.st_blksize;
288                 st->st_blocks = kbuf.st_blocks;
289                 st->st_atime = kbuf.st_atime;
290 #ifdef _HAVE___UNUSED1
291                 st->__unused1 = 0;
292 #endif
293                 st->st_mtime = kbuf.st_mtime;
294 #ifdef _HAVE___UNUSED2
295                 st->__unused2 = 0;
296 #endif
297                 st->st_ctime = kbuf.st_ctime;
298 #ifdef _HAVE___UNUSED3
299                 st->__unused3 = 0;
300 #endif
301 #ifdef _HAVE___UNUSED4
302                 st->__unused4 = 0;
303 #endif
304 #ifdef _HAVE___UNUSED5
305                 st->__unused5 = 0;
306 #endif
307                 return ret;
308
309         default:
310                 errno = EINVAL;
311                 return -1;
312         }
313 }
314 #endif
315
316
317  int mkdir(const char *name, mode_t mode)
318 {
319         if (smbw_path(name)) {
320                 return smbw_mkdir(name, mode);
321         }
322
323         return real_mkdir(name, mode);
324 }
325
326
327 #ifdef LINUX
328  int __xstat(int vers, const char *name, struct stat *st)
329 {
330         struct kernel_stat kbuf;
331         int ret;
332
333         if (smbw_path(name)) {
334                 return smbw_stat(name, st);
335         }
336
337         switch (vers) {
338         case _STAT_VER_LINUX_OLD:
339                 /* Nothing to do.  The struct is in the form the kernel expects
340                    it to be.  */
341                 return real_stat(name, (struct kernel_stat *)st);
342                 break;
343
344         case _STAT_VER_LINUX:
345                 /* Do the system call.  */
346                 ret = real_stat(name, &kbuf);
347
348                 st->st_dev = kbuf.st_dev;
349 #ifdef _HAVE___PAD1
350                 st->__pad1 = 0;
351 #endif
352                 st->st_ino = kbuf.st_ino;
353                 st->st_mode = kbuf.st_mode;
354                 st->st_nlink = kbuf.st_nlink;
355                 st->st_uid = kbuf.st_uid;
356                 st->st_gid = kbuf.st_gid;
357                 st->st_rdev = kbuf.st_rdev;
358 #ifdef _HAVE___PAD2
359                 st->__pad2 = 0;
360 #endif
361                 st->st_size = kbuf.st_size;
362                 st->st_blksize = kbuf.st_blksize;
363                 st->st_blocks = kbuf.st_blocks;
364                 st->st_atime = kbuf.st_atime;
365 #ifdef _HAVE___UNUSED1
366                 st->__unused1 = 0;
367 #endif
368                 st->st_mtime = kbuf.st_mtime;
369 #ifdef _HAVE___UNUSED2
370                 st->__unused2 = 0;
371 #endif
372                 st->st_ctime = kbuf.st_ctime;
373 #ifdef _HAVE___UNUSED3
374                 st->__unused3 = 0;
375 #endif
376 #ifdef _HAVE___UNUSED4
377                 st->__unused4 = 0;
378 #endif
379 #ifdef _HAVE___UNUSED5
380                 st->__unused5 = 0;
381 #endif
382                 return ret;
383
384         default:
385                 errno = EINVAL;
386                 return -1;
387         }
388 }
389 #endif
390
391  int stat(const char *name, struct stat *st)
392 {
393 #if HAVE___XSTAT
394         return __xstat(_STAT_VER, name, st);
395 #else
396         if (smbw_path(name)) {
397                 return smbw_stat(name, st);
398         }
399         return real_stat(name, st);
400 #endif
401 }
402
403  int lstat(const char *name, struct stat *st)
404 {
405 #if HAVE___LXSTAT
406         return __lxstat(_STAT_VER, name, st);
407 #else
408         if (smbw_path(name)) {
409                 return smbw_stat(name, st);
410         }
411         return real_lstat(name, st);
412 #endif
413 }
414
415  int fstat(int fd, struct stat *st)
416 {
417 #if HAVE___LXSTAT
418         return __fxstat(_STAT_VER, fd, st);
419 #else
420         if (smbw_fd(fd)) {
421                 return smbw_fstat(fd, st);
422         }
423         return real_fstat(fd, st);
424 #endif
425 }
426
427
428  int unlink(const char *name)
429 {
430         if (smbw_path(name)) {
431                 return smbw_unlink(name);
432         }
433
434         return real_unlink(name);
435 }
436
437
438  int utime(const char *name,void *tvp)
439 {
440         if (smbw_path(name)) {
441                 return smbw_utime(name, tvp);
442         }
443
444         return real_utime(name, tvp);
445 }
446
447  int readlink(char *path, char *buf, size_t bufsize)
448 {
449         if (smbw_path(path)) {
450                 return smbw_readlink(path, buf, bufsize);
451         }
452
453         return real_readlink(path, buf, bufsize);
454 }
455
456
457  int rename(const char *oldname,const char *newname)
458 {
459         int p1, p2;
460         p1 = smbw_path(oldname); 
461         p2 = smbw_path(newname); 
462         if (p1 ^ p2) {
463                 /* can't cross filesystem boundaries */
464                 errno = EXDEV;
465                 return -1;
466         }
467         if (p1 && p2) {
468                 return smbw_rename(oldname, newname);
469         }
470
471         return real_rename(oldname, newname);
472 }
473
474  int rmdir(const char *name)
475 {
476         if (smbw_path(name)) {
477                 return smbw_rmdir(name);
478         }
479
480         return real_rmdir(name);
481 }
482
483
484  int symlink(const char *topath,const char *frompath)
485 {
486         int p1, p2;
487         p1 = smbw_path(topath); 
488         p2 = smbw_path(frompath); 
489         if (p1 || p2) {
490                 /* can't handle symlinks */
491                 errno = EPERM;
492                 return -1;
493         }
494
495         return real_symlink(topath, frompath);
496 }
497
498  int dup(int fd)
499 {
500         if (smbw_fd(fd)) {
501                 return smbw_dup(fd);
502         }
503
504         return real_dup(fd);
505 }
506
507  int dup2(int oldfd, int newfd)
508 {
509         if (smbw_fd(newfd)) {
510                 close(newfd);
511         }
512
513         if (smbw_fd(oldfd)) {
514                 return smbw_dup2(oldfd, newfd);
515         }
516
517         return real_dup2(oldfd, newfd);
518 }
519
520  DIR *opendir(const char *name)
521 {
522         if (smbw_path(name)) {
523                 return smbw_opendir(name);
524         }
525
526         return real_opendir(name);
527 }
528
529
530  struct dirent *readdir(DIR *dir)
531 {
532         if (smbw_dirp(dir)) {
533                 return smbw_readdir(dir);
534         }
535
536         return real_readdir(dir);
537 }
538
539  int closedir(DIR *dir)
540 {
541         if (smbw_dirp(dir)) {
542                 return smbw_closedir(dir);
543         }
544
545         return real_closedir(dir);
546 }
547
548 #ifndef NO_TELLDIR
549  off_t telldir(DIR *dir)
550 {
551         if (smbw_dirp(dir)) {
552                 return smbw_telldir(dir);
553         }
554
555         return real_telldir(dir);
556 }
557 #endif
558
559 #ifndef NO_SEEKDIR
560  void seekdir(DIR *dir, off_t offset)
561 {
562         if (smbw_dirp(dir)) {
563                 smbw_seekdir(dir, offset);
564                 return;
565         }
566
567         real_seekdir(dir, offset);
568 }
569 #endif