summaryrefslogtreecommitdiff
path: root/examples/libsmbclient/smbwrapper/wrapper.c
blob: 3811b043566454e7678cf36f8e423ee5994e4e90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
/* 
   Unix SMB/Netbios implementation.
   Version 2.0
   SMB wrapper functions
   Copyright (C) Andrew Tridgell 1998
   Copyright (C) Derrell Lipman 2002-2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * This is a rewrite of the original wrapped.c file, using libdl to obtain
 * pointers into the C library rather than attempting to find undocumented
 * functions in the C library to call for native file access.  The problem
 * with the original implementation's paradigm is that samba manipulates
 * defines such that it gets the sizes of structures that it wants
 * (e.g. mapping 32-bit functions to 64-bit functions with their associated
 * 64-bit structure fields), but programs run under smbsh or using
 * smbwrapper.so were not necessarily compiled with the same flags.  As an
 * example of the problem, a program calling stat() passes a pointer to a
 * "struct stat" but the fields in that structure are different in samba than
 * they are in the calling program if the calling program was not compiled to
 * force stat() to be mapped to stat64().
 *
 * In this version, we provide an interface to each of the native functions,
 * not just the ones that samba is compiled to map to.  We obtain the function
 * pointers from the C library using dlsym(), and for native file operations,
 * directly call the same function that the calling application was
 * requesting.  Since the size of the calling application's structures vary
 * depending on what function was called, we use our own internal structures
 * for passing information to/from the SMB equivalent functions, and map them
 * back to the native structures before returning the result to the caller.
 *
 * This implementation was completed 25 December 2002.
 * Derrell Lipman
 */

/* We do not want auto munging of 32->64 bit names in this file (only) */
#undef _FILE_OFFSET_BITS
#undef _GNU_SOURCE

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <utime.h>
#include <stdio.h>
#include <dirent.h>
#include <signal.h>
#include <stdarg.h>
#include <string.h>
#ifdef __USE_GNU
# define SMBW_USE_GNU
#endif
#define __USE_GNU             /* need this to have RTLD_NEXT defined */
#include <dlfcn.h>
#ifndef SMBW_USE_GNU
# undef __USE_GNU
#endif
#include <errno.h>
#include "libsmbclient.h"
#include "bsd-strlfunc.h"
#include "wrapper.h"

/*
 * Debug bits:
 * 0x0 = none
 * 0x1 = display symbol definitions not found in C library
 * 0x2 = show wrapper functions being called
 * 0x4 = log to file SMBW_DEBUG_FILE instead of stderr
 */
#define SMBW_DEBUG      0x0
#define SMBW_DEBUG_FILE "/tmp/smbw.log"

int      smbw_debug = 0;

#if SMBW_DEBUG & 0x2
static int      debugFd = 2;
#endif

#ifndef ENOTSUP
#define ENOTSUP EOPNOTSUPP
#endif

/*
 * None of the methods of having the initialization function called
 * automatically upon shared library startup are effective in all situations.
 * We provide the "-init" parameter to the linker which is effective most of
 * the time, but fails for applications that provide their own shared
 * libraries with _init() functions (e.g. ps).  We can't use "-z initfirst"
 * because the environment isn't yet set up at that point, so we can't find
 * our shared memory identifier (see shared.c).  We therefore must resort to
 * this tried-and-true method of keeping an "initialized" flag.  We check it
 * prior to calling the initialize() function to save a function call (a slow
 * operation on x86).
 */
#if SMBW_DEBUG & 0x2
#  define check_init(buf)                                               \
        do {                                                            \
                int saved_errno = errno;                                \
                if (! initialized) initialize();                        \
                (* smbw_libc.write)(debugFd, "["buf"]", sizeof(buf)+1); \
                errno = saved_errno;                                    \
        } while (0)
#else
#  define check_init(buf)                               \
        do {                                            \
                if (! initialized) smbw_initialize();   \
        } while (0)
#endif

static void initialize(void);

static int initialized = 0;

SMBW_libc_pointers smbw_libc;

/*
 * A public entry point used by the "-init" option to the linker.
 */
void smbw_initialize(void)
{
        initialize();
}

