home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3999 / file.c next >
Encoding:
C/C++ Source or Header  |  1991-09-09  |  6.7 KB  |  411 lines

  1. /*
  2.  * @(#)file.c    1.6 91/09/06
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/ptrace.h>
  8. #include <sys/file.h>
  9. #include <sys/stat.h>
  10. #include <sys/time.h>
  11.  
  12. #include "defs.h"
  13.  
  14. static Xlat *op, openmodes[] = {
  15.     {O_RDONLY, "RDONLY"},    /* read enable */
  16.     {O_WRONLY, "WRONLY"},    /* write enable */
  17.     {O_RDWR, "RDWR"},    /* read/write enable */
  18.     {_FNDELAY, "NDELAY"},    /* non blocking I/O (4.2 style) */
  19.     {_FAPPEND, "APPEND"},    /* append (writes guaranteed at the end) */
  20.     {_FMARK, "MARK"},    /* internal; mark during gc() */
  21.     {_FDEFER, "DEFER"},    /* internal; defer for next gc pass */
  22.     {_FASYNC, "ASYNC"},    /* signal pgrp when data ready */
  23.     {_FSHLOCK, "SHLOCK"},    /* BSD flock() shared lock present */
  24.     {_FEXLOCK, "EXLOCK"},    /* BSD flock() exclusive lock present */
  25.     {_FCREAT, "CREAT"},    /* open with file create */
  26.     {_FTRUNC, "TRUNC"},    /* open with truncation */
  27.     {_FEXCL, "EXCL"},    /* error on open if file exists */
  28.     {_FNBIO, "NBIO"},    /* non blocking I/O (sys5 style) */
  29.     {_FSYNC, "SYNC"},    /* do all writes synchronously */
  30.     {_FNONBLOCK, "NONBLOCK"},    /* non blocking I/O (POSIX style) */
  31.     {_FNOCTTY, "NOCTTY"},    /* don't assign a ctty on this open */
  32.     {0, NULL},
  33. };
  34.  
  35. int
  36. sys_open(tcp)
  37. struct tcb *tcp;
  38. {
  39.     if (entering(tcp)) {
  40.         printstr(tcp->pid, tcp->u_args[0], -1);
  41.         fprintf(outf, ", ");
  42.         /* flags */
  43.         printxval(openmodes, tcp->u_args[1] & O_ACCMODE, "O_???");
  44.         addflags(openmodes, tcp->u_args[1]);
  45.         /* mode */
  46.         fprintf(outf, ", %o", tcp->u_args[2]);
  47.     }
  48.     return 0;
  49. }
  50.  
  51. int
  52. sys_creat(tcp)
  53. struct tcb *tcp;
  54. {
  55.     if (entering(tcp)) {
  56.         printstr(tcp->pid, tcp->u_args[0], -1);
  57.         fprintf(outf, ", %o", tcp->u_args[1]);
  58.     }
  59.     return 0;
  60. }
  61.  
  62. int
  63. sys_umask(tcp)
  64. struct tcb *tcp;
  65. {
  66.     if (entering(tcp)) {
  67.         fprintf(outf, "%o", tcp->u_args[0]);
  68.     }
  69.     return 0;
  70. }
  71.  
  72. static Xlat whence[] = {
  73.     0,    "SET",
  74.     1,    "CUR",
  75.     2,    "END",
  76.     0,    NULL,
  77. };
  78.  
  79. int
  80. sys_lseek(tcp)
  81. struct tcb *tcp;
  82. {
  83.     if (entering(tcp)) {
  84.         fprintf(outf, "%u, %u, ", tcp->u_args[0], tcp->u_args[1]);
  85.         printxval(whence, tcp->u_args[2], "L_???");
  86.     }
  87.     return 0;
  88. }
  89.  
  90. int
  91. sys_truncate(tcp)
  92. struct tcb *tcp;
  93. {
  94.     if (entering(tcp)) {
  95.         printstr(tcp->pid, tcp->u_args[0], -1);
  96.         fprintf(outf, ", %u", tcp->u_args[1]);
  97.     }
  98.     return 0;
  99. }
  100.  
  101. int
  102. sys_ftruncate(tcp)
  103. struct tcb *tcp;
  104. {
  105.     if (entering(tcp)) {
  106.         fprintf(outf, "%u, %u", tcp->u_args[0], tcp->u_args[1]);
  107.     }
  108.     return 0;
  109. }
  110.  
  111.  
  112. /* several stats */
  113. static void
  114. printstat(pid, addr)
  115. {
  116.     struct stat statbuf;
  117.  
  118.     if (umove(pid, addr, sizeof statbuf, (char *)&statbuf) < 0) {
  119.         fprintf(outf, "[...]");
  120.         return;
  121.     }
  122.     fprintf(outf,
  123.     "[dev %x %x ino %u nlnks %d ...]",
  124.         major(statbuf.st_dev), minor(statbuf.st_dev),
  125.         statbuf.st_ino, statbuf.st_nlink);
  126. }
  127.  
  128. int
  129. sys_stat(tcp)
  130. struct tcb *tcp;
  131. {
  132.     if (entering(tcp)) {
  133.         printstr(tcp->pid, tcp->u_args[0], -1);
  134.         fprintf(outf, ", ");
  135.     } else {
  136.         if (syserror(tcp))
  137.             fprintf(outf, "%#x", tcp->u_args[1]);
  138.         else
  139.             printstat(tcp->pid, tcp->u_args[1]);
  140.     }
  141.     return 0;
  142. }
  143.  
  144. int
  145. sys_fstat(tcp)
  146. struct tcb *tcp;
  147. {
  148.     if (entering(tcp)) {
  149.         fprintf(outf, "%u, ", tcp->u_args[0]);
  150.     } else {
  151.         if (syserror(tcp))
  152.             fprintf(outf, "%#x", tcp->u_args[1]);
  153.         else
  154.             printstat(tcp->pid, tcp->u_args[1]);
  155.     }
  156.     return 0;
  157. }
  158.  
  159. int
  160. sys_lstat(tcp)
  161. struct tcb *tcp;
  162. {
  163.     if (entering(tcp)) {
  164.         printstr(tcp->pid, tcp->u_args[0], -1);
  165.         fprintf(outf, ", ");
  166.     } else {
  167.         if (syserror(tcp))
  168.             fprintf(outf, "%#x", tcp->u_args[1]);
  169.         else
  170.             printstat(tcp->pid, tcp->u_args[1]);
  171.     }
  172.     return 0;
  173. }
  174.  
  175. /* directory */
  176. int
  177. sys_chdir(tcp)
  178. struct tcb *tcp;
  179. {
  180.     if (entering(tcp)) {
  181.         printstr(tcp->pid, tcp->u_args[0], -1);
  182.     }
  183.     return 0;
  184. }
  185.  
  186. int
  187. sys_mkdir(tcp)
  188. struct tcb *tcp;
  189. {
  190.     if (entering(tcp)) {
  191.         printstr(tcp->pid, tcp->u_args[0], -1);
  192.         fprintf(outf, ", %o", tcp->u_args[1]);
  193.     }
  194.     return 0;
  195. }
  196.  
  197. int
  198. sys_rmdir(tcp)
  199. struct tcb *tcp;
  200. {
  201.     if (entering(tcp)) {
  202.         printstr(tcp->pid, tcp->u_args[0], -1);
  203.     }
  204.     return 0;
  205. }
  206.  
  207. int
  208. sys_fchdir(tcp)
  209. struct tcb *tcp;
  210. {
  211.     if (entering(tcp)) {
  212.         fprintf(outf, "%u", tcp->u_args[0]);
  213.     }
  214.     return 0;
  215. }
  216.  
  217. int
  218. sys_chroot(tcp)
  219. struct tcb *tcp;
  220. {
  221.     if (entering(tcp)) {
  222.         printstr(tcp->pid, tcp->u_args[0], -1);
  223.     }
  224.     return 0;
  225. }
  226.  
  227. int
  228. sys_fchroot(tcp)
  229. struct tcb *tcp;
  230. {
  231.     if (entering(tcp)) {
  232.         fprintf(outf, "%u", tcp->u_args[0]);
  233.     }
  234.     return 0;
  235. }
  236.  
  237. int
  238. sys_link(tcp)
  239. struct tcb *tcp;
  240. {
  241.     if (entering(tcp)) {
  242.         printstr(tcp->pid, tcp->u_args[0], -1);
  243.         fprintf(outf, ", ");
  244.         printstr(tcp->pid, tcp->u_args[1], -1);
  245.     }
  246.     return 0;
  247. }
  248.  
  249. int
  250. sys_unlink(tcp)
  251. struct tcb *tcp;
  252. {
  253.     if (entering(tcp)) {
  254.         printstr(tcp->pid, tcp->u_args[0], -1);
  255.     }
  256.     return 0;
  257. }
  258.  
  259. int
  260. sys_symlink(tcp)
  261. struct tcb *tcp;
  262. {
  263.     if (entering(tcp)) {
  264.         printstr(tcp->pid, tcp->u_args[0], -1);
  265.         fprintf(outf, ", ");
  266.         printstr(tcp->pid, tcp->u_args[1], -1);
  267.     }
  268.     return 0;
  269. }
  270.  
  271. int
  272. sys_readlink(tcp)
  273. struct tcb *tcp;
  274. {
  275.     if (entering(tcp)) {
  276.         printstr(tcp->pid, tcp->u_args[0], -1);
  277.         fprintf(outf, ", ");
  278.     } else {
  279.         if (syserror(tcp))
  280.             fprintf(outf, "%#x", tcp->u_args[1]);
  281.         else
  282.             printstr(tcp->pid, tcp->u_args[1], tcp->u_rval);
  283.         fprintf(outf, ", %u", tcp->u_args[2]);
  284.     }
  285.     return 0;
  286. }
  287.  
  288. int
  289. sys_rename(tcp)
  290. struct tcb *tcp;
  291. {
  292.     if (entering(tcp)) {
  293.         printstr(tcp->pid, tcp->u_args[0], -1);
  294.         fprintf(outf, ", ");
  295.         printstr(tcp->pid, tcp->u_args[1], -1);
  296.     }
  297.     return 0;
  298. }
  299.  
  300. int
  301. sys_chown(tcp)
  302. struct tcb *tcp;
  303. {
  304.     if (entering(tcp)) {
  305.         printstr(tcp->pid, tcp->u_args[0], -1);
  306.         fprintf(outf, ", %u, %u", tcp->u_args[1], tcp->u_args[2]);
  307.     }
  308.     return 0;
  309. }
  310.  
  311. int
  312. sys_fchown(tcp)
  313. struct tcb *tcp;
  314. {
  315.     if (entering(tcp)) {
  316.         fprintf(outf, "%u, %u, %u",
  317.             tcp->u_args[0], tcp->u_args[1], tcp->u_args[2]);
  318.     }
  319.     return 0;
  320. }
  321.  
  322. int
  323. sys_chmod(tcp)
  324. struct tcb *tcp;
  325. {
  326.     if (entering(tcp)) {
  327.         printstr(tcp->pid, tcp->u_args[0], -1);
  328.         fprintf(outf, ", %o", tcp->u_args[1]);
  329.     }
  330.     return 0;
  331. }
  332.  
  333. int
  334. sys_fchmod(tcp)
  335. struct tcb *tcp;
  336. {
  337.     if (entering(tcp)) {
  338.         fprintf(outf, "%u, %o", tcp->u_args[0], tcp->u_args[1]);
  339.     }
  340.     return 0;
  341. }
  342.  
  343. int
  344. sys_utimes(tcp)
  345. struct tcb *tcp;
  346. {
  347.     if (entering(tcp)) {
  348.         printstr(tcp->pid, tcp->u_args[0], -1);
  349.         fprintf(outf, ", ");
  350.         printtv(tcp->pid, tcp->u_args[1]);
  351.     }
  352.     return 0;
  353. }
  354.  
  355. int
  356. sys_mknod(tcp)
  357. struct tcb *tcp;
  358. {
  359.     int mode = tcp->u_args[1];
  360.  
  361.     if (entering(tcp)) {
  362.         printstr(tcp->pid, tcp->u_args[0], -1);
  363.         fprintf(outf, ", %o(%s)", mode,
  364.             mode==S_IFCHR?"CHR":(
  365.                 mode==S_IFBLK?"BLK":(
  366.                     mode==S_IFREG?"REG":(
  367.                         mode==S_IFIFO?"FIFO":""
  368.                     )
  369.                 )
  370.             )
  371.         );
  372.         fprintf(outf, ", dev %x %x",
  373.                 major(tcp->u_args[2]), minor(tcp->u_args[2]));
  374.     }
  375.     return 0;
  376. }
  377.  
  378. int
  379. sys_mkfifo(tcp)
  380. struct tcb *tcp;
  381. {
  382.     if (entering(tcp)) {
  383.         printstr(tcp->pid, tcp->u_args[0], -1);
  384.         fprintf(outf, ", %o", tcp->u_args[1]);
  385.     }
  386.     return 0;
  387. }
  388.  
  389. int
  390. sys_fsync(tcp)
  391. struct tcb *tcp;
  392. {
  393.     if (entering(tcp)) {
  394.         fprintf(outf, "%u", tcp->u_args[0]);
  395.     }
  396.     return 0;
  397. }
  398.  
  399. int
  400. sys_getdents(tcp)
  401. struct tcb *tcp;
  402. {
  403.     if (entering(tcp)) {
  404.         fprintf(outf, "%u", tcp->u_args[0]);
  405.         fprintf(outf, ", %#x", tcp->u_args[1]);
  406.         fprintf(outf, ", %u", tcp->u_args[2]);
  407.     }
  408.     return 0;
  409. }
  410.  
  411.