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