static void initialize(void)
{
        int saved_errno;
#if SMBW_DEBUG & 0x1
        char *error;
#endif
        
        saved_errno = errno;
        
        if (initialized) {
                return;
        }
        initialized = 1;
        
#if SMBW_DEBUG & 0x1
# define GETSYM(symname, symstring)                                      \
        if ((smbw_libc.symname = dlsym(RTLD_NEXT, symstring)) == NULL) { \
                if (smbw_libc.write != NULL &&                           \
                    (error = dlerror()) != NULL) {                       \
                        (* smbw_libc.write)(1, error, strlen(error));    \
                        (* smbw_libc.write)(1, "\n", 1);                 \
                }                                                        \
        }
#else
# define GETSYM(symname, symstring)                     \
        smbw_libc.symname = dlsym(RTLD_NEXT, symstring);
#endif
        
        /*
         * Get pointers to each of the symbols we'll need, from the C library
         *
         * Some of these symbols may not be found in the C library.  That's
         * fine.  We declare all of them here, and if the C library supports
         * them, they may be called so we have the wrappers for them.  If the
         * C library doesn't support them, then the wrapper function will
         * never be called, and the null pointer will never be dereferenced.
         */
        GETSYM(write, "write"); /* first, to allow debugging */
        GETSYM(open, "open");
        GETSYM(_open, "_open");
        GETSYM(__open, "__open");
        GETSYM(open64, "open64");
        GETSYM(_open64, "_open64");
        GETSYM(__open64, "__open64");
        GETSYM(pread, "pread");
        GETSYM(pread64, "pread64");
        GETSYM(pwrite, "pwrite");
        GETSYM(pwrite64, "pwrite64");
        GETSYM(close, "close");
        GETSYM(__close, "__close");
        GETSYM(_close, "_close");
        GETSYM(fcntl, "fcntl");
        GETSYM(__fcntl, "__fcntl");
        GETSYM(_fcntl, "_fcntl");
        GETSYM(getdents, "getdents");
        GETSYM(__getdents, "__getdents");
        GETSYM(_getdents, "_getdents");
        GETSYM(getdents64, "getdents64");
        GETSYM(lseek, "lseek");
        GETSYM(__lseek, "__lseek");
        GETSYM(_lseek, "_lseek");
        GETSYM(lseek64, "lseek64");
        GETSYM(__lseek64, "__lseek64");
        GETSYM(_lseek64, "_lseek64");
        GETSYM(read, "read");
        GETSYM(__read, "__read");
        GETSYM(_read, "_read");
        GETSYM(__write, "__write");
        GETSYM(_write, "_write");
        GETSYM(access, "access");
        GETSYM(chmod, "chmod");
        GETSYM(fchmod, "fchmod");
        GETSYM(chown, "chown");
        GETSYM(fchown, "fchown");
        GETSYM(__xstat, "__xstat");
        GETSYM(getcwd, "getcwd");
        GETSYM(mkdir, "mkdir");
        GETSYM(__fxstat, "__fxstat");
        GETSYM(__lxstat, "__lxstat");
        GETSYM(stat, "stat");
        GETSYM(lstat, "lstat");
        GETSYM(fstat, "fstat");
        GETSYM(unlink, "unlink");
        GETSYM(utime, "utime");
        GETSYM(utimes, "utimes");
        GETSYM(readlink, "readlink");
        GETSYM(rename, "rename");
        GETSYM(rmdir, "rmdir");
        GETSYM(symlink, "symlink");
        GETSYM(dup, "dup");
        GETSYM(dup2, "dup2");
        GETSYM(opendir, "opendir");
        GETSYM(readdir, "readdir");
        GETSYM(closedir, "closedir");
        GETSYM(telldir, "telldir");
        GETSYM(seekdir, "seekdir");
        GETSYM(creat, "creat");
        GETSYM(creat64, "creat64");
        GETSYM(__xstat64, "__xstat64");
        GETSYM(stat64, "stat64");
        GETSYM(__fxstat64, "__fxstat64");
        GETSYM(fstat64, "fstat64");
        GETSYM(__lxstat64, "__lxstat64");
        GETSYM(lstat64, "lstat64");
        GETSYM(_llseek, "_llseek");
        GETSYM(readdir64, "readdir64");
        GETSYM(readdir_r, "readdir_r");
        GETSYM(readdir64_r, "readdir64_r");
        GETSYM(setxattr, "setxattr");
        GETSYM(lsetxattr, "lsetxattr");
        GETSYM(fsetxattr, "fsetxattr");
        GETSYM(getxattr, "getxattr");
        GETSYM(lgetxattr, "lgetxattr");
        GETSYM(fgetxattr, "fgetxattr");
        GETSYM(removexattr, "removexattr");
        GETSYM(lremovexattr, "lremovexattr");
        GETSYM(fremovexattr, "fremovexattr");
        GETSYM(listxattr, "listxattr");
        GETSYM(llistxattr, "llistxattr");
        GETSYM(flistxattr, "flistxattr");
        GETSYM(chdir, "chdir");
        GETSYM(fchdir, "fchdir");
        GETSYM(fork, "fork");
        GETSYM(select, "select");
        GETSYM(_select, "_select");
        GETSYM(__select, "__select");
        
#if SMBW_DEBUG & 4
        {
                if ((debugFd =
                     open(SMBW_DEBUG_FILE, O_WRONLY | O_CREAT | O_APPEND)) < 0)
                {
#               define SMBW_MESSAGE    "Could not create " SMBW_DEBUG_FILE "\n" 
                        (* smbw_libc.write)(1, SMBW_MESSAGE, sizeof(SMBW_MESSAGE));
#               undef SMBW_MESSAGE
                        exit(1);
                }
        }
#endif
        
        errno = saved_errno;
}

/**
 ** Static Functions
 **/

static void stat_convert(struct SMBW_stat *src, struct stat *dest)
{
        memset(dest, '\0', sizeof(*dest));
	dest->st_size = src->s_size;
	dest->st_mode = src->s_mode;
	dest->st_ino = src->s_ino;
	dest->st_dev = src->s_dev;
	dest->st_rdev = src->s_rdev;
	dest->st_nlink = src->s_nlink;
	dest->st_uid = src->s_uid;
	dest->st_gid = src->s_gid;
	dest->st_atime = src->s_atime;
	dest->st_mtime = src->s_mtime;
	dest->st_ctime = src->s_ctime;
	dest->st_blksize = src->s_blksize;
	dest->st_blocks = src->s_blocks;
}

static void stat64_convert(struct SMBW_stat *src, struct stat64 *dest)
{
        memset(dest, '\0', sizeof(*dest));
	dest->st_size = src->s_size;
	dest->st_mode = src->s_mode;
	dest->st_ino = src->s_ino;
	dest->st_dev = src->s_dev;
	dest->st_rdev = src->s_rdev;
	dest->st_nlink = src->s_nlink;
	dest->st_uid = src->s_uid;
	dest->st_gid = src->s_gid;
	dest->st_atime = src->s_atime;
	dest->st_mtime = src->s_mtime;
	dest->st_ctime = src->s_ctime;
	dest->st_blksize = src->s_blksize;
	dest->st_blocks = src->s_blocks;
}

