]> granicus.if.org Git - strace/blob - file.c
2004-10-07 Roland McGrath <roland@redhat.com>
[strace] / file.c
1 /*
2 #ifdef LINUX
3  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
4  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
5  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
6  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  *      $Id$
32  */
33
34 #include "defs.h"
35
36 #include <dirent.h>
37 #ifdef LINUX
38 #define dirent kernel_dirent
39 #define dirent64 kernel_dirent64
40 #include <linux/types.h>
41 #include <linux/dirent.h>
42 #undef dirent
43 #undef dirent64
44 #else
45 #define kernel_dirent dirent
46 #endif
47
48 #ifdef LINUX
49 #  ifdef LINUXSPARC
50 struct stat {
51         unsigned short  st_dev;
52         unsigned int    st_ino;
53         unsigned short  st_mode;
54         short           st_nlink;
55         unsigned short  st_uid;
56         unsigned short  st_gid;
57         unsigned short  st_rdev;
58         unsigned int    st_size;
59         int             st_atime;
60         unsigned int    __unused1;
61         int             st_mtime;
62         unsigned int    __unused2;
63         int             st_ctime;
64         unsigned int    __unused3;
65         int             st_blksize;
66         int             st_blocks;
67         unsigned int    __unused4[2];
68 };
69 #if defined(SPARC64)
70 struct stat_sparc64 {
71         unsigned int    st_dev;
72         unsigned long   st_ino;
73         unsigned int    st_mode;
74         unsigned int    st_nlink;
75         unsigned int    st_uid;
76         unsigned int    st_gid;
77         unsigned int    st_rdev;
78         long            st_size;
79         long            st_atime;
80         long            st_mtime;
81         long            st_ctime;
82         long            st_blksize;
83         long            st_blocks;
84         unsigned long   __unused4[2];
85 };
86 #endif /* SPARC64 */
87 #    define stat kernel_stat
88 #    include <asm/stat.h>
89 #    undef stat
90 #  else
91 #    undef dev_t
92 #    undef ino_t
93 #    undef mode_t
94 #    undef nlink_t
95 #    undef uid_t
96 #    undef gid_t
97 #    undef off_t
98 #    undef loff_t
99
100 #    define dev_t __kernel_dev_t
101 #    define ino_t __kernel_ino_t
102 #    define mode_t __kernel_mode_t
103 #    define nlink_t __kernel_nlink_t
104 #    define uid_t __kernel_uid_t
105 #    define gid_t __kernel_gid_t
106 #    define off_t __kernel_off_t
107 #    define loff_t __kernel_loff_t
108
109 #    include <asm/stat.h>
110
111 #    undef dev_t
112 #    undef ino_t
113 #    undef mode_t
114 #    undef nlink_t
115 #    undef uid_t
116 #    undef gid_t
117 #    undef off_t
118 #    undef loff_t
119
120 #    define dev_t dev_t
121 #    define ino_t ino_t
122 #    define mode_t mode_t
123 #    define nlink_t nlink_t
124 #    define uid_t uid_t
125 #    define gid_t gid_t
126 #    define off_t off_t
127 #    define loff_t loff_t
128 #  endif
129 #  ifdef HPPA   /* asm-parisc/stat.h defines stat64 */
130 #    undef stat64
131 #  endif
132 #  define stat libc_stat
133 #  define stat64 libc_stat64
134 #  include <sys/stat.h>
135 #  undef stat
136 #  undef stat64
137    /* These might be macros. */
138 #  undef st_atime
139 #  undef st_mtime
140 #  undef st_ctime
141 #  ifdef HPPA
142 #    define stat64 hpux_stat64
143 #  endif
144 #else
145 #  include <sys/stat.h>
146 #endif
147
148 #include <fcntl.h>
149
150 #ifdef SVR4
151 #  include <sys/cred.h>
152 #endif /* SVR4 */
153
154 #ifdef HAVE_SYS_VFS_H
155 #include <sys/vfs.h>
156 #endif
157
158 #ifdef HAVE_LINUX_XATTR_H
159 #include <linux/xattr.h>
160 #elif defined linux
161 #define XATTR_CREATE 1
162 #define XATTR_REPLACE 2
163 #endif
164
165 #ifdef FREEBSD
166 #include <sys/param.h>
167 #include <sys/mount.h>
168 #include <sys/stat.h>
169 #endif
170
171 #if HAVE_LONG_LONG_OFF_T
172 /*
173  * Ugly hacks for systems that have typedef long long off_t
174  */
175
176 #define stat64 stat
177 #define HAVE_STAT64 1   /* Ugly hack */
178
179 #define sys_stat64      sys_stat
180 #define sys_fstat64     sys_fstat
181 #define sys_lstat64     sys_lstat
182 #define sys_lseek64     sys_lseek
183 #define sys_truncate64  sys_truncate
184 #define sys_ftruncate64 sys_ftruncate
185 #endif
186
187 #ifdef MAJOR_IN_SYSMACROS
188 #include <sys/sysmacros.h>
189 #endif
190
191 #ifdef MAJOR_IN_MKDEV
192 #include <sys/mkdev.h>
193 #endif
194
195 #ifdef HAVE_SYS_ASYNCH_H
196 #include <sys/asynch.h>
197 #endif
198
199 #ifdef SUNOS4
200 #include <ustat.h>
201 #endif
202
203 /*
204  * This is a really dirty trick but it should always work.  Traditional
205  * Unix says r/w/rw are 0/1/2, so we make them true flags 1/2/3 by
206  * adding 1.  Just remember to add 1 to any arg decoded with openmodes.
207  */
208 const struct xlat openmodes[] = {
209         { O_RDWR+1,     "O_RDWR"        },
210         { O_RDONLY+1,   "O_RDONLY"      },
211         { O_WRONLY+1,   "O_WRONLY"      },
212         { O_NONBLOCK,   "O_NONBLOCK"    },
213         { O_APPEND,     "O_APPEND"      },
214         { O_CREAT,      "O_CREAT"       },
215         { O_TRUNC,      "O_TRUNC"       },
216         { O_EXCL,       "O_EXCL"        },
217         { O_NOCTTY,     "O_NOCTTY"      },
218 #ifdef O_SYNC
219         { O_SYNC,       "O_SYNC"        },
220 #endif
221 #ifdef O_ASYNC
222         { O_ASYNC,      "O_ASYNC"       },
223 #endif
224 #ifdef O_DSYNC
225         { O_DSYNC,      "O_DSYNC"       },
226 #endif
227 #ifdef O_RSYNC
228         { O_RSYNC,      "O_RSYNC"       },
229 #endif
230 #ifdef O_NDELAY
231         { O_NDELAY,     "O_NDELAY"      },
232 #endif
233 #ifdef O_PRIV
234         { O_PRIV,       "O_PRIV"        },
235 #endif
236 #ifdef O_DIRECT
237         { O_DIRECT,     "O_DIRECT"      },
238 #endif
239 #ifdef O_LARGEFILE
240         { O_LARGEFILE,  "O_LARGEFILE"   },
241 #endif
242 #ifdef O_DIRECTORY
243         { O_DIRECTORY,  "O_DIRECTORY"   },
244 #endif
245 #ifdef O_NOFOLLOW
246         { O_NOFOLLOW,   "O_NOFOLLOW"    },
247 #endif
248
249 #ifdef FNDELAY
250         { FNDELAY,      "FNDELAY"       },
251 #endif
252 #ifdef FAPPEND
253         { FAPPEND,      "FAPPEND"       },
254 #endif
255 #ifdef FMARK
256         { FMARK,        "FMARK"         },
257 #endif
258 #ifdef FDEFER
259         { FDEFER,       "FDEFER"        },
260 #endif
261 #ifdef FASYNC
262         { FASYNC,       "FASYNC"        },
263 #endif
264 #ifdef FSHLOCK
265         { FSHLOCK,      "FSHLOCK"       },
266 #endif
267 #ifdef FEXLOCK
268         { FEXLOCK,      "FEXLOCK"       },
269 #endif
270 #ifdef FCREAT
271         { FCREAT,       "FCREAT"        },
272 #endif
273 #ifdef FTRUNC
274         { FTRUNC,       "FTRUNC"        },
275 #endif
276 #ifdef FEXCL
277         { FEXCL,        "FEXCL"         },
278 #endif
279 #ifdef FNBIO
280         { FNBIO,        "FNBIO"         },
281 #endif
282 #ifdef FSYNC
283         { FSYNC,        "FSYNC"         },
284 #endif
285 #ifdef FNOCTTY
286         { FNOCTTY,      "FNOCTTY"       },
287 #endif
288 #ifdef O_SHLOCK
289         { O_SHLOCK,     "O_SHLOCK"      },
290 #endif
291 #ifdef O_EXLOCK
292         { O_EXLOCK,     "O_EXLOCK"      },
293 #endif
294         { 0,            NULL            },
295 };
296
297 int
298 sys_open(tcp)
299 struct tcb *tcp;
300 {
301         if (entering(tcp)) {
302                 printpath(tcp, tcp->u_arg[0]);
303                 tprintf(", ");
304                 /* flags */
305                 printflags(openmodes, tcp->u_arg[1] + 1);
306                 if (tcp->u_arg[1] & O_CREAT) {
307                         /* mode */
308                         tprintf(", %#lo", tcp->u_arg[2]);
309                 }
310         }
311         return 0;
312 }
313
314 #ifdef LINUXSPARC
315 const struct xlat openmodessol[] = {
316         { 0,            "O_RDWR"        },
317         { 1,            "O_RDONLY"      },
318         { 2,            "O_WRONLY"      },
319         { 0x80,         "O_NONBLOCK"    },
320         { 8,            "O_APPEND"      },
321         { 0x100,        "O_CREAT"       },
322         { 0x200,        "O_TRUNC"       },
323         { 0x400,        "O_EXCL"        },
324         { 0x800,        "O_NOCTTY"      },
325         { 0x10,         "O_SYNC"        },
326         { 0x40,         "O_DSYNC"       },
327         { 0x8000,       "O_RSYNC"       },
328         { 4,            "O_NDELAY"      },
329         { 0x1000,       "O_PRIV"        },
330         { 0,            NULL            },
331 };
332
333 int
334 solaris_open(tcp)
335 struct tcb *tcp;
336 {
337         if (entering(tcp)) {
338                 printpath(tcp, tcp->u_arg[0]);
339                 tprintf(", ");
340                 /* flags */
341                 printflags(openmodessol, tcp->u_arg[1] + 1);
342                 if (tcp->u_arg[1] & 0x100) {
343                         /* mode */
344                         tprintf(", %#lo", tcp->u_arg[2]);
345                 }
346         }
347         return 0;
348 }
349
350 #endif
351
352 int
353 sys_creat(tcp)
354 struct tcb *tcp;
355 {
356         if (entering(tcp)) {
357                 printpath(tcp, tcp->u_arg[0]);
358                 tprintf(", %#lo", tcp->u_arg[1]);
359         }
360         return 0;
361 }
362
363 static const struct xlat access_flags[] = {
364         { F_OK,         "F_OK",         },
365         { R_OK,         "R_OK"          },
366         { W_OK,         "W_OK"          },
367         { X_OK,         "X_OK"          },
368 #ifdef EFF_ONLY_OK
369         { EFF_ONLY_OK,  "EFF_ONLY_OK"   },
370 #endif
371 #ifdef EX_OK
372         { EX_OK,        "EX_OK"         },
373 #endif
374         { 0,            NULL            },
375 };
376
377 int
378 sys_access(tcp)
379 struct tcb *tcp;
380 {
381         if (entering(tcp)) {
382                 printpath(tcp, tcp->u_arg[0]);
383                 tprintf(", ");
384                 printflags(access_flags, tcp->u_arg[1]);
385         }
386         return 0;
387 }
388
389 int
390 sys_umask(tcp)
391 struct tcb *tcp;
392 {
393         if (entering(tcp)) {
394                 tprintf("%#lo", tcp->u_arg[0]);
395         }
396         return RVAL_OCTAL;
397 }
398
399 static const struct xlat whence[] = {
400         { SEEK_SET,     "SEEK_SET"      },
401         { SEEK_CUR,     "SEEK_CUR"      },
402         { SEEK_END,     "SEEK_END"      },
403         { 0,            NULL            },
404 };
405
406 #ifndef HAVE_LONG_LONG_OFF_T
407 int
408 sys_lseek(tcp)
409 struct tcb *tcp;
410 {
411         off_t offset;
412         int _whence;
413
414         if (entering(tcp)) {
415                 tprintf("%ld, ", tcp->u_arg[0]);
416                 offset = tcp->u_arg[1];
417                 _whence = tcp->u_arg[2];
418                 if (_whence == SEEK_SET)
419                         tprintf("%lu, ", offset);
420                 else
421                         tprintf("%ld, ", offset);
422                 printxval(whence, _whence, "SEEK_???");
423         }
424         return RVAL_UDECIMAL;
425 }
426 #endif
427
428 #ifdef LINUX
429 int
430 sys_llseek (tcp)
431 struct tcb *tcp;
432 {
433     if (entering(tcp)) {
434         if (tcp->u_arg[4] == SEEK_SET)
435             tprintf("%ld, %llu, ", tcp->u_arg[0],
436                     LONG_LONG(tcp->u_arg[1], tcp->u_arg[2]));
437         else
438             tprintf("%ld, %lld, ", tcp->u_arg[0],
439                     LONG_LONG(tcp->u_arg[1], tcp->u_arg[2]));
440     }
441     else {
442         long long int off;
443         if (syserror(tcp) || umove(tcp, tcp->u_arg[3], &off) < 0)
444             tprintf("%#lx, ", tcp->u_arg[3]);
445         else
446             tprintf("[%llu], ", off);
447         printxval(whence, tcp->u_arg[4], "SEEK_???");
448     }
449     return 0;
450 }
451
452 int
453 sys_readahead (tcp)
454 struct tcb *tcp;
455 {
456     if (entering(tcp)) {
457         tprintf("%ld, %lld, %ld", tcp->u_arg[0],
458 # if defined IA64 || defined X86_64 || defined ALPHA
459                 (long long int) tcp->u_arg[1], tcp->u_arg[2]
460 # else
461                 LONG_LONG(tcp->u_arg[1], tcp->u_arg[2]), tcp->u_arg[3]
462 # endif
463                 );
464     }
465     return 0;
466 }
467 #endif
468
469 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
470 int
471 sys_lseek64 (tcp)
472 struct tcb *tcp;
473 {
474         if (entering(tcp)) {
475                 long long offset;
476                 ALIGN64 (tcp, 1);       /* FreeBSD aligns off_t args */
477                 offset = LONG_LONG(tcp->u_arg [1], tcp->u_arg[2]);
478                 if (tcp->u_arg[3] == SEEK_SET)
479                         tprintf("%ld, %llu, ", tcp->u_arg[0], offset);
480                 else
481                         tprintf("%ld, %lld, ", tcp->u_arg[0], offset);
482                 printxval(whence, tcp->u_arg[3], "SEEK_???");
483         }
484         return RVAL_LUDECIMAL;
485 }
486 #endif
487
488 #ifndef HAVE_LONG_LONG_OFF_T
489 int
490 sys_truncate(tcp)
491 struct tcb *tcp;
492 {
493         if (entering(tcp)) {
494                 printpath(tcp, tcp->u_arg[0]);
495                 tprintf(", %lu", tcp->u_arg[1]);
496         }
497         return 0;
498 }
499 #endif
500
501 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
502 int
503 sys_truncate64(tcp)
504 struct tcb *tcp;
505 {
506         if (entering(tcp)) {
507                 ALIGN64 (tcp, 1);
508                 printpath(tcp, tcp->u_arg[0]);
509                 tprintf(", %llu", LONG_LONG(tcp->u_arg[1],tcp->u_arg[2]));
510         }
511         return 0;
512 }
513 #endif
514
515 #ifndef HAVE_LONG_LONG_OFF_T
516 int
517 sys_ftruncate(tcp)
518 struct tcb *tcp;
519 {
520         if (entering(tcp)) {
521                 tprintf("%ld, %lu", tcp->u_arg[0], tcp->u_arg[1]);
522         }
523         return 0;
524 }
525 #endif
526
527 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
528 int
529 sys_ftruncate64(tcp)
530 struct tcb *tcp;
531 {
532         if (entering(tcp)) {
533                 ALIGN64 (tcp, 1);
534                 tprintf("%ld, %llu", tcp->u_arg[0],
535                         LONG_LONG(tcp->u_arg[1] ,tcp->u_arg[2]));
536         }
537         return 0;
538 }
539 #endif
540
541 /* several stats */
542
543 static const struct xlat modetypes[] = {
544         { S_IFREG,      "S_IFREG"       },
545         { S_IFSOCK,     "S_IFSOCK"      },
546         { S_IFIFO,      "S_IFIFO"       },
547         { S_IFLNK,      "S_IFLNK"       },
548         { S_IFDIR,      "S_IFDIR"       },
549         { S_IFBLK,      "S_IFBLK"       },
550         { S_IFCHR,      "S_IFCHR"       },
551         { 0,            NULL            },
552 };
553
554 static const char *
555 sprintmode(mode)
556 int mode;
557 {
558         static char buf[64];
559         const char *s;
560
561         if ((mode & S_IFMT) == 0)
562                 s = "";
563         else if ((s = xlookup(modetypes, mode & S_IFMT)) == NULL) {
564                 sprintf(buf, "%#o", mode);
565                 return buf;
566         }
567         sprintf(buf, "%s%s%s%s", s,
568                 (mode & S_ISUID) ? "|S_ISUID" : "",
569                 (mode & S_ISGID) ? "|S_ISGID" : "",
570                 (mode & S_ISVTX) ? "|S_ISVTX" : "");
571         mode &= ~(S_IFMT|S_ISUID|S_ISGID|S_ISVTX);
572         if (mode)
573                 sprintf(buf + strlen(buf), "|%#o", mode);
574         s = (*buf == '|') ? buf + 1 : buf;
575         return *s ? s : "0";
576 }
577
578 static char *
579 sprinttime(t)
580 time_t t;
581 {
582         struct tm *tmp;
583         static char buf[32];
584
585         if (t == 0) {
586                 sprintf(buf, "0");
587                 return buf;
588         }
589         tmp = localtime(&t);
590         sprintf(buf, "%02d/%02d/%02d-%02d:%02d:%02d",
591                 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
592                 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
593         return buf;
594 }
595
596 #ifdef LINUXSPARC
597 typedef struct {
598         int     tv_sec;
599         int     tv_nsec;
600 } timestruct_t;
601
602 struct solstat {
603         unsigned        st_dev;
604         int             st_pad1[3];     /* network id */
605         unsigned        st_ino;
606         unsigned        st_mode;
607         unsigned        st_nlink;
608         unsigned        st_uid;
609         unsigned        st_gid;
610         unsigned        st_rdev;
611         int             st_pad2[2];
612         int             st_size;
613         int             st_pad3;        /* st_size, off_t expansion */
614         timestruct_t    st_atime;
615         timestruct_t    st_mtime;
616         timestruct_t    st_ctime;
617         int             st_blksize;
618         int             st_blocks;
619         char            st_fstype[16];
620         int             st_pad4[8];     /* expansion area */
621 };
622
623 static void
624 printstatsol(tcp, addr)
625 struct tcb *tcp;
626 long addr;
627 {
628         struct solstat statbuf;
629
630         if (!addr) {
631                 tprintf("NULL");
632                 return;
633         }
634         if (syserror(tcp) || !verbose(tcp)) {
635                 tprintf("%#lx", addr);
636                 return;
637         }
638         if (umove(tcp, addr, &statbuf) < 0) {
639                 tprintf("{...}");
640                 return;
641         }
642         if (!abbrev(tcp)) {
643                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
644                         (unsigned long) ((statbuf.st_dev >> 18) & 0x3fff),
645                         (unsigned long) (statbuf.st_dev & 0x3ffff),
646                         (unsigned long) statbuf.st_ino,
647                         sprintmode(statbuf.st_mode));
648                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
649                         (unsigned long) statbuf.st_nlink,
650                         (unsigned long) statbuf.st_uid,
651                         (unsigned long) statbuf.st_gid);
652                 tprintf("st_blksize=%lu, ", (unsigned long) statbuf.st_blksize);
653                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);
654         }
655         else
656                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
657         switch (statbuf.st_mode & S_IFMT) {
658         case S_IFCHR: case S_IFBLK:
659                 tprintf("st_rdev=makedev(%lu, %lu), ",
660                         (unsigned long) ((statbuf.st_rdev >> 18) & 0x3fff),
661                         (unsigned long) (statbuf.st_rdev & 0x3ffff));
662                 break;
663         default:
664                 tprintf("st_size=%u, ", statbuf.st_size);
665                 break;
666         }
667         if (!abbrev(tcp)) {
668                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
669                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
670                 tprintf("st_ctime=%s}", sprinttime(statbuf.st_ctime));
671         }
672         else
673                 tprintf("...}");
674 }
675
676 #if defined (SPARC64)
677 static void
678 printstat_sparc64(tcp, addr)
679 struct tcb *tcp;
680 long addr;
681 {
682         struct stat_sparc64 statbuf;
683
684         if (!addr) {
685                 tprintf("NULL");
686                 return;
687         }
688         if (syserror(tcp) || !verbose(tcp)) {
689                 tprintf("%#lx", addr);
690                 return;
691         }
692         if (umove(tcp, addr, &statbuf) < 0) {
693                 tprintf("{...}");
694                 return;
695         }
696
697         if (!abbrev(tcp)) {
698                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
699                         (unsigned long) major(statbuf.st_dev),
700                         (unsigned long) minor(statbuf.st_dev),
701                         (unsigned long) statbuf.st_ino,
702                         sprintmode(statbuf.st_mode));
703                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
704                         (unsigned long) statbuf.st_nlink,
705                         (unsigned long) statbuf.st_uid,
706                         (unsigned long) statbuf.st_gid);
707                 tprintf("st_blksize=%lu, ",
708                         (unsigned long) statbuf.st_blksize);
709                 tprintf("st_blocks=%lu, ",
710                         (unsigned long) statbuf.st_blocks);
711         }
712         else
713                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
714         switch (statbuf.st_mode & S_IFMT) {
715         case S_IFCHR: case S_IFBLK:
716                 tprintf("st_rdev=makedev(%lu, %lu), ",
717                         (unsigned long) major(statbuf.st_rdev),
718                         (unsigned long) minor(statbuf.st_rdev));
719                 break;
720         default:
721                 tprintf("st_size=%lu, ", statbuf.st_size);
722                 break;
723         }
724         if (!abbrev(tcp)) {
725                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
726                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
727                 tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime));
728                 tprintf("}");
729         }
730         else
731                 tprintf("...}");
732 }
733 #endif /* SPARC64 */
734 #endif /* LINUXSPARC */
735
736 const struct xlat fileflags[] = {
737 #ifdef FREEBSD
738         { UF_NODUMP,    "UF_NODUMP"     },
739         { UF_IMMUTABLE, "UF_IMMUTABLE"  },
740         { UF_APPEND,    "UF_APPEND"     },
741         { UF_OPAQUE,    "UF_OPAQUE"     },
742         { UF_NOUNLINK,  "UF_NOUNLINK"   },
743         { SF_ARCHIVED,  "SF_ARCHIVED"   },
744         { SF_IMMUTABLE, "SF_IMMUTABLE"  },
745         { SF_APPEND,    "SF_APPEND"     },
746         { SF_NOUNLINK,  "SF_NOUNLINK"   },
747 #elif UNIXWARE >= 2
748 #ifdef  _S_ISMLD
749         { _S_ISMLD,     "_S_ISMLD"      },
750 #endif
751 #ifdef  _S_ISMOUNTED
752         { _S_ISMOUNTED, "_S_ISMOUNTED"  },
753 #endif
754 #endif
755         { 0,            NULL            },
756 };
757
758 #ifdef FREEBSD
759 int
760 sys_chflags(tcp)
761 struct tcb *tcp;
762 {
763         if (entering(tcp)) {
764                 printpath(tcp, tcp->u_arg[0]);
765                 tprintf(", ");
766                 if (tcp->u_arg[1])
767                         printflags(fileflags, tcp->u_arg[1]);
768                 else
769                         tprintf("0");
770         }
771         return 0;
772 }
773
774 int
775 sys_fchflags(tcp)
776 struct tcb *tcp;
777 {
778         if (entering(tcp)) {
779                 tprintf("%ld, ", tcp->u_arg[0]);
780                 if (tcp->u_arg[1])
781                         printflags(fileflags, tcp->u_arg[1]);
782                 else
783                         tprintf("0");
784         }
785         return 0;
786 }
787 #endif
788
789 #ifndef HAVE_LONG_LONG_OFF_T
790 static void
791 realprintstat(tcp, statbuf)
792 struct tcb *tcp;
793 struct stat *statbuf;
794 {
795     if (!abbrev(tcp)) {
796             tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
797                     (unsigned long) major(statbuf->st_dev),
798                     (unsigned long) minor(statbuf->st_dev),
799                     (unsigned long) statbuf->st_ino,
800                     sprintmode(statbuf->st_mode));
801             tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
802                     (unsigned long) statbuf->st_nlink,
803                     (unsigned long) statbuf->st_uid,
804                     (unsigned long) statbuf->st_gid);
805 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
806             tprintf("st_blksize=%lu, ", (unsigned long) statbuf->st_blksize);
807 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
808 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
809             tprintf("st_blocks=%lu, ", (unsigned long) statbuf->st_blocks);
810 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
811     }
812     else
813             tprintf("{st_mode=%s, ", sprintmode(statbuf->st_mode));
814     switch (statbuf->st_mode & S_IFMT) {
815     case S_IFCHR: case S_IFBLK:
816 #ifdef HAVE_STRUCT_STAT_ST_RDEV
817             tprintf("st_rdev=makedev(%lu, %lu), ",
818                     (unsigned long) major(statbuf->st_rdev),
819                     (unsigned long) minor(statbuf->st_rdev));
820 #else /* !HAVE_STRUCT_STAT_ST_RDEV */
821             tprintf("st_size=makedev(%lu, %lu), ",
822                     (unsigned long) major(statbuf->st_size),
823                     (unsigned long) minor(statbuf->st_size));
824 #endif /* !HAVE_STRUCT_STAT_ST_RDEV */
825             break;
826     default:
827             tprintf("st_size=%lu, ", statbuf->st_size);
828             break;
829     }
830     if (!abbrev(tcp)) {
831             tprintf("st_atime=%s, ", sprinttime(statbuf->st_atime));
832             tprintf("st_mtime=%s, ", sprinttime(statbuf->st_mtime));
833             tprintf("st_ctime=%s", sprinttime(statbuf->st_ctime));
834 #if HAVE_STRUCT_STAT_ST_FLAGS
835                 tprintf(", st_flags=");
836                 if (statbuf->st_flags) {
837                         printflags(fileflags, statbuf->st_flags);
838                 } else
839                         tprintf("0");
840 #endif
841 #if HAVE_STRUCT_STAT_ST_ACLCNT
842                 tprintf(", st_aclcnt=%d", statbuf->st_aclcnt);
843 #endif
844 #if HAVE_STRUCT_STAT_ST_LEVEL
845                 tprintf(", st_level=%ld", statbuf->st_level);
846 #endif
847 #if HAVE_STRUCT_STAT_ST_FSTYPE
848                 tprintf(", st_fstype=%.*s",
849                         (int) sizeof statbuf->st_fstype, statbuf->st_fstype);
850 #endif
851 #if HAVE_STRUCT_STAT_ST_GEN
852                 tprintf(", st_gen=%u", statbuf->st_gen);
853 #endif
854                 tprintf("}");
855     }
856     else
857             tprintf("...}");
858 }
859
860
861 static void
862 printstat(tcp, addr)
863 struct tcb *tcp;
864 long addr;
865 {
866         struct stat statbuf;
867
868 #ifdef LINUXSPARC
869         if (current_personality == 1) {
870                 printstatsol(tcp, addr);
871                 return;
872         }
873 #ifdef SPARC64
874         else if (current_personality == 2) {
875                 printstat_sparc64(tcp, addr);
876                 return;
877         }
878 #endif
879 #endif /* LINUXSPARC */
880
881         if (!addr) {
882                 tprintf("NULL");
883                 return;
884         }
885         if (syserror(tcp) || !verbose(tcp)) {
886                 tprintf("%#lx", addr);
887                 return;
888         }
889         if (umove(tcp, addr, &statbuf) < 0) {
890                 tprintf("{...}");
891                 return;
892         }
893
894         realprintstat(tcp, &statbuf);
895 }
896 #endif  /* !HAVE_LONG_LONG_OFF_T */
897
898 #ifdef HAVE_STAT64
899 static void
900 printstat64(tcp, addr)
901 struct tcb *tcp;
902 long addr;
903 {
904         struct stat64 statbuf;
905
906 #ifdef LINUXSPARC
907         if (current_personality == 1) {
908                 printstatsol(tcp, addr);
909                 return;
910         }
911 #ifdef SPARC64
912         else if (current_personality == 2) {
913                 printstat_sparc64(tcp, addr);
914                 return;
915         }
916 #endif
917 #endif /* LINUXSPARC */
918
919         if (!addr) {
920                 tprintf("NULL");
921                 return;
922         }
923         if (syserror(tcp) || !verbose(tcp)) {
924                 tprintf("%#lx", addr);
925                 return;
926         }
927         if (umove(tcp, addr, &statbuf) < 0) {
928                 tprintf("{...}");
929                 return;
930         }
931
932         if (!abbrev(tcp)) {
933 #ifdef HAVE_LONG_LONG
934                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%llu, st_mode=%s, ",
935 #else
936                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
937 #endif
938                         (unsigned long) major(statbuf.st_dev),
939                         (unsigned long) minor(statbuf.st_dev),
940 #ifdef HAVE_LONG_LONG
941                         (unsigned long long) statbuf.st_ino,
942 #else
943                         (unsigned long) statbuf.st_ino,
944 #endif
945                         sprintmode(statbuf.st_mode));
946                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
947                         (unsigned long) statbuf.st_nlink,
948                         (unsigned long) statbuf.st_uid,
949                         (unsigned long) statbuf.st_gid);
950 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
951                 tprintf("st_blksize=%lu, ",
952                         (unsigned long) statbuf.st_blksize);
953 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
954 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
955                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);
956 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
957         }
958         else
959                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
960         switch (statbuf.st_mode & S_IFMT) {
961         case S_IFCHR: case S_IFBLK:
962 #ifdef HAVE_STRUCT_STAT_ST_RDEV
963                 tprintf("st_rdev=makedev(%lu, %lu), ",
964                         (unsigned long) major(statbuf.st_rdev),
965                         (unsigned long) minor(statbuf.st_rdev));
966 #else /* !HAVE_STRUCT_STAT_ST_RDEV */
967                 tprintf("st_size=makedev(%lu, %lu), ",
968                         (unsigned long) major(statbuf.st_size),
969                         (unsigned long) minor(statbuf.st_size));
970 #endif /* !HAVE_STRUCT_STAT_ST_RDEV */
971                 break;
972         default:
973                 tprintf("st_size=%llu, ", statbuf.st_size);
974                 break;
975         }
976         if (!abbrev(tcp)) {
977                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
978                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
979                 tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime));
980 #if HAVE_STRUCT_STAT_ST_FLAGS
981                 tprintf(", st_flags=");
982                 if (statbuf.st_flags) {
983                         printflags(fileflags, statbuf.st_flags);
984                 } else
985                         tprintf("0");
986 #endif
987 #if HAVE_STRUCT_STAT_ST_ACLCNT
988                 tprintf(", st_aclcnt=%d", statbuf.st_aclcnt);
989 #endif
990 #if HAVE_STRUCT_STAT_ST_LEVEL
991                 tprintf(", st_level=%ld", statbuf.st_level);
992 #endif
993 #if HAVE_STRUCT_STAT_ST_FSTYPE
994                 tprintf(", st_fstype=%.*s",
995                         (int) sizeof statbuf.st_fstype, statbuf.st_fstype);
996 #endif
997 #if HAVE_STRUCT_STAT_ST_GEN
998                 tprintf(", st_gen=%u", statbuf.st_gen);
999 #endif
1000                 tprintf("}");
1001         }
1002         else
1003                 tprintf("...}");
1004 }
1005 #endif /* HAVE_STAT64 */
1006
1007 #if defined(LINUX) && defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1008 static void
1009 convertoldstat(oldbuf, newbuf)
1010 const struct __old_kernel_stat *oldbuf;
1011 struct stat *newbuf;
1012 {
1013     newbuf->st_dev=oldbuf->st_dev;
1014     newbuf->st_ino=oldbuf->st_ino;
1015     newbuf->st_mode=oldbuf->st_mode;
1016     newbuf->st_nlink=oldbuf->st_nlink;
1017     newbuf->st_uid=oldbuf->st_uid;
1018     newbuf->st_gid=oldbuf->st_gid;
1019     newbuf->st_rdev=oldbuf->st_rdev;
1020     newbuf->st_size=oldbuf->st_size;
1021     newbuf->st_atime=oldbuf->st_atime;
1022     newbuf->st_mtime=oldbuf->st_mtime;
1023     newbuf->st_ctime=oldbuf->st_ctime;
1024     newbuf->st_blksize=0;       /* not supported in old_stat */
1025     newbuf->st_blocks=0;                /* not supported in old_stat */
1026 }
1027
1028
1029 static void
1030 printoldstat(tcp, addr)
1031 struct tcb *tcp;
1032 long addr;
1033 {
1034         struct __old_kernel_stat statbuf;
1035         struct stat newstatbuf;
1036
1037 #ifdef LINUXSPARC
1038         if (current_personality == 1) {
1039                 printstatsol(tcp, addr);
1040                 return;
1041         }
1042 #endif /* LINUXSPARC */
1043
1044         if (!addr) {
1045                 tprintf("NULL");
1046                 return;
1047         }
1048         if (syserror(tcp) || !verbose(tcp)) {
1049                 tprintf("%#lx", addr);
1050                 return;
1051         }
1052         if (umove(tcp, addr, &statbuf) < 0) {
1053                 tprintf("{...}");
1054                 return;
1055         }
1056
1057         convertoldstat(&statbuf, &newstatbuf);
1058         realprintstat(tcp, &newstatbuf);
1059 }
1060 #endif /* LINUX && !IA64 && !HPPA && !X86_64 && !S390 && !S390X */
1061
1062 #ifndef HAVE_LONG_LONG_OFF_T
1063 int
1064 sys_stat(tcp)
1065 struct tcb *tcp;
1066 {
1067         if (entering(tcp)) {
1068                 printpath(tcp, tcp->u_arg[0]);
1069                 tprintf(", ");
1070         } else {
1071                 printstat(tcp, tcp->u_arg[1]);
1072         }
1073         return 0;
1074 }
1075 #endif
1076
1077 int
1078 sys_stat64(tcp)
1079 struct tcb *tcp;
1080 {
1081 #ifdef HAVE_STAT64
1082         if (entering(tcp)) {
1083                 printpath(tcp, tcp->u_arg[0]);
1084                 tprintf(", ");
1085         } else {
1086                 printstat64(tcp, tcp->u_arg[1]);
1087         }
1088         return 0;
1089 #else
1090         return printargs(tcp);
1091 #endif
1092 }
1093
1094 #if defined(LINUX) && defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1095 int
1096 sys_oldstat(tcp)
1097 struct tcb *tcp;
1098 {
1099         if (entering(tcp)) {
1100                 printpath(tcp, tcp->u_arg[0]);
1101                 tprintf(", ");
1102         } else {
1103                 printoldstat(tcp, tcp->u_arg[1]);
1104         }
1105         return 0;
1106 }
1107 #endif /* LINUX && HAVE_STRUCT___OLD_KERNEL_STAT */
1108
1109 #ifndef HAVE_LONG_LONG_OFF_T
1110 int
1111 sys_fstat(tcp)
1112 struct tcb *tcp;
1113 {
1114         if (entering(tcp))
1115                 tprintf("%ld, ", tcp->u_arg[0]);
1116         else {
1117                 printstat(tcp, tcp->u_arg[1]);
1118         }
1119         return 0;
1120 }
1121 #endif
1122
1123 int
1124 sys_fstat64(tcp)
1125 struct tcb *tcp;
1126 {
1127 #ifdef HAVE_STAT64
1128         if (entering(tcp))
1129                 tprintf("%ld, ", tcp->u_arg[0]);
1130         else {
1131                 printstat64(tcp, tcp->u_arg[1]);
1132         }
1133         return 0;
1134 #else
1135         return printargs(tcp);
1136 #endif
1137 }
1138
1139 #if defined(LINUX) && defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1140 int
1141 sys_oldfstat(tcp)
1142 struct tcb *tcp;
1143 {
1144         if (entering(tcp))
1145                 tprintf("%ld, ", tcp->u_arg[0]);
1146         else {
1147                 printoldstat(tcp, tcp->u_arg[1]);
1148         }
1149         return 0;
1150 }
1151 #endif /* LINUX && HAVE_STRUCT___OLD_KERNEL_STAT */
1152
1153 #ifndef HAVE_LONG_LONG_OFF_T
1154 int
1155 sys_lstat(tcp)
1156 struct tcb *tcp;
1157 {
1158         if (entering(tcp)) {
1159                 printpath(tcp, tcp->u_arg[0]);
1160                 tprintf(", ");
1161         } else {
1162                 printstat(tcp, tcp->u_arg[1]);
1163         }
1164         return 0;
1165 }
1166 #endif
1167
1168 int
1169 sys_lstat64(tcp)
1170 struct tcb *tcp;
1171 {
1172 #ifdef HAVE_STAT64
1173         if (entering(tcp)) {
1174                 printpath(tcp, tcp->u_arg[0]);
1175                 tprintf(", ");
1176         } else {
1177                 printstat64(tcp, tcp->u_arg[1]);
1178         }
1179         return 0;
1180 #else
1181         return printargs(tcp);
1182 #endif
1183 }
1184
1185 #if defined(LINUX) && defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1186 int
1187 sys_oldlstat(tcp)
1188 struct tcb *tcp;
1189 {
1190         if (entering(tcp)) {
1191                 printpath(tcp, tcp->u_arg[0]);
1192                 tprintf(", ");
1193         } else {
1194                 printoldstat(tcp, tcp->u_arg[1]);
1195         }
1196         return 0;
1197 }
1198 #endif /* LINUX && HAVE_STRUCT___OLD_KERNEL_STAT */
1199
1200
1201 #if defined(SVR4) || defined(LINUXSPARC)
1202
1203 int
1204 sys_xstat(tcp)
1205 struct tcb *tcp;
1206 {
1207         if (entering(tcp)) {
1208                 tprintf("%ld, ", tcp->u_arg[0]);
1209                 printpath(tcp, tcp->u_arg[1]);
1210                 tprintf(", ");
1211         } else {
1212 #ifdef _STAT64_VER
1213                 if (tcp->u_arg[0] == _STAT64_VER)
1214                         printstat64 (tcp, tcp->u_arg[2]);
1215                 else
1216 #endif
1217                 printstat(tcp, tcp->u_arg[2]);
1218         }
1219         return 0;
1220 }
1221
1222 int
1223 sys_fxstat(tcp)
1224 struct tcb *tcp;
1225 {
1226         if (entering(tcp))
1227                 tprintf("%ld, %ld, ", tcp->u_arg[0], tcp->u_arg[1]);
1228         else {
1229 #ifdef _STAT64_VER
1230                 if (tcp->u_arg[0] == _STAT64_VER)
1231                         printstat64 (tcp, tcp->u_arg[2]);
1232                 else
1233 #endif
1234                 printstat(tcp, tcp->u_arg[2]);
1235         }
1236         return 0;
1237 }
1238
1239 int
1240 sys_lxstat(tcp)
1241 struct tcb *tcp;
1242 {
1243         if (entering(tcp)) {
1244                 tprintf("%ld, ", tcp->u_arg[0]);
1245                 printpath(tcp, tcp->u_arg[1]);
1246                 tprintf(", ");
1247         } else {
1248 #ifdef _STAT64_VER
1249                 if (tcp->u_arg[0] == _STAT64_VER)
1250                         printstat64 (tcp, tcp->u_arg[2]);
1251                 else
1252 #endif
1253                 printstat(tcp, tcp->u_arg[2]);
1254         }
1255         return 0;
1256 }
1257
1258 int
1259 sys_xmknod(tcp)
1260 struct tcb *tcp;
1261 {
1262         int mode = tcp->u_arg[2];
1263
1264         if (entering(tcp)) {
1265                 tprintf("%ld, ", tcp->u_arg[0]);
1266                 printpath(tcp, tcp->u_arg[1]);
1267                 tprintf(", %s", sprintmode(mode));
1268                 switch (mode & S_IFMT) {
1269                 case S_IFCHR: case S_IFBLK:
1270 #ifdef LINUXSPARC
1271                         tprintf(", makedev(%lu, %lu)",
1272                                 (unsigned long) ((tcp->u_arg[3] >> 18) & 0x3fff),
1273                                 (unsigned long) (tcp->u_arg[3] & 0x3ffff));
1274 #else
1275                         tprintf(", makedev(%lu, %lu)",
1276                                 (unsigned long) major(tcp->u_arg[3]),
1277                                 (unsigned long) minor(tcp->u_arg[3]));
1278 #endif
1279                         break;
1280                 default:
1281                         break;
1282                 }
1283         }
1284         return 0;
1285 }
1286
1287 #ifdef HAVE_SYS_ACL_H
1288
1289 #include <sys/acl.h>
1290
1291 const struct xlat aclcmds[] = {
1292 #ifdef SETACL
1293         { SETACL,       "SETACL"        },
1294 #endif
1295 #ifdef GETACL
1296         { GETACL,       "GETACL"        },
1297 #endif
1298 #ifdef GETACLCNT
1299         { GETACLCNT,    "GETACLCNT"     },
1300 #endif
1301 #ifdef ACL_GET
1302         { ACL_GET,      "ACL_GET"       },
1303 #endif
1304 #ifdef ACL_SET
1305         { ACL_SET,      "ACL_SET"       },
1306 #endif
1307 #ifdef ACL_CNT
1308         { ACL_CNT,      "ACL_CNT"       },
1309 #endif
1310         { 0,            NULL            },
1311 };
1312
1313 int
1314 sys_acl(tcp)
1315 struct tcb *tcp;
1316 {
1317         if (entering(tcp)) {
1318                 printpath(tcp, tcp->u_arg[0]);
1319                 tprintf(", ");
1320                 printxval(aclcmds, tcp->u_arg[1], "???ACL???");
1321                 tprintf(", %ld", tcp->u_arg[2]);
1322                 /*
1323                  * FIXME - dump out the list of aclent_t's pointed to
1324                  * by "tcp->u_arg[3]" if it's not NULL.
1325                  */
1326                 if (tcp->u_arg[3])
1327                         tprintf(", %#lx", tcp->u_arg[3]);
1328                 else
1329                         tprintf(", NULL");
1330         }
1331         return 0;
1332 }
1333
1334
1335 int
1336 sys_facl(tcp)
1337 struct tcb *tcp;
1338 {
1339         if (entering(tcp)) {
1340                 tprintf("%ld, ", tcp->u_arg[0]);
1341                 printxval(aclcmds, tcp->u_arg[1], "???ACL???");
1342                 tprintf(", %ld", tcp->u_arg[2]);
1343                 /*
1344                  * FIXME - dump out the list of aclent_t's pointed to
1345                  * by "tcp->u_arg[3]" if it's not NULL.
1346                  */
1347                 if (tcp->u_arg[3])
1348                         tprintf(", %#lx", tcp->u_arg[3]);
1349                 else
1350                         tprintf(", NULL");
1351         }
1352         return 0;
1353 }
1354
1355
1356 const struct xlat aclipc[] = {
1357 #ifdef IPC_SHM
1358         { IPC_SHM,      "IPC_SHM"       },
1359 #endif
1360 #ifdef IPC_SEM
1361         { IPC_SEM,      "IPC_SEM"       },
1362 #endif
1363 #ifdef IPC_MSG
1364         { IPC_MSG,      "IPC_MSG"       },
1365 #endif
1366         { 0,            NULL            },
1367 };
1368
1369
1370 int
1371 sys_aclipc(tcp)
1372 struct tcb *tcp;
1373 {
1374         if (entering(tcp)) {
1375                 printxval(aclipc, tcp->u_arg[0], "???IPC???");
1376                 tprintf(", %#lx, ", tcp->u_arg[1]);
1377                 printxval(aclcmds, tcp->u_arg[2], "???ACL???");
1378                 tprintf(", %ld", tcp->u_arg[3]);
1379                 /*
1380                  * FIXME - dump out the list of aclent_t's pointed to
1381                  * by "tcp->u_arg[4]" if it's not NULL.
1382                  */
1383                 if (tcp->u_arg[4])
1384                         tprintf(", %#lx", tcp->u_arg[4]);
1385                 else
1386                         tprintf(", NULL");
1387         }
1388         return 0;
1389 }
1390
1391
1392
1393 #endif /* HAVE_SYS_ACL_H */
1394
1395 #endif /* SVR4 || LINUXSPARC */
1396
1397 #ifdef LINUX
1398
1399 static const struct xlat fsmagic[] = {
1400         { 0x73757245,   "CODA_SUPER_MAGIC"      },
1401         { 0x012ff7b7,   "COH_SUPER_MAGIC"       },
1402         { 0x1373,       "DEVFS_SUPER_MAGIC"     },
1403         { 0x1cd1,       "DEVPTS_SUPER_MAGIC"    },
1404         { 0x414A53,     "EFS_SUPER_MAGIC"       },
1405         { 0xef51,       "EXT2_OLD_SUPER_MAGIC"  },
1406         { 0xef53,       "EXT2_SUPER_MAGIC"      },
1407         { 0x137d,       "EXT_SUPER_MAGIC"       },
1408         { 0xf995e849,   "HPFS_SUPER_MAGIC"      },
1409         { 0x9660,       "ISOFS_SUPER_MAGIC"     },
1410         { 0x137f,       "MINIX_SUPER_MAGIC"     },
1411         { 0x138f,       "MINIX_SUPER_MAGIC2"    },
1412         { 0x2468,       "MINIX2_SUPER_MAGIC"    },
1413         { 0x2478,       "MINIX2_SUPER_MAGIC2"   },
1414         { 0x4d44,       "MSDOS_SUPER_MAGIC"     },
1415         { 0x564c,       "NCP_SUPER_MAGIC"       },
1416         { 0x6969,       "NFS_SUPER_MAGIC"       },
1417         { 0x9fa0,       "PROC_SUPER_MAGIC"      },
1418         { 0x002f,       "QNX4_SUPER_MAGIC"      },
1419         { 0x52654973,   "REISERFS_SUPER_MAGIC"  },
1420         { 0x02011994,   "SHMFS_SUPER_MAGIC"     },
1421         { 0x517b,       "SMB_SUPER_MAGIC"       },
1422         { 0x012ff7b6,   "SYSV2_SUPER_MAGIC"     },
1423         { 0x012ff7b5,   "SYSV4_SUPER_MAGIC"     },
1424         { 0x00011954,   "UFS_MAGIC"             },
1425         { 0x54190100,   "UFS_CIGAM"             },
1426         { 0x012ff7b4,   "XENIX_SUPER_MAGIC"     },
1427         { 0x012fd16d,   "XIAFS_SUPER_MAGIC"     },
1428         { 0x62656572,   "SYSFS_MAGIC"           },
1429         { 0,            NULL                    },
1430 };
1431
1432 #endif /* LINUX */
1433
1434 #ifndef SVR4
1435
1436 static const char *
1437 sprintfstype(magic)
1438 int magic;
1439 {
1440         static char buf[32];
1441 #ifdef LINUX
1442         const char *s;
1443
1444         s = xlookup(fsmagic, magic);
1445         if (s) {
1446                 sprintf(buf, "\"%s\"", s);
1447                 return buf;
1448         }
1449 #endif /* LINUX */
1450         sprintf(buf, "%#x", magic);
1451         return buf;
1452 }
1453
1454 static void
1455 printstatfs(tcp, addr)
1456 struct tcb *tcp;
1457 long addr;
1458 {
1459         struct statfs statbuf;
1460
1461         if (syserror(tcp) || !verbose(tcp)) {
1462                 tprintf("%#lx", addr);
1463                 return;
1464         }
1465         if (umove(tcp, addr, &statbuf) < 0) {
1466                 tprintf("{...}");
1467                 return;
1468         }
1469 #ifdef ALPHA
1470
1471         tprintf("{f_type=%s, f_fbsize=%u, f_blocks=%u, f_bfree=%u, ",
1472                 sprintfstype(statbuf.f_type),
1473                 statbuf.f_bsize, statbuf.f_blocks, statbuf.f_bfree);
1474         tprintf("f_bavail=%u, f_files=%u, f_ffree=%u, f_fsid={%d, %d}, f_namelen=%u",
1475                 statbuf.f_bavail,statbuf.f_files, statbuf.f_ffree,
1476                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1],
1477                 statbuf.f_namelen);
1478 #else /* !ALPHA */
1479         tprintf("{f_type=%s, f_bsize=%lu, f_blocks=%lu, f_bfree=%lu, ",
1480                 sprintfstype(statbuf.f_type),
1481                 (unsigned long)statbuf.f_bsize,
1482                 (unsigned long)statbuf.f_blocks,
1483                 (unsigned long)statbuf.f_bfree);
1484         tprintf("f_bavail=%lu, f_files=%lu, f_ffree=%lu, f_fsid={%d, %d}",
1485                 (unsigned long)statbuf.f_bavail,
1486                 (unsigned long)statbuf.f_files,
1487                 (unsigned long)statbuf.f_ffree,
1488                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1]);
1489 #ifdef LINUX
1490         tprintf(", f_namelen=%lu", (unsigned long)statbuf.f_namelen);
1491 #endif /* LINUX */
1492 #endif /* !ALPHA */
1493 #ifdef _STATFS_F_FRSIZE
1494         tprintf(", f_frsize=%lu", (unsigned long)statbuf.f_frsize);
1495 #endif
1496         tprintf("}");
1497 }
1498
1499 int
1500 sys_statfs(tcp)
1501 struct tcb *tcp;
1502 {
1503         if (entering(tcp)) {
1504                 printpath(tcp, tcp->u_arg[0]);
1505                 tprintf(", ");
1506         } else {
1507                 printstatfs(tcp, tcp->u_arg[1]);
1508         }
1509         return 0;
1510 }
1511
1512 int
1513 sys_fstatfs(tcp)
1514 struct tcb *tcp;
1515 {
1516         if (entering(tcp)) {
1517                 tprintf("%lu, ", tcp->u_arg[0]);
1518         } else {
1519                 printstatfs(tcp, tcp->u_arg[1]);
1520         }
1521         return 0;
1522 }
1523
1524 #ifdef LINUX
1525 static void
1526 printstatfs64(tcp, addr)
1527 struct tcb *tcp;
1528 long addr;
1529 {
1530         struct statfs64 statbuf;
1531
1532         if (syserror(tcp) || !verbose(tcp)) {
1533                 tprintf("%#lx", addr);
1534                 return;
1535         }
1536         if (umove(tcp, addr, &statbuf) < 0) {
1537                 tprintf("{...}");
1538                 return;
1539         }
1540 #ifdef ALPHA
1541
1542         tprintf("{f_type=%s, f_fbsize=%u, f_blocks=%u, f_bfree=%u, ",
1543                 sprintfstype(statbuf.f_type),
1544                 statbuf.f_bsize, statbuf.f_blocks, statbuf.f_bfree);
1545         tprintf("f_bavail=%u, f_files=%u, f_ffree=%u, f_fsid={%d, %d}, f_namelen=%u",
1546                 statbuf.f_bavail,statbuf.f_files, statbuf.f_ffree,
1547                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1],
1548                 statbuf.f_namelen);
1549 #else /* !ALPHA */
1550         tprintf("{f_type=%s, f_bsize=%lu, f_blocks=%lu, f_bfree=%lu, ",
1551                 sprintfstype(statbuf.f_type),
1552                 (unsigned long)statbuf.f_bsize,
1553                 (unsigned long)statbuf.f_blocks,
1554                 (unsigned long)statbuf.f_bfree);
1555         tprintf("f_bavail=%lu, f_files=%lu, f_ffree=%lu, f_fsid={%d, %d}",
1556                 (unsigned long)statbuf.f_bavail,
1557                 (unsigned long)statbuf.f_files,
1558                 (unsigned long)statbuf.f_ffree,
1559                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1]);
1560         tprintf(", f_namelen=%lu", (unsigned long)statbuf.f_namelen);
1561 #endif /* !ALPHA */
1562 #ifdef _STATFS_F_FRSIZE
1563         tprintf(", f_frsize=%lu", (unsigned long)statbuf.f_frsize);
1564 #endif
1565         tprintf("}");
1566 }
1567
1568 int
1569 sys_statfs64(tcp)
1570 struct tcb *tcp;
1571 {
1572         if (entering(tcp)) {
1573                 printpath(tcp, tcp->u_arg[0]);
1574                 tprintf(", %lu, ", tcp->u_arg[1]);
1575         } else {
1576                 if (tcp->u_arg[1] == sizeof (struct statfs64))
1577                         printstatfs64(tcp, tcp->u_arg[2]);
1578                 else
1579                         tprintf("{???}");
1580         }
1581         return 0;
1582 }
1583
1584 int
1585 sys_fstatfs64(tcp)
1586 struct tcb *tcp;
1587 {
1588         if (entering(tcp)) {
1589                 tprintf("%lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
1590         } else {
1591                 if (tcp->u_arg[1] == sizeof (struct statfs64))
1592                         printstatfs64(tcp, tcp->u_arg[2]);
1593                 else
1594                         tprintf("{???}");
1595         }
1596         return 0;
1597 }
1598 #endif
1599
1600 #if defined(LINUX) && defined(__alpha)
1601
1602 int
1603 osf_statfs(tcp)
1604 struct tcb *tcp;
1605 {
1606         if (entering(tcp)) {
1607                 printpath(tcp, tcp->u_arg[0]);
1608                 tprintf(", ");
1609         } else {
1610                 printstatfs(tcp, tcp->u_arg[1]);
1611                 tprintf(", %lu", tcp->u_arg[2]);
1612         }
1613         return 0;
1614 }
1615
1616 int
1617 osf_fstatfs(tcp)
1618 struct tcb *tcp;
1619 {
1620         if (entering(tcp)) {
1621                 tprintf("%lu, ", tcp->u_arg[0]);
1622         } else {
1623                 printstatfs(tcp, tcp->u_arg[1]);
1624                 tprintf(", %lu", tcp->u_arg[2]);
1625         }
1626         return 0;
1627 }
1628 #endif /* LINUX && __alpha */
1629
1630 #endif /* !SVR4 */
1631
1632 #ifdef SUNOS4
1633
1634 int
1635 sys_ustat(tcp)
1636 struct tcb *tcp;
1637 {
1638         struct ustat statbuf;
1639
1640         if (entering(tcp)) {
1641                 tprintf("makedev(%lu, %lu), ",
1642                                 (long) major(tcp->u_arg[0]),
1643                                 (long) minor(tcp->u_arg[0]));
1644         }
1645         else {
1646                 if (syserror(tcp) || !verbose(tcp))
1647                         tprintf("%#lx", tcp->u_arg[1]);
1648                 else if (umove(tcp, tcp->u_arg[1], &statbuf) < 0)
1649                         tprintf("{...}");
1650                 else {
1651                         tprintf("{f_tfree=%lu, f_tinode=%lu, ",
1652                                 statbuf.f_tfree, statbuf.f_tinode);
1653                         tprintf("f_fname=\"%.*s\", ",
1654                                 (int) sizeof(statbuf.f_fname),
1655                                 statbuf.f_fname);
1656                         tprintf("f_fpack=\"%.*s\"}",
1657                                 (int) sizeof(statbuf.f_fpack),
1658                                 statbuf.f_fpack);
1659                 }
1660         }
1661         return 0;
1662 }
1663
1664 #endif /* SUNOS4 */
1665
1666 int
1667 sys_pivotroot(tcp)
1668 struct tcb *tcp;
1669 {
1670         if (entering(tcp)) {
1671                 printpath(tcp, tcp->u_arg[0]);
1672                 tprintf(", ");
1673                 printpath(tcp, tcp->u_arg[1]);
1674         }
1675         return 0;
1676 }
1677
1678
1679 /* directory */
1680 int
1681 sys_chdir(tcp)
1682 struct tcb *tcp;
1683 {
1684         if (entering(tcp)) {
1685                 printpath(tcp, tcp->u_arg[0]);
1686         }
1687         return 0;
1688 }
1689
1690 int
1691 sys_mkdir(tcp)
1692 struct tcb *tcp;
1693 {
1694         if (entering(tcp)) {
1695                 printpath(tcp, tcp->u_arg[0]);
1696                 tprintf(", %#lo", tcp->u_arg[1]);
1697         }
1698         return 0;
1699 }
1700
1701 int
1702 sys_rmdir(tcp)
1703 struct tcb *tcp;
1704 {
1705         if (entering(tcp)) {
1706                 printpath(tcp, tcp->u_arg[0]);
1707         }
1708         return 0;
1709 }
1710
1711 int
1712 sys_fchdir(tcp)
1713 struct tcb *tcp;
1714 {
1715         if (entering(tcp)) {
1716                 tprintf("%ld", tcp->u_arg[0]);
1717         }
1718         return 0;
1719 }
1720
1721 int
1722 sys_chroot(tcp)
1723 struct tcb *tcp;
1724 {
1725         if (entering(tcp)) {
1726                 printpath(tcp, tcp->u_arg[0]);
1727         }
1728         return 0;
1729 }
1730
1731 int
1732 sys_fchroot(tcp)
1733 struct tcb *tcp;
1734 {
1735         if (entering(tcp)) {
1736                 tprintf("%ld", tcp->u_arg[0]);
1737         }
1738         return 0;
1739 }
1740
1741 int
1742 sys_link(tcp)
1743 struct tcb *tcp;
1744 {
1745         if (entering(tcp)) {
1746                 printpath(tcp, tcp->u_arg[0]);
1747                 tprintf(", ");
1748                 printpath(tcp, tcp->u_arg[1]);
1749         }
1750         return 0;
1751 }
1752
1753 int
1754 sys_unlink(tcp)
1755 struct tcb *tcp;
1756 {
1757         if (entering(tcp)) {
1758                 printpath(tcp, tcp->u_arg[0]);
1759         }
1760         return 0;
1761 }
1762
1763 int
1764 sys_symlink(tcp)
1765 struct tcb *tcp;
1766 {
1767         if (entering(tcp)) {
1768                 printpath(tcp, tcp->u_arg[0]);
1769                 tprintf(", ");
1770                 printpath(tcp, tcp->u_arg[1]);
1771         }
1772         return 0;
1773 }
1774
1775 int
1776 sys_readlink(tcp)
1777 struct tcb *tcp;
1778 {
1779         if (entering(tcp)) {
1780                 printpath(tcp, tcp->u_arg[0]);
1781                 tprintf(", ");
1782         } else {
1783                 if (syserror(tcp))
1784                         tprintf("%#lx", tcp->u_arg[1]);
1785                 else
1786                         printpathn(tcp, tcp->u_arg[1], tcp->u_rval);
1787                 tprintf(", %lu", tcp->u_arg[2]);
1788         }
1789         return 0;
1790 }
1791
1792 int
1793 sys_rename(tcp)
1794 struct tcb *tcp;
1795 {
1796         if (entering(tcp)) {
1797                 printpath(tcp, tcp->u_arg[0]);
1798                 tprintf(", ");
1799                 printpath(tcp, tcp->u_arg[1]);
1800         }
1801         return 0;
1802 }
1803
1804 int
1805 sys_chown(tcp)
1806 struct tcb *tcp;
1807 {
1808         if (entering(tcp)) {
1809                 printpath(tcp, tcp->u_arg[0]);
1810                 printuid(", ", tcp->u_arg[1]);
1811                 printuid(", ", tcp->u_arg[2]);
1812         }
1813         return 0;
1814 }
1815
1816 int
1817 sys_fchown(tcp)
1818 struct tcb *tcp;
1819 {
1820         if (entering(tcp)) {
1821                 tprintf("%ld", tcp->u_arg[0]);
1822                 printuid(", ", tcp->u_arg[1]);
1823                 printuid(", ", tcp->u_arg[2]);
1824         }
1825         return 0;
1826 }
1827
1828 int
1829 sys_chmod(tcp)
1830 struct tcb *tcp;
1831 {
1832         if (entering(tcp)) {
1833                 printpath(tcp, tcp->u_arg[0]);
1834                 tprintf(", %#lo", tcp->u_arg[1]);
1835         }
1836         return 0;
1837 }
1838
1839 int
1840 sys_fchmod(tcp)
1841 struct tcb *tcp;
1842 {
1843         if (entering(tcp)) {
1844                 tprintf("%ld, %#lo", tcp->u_arg[0], tcp->u_arg[1]);
1845         }
1846         return 0;
1847 }
1848
1849 #ifdef ALPHA
1850 int
1851 sys_osf_utimes(tcp)
1852 struct tcb *tcp;
1853 {
1854     if (entering(tcp)) {
1855         printpath(tcp, tcp->u_arg[0]);
1856         tprintf(", ");
1857         printtv32(tcp, tcp->u_arg[1]);
1858     }
1859     return 0;
1860 }
1861 #endif
1862
1863 int
1864 sys_utimes(tcp)
1865 struct tcb *tcp;
1866 {
1867         if (entering(tcp)) {
1868                 printpath(tcp, tcp->u_arg[0]);
1869                 tprintf(", ");
1870                 printtv(tcp, tcp->u_arg[1]);
1871         }
1872         return 0;
1873 }
1874
1875 int
1876 sys_utime(tcp)
1877 struct tcb *tcp;
1878 {
1879         long ut[2];
1880
1881         if (entering(tcp)) {
1882                 printpath(tcp, tcp->u_arg[0]);
1883                 tprintf(", ");
1884                 if (!tcp->u_arg[1])
1885                         tprintf("NULL");
1886                 else if (!verbose(tcp))
1887                         tprintf("%#lx", tcp->u_arg[1]);
1888                 else if (umoven(tcp, tcp->u_arg[1], sizeof ut,
1889                     (char *) ut) < 0)
1890                         tprintf("[?, ?]");
1891                 else {
1892                         tprintf("[%s,", sprinttime(ut[0]));
1893                         tprintf(" %s]", sprinttime(ut[1]));
1894                 }
1895         }
1896         return 0;
1897 }
1898
1899 int
1900 sys_mknod(tcp)
1901 struct tcb *tcp;
1902 {
1903         int mode = tcp->u_arg[1];
1904
1905         if (entering(tcp)) {
1906                 printpath(tcp, tcp->u_arg[0]);
1907                 tprintf(", %s", sprintmode(mode));
1908                 switch (mode & S_IFMT) {
1909                 case S_IFCHR: case S_IFBLK:
1910 #ifdef LINUXSPARC
1911                         if (current_personality == 1)
1912                         tprintf(", makedev(%lu, %lu)",
1913                                 (unsigned long) ((tcp->u_arg[2] >> 18) & 0x3fff),
1914                                 (unsigned long) (tcp->u_arg[2] & 0x3ffff));
1915                         else
1916 #endif
1917                         tprintf(", makedev(%lu, %lu)",
1918                                 (unsigned long) major(tcp->u_arg[2]),
1919                                 (unsigned long) minor(tcp->u_arg[2]));
1920                         break;
1921                 default:
1922                         break;
1923                 }
1924         }
1925         return 0;
1926 }
1927
1928 int
1929 sys_mkfifo(tcp)
1930 struct tcb *tcp;
1931 {
1932         if (entering(tcp)) {
1933                 printpath(tcp, tcp->u_arg[0]);
1934                 tprintf(", %#lo", tcp->u_arg[1]);
1935         }
1936         return 0;
1937 }
1938
1939 int
1940 sys_fsync(tcp)
1941 struct tcb *tcp;
1942 {
1943         if (entering(tcp)) {
1944                 tprintf("%ld", tcp->u_arg[0]);
1945         }
1946         return 0;
1947 }
1948
1949 #ifdef LINUX
1950
1951 static void
1952 printdir(tcp, addr)
1953 struct tcb *tcp;
1954 long addr;
1955 {
1956         struct dirent d;
1957
1958         if (!verbose(tcp)) {
1959                 tprintf("%#lx", addr);
1960                 return;
1961         }
1962         if (umove(tcp, addr, &d) < 0) {
1963                 tprintf("{...}");
1964                 return;
1965         }
1966         tprintf("{d_ino=%ld, ", (unsigned long) d.d_ino);
1967         tprintf("d_name=");
1968         printpathn(tcp, (long) ((struct dirent *) addr)->d_name, d.d_reclen);
1969         tprintf("}");
1970 }
1971
1972 int
1973 sys_readdir(tcp)
1974 struct tcb *tcp;
1975 {
1976         if (entering(tcp)) {
1977                 tprintf("%lu, ", tcp->u_arg[0]);
1978         } else {
1979                 if (syserror(tcp) || tcp->u_rval == 0 || !verbose(tcp))
1980                         tprintf("%#lx", tcp->u_arg[1]);
1981                 else
1982                         printdir(tcp, tcp->u_arg[1]);
1983                 /* Not much point in printing this out, it is always 1. */
1984                 if (tcp->u_arg[2] != 1)
1985                         tprintf(", %lu", tcp->u_arg[2]);
1986         }
1987         return 0;
1988 }
1989
1990 #endif /* LINUX */
1991
1992 #if defined FREEBSD || defined LINUX
1993 const struct xlat direnttypes[] = {
1994         { DT_UNKNOWN,   "DT_UNKNOWN"    },
1995         { DT_FIFO,      "DT_FIFO"       },
1996         { DT_CHR,       "DT_CHR"        },
1997         { DT_DIR,       "DT_DIR"        },
1998         { DT_BLK,       "DT_BLK"        },
1999         { DT_REG,       "DT_REG"        },
2000         { DT_LNK,       "DT_LNK"        },
2001         { DT_SOCK,      "DT_SOCK"       },
2002         { DT_WHT,       "DT_WHT"        },
2003         { 0,            NULL            },
2004 };
2005
2006 #endif
2007
2008 int
2009 sys_getdents(tcp)
2010 struct tcb *tcp;
2011 {
2012         int i, len, dents = 0;
2013         char *buf;
2014
2015         if (entering(tcp)) {
2016                 tprintf("%lu, ", tcp->u_arg[0]);
2017                 return 0;
2018         }
2019         if (syserror(tcp) || !verbose(tcp)) {
2020                 tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
2021                 return 0;
2022         }
2023         len = tcp->u_rval;
2024         if ((buf = malloc(len)) == NULL) {
2025                 tprintf("out of memory\n");
2026                 return 0;
2027         }
2028         if (umoven(tcp, tcp->u_arg[1], len, buf) < 0) {
2029                 tprintf("{...}, %lu", tcp->u_arg[2]);
2030                 free(buf);
2031                 return 0;
2032         }
2033         if (!abbrev(tcp))
2034                 tprintf("{");
2035         for (i = 0; i < len;) {
2036                 struct kernel_dirent *d = (struct kernel_dirent *) &buf[i];
2037 #ifdef LINUX
2038                 if (!abbrev(tcp)) {
2039                         tprintf("%s{d_ino=%lu, d_off=%lu, ",
2040                                 i ? " " : "", d->d_ino, d->d_off);
2041                         tprintf("d_reclen=%u, d_name=\"%s\"}",
2042                                 d->d_reclen, d->d_name);
2043                 }
2044 #endif /* LINUX */
2045 #ifdef SVR4
2046                 if (!abbrev(tcp)) {
2047                         tprintf("%s{d_ino=%lu, d_off=%lu, ",
2048                                 i ? " " : "",
2049                                 (unsigned long) d->d_ino,
2050                                 (unsigned long) d->d_off);
2051                         tprintf("d_reclen=%u, d_name=\"%s\"}",
2052                                 d->d_reclen, d->d_name);
2053                 }
2054 #endif /* SVR4 */
2055 #ifdef SUNOS4
2056                 if (!abbrev(tcp)) {
2057                         tprintf("%s{d_off=%lu, d_fileno=%lu, d_reclen=%u, ",
2058                                 i ? " " : "", d->d_off, d->d_fileno,
2059                                 d->d_reclen);
2060                         tprintf("d_namlen=%u, d_name=\"%.*s\"}",
2061                                 d->d_namlen, d->d_namlen, d->d_name);
2062                 }
2063 #endif /* SUNOS4 */
2064 #ifdef FREEBSD
2065                 if (!abbrev(tcp)) {
2066                         tprintf("%s{d_fileno=%u, d_reclen=%u, d_type=",
2067                                 i ? " " : "", d->d_fileno, d->d_reclen);
2068                         printxval(direnttypes, d->d_type, "DT_???");
2069                         tprintf(", d_namlen=%u, d_name=\"%.*s\"}",
2070                                 d->d_namlen, d->d_namlen, d->d_name);
2071                 }
2072 #endif /* FREEBSD */
2073                 if (!d->d_reclen) {
2074                         tprintf("/* d_reclen == 0, problem here */");
2075                         break;
2076                 }
2077                 i += d->d_reclen;
2078                 dents++;
2079         }
2080         if (!abbrev(tcp))
2081                 tprintf("}");
2082         else
2083                 tprintf("/* %u entries */", dents);
2084         tprintf(", %lu", tcp->u_arg[2]);
2085         free(buf);
2086         return 0;
2087 }
2088
2089
2090 #if _LFS64_LARGEFILE
2091 int
2092 sys_getdents64(tcp)
2093 struct tcb *tcp;
2094 {
2095         int i, len, dents = 0;
2096         char *buf;
2097
2098         if (entering(tcp)) {
2099                 tprintf("%lu, ", tcp->u_arg[0]);
2100                 return 0;
2101         }
2102         if (syserror(tcp) || !verbose(tcp)) {
2103                 tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
2104                 return 0;
2105         }
2106         len = tcp->u_rval;
2107         if ((buf = malloc(len)) == NULL) {
2108                 tprintf("out of memory\n");
2109                 return 0;
2110         }
2111         if (umoven(tcp, tcp->u_arg[1], len, buf) < 0) {
2112                 tprintf("{...}, %lu", tcp->u_arg[2]);
2113                 free(buf);
2114                 return 0;
2115         }
2116         if (!abbrev(tcp))
2117                 tprintf("{");
2118         for (i = 0; i < len;) {
2119                 struct dirent64 *d = (struct dirent64 *) &buf[i];
2120 #if defined(LINUX) || defined(SVR4)
2121                 if (!abbrev(tcp)) {
2122                         tprintf("%s{d_ino=%llu, d_off=%llu, ",
2123                                 i ? " " : "",
2124                                 d->d_ino,
2125                                 d->d_off);
2126 #ifdef LINUX
2127                         tprintf("d_type=");
2128                         printxval(direnttypes, d->d_type, "DT_???");
2129                         tprintf(", ");
2130 #endif
2131                         tprintf("d_reclen=%u, d_name=\"%s\"}",
2132                                 d->d_reclen, d->d_name);
2133                 }
2134 #endif /* LINUX || SVR4 */
2135 #ifdef SUNOS4
2136                 if (!abbrev(tcp)) {
2137                         tprintf("%s{d_off=%lu, d_fileno=%lu, d_reclen=%u, ",
2138                                 i ? " " : "", d->d_off, d->d_fileno,
2139                                 d->d_reclen);
2140                         tprintf("d_namlen=%u, d_name=\"%.*s\"}",
2141                                 d->d_namlen, d->d_namlen, d->d_name);
2142                 }
2143 #endif /* SUNOS4 */
2144                 i += d->d_reclen;
2145                 dents++;
2146         }
2147         if (!abbrev(tcp))
2148                 tprintf("}");
2149         else
2150                 tprintf("/* %u entries */", dents);
2151         tprintf(", %lu", tcp->u_arg[2]);
2152         free(buf);
2153         return 0;
2154 }
2155 #endif
2156
2157 #ifdef FREEBSD
2158 int
2159 sys_getdirentries(tcp)
2160 struct tcb * tcp;
2161 {
2162         int i, len, dents = 0;
2163         long basep;
2164         char *buf;
2165
2166         if (entering(tcp)) {
2167                 tprintf("%lu, ", tcp->u_arg[0]);
2168                 return 0;
2169         }
2170         if (syserror(tcp) || !verbose(tcp)) {
2171                 tprintf("%#lx, %lu, %#lx", tcp->u_arg[1], tcp->u_arg[2], tcp->u_arg[3]);
2172                 return 0;
2173         }
2174         len = tcp->u_rval;
2175         if ((buf = malloc(len)) == NULL) {
2176                 tprintf("out of memory\n");
2177                 return 0;
2178         }
2179         if (umoven(tcp, tcp->u_arg[1], len, buf) < 0) {
2180                 tprintf("{...}, %lu, %#lx", tcp->u_arg[2], tcp->u_arg[3]);
2181                 free(buf);
2182                 return 0;
2183         }
2184         if (!abbrev(tcp))
2185                 tprintf("{");
2186         for (i = 0; i < len;) {
2187                 struct kernel_dirent *d = (struct kernel_dirent *) &buf[i];
2188                 if (!abbrev(tcp)) {
2189                         tprintf("%s{d_fileno=%u, d_reclen=%u, d_type=",
2190                                 i ? " " : "", d->d_fileno, d->d_reclen);
2191                         printxval(direnttypes, d->d_type, "DT_???");
2192                         tprintf(", d_namlen=%u, d_name=\"%.*s\"}",
2193                                 d->d_namlen, d->d_namlen, d->d_name);
2194                 }
2195                 i += d->d_reclen;
2196                 dents++;
2197         }
2198         if (!abbrev(tcp))
2199                 tprintf("}");
2200         else
2201                 tprintf("/* %u entries */", dents);
2202         free(buf);
2203         tprintf(", %lu", tcp->u_arg[2]);
2204         if (umove(tcp, tcp->u_arg[3], &basep) < 0)
2205                 tprintf(", %#lx", tcp->u_arg[3]);
2206         else
2207                 tprintf(", [%lu]", basep);
2208         return 0;
2209 }
2210 #endif
2211
2212 #ifdef LINUX
2213 int
2214 sys_getcwd(tcp)
2215 struct tcb *tcp;
2216 {
2217     if (exiting(tcp)) {
2218         if (syserror(tcp))
2219             tprintf("%#lx", tcp->u_arg[0]);
2220         else
2221             printpathn(tcp, tcp->u_arg[0], tcp->u_rval - 1);
2222         tprintf(", %lu", tcp->u_arg[1]);
2223     }
2224     return 0;
2225 }
2226 #endif /* LINUX */
2227
2228 #ifdef FREEBSD
2229 int
2230 sys___getcwd(tcp)
2231 struct tcb *tcp;
2232 {
2233     if (exiting(tcp)) {
2234         if (syserror(tcp))
2235             tprintf("%#lx", tcp->u_arg[0]);
2236         else
2237             printpathn(tcp, tcp->u_arg[0], tcp->u_arg[1]);
2238         tprintf(", %lu", tcp->u_arg[1]);
2239     }
2240     return 0;
2241 }
2242 #endif
2243
2244 #ifdef HAVE_SYS_ASYNCH_H
2245
2246 int
2247 sys_aioread(tcp)
2248 struct tcb *tcp;
2249 {
2250         struct aio_result_t res;
2251
2252         if (entering(tcp)) {
2253                 tprintf("%lu, ", tcp->u_arg[0]);
2254         } else {
2255                 if (syserror(tcp))
2256                         tprintf("%#lx", tcp->u_arg[1]);
2257                 else
2258                         printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2259                 tprintf(", %lu, %lu, ", tcp->u_arg[2], tcp->u_arg[3]);
2260                 printxval(whence, tcp->u_arg[4], "L_???");
2261                 if (syserror(tcp) || tcp->u_arg[5] == 0
2262                     || umove(tcp, tcp->u_arg[5], &res) < 0)
2263                         tprintf(", %#lx", tcp->u_arg[5]);
2264                 else
2265                         tprintf(", {aio_return %d aio_errno %d}",
2266                                 res.aio_return, res.aio_errno);
2267         }
2268         return 0;
2269 }
2270
2271 int
2272 sys_aiowrite(tcp)
2273 struct tcb *tcp;
2274 {
2275         struct aio_result_t res;
2276
2277         if (entering(tcp)) {
2278                 tprintf("%lu, ", tcp->u_arg[0]);
2279                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2280                 tprintf(", %lu, %lu, ", tcp->u_arg[2], tcp->u_arg[3]);
2281                 printxval(whence, tcp->u_arg[4], "L_???");
2282         }
2283         else {
2284                 if (tcp->u_arg[5] == 0)
2285                         tprintf(", NULL");
2286                 else if (syserror(tcp)
2287                     || umove(tcp, tcp->u_arg[5], &res) < 0)
2288                         tprintf(", %#lx", tcp->u_arg[5]);
2289                 else
2290                         tprintf(", {aio_return %d aio_errno %d}",
2291                                 res.aio_return, res.aio_errno);
2292         }
2293         return 0;
2294 }
2295
2296 int
2297 sys_aiowait(tcp)
2298 struct tcb *tcp;
2299 {
2300         if (entering(tcp))
2301                 printtv(tcp, tcp->u_arg[0]);
2302         return 0;
2303 }
2304
2305 int
2306 sys_aiocancel(tcp)
2307 struct tcb *tcp;
2308 {
2309         struct aio_result_t res;
2310
2311         if (exiting(tcp)) {
2312                 if (tcp->u_arg[0] == 0)
2313                         tprintf("NULL");
2314                 else if (syserror(tcp)
2315                     || umove(tcp, tcp->u_arg[0], &res) < 0)
2316                         tprintf("%#lx", tcp->u_arg[0]);
2317                 else
2318                         tprintf("{aio_return %d aio_errno %d}",
2319                                 res.aio_return, res.aio_errno);
2320         }
2321         return 0;
2322 }
2323
2324 #endif /* HAVE_SYS_ASYNCH_H */
2325
2326 const struct xlat xattrflags[] = {
2327 #ifdef XATTR_CREATE
2328         { XATTR_CREATE,  "XATTR_CREATE" },
2329         { XATTR_REPLACE, "XATTR_REPLACE" },
2330 #endif
2331         { 0,             NULL }
2332 };
2333
2334 static void
2335 print_xattr_val(tcp, failed, arg, insize, size)
2336 struct tcb *tcp;
2337 int failed;
2338 unsigned long arg;
2339 size_t size;
2340 {
2341     unsigned char buf[4 * size + 1];
2342     if (!failed && umoven(tcp, arg, size, &buf[3 * size]) >= 0) {
2343         unsigned char *out = buf;
2344         unsigned char *in = &buf[3 * size];
2345         size_t i;
2346         for (i = 0; i < size; ++i)
2347             if (isprint(in[i]))
2348                 *out++ = in[i];
2349             else {
2350 #define tohex(n) "0123456789abcdef"[n]
2351                 *out++ = '\\';
2352                 *out++ = 'x';
2353                 *out++ = tohex(in[i] / 16);
2354                 *out++ = tohex(in[i] % 16);
2355             }
2356         /* Don't print terminating NUL if there is one.  */
2357         if (in[i - 1] == '\0')
2358             out -= 4;
2359         *out = '\0';
2360         tprintf(", \"%s\", %zd", buf, insize);
2361     } else
2362         tprintf(", 0x%lx, %zd", arg, insize);
2363 }
2364
2365 int
2366 sys_setxattr(tcp)
2367 struct tcb *tcp;
2368 {
2369     if (entering(tcp)) {
2370         printpath(tcp, tcp->u_arg[0]);
2371         tprintf(", ");
2372         printstr(tcp, tcp->u_arg[1], -1);
2373         print_xattr_val(tcp, 0, tcp->u_arg[2], tcp->u_arg[3], tcp->u_arg[3]);
2374         tprintf(", ");
2375         printflags(xattrflags, tcp->u_arg[4]);
2376     }
2377     return 0;
2378 }
2379
2380 int
2381 sys_fsetxattr(tcp)
2382 struct tcb *tcp;
2383 {
2384     if (entering(tcp)) {
2385         tprintf("%ld, ", tcp->u_arg[0]);
2386         printstr(tcp, tcp->u_arg[1], -1);
2387         print_xattr_val(tcp, 0, tcp->u_arg[2], tcp->u_arg[3], tcp->u_arg[3]);
2388         tprintf(", ");
2389         printflags(xattrflags, tcp->u_arg[4]);
2390     }
2391     return 0;
2392 }
2393
2394 int
2395 sys_getxattr(tcp)
2396 struct tcb *tcp;
2397 {
2398     if (entering(tcp)) {
2399         printpath(tcp, tcp->u_arg[0]);
2400         tprintf(", ");
2401         printstr(tcp, tcp->u_arg[1], -1);
2402     } else {
2403         print_xattr_val(tcp, syserror(tcp), tcp->u_arg[2], tcp->u_arg[3],
2404                         tcp->u_rval);
2405     }
2406     return 0;
2407 }
2408
2409 int
2410 sys_fgetxattr(tcp)
2411 struct tcb *tcp;
2412 {
2413     if (entering(tcp)) {
2414         tprintf("%ld, ", tcp->u_arg[0]);
2415         printstr(tcp, tcp->u_arg[1], -1);
2416     } else {
2417         print_xattr_val(tcp, syserror(tcp), tcp->u_arg[2], tcp->u_arg[3],
2418                         tcp->u_rval);
2419     }
2420     return 0;
2421 }
2422
2423 int
2424 sys_listxattr(tcp)
2425 struct tcb *tcp;
2426 {
2427     if (entering(tcp)) {
2428         printpath(tcp, tcp->u_arg[0]);
2429     } else {
2430         /* XXX Print value in format */
2431         tprintf(", %p, %lu", (void *) tcp->u_arg[1], tcp->u_arg[2]);
2432     }
2433     return 0;
2434 }
2435
2436 int
2437 sys_flistxattr(tcp)
2438 struct tcb *tcp;
2439 {
2440     if (entering(tcp)) {
2441         tprintf("%ld", tcp->u_arg[0]);
2442     } else {
2443         /* XXX Print value in format */
2444         tprintf(", %p, %lu", (void *) tcp->u_arg[1], tcp->u_arg[2]);
2445     }
2446     return 0;
2447 }
2448
2449 int
2450 sys_removexattr(tcp)
2451 struct tcb *tcp;
2452 {
2453     if (entering(tcp)) {
2454         printpath(tcp, tcp->u_arg[0]);
2455         tprintf(", ");
2456         printstr(tcp, tcp->u_arg[1], -1);
2457     }
2458     return 0;
2459 }
2460
2461 int
2462 sys_fremovexattr(tcp)
2463 struct tcb *tcp;
2464 {
2465     if (entering(tcp)) {
2466         tprintf("%ld, ", tcp->u_arg[0]);
2467         printstr(tcp, tcp->u_arg[1], -1);
2468     }
2469     return 0;
2470 }
2471
2472
2473 static const struct xlat advise[] = {
2474   { POSIX_FADV_NORMAL,          "POSIX_FADV_NORMAL"     },
2475   { POSIX_FADV_RANDOM,          "POSIX_FADV_RANDOM"     },
2476   { POSIX_FADV_SEQUENTIAL,      "POSIX_FADV_SEQUENTIAL" },
2477   { POSIX_FADV_WILLNEED,        "POSIX_FADV_WILLNEED"   },
2478   { POSIX_FADV_DONTNEED,        "POSIX_FADV_DONTNEED"   },
2479   { POSIX_FADV_NOREUSE,         "POSIX_FADV_NOREUSE"    },
2480   { 0,                          NULL                    }
2481 };
2482
2483
2484 #if defined LINUX && (defined I386 || defined X86_64)
2485 int
2486 sys_fadvise64(tcp)
2487 struct tcb *tcp;
2488 {
2489     if (entering(tcp)) {
2490         tprintf("%ld, %lld, %ld, ",
2491                 tcp->u_arg[0],
2492 # if defined IA64 || defined X86_64 || defined ALPHA
2493                 (long long int) tcp->u_arg[1], tcp->u_arg[2]);
2494         printxval(advise, tcp->u_arg[3], "POSIX_FADV_???");
2495 #else
2496                 LONG_LONG(tcp->u_arg[1], tcp->u_arg[2]), tcp->u_arg[3]);
2497         printxval(advise, tcp->u_arg[4], "POSIX_FADV_???");
2498 #endif
2499     }
2500     return 0;
2501 }
2502 #endif
2503
2504
2505 int
2506 sys_fadvise64_64(tcp)
2507 struct tcb *tcp;
2508 {
2509     if (entering(tcp)) {
2510         tprintf("%ld, %lld, %lld, ",
2511                 tcp->u_arg[0],
2512 # if defined IA64 || defined X86_64 || defined ALPHA
2513                 (long long int) tcp->u_arg[1], (long long int) tcp->u_arg[2]);
2514         printxval(advise, tcp->u_arg[3], "POSIX_FADV_???");
2515 #else
2516                 LONG_LONG(tcp->u_arg[1], tcp->u_arg[2]),
2517                 LONG_LONG(tcp->u_arg[3], tcp->u_arg[4]));
2518         printxval(advise, tcp->u_arg[5], "POSIX_FADV_???");
2519 #endif
2520     }
2521     return 0;
2522 }