static void dirent_convert(struct SMBW_dirent *src, struct dirent *dest)
{
        char *p;
        
        memset(dest, '\0', sizeof(*dest));
	dest->d_ino = src->d_ino;
	dest->d_off = src->d_off;
        
        switch(src->d_type)
        {
        case SMBC_WORKGROUP:
        case SMBC_SERVER:
        case SMBC_FILE_SHARE:
        case SMBC_DIR:
                dest->d_type = DT_DIR;
                break;
                
        case SMBC_FILE:
                dest->d_type = DT_REG;
                break;
                
        case SMBC_PRINTER_SHARE:
                dest->d_type = DT_CHR;
                break;
                
        case SMBC_COMMS_SHARE:
                dest->d_type = DT_SOCK;
                break;
                
        case SMBC_IPC_SHARE:
                dest->d_type = DT_FIFO;
                break;
                
        case SMBC_LINK:
                dest->d_type = DT_LNK;
                break;
        }
        
	dest->d_reclen = src->d_reclen;
	smbw_strlcpy(dest->d_name, src->d_name, sizeof(dest->d_name));
        p = dest->d_name + strlen(dest->d_name) + 1;
        smbw_strlcpy(p,
                     src->d_comment,
                     sizeof(dest->d_name) - (p - dest->d_name));
}

static void dirent64_convert(struct SMBW_dirent *src, struct dirent64 *dest)
{
        char *p;
        
        memset(dest, '\0', sizeof(*dest));
	dest->d_ino = src->d_ino;
	dest->d_off = src->d_off;
        
        switch(src->d_type)
        {
        case SMBC_WORKGROUP:
        case SMBC_SERVER:
        case SMBC_FILE_SHARE:
        case SMBC_DIR:
                dest->d_type = DT_DIR;
                break;
                
        case SMBC_FILE:
                dest->d_type = DT_REG;
                break;
                
        case SMBC_PRINTER_SHARE:
                dest->d_type = DT_CHR;
                break;
                
        case SMBC_COMMS_SHARE:
                dest->d_type = DT_SOCK;
                break;
                
        case SMBC_IPC_SHARE:
                dest->d_type = DT_FIFO;
                break;
                
        case SMBC_LINK:
                dest->d_type = DT_LNK;
                break;
        }
        
	dest->d_reclen = src->d_reclen;
	smbw_strlcpy(dest->d_name, src->d_name, sizeof(dest->d_name));
        p = dest->d_name + strlen(dest->d_name) + 1;
        smbw_strlcpy(p,
                     src->d_comment,
                     sizeof(dest->d_name) - (p - dest->d_name));
}

static int openx(char *name, int flags, mode_t mode, int (* f)(char *, int, mode_t))
{
	if (smbw_path(name)) {
		return smbw_open(name, flags, mode);
	}
        
        return (* f)(name, flags, mode);
}

static int closex(int fd, int (* f)(int fd))
{
	if (smbw_fd(fd)) {
		return smbw_close(fd);
	}
        
        return (* f)(fd);
}

static int fcntlx(int fd, int cmd, long arg, int (* f)(int, int, long))
{
	if (smbw_fd(fd)) {
		return smbw_fcntl(fd, cmd, arg);
	}
        
        return (* f)(fd, cmd, arg);
}

static int getdentsx(int fd, struct dirent *external, unsigned int count, int (* f)(int, struct dirent *, unsigned int))
{
	if (smbw_fd(fd)) {
                int i;
                int internal_count;
                struct SMBW_dirent *internal;
                int ret;
                int n;
                
                /*
                 * LIMITATION: If they pass a count which is not a multiple of
                 * the size of struct dirent, they will not get a partial
                 * structure; we ignore the excess count.
                 */
                n = (count / sizeof(struct dirent));
                
                internal_count = sizeof(struct SMBW_dirent) * n;
                internal = malloc(internal_count);
                if (internal == NULL) {
                        errno = ENOMEM;
                        return -1;
                }
		ret = smbw_getdents(fd, internal, internal_count);
                if (ret <= 0)
                        return ret;
                
                ret = sizeof(struct dirent) * n;
                
                for (i = 0; i < n; i++)
                        dirent_convert(&internal[i], &external[i]);
                
                return ret;
	}
        
        return (* f)(fd, external, count);
}

static off_t lseekx(int fd,
                    off_t offset,
                    int whence,
                    off_t (* f)(int, off_t, int))
{
        off_t           ret;
        
        /*
         * We have left the definitions of the smbw_ functions undefined,
         * because types such as off_t can differ in meaning betweent his
         * function and smbw.c et al.  Functions that return other than an
         * integer value, however, MUST have their return value defined.
         */
        off64_t         smbw_lseek();
        
        if (smbw_fd(fd)) {
		return (off_t) smbw_lseek(fd, offset, whence);
	}
        
        ret = (* f)(fd, offset, whence);
        if (smbw_debug)
        {
                printf("lseekx(%d, 0x%llx) returned 0x%llx\n",
                       fd,
                       (unsigned long long) offset,
                       (unsigned long long) ret);
        }
        return ret;
}

static off64_t lseek64x(int fd,
                        off64_t offset,
                        int whence,
                        off64_t (* f)(int, off64_t, int))
{
        off64_t         ret;
        
        /*
         * We have left the definitions of the smbw_ functions undefined,
         * because types such as off_t can differ in meaning betweent his
         * function and smbw.c et al.  Functions that return other than an
         * integer value, however, MUST have their return value defined.
         */
        off64_t         smbw_lseek();
        
	if (smbw_fd(fd))
		ret = smbw_lseek(fd, offset, whence);
        else
                ret = (* f)(fd, offset, whence);
        if (smbw_debug)
        {
                printf("lseek64x(%d, 0x%llx) returned 0x%llx\n",
                       fd,
                       (unsigned long long) offset,
                       (unsigned long long) ret);
        }
        return ret;
}

static ssize_t readx(int fd, void *buf, size_t count, ssize_t (* f)(int, void *, size_t))
{
	if (smbw_fd(fd)) {
		return smbw_read(fd, buf, count);
	}
        
        return (* f)(fd, buf, count);
}

static ssize_t writex(int fd, void *buf, size_t count, ssize_t (* f)(int, void *, size_t))
{
	if (smbw_fd(fd)) {
		return smbw_write(fd, buf, count);
	}
        
        return (* f)(fd, buf, count);
}


/**
 ** Wrapper Functions
 **/

int open(__const char *name, int flags, ...)
{
        va_list ap;
        mode_t mode;
        
        va_start(ap, flags);
        mode = va_arg(ap, mode_t);
        va_end(ap);
        
        check_init("open");
        
        return openx((char *) name, flags, mode, smbw_libc.open);
}

int _open(char *name, int flags, mode_t mode)
{
        check_init("open");
        
        return openx(name, flags, mode, smbw_libc._open);
}

int __open(char *name, int flags, mode_t mode)
{
        check_init("open");
        
        return openx(name, flags, mode, smbw_libc.__open);
}

int open64 (__const char *name, int flags, ...)
{
        va_list ap;
        mode_t mode;
        
        va_start(ap, flags);
        mode = va_arg(ap, mode_t);
        va_end(ap);
        
        check_init("open64");
        return openx((char *) name, flags, mode, smbw_libc.open64);
}

int _open64(char *name, int flags, mode_t mode)
{
        check_init("_open64");
        return openx(name, flags, mode, smbw_libc._open64);
}

int __open64(char *name, int flags, mode_t mode)
{
        check_init("__open64");
        return openx(name, flags, mode, smbw_libc.__open64);
}

ssize_t pread(int fd, void *buf, size_t size, off_t ofs)
{
        check_init("pread");
        
	if (smbw_fd(fd)) {
		return smbw_pread(fd, buf, size, ofs);
	}
        
        return (* smbw_libc.pread)(fd, buf, size, ofs);
}

ssize_t pread64(int fd, void *buf, size_t size, off64_t ofs)
{
        check_init("pread64");
        
	if (smbw_fd(fd)) {
		return smbw_pread(fd, buf, size, (off_t) ofs);
	}
        
        return (* smbw_libc.pread64)(fd, buf, size, ofs);
}

ssize_t pwrite(int fd, const void *buf, size_t size, off_t ofs)
{
        check_init("pwrite");
        
	if (smbw_fd(fd)) {
		return smbw_pwrite(fd, (void *) buf, size, ofs);
	}
        
        return (* smbw_libc.pwrite)(fd, (void *) buf, size, ofs);
}

ssize_t pwrite64(int fd,  const void *buf, size_t size, off64_t ofs)
{
        check_init("pwrite64");
        
	if (smbw_fd(fd)) {
		return smbw_pwrite(fd, (void *) buf, size, (off_t) ofs);
	}
        
        return (* smbw_libc.pwrite64)(fd, (void *) buf, size, ofs);
}

int chdir(const char *name)
{
        check_init("chdir");
        return smbw_chdir((char *) name);;
}

int __chdir(char *name)
{
        check_init("__chdir");
        return smbw_chdir(name);
}

int _chdir(char *name)
{
        check_init("_chdir");
        return smbw_chdir(name);
}

int close(int fd)
{
        check_init("close");
        return closex(fd, smbw_libc.close);
}

int __close(int fd)
{
        check_init("__close");
        return closex(fd, smbw_libc.__close);
}

int _close(int fd)
{
        check_init("_close");
        return closex(fd, smbw_libc._close);
}

int fchdir(int fd)
{
        check_init("fchdir");
	return smbw_fchdir(fd);
}

int __fchdir(int fd)
{
        check_init("__fchdir");
	return fchdir(fd);
}

int _fchdir(int fd)
{
        check_init("_fchdir");
	return fchdir(fd);
}

int fcntl (int fd, int cmd, ...)
{
        va_list ap;
        long arg;
        
        va_start(ap, cmd);
        arg = va_arg(ap, long);
        va_end(ap);
        
        check_init("fcntl");
        return fcntlx(fd, cmd, arg, smbw_libc.fcntl);
}

int __fcntl(int fd, int cmd, ...)
{
        va_list ap;
        long arg;
        
        va_start(ap, cmd);
        arg = va_arg(ap, long);
        va_end(ap);
        
        check_init("__fcntl");
        return fcntlx(fd, cmd, arg, smbw_libc.__fcntl);
}

int _fcntl(int fd, int cmd, ...)
{
        va_list ap;
        long arg;
        
        va_start(ap, cmd);
        arg = va_arg(ap, long);
        va_end(ap);
        
        check_init("_fcntl");
        return fcntlx(fd, cmd, arg, smbw_libc._fcntl);
}

int getdents(int fd, struct dirent *dirp, unsigned int count)
{
        check_init("getdents");
        return getdentsx(fd, dirp, count, smbw_libc.getdents);
}

int __getdents(int fd, struct dirent *dirp, unsigned int count)
{
        check_init("__getdents");
        return getdentsx(fd, dirp, count, smbw_libc.__getdents);
}

int _getdents(int fd, struct dirent *dirp, unsigned int count)
{
        check_init("_getdents");
        return getdentsx(fd, dirp, count, smbw_libc._getdents);
}

int getdents64(int fd, struct dirent64 *external, unsigned int count)
{
        check_init("getdents64");
	if (smbw_fd(fd)) {
                int i;
                struct SMBW_dirent *internal;
                int ret;
                int n;
                
                /*
                 * LIMITATION: If they pass a count which is not a multiple of
                 * the size of struct dirent, they will not get a partial
                 * structure; we ignore the excess count.
                 */
                n = (count / sizeof(struct dirent64));
                
                internal = malloc(sizeof(struct SMBW_dirent) * n);
                if (internal == NULL) {
                        errno = ENOMEM;
                        return -1;
                }
		ret = smbw_getdents(fd, internal, count);
                if (ret <= 0)
                        return ret;
                
                ret = sizeof(struct dirent) * count;
                
                for (i = 0; count; i++, count--)
                        dirent64_convert(&internal[i], &external[i]);
                
                return ret;
	}
        
        return (* smbw_libc.getdents64)(fd, external, count);
}

off_t lseek(int fd, off_t offset, int whence)
{
        off_t           ret;
        check_init("lseek");
        ret = lseekx(fd, offset, whence, smbw_libc.lseek);
        if (smbw_debug)
        {
                printf("lseek(%d, 0x%llx) returned 0x%llx\n",
                       fd,
                       (unsigned long long) offset,
                       (unsigned long long) ret);
        }
        return ret;
}

off_t __lseek(int fd, off_t offset, int whence)
{
        off_t           ret;
        check_init("__lseek");
        ret = lseekx(fd, offset, whence, smbw_libc.__lseek);
        if (smbw_debug)
        {
                printf("__lseek(%d, 0x%llx) returned 0x%llx\n",
                       fd,
                       (unsigned long long) offset,
                       (unsigned long long) ret);
        }
        return ret;
}

off_t _lseek(int fd, off_t offset, int whence)
{
        off_t           ret;
        check_init("_lseek");
        ret = lseekx(fd, offset, whence, smbw_libc._lseek);
        if (smbw_debug)
        {
                printf("_lseek(%d, 0x%llx) returned 0x%llx\n",
                       fd,
                       (unsigned long long) offset,
                       (unsigned long long) ret);
        }
        return ret;
}

off64_t lseek64(int fd, off64_t offset, int whence)
{
        off64_t         ret;
        check_init("lseek64");
        ret = lseek64x(fd, offset, whence, smbw_libc.lseek64);
        if (smbw_debug)
        {
                printf("lseek64(%d, 0x%llx) returned 0x%llx\n",
                       fd,
                       (unsigned long long) offset,
                       (unsigned long long) ret);
        }
        return ret;
}

off64_t __lseek64(int fd, off64_t offset, int whence)
{
        check_init("__lseek64");
        return lseek64x(fd, offset, whence, smbw_libc.__lseek64);
}

off64_t _lseek64(int fd, off64_t offset, int whence)
{
        off64_t         ret;
        check_init("_lseek64");
        ret = lseek64x(fd, offset, whence, smbw_libc._lseek64);
        if (smbw_debug)
        {
                printf("_lseek64(%d, 0x%llx) returned 0x%llx\n",
                       fd,
                       (unsigned long long) offset,
                       (unsigned long long) ret);
        }
        return ret;
}

ssize_t read(int fd, void *buf, size_t count)
{
        check_init("read");
        return readx(fd, buf, count, smbw_libc.read);
}

ssize_t __read(int fd, void *buf, size_t count)
{
        check_init("__read");
        return readx(fd, buf, count, smbw_libc.__read);
}

ssize_t _read(int fd, void *buf, size_t count)
{
        check_init("_read");
        return readx(fd, buf, count, smbw_libc._read);
}

ssize_t write(int fd, const void *buf, size_t count)
{
        check_init("write");
        return writex(fd, (void *) buf, count, smbw_libc.write);
}

ssize_t __write(int fd, const void *buf, size_t count)
{
        check_init("__write");
        return writex(fd, (void *) buf, count, smbw_libc.__write);
}

ssize_t _write(int fd, const void *buf, size_t count)
{
        check_init("_write");
        return writex(fd, (void *) buf, count, smbw_libc._write);
}

int access(const char *name, int mode)
{
        check_init("access");
        
	if (smbw_path((char *) name)) {
		return smbw_access((char *) name, mode);
	}
        
        return (* smbw_libc.access)((char *) name, mode);
}

int chmod(const char *name, mode_t mode)
{
        check_init("chmod");
        
	if (smbw_path((char *) name)) {
		return smbw_chmod((char *) name, mode);
	}
        
        return (* smbw_libc.chmod)((char *) name, mode);
}

int fchmod(int fd, mode_t mode)
{
        check_init("fchmod");
        
	if (smbw_fd(fd)) {
                /* Not yet implemented in libsmbclient */
                return ENOTSUP;
	}
        
        return (* smbw_libc.fchmod)(fd, mode);
}

int chown(const char *name, uid_t owner, gid_t group)
{
        check_init("chown");
        
	if (smbw_path((char *) name)) {
		return smbw_chown((char *) name, owner, group);
	}
        
        return (* smbw_libc.chown)((char *) name, owner, group);
}

int fchown(int fd, uid_t owner, gid_t group)
{
        check_init("fchown");
        
	if (smbw_fd(fd)) {
                /* Not yet implemented in libsmbclient */
                return ENOTSUP;
	}
        
        return (* smbw_libc.fchown)(fd, owner, group);
}

char *getcwd(char *buf, size_t size)
{
        check_init("getcwd");
	return (char *)smbw_getcwd(buf, size);
}

int mkdir(const char *name, mode_t mode)
{
        check_init("mkdir");
        
	if (smbw_path((char *) name)) {
		return smbw_mkdir((char *) name, mode);
	}
        
        return (* smbw_libc.mkdir)((char *) name, mode);
}

int __fxstat(int vers, int fd, struct stat *st)
{
        check_init("__fxstat");
        
	if (smbw_fd(fd)) {
                struct SMBW_stat statbuf;
		int ret = smbw_fstat(fd, &statbuf);
                stat_convert(&statbuf, st);
                return ret;
	}
        
        return (* smbw_libc.__fxstat)(vers, fd, st);
}

int __xstat(int vers, const char *name, struct stat *st)
{
        check_init("__xstat");
        
	if (smbw_path((char *) name)) {
                struct SMBW_stat statbuf;
		int ret = smbw_stat((char *) name, &statbuf);
                stat_convert(&statbuf, st);
                return ret;
	}
        
        return (* smbw_libc.__xstat)(vers, (char *) name, st);
}

int __lxstat(int vers, const char *name, struct stat *st)
{
        check_init("__lxstat");
        
	if (smbw_path((char *) name)) {
                struct SMBW_stat statbuf;
		int ret = smbw_stat((char *) name, &statbuf);
                stat_convert(&statbuf, st);
                return ret;
	}
        
        return (* smbw_libc.__lxstat)(vers, (char *) name, st);
}

int stat(const char *name, struct stat *st)
{
        check_init("stat");
        
	if (smbw_path((char *) name)) {
                struct SMBW_stat statbuf;
		int ret = smbw_stat((char *) name, &statbuf);
                stat_convert(&statbuf, st);
                return ret;
	}
        
        return (* smbw_libc.stat)((char *) name, st);
}

int lstat(const char *name, struct stat *st)
{
        check_init("lstat");
        
	if (smbw_path((char *) name)) {
                struct SMBW_stat statbuf;
                int ret = smbw_stat((char *) name, &statbuf);
                stat_convert(&statbuf, st);
                return ret;
	}
        
        return (* smbw_libc.lstat)((char *) name, st);
}

int fstat(int fd, struct stat *st)
{
        check_init("fstat");
        
	if (smbw_fd(fd)) {
                struct SMBW_stat statbuf;
		int ret = smbw_fstat(fd, &statbuf);
                stat_convert(&statbuf, st);
                return ret;
	}
        
        return (* smbw_libc.fstat)(fd, st);
}

int unlink(const char *name)
{
        check_init("unlink");
        
	if (smbw_path((char *) name)) {
		return smbw_unlink((char *) name);
	}
        
        return (* smbw_libc.unlink)((char *) name);
}

int utime(const char *name, const struct utimbuf *tvp)
{
        check_init("utime");
        
	if (smbw_path(name)) {
		return smbw_utime(name, (struct utimbuf *) tvp);
	}
        
        return (* smbw_libc.utime)((char *) name, (struct utimbuf *) tvp);
}

int utimes(const char *name, const struct timeval *tvp)
{
        check_init("utimes");
        
	if (smbw_path(name)) {
		return smbw_utimes(name, (struct timeval *) tvp);
	}
        
        return (* smbw_libc.utimes)((char *) name, (struct timeval *) tvp);
}

ssize_t readlink(const char *path, char *buf, size_t bufsize)
{
        check_init("readlink");
        
	if (smbw_path((char *) path)) {
		return smbw_readlink(path, (char *) buf, bufsize);
	}
        
        return (* smbw_libc.readlink)((char *) path, buf, bufsize);
}

int rename(const char *oldname, const char *newname)
{
	int p1, p2;
        
        check_init("rename");
        
	p1 = smbw_path((char *) oldname); 
	p2 = smbw_path((char *) newname); 
	if (p1 ^ p2) {
		/* can't cross filesystem boundaries */
		errno = EXDEV;
		return -1;
	}
	if (p1 && p2) {
		return smbw_rename((char *) oldname, (char *) newname);
	}
        
        return (* smbw_libc.rename)((char *) oldname, (char *) newname);
}

int rmdir(const char *name)
{
        check_init("rmdir");
        
	if (smbw_path((char *) name)) {
		return smbw_rmdir((char *) name);
	}
        
        return (* smbw_libc.rmdir)((char *) name);
}

int symlink(const char *topath, const char *frompath)
{
	int p1, p2;
        
        check_init("symlink");
        
	p1 = smbw_path((char *) topath); 
	p2 = smbw_path((char *) frompath); 
	if (p1 || p2) {
		/* can't handle symlinks */
		errno = EPERM;
		return -1;
	}
        
        return (* smbw_libc.symlink)((char *) topath, (char *) frompath);
}

int dup(int fd)
{
        check_init("dup");
        
	if (smbw_fd(fd)) {
		return smbw_dup(fd);
	}
        
        return (* smbw_libc.dup)(fd);
}

int dup2(int oldfd, int newfd)
{
        check_init("dup2");
        
	if (smbw_fd(newfd)) {
		(* smbw_libc.close)(newfd);
	}
        
	if (smbw_fd(oldfd)) {
		return smbw_dup2(oldfd, newfd);
	}
        
        return (* smbw_libc.dup2)(oldfd, newfd);
}


DIR *opendir(const char *name)
{
        check_init("opendir");
        
	if (smbw_path((char *) name)) {
		return (void *)smbw_opendir((char *) name);
	}
        
        return (* smbw_libc.opendir)((char *) name);
}

struct dirent *readdir(DIR *dir)
{
        check_init("readdir");
        
	if (smbw_dirp(dir)) {
                static struct dirent external;
                struct SMBW_dirent * internal = (void *)smbw_readdir(dir);
                if (internal != NULL) {
                        dirent_convert(internal, &external);
                        return &external;
                }
                return NULL;
	}
        return (* smbw_libc.readdir)(dir);
}

int closedir(DIR *dir)
{
        check_init("closedir");
        
	if (smbw_dirp(dir)) {
		return smbw_closedir(dir);
	}
        
        return (* smbw_libc.closedir)(dir);
}

long telldir(DIR *dir)
{
        check_init("telldir");
        
	if (smbw_dirp(dir)) {
		return (long) smbw_telldir(dir);
	}
        
        return (* smbw_libc.telldir)(dir);
}

void seekdir(DIR *dir, long offset)
{
        check_init("seekdir");
        
	if (smbw_dirp(dir)) {
		smbw_seekdir(dir, (long long) offset);
		return;
	}
        
        (* smbw_libc.seekdir)(dir, offset);
}

int creat(const char *path, mode_t mode)
{
	extern int creat_bits;
        
        check_init("creat");
	return openx((char *) path, creat_bits, mode, smbw_libc.open);
}

int creat64(const char *path, mode_t mode)
{
	extern int creat_bits;
        
        check_init("creat64");
	return openx((char *) path, creat_bits, mode, smbw_libc.open64);
}

int __xstat64 (int ver, const char *name, struct stat64 *st64)
{
        check_init("__xstat64");
        
	if (smbw_path((char *) name)) {
                struct SMBW_stat statbuf;
		int ret = smbw_stat((char *) name, &statbuf);
		stat64_convert(&statbuf, st64);
		return ret;
	}
        
        return (* smbw_libc.__xstat64)(ver, (char *) name, st64);
}

int stat64(const char *name, struct stat64 *st64)
{
        check_init("stat64");
        
	if (smbw_path((char *) name)) {
                struct SMBW_stat statbuf;
		int ret = smbw_stat((char *) name, &statbuf);
		stat64_convert(&statbuf, st64);
		return ret;
	}
        
        return (* smbw_libc.stat64)((char *) name, st64);
}

int __fxstat64(int ver, int fd, struct stat64 *st64)
{
        check_init("__fxstat64");
        
	if (smbw_fd(fd)) {
                struct SMBW_stat statbuf;
		int ret = smbw_fstat(fd, &statbuf);
		stat64_convert(&statbuf, st64);
		return ret;
	}
        
        return (* smbw_libc.__fxstat64)(ver, fd, st64);
}

int fstat64(int fd, struct stat64 *st64)
{
        check_init("fstat64");
        
	if (smbw_fd(fd)) {
                struct SMBW_stat statbuf;
		int ret = smbw_fstat(fd, &statbuf);
		stat64_convert(&statbuf, st64);
		return ret;
	}
        
        return (* smbw_libc.fstat64)(fd, st64);
}

int __lxstat64(int ver, const char *name, struct stat64 *st64)
{
        check_init("__lxstat64");
        
	if (smbw_path((char *) name)) {
                struct SMBW_stat statbuf;
		int ret = smbw_stat(name, &statbuf);
		stat64_convert(&statbuf, st64);
		return ret;
	}
        
        return (* smbw_libc.__lxstat64)(ver, (char *) name, st64);
}

int lstat64(const char *name, struct stat64 *st64)
{
        check_init("lstat64");
        
	if (smbw_path((char *) name)) {
                struct SMBW_stat statbuf;
		int ret = smbw_stat((char *) name, &statbuf);
		stat64_convert(&statbuf, st64);
		return ret;
	}
        
        return (* smbw_libc.lstat64)((char *) name, st64);
}

int _llseek(unsigned int fd,  unsigned  long  offset_high, unsigned  long  offset_low,  loff_t  *result, unsigned int whence)
{
        check_init("_llseek");
        
	if (smbw_fd(fd)) {
		*result = lseek(fd, offset_low, whence);
                return (*result < 0 ? -1 : 0);
	}
        
        return (* smbw_libc._llseek)(fd, offset_high, offset_low, result, whence);
}

struct dirent64 *readdir64(DIR *dir)
{
        check_init("readdir64");
        
	if (smbw_dirp(dir)) {
                static struct dirent64 external;
                struct SMBW_dirent * internal = (void *)smbw_readdir(dir);
                if (internal != NULL) {
                        dirent64_convert(internal, &external);
                        return &external;
                }
                return NULL;
	}
        
        return (* smbw_libc.readdir64)(dir);
}

int readdir_r(DIR *dir, struct dirent *external, struct dirent **result)
{
        check_init("readdir_r");
        
	if (smbw_dirp(dir)) {
                struct SMBW_dirent internal;
                int ret = smbw_readdir_r(dir, &internal, NULL);
                if (ret == 0) {
                        dirent_convert(&internal, external);
                        *result = external;
                }
		return ret;
	}
        
        return (* smbw_libc.readdir_r)(dir, external, result);
}

int readdir64_r(DIR *dir, struct dirent64 *external, struct dirent64 **result)
{
        check_init("readdir64_r");
        
	if (smbw_dirp(dir)) {
                struct SMBW_dirent internal;
                int ret = smbw_readdir_r(dir, &internal, NULL);
                if (ret == 0) {
                        dirent64_convert(&internal, external);
                        *result = external;
                }
		return ret;
	}
        
        return (* smbw_libc.readdir64_r)(dir, external, result);
}

int fork(void)
{
        check_init("fork");
	return smbw_fork();
}

int setxattr(const char *fname,
             const char *name,
             const void *value,
             size_t size,
             int flags)
{
	if (smbw_path(fname)) {
		return smbw_setxattr(fname, name, value, size, flags);
	}
        
        return (* smbw_libc.setxattr)(fname, name, value, size, flags);
}

int lsetxattr(const char *fname,
              const char *name,
              const void *value,
              size_t size,
              int flags)
{
	if (smbw_path(fname)) {
		return smbw_lsetxattr(fname, name, value, size, flags);
	}
        
        return (* smbw_libc.lsetxattr)(fname, name, value, size, flags);
}

int fsetxattr(int fd,
              const char *name,
              const void *value,
              size_t size,
              int flags)
{
	if (smbw_fd(fd)) {
		return smbw_fsetxattr(fd, name, value, size, flags);
	}
        
        return (* smbw_libc.fsetxattr)(fd, name, value, size, flags);
}

int getxattr(const char *fname,
             const char *name,
             const void *value,
             size_t size)
{
	if (smbw_path(fname)) {
		return smbw_getxattr(fname, name, value, size);
	}
        
        return (* smbw_libc.getxattr)(fname, name, value, size);
}

int lgetxattr(const char *fname,
              const char *name,
              const void *value,
              size_t size)
{
	if (smbw_path(fname)) {
		return smbw_lgetxattr(fname, name, value, size);
	}
        
        return (* smbw_libc.lgetxattr)(fname, name, value, size);
}

int fgetxattr(int fd,
              const char *name,
              const void *value,
              size_t size)
{
	if (smbw_fd(fd)) {
		return smbw_fgetxattr(fd, name, value, size);
	}
        
        return (* smbw_libc.fgetxattr)(fd, name, value, size);
}

int removexattr(const char *fname,
                const char *name)
{
	if (smbw_path(fname)) {
		return smbw_removexattr(fname, name);
	}
        
        return (* smbw_libc.removexattr)(fname, name);
}

int lremovexattr(const char *fname,
                 const char *name)
{
	if (smbw_path(fname)) {
		return smbw_lremovexattr(fname, name);
	}
        
        return (* smbw_libc.lremovexattr)(fname, name);
}

int fremovexattr(int fd,
                 const char *name)
{
	if (smbw_fd(fd)) {
		return smbw_fremovexattr(fd, name);
	}
        
        return (* smbw_libc.fremovexattr)(fd, name);
}

int listxattr(const char *fname,
              char *list,
              size_t size)
{
	if (smbw_path(fname)) {
		return smbw_listxattr(fname, list, size);
	}
        
        return (* smbw_libc.listxattr)(fname, list, size);
}

int llistxattr(const char *fname,
               char *list,
               size_t size)
{
	if (smbw_path(fname)) {
		return smbw_llistxattr(fname, list, size);
	}
        
        return (* smbw_libc.llistxattr)(fname, list, size);
}

int flistxattr(int fd,
               char *list,
               size_t size)
{
	if (smbw_fd(fd)) {
                return smbw_flistxattr(fd, list, size);
	}
        
        return (* smbw_libc.flistxattr)(fd, list, size);
}


/*
 * We're ending up with a different implementation of malloc() with smbwrapper
 * than without it.  The one with it does not support returning a non-NULL
 * pointer from a call to malloc(0), and many apps depend on getting a valid
 * pointer when requesting zero length (e.g. df, emacs).
 *
 * Unfortunately, initializing the smbw_libc[] array via the dynamic link
 * library (libdl) requires malloc so we can't just do the same type of
 * mapping to the C library as we do with everything else.  We need to
 * implement a different way of allocating memory that ensures that the C
 * library version of malloc() gets used.  This is the only place where we
 * kludge the code to use an undocumented interface to the C library.
 *
 * If anyone can come up with a way to dynamically link to the C library
 * rather than using this undocumented interface, I'd sure love to hear about
 * it.  Better yet, if you can figure out where the alternate malloc()
 * functions are coming from and arrange for them not to be called, that would
 * be even better.  We should try to avoid wrapping functions that don't
 * really require it.
 */

void *malloc(size_t size)
{
        void *__libc_malloc(size_t size);
        return __libc_malloc(size);
}

void *calloc(size_t nmemb, size_t size)
{
        void *__libc_calloc(size_t nmemb, size_t size);
        return __libc_calloc(nmemb, size);
}

void *realloc(void *ptr, size_t size)
{
        void *__libc_realloc(void *ptr, size_t size);
        return __libc_realloc(ptr, size);
}

void free(void *ptr)
{
        static int      in_progress = 0;
        void __libc_free(void *ptr);
        
        if (in_progress) return;
        in_progress = 1;
        __libc_free(ptr);
        in_progress = 0;
}


#if 0                           /* SELECT */

static struct sigaction user_action[_NSIG];

static void
smbw_sigaction_handler(int signum,
                       siginfo_t *info,
                       void *context)
{
        /* Our entire purpose for trapping signals is to call this! */
        sys_select_signal();
        
        /* Call the user's handler */
        if (user_action[signum].sa_handler != SIG_IGN &&
            user_action[signum].sa_handler != SIG_DFL &&
            user_action[signum].sa_handler != SIG_ERR) {
                (* user_action[signum].sa_sigaction)(signum, info, context);
        }
}


/*
 * Every Samba signal handler must call sys_select_signal() to avoid a race
 * condition, so we'll take whatever signal handler is currently assigned,
 * call call sys_select_signal() in addition to their call.
 */
static int
do_select(int n,
          fd_set *readfds,
          fd_set *writefds,
          fd_set *exceptfds,
          struct timeval *timeout,
          int (* select_fn)(int n,
                            fd_set *readfds,
                            fd_set *writefds,
                            fd_set *exceptfds,
                            struct timeval *timeout))
{
        int i;
        int ret;
        int saved_errno;
        sigset_t sigset;
        struct sigaction new_action;
        
        saved_errno = errno;
        for (i=1; i<_NSIG; i++) {
                sigemptyset(&sigset);
                new_action.sa_mask = sigset;
                new_action.sa_flags = SA_SIGINFO;
                new_action.sa_sigaction = smbw_sigaction_handler;
                
                if (sigaction(i, &new_action, &user_action[i]) < 0) {
                        if (errno != EINVAL) {
                                return -1;
                        }
                }
        }
        errno = saved_errno;
        
        ret = (* select_fn)(n, readfds, writefds, exceptfds, timeout);
        saved_errno = errno;
        
        for (i=0; i<_NSIG; i++) {
                (void) sigaction(i, &user_action[i], NULL);
        }
        
        errno = saved_errno;
        return ret;
}

int
select(int n,
       fd_set *readfds,
       fd_set *writefds,
       fd_set *exceptfds,
       struct timeval *timeout)
{
        check_init("select");
        
        return do_select(n, readfds, writefds, exceptfds,
                         timeout, smbw_libc.select);
}

int
_select(int n,
        fd_set *readfds,
        fd_set *writefds,
        fd_set *exceptfds,
        struct timeval *timeout)
{
        check_init("_select");
        
        return do_select(n, readfds, writefds, exceptfds,
                         timeout, smbw_libc._select);
}

int
__select(int n,
         fd_set *readfds,
         fd_set *writefds,
         fd_set *exceptfds,
         struct timeval *timeout)
{
        check_init("__select");
        
        return do_select(n, readfds, writefds, exceptfds,
                         timeout, smbw_libc.__select);
}

#endif