home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / stat.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  8KB  |  313 lines

  1. /*
  2.  * stat, fstat, lstat emulation for TOS
  3.  * written by Eric R. Smith and placed in the public domain
  4.  */
  5.  
  6. #include <limits.h>
  7. #include <types.h>
  8. #include <stat.h>
  9. #include <ctype.h>
  10. #include <errno.h>
  11. #include <osbind.h>
  12. #include <mintbind.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include <unistd.h>
  16. #include <ioctl.h>    /* for FSTAT */
  17. #include "lib.h"
  18.  
  19. extern int __mint;
  20.  
  21. ino_t    __inode = 32;        /* used in readdir also */
  22.  
  23. /* for backwards compatibilty: if nonzero, files are checked to see if
  24.  * they have the TOS executable magic number in them
  25.  */
  26.  
  27. int    _x_Bit_set_in_stat = 0;
  28.  
  29.  
  30.  
  31. /* date for files (like root directories) that don't have one */
  32. #define OLDDATE _unixtime(0,0)
  33.  
  34. /*
  35.  * macro for converting a long in DOS format to one in Unix format. "x"
  36.  * _must_ be an lvalue!
  37.  */
  38.  
  39. #define CONVERT(x) (x = _unixtime( ((short *)&x)[0], ((short *)&x)[1] ))
  40.  
  41. /*
  42.  * common routine for stat() and lstat(); if "lflag" is 0, then symbolic
  43.  * links are automatically followed (like stat), if 1 then they are not
  44.  * (like lstat)
  45.  */
  46.  
  47. static int do_stat __PROTO((const char *_path, struct stat *st, 
  48.     int lflag));
  49.  
  50. static int
  51. do_stat(_path, st, lflag)
  52.     const char *_path;
  53.     struct stat *st;
  54.     int lflag;
  55. {
  56.     long    r;
  57.     _DTA    *olddta;
  58.     int    nval;
  59.     char    path[PATH_MAX];
  60.     char    *ext, drv;
  61.     int    fd;
  62.     short    magic;
  63.     _DTA    d;
  64.     int    isdot = 0;
  65.  
  66.     if (!_path) {
  67.         errno = EFAULT;
  68.         return -1;
  69.     }
  70.  
  71. /*
  72.  * _unx2dos returns 1 for device names (like /dev/con)
  73.  */
  74.     nval = _unx2dos(_path, path);
  75.  
  76. /* for MiNT 0.9 and up, we use the built in stat() call */
  77.     if (__mint >= 9) {
  78.         r = Fxattr(lflag, path, st);
  79.         if (r) {
  80.             errno = (int) -r;
  81.             return -1;
  82.         }
  83.         CONVERT(st->st_mtime);
  84.         CONVERT(st->st_atime);
  85.         CONVERT(st->st_ctime);
  86.     /* Most versions of Unix count in 512 byte blocks */
  87.         st->st_blocks = (st->st_blocks * st->st_blksize) / 512;
  88.         return 0;
  89.     }
  90.  
  91. /* otherwise, check to see if we have a name like CON: or AUX: */
  92.     if (nval == 1) {
  93.         st->st_mode = S_IFCHR | 0600;
  94.         st->st_attr = 0;
  95.         st->st_ino = ++__inode;
  96.         st->st_rdev = 0;
  97.         st->st_mtime = st->st_ctime = st->st_atime = 
  98.             time((time_t *)0) - 2;
  99.         st->st_dev = 0;
  100.         st->st_nlink = 1;
  101.         st->st_uid = geteuid();
  102.         st->st_gid = getegid();
  103.         st->st_size = st->st_blocks = 0;
  104.         st->st_blksize = 1024;
  105.         return 0;
  106.     }
  107.  
  108. /* A file name: check for root directory of a drive */
  109.     if (path[0] == '\\' && path[1] == 0) {
  110.         drv = Dgetdrv() + 'A';
  111.         goto rootdir;
  112.     }
  113.  
  114.     if ( ((drv = path[0]) != 0) && path[1] == ':' &&
  115.          (path[2] == 0 || (path[2] == '\\' && path[3] == 0)) ) {
  116. rootdir:
  117.         st->st_mode = S_IFDIR | 0755;
  118.         st->st_attr = FA_DIR;
  119.         st->st_dev = isupper(drv) ? drv - 'A' : drv - 'a';
  120.         st->st_ino = 2;
  121.         st->st_mtime = st->st_ctime = st->st_atime = OLDDATE;
  122.         goto fill_dir;
  123.     }
  124.  
  125. /* forbid wildcards in path names */
  126.     if (index(path, '*') || index(path, '?')) {
  127.         errno = ENOENT;
  128.         return -1;
  129.     }
  130.  
  131. /* OK, here we're going to have to do an Fsfirst to get the date */
  132. /* NOTE: Fsfirst(".",-1) or Fsfirst("..",-1) both fail under TOS,
  133.  * so we kludge around this by using the fact that Fsfirst(".\*.*"
  134.  * or "..\*.*" will return the correct file first (except, of course,
  135.  * in root directories :-( ).
  136.  */
  137.  
  138. /* find the end of the string */
  139.     for (ext = path; ext[0] && ext[1]; ext++) ;
  140.  
  141. /* add appropriate kludge if necessary */
  142.     if (*ext == '.' && (ext == path || ext[-1] == '\\' || ext[-1] == '.')) {
  143.         isdot = 1;
  144.         strcat(path, "\\*.*");
  145.     }
  146.     olddta = Fgetdta();
  147.     Fsetdta(&d);
  148.     r = Fsfirst(path, 0xff);
  149.     Fsetdta(olddta);
  150.     if (r < 0) {
  151.         if (isdot && r == -ENOENT) goto rootdir;
  152.         errno = (int) -r;
  153.         return -1;
  154.     }    
  155.  
  156.     if (isdot && ((d.dta_name[0] != '.') || (d.dta_name[1]))) {
  157.         goto rootdir;
  158.     }
  159.  
  160.     st->st_mtime = st->st_ctime = st->st_atime =
  161.         _unixtime(d.dta_time, d.dta_date);
  162.     if (((drv = *path) != 0) && path[1] == ':')
  163.         st->st_dev = toupper(drv) - 'A';
  164.     else
  165.         st->st_dev = Dgetdrv();
  166.  
  167.     st->st_ino = __inode++;
  168.     st->st_attr = d.dta_attribute;
  169.     if (__mint && st->st_dev == ('Q' - 'A'))
  170.             st->st_mode = 0644 | S_IFIFO;
  171.     else {
  172.         st->st_mode = 0644 | (st->st_attr & FA_DIR ?
  173.                   S_IFDIR | 0111 : S_IFREG);
  174.     }
  175.  
  176.     if (st->st_attr & FA_RDONLY)
  177.         st->st_mode &= ~0222;    /* no write permission */
  178.     if (st->st_attr & FA_HIDDEN)
  179.         st->st_mode &= ~0444;    /* no read permission */
  180.  
  181. /* check for a file with an executable extension */
  182.     ext = strrchr(_path, '.');
  183.     if (ext) {
  184.         if (!strcmp(ext, ".ttp") || !strcmp(ext, ".prg") ||
  185.             !strcmp(ext, ".tos") || !strcmp(ext, ".g") ||
  186.             !strcmp(ext, ".sh")     || !strcmp(ext, ".bat")) {
  187.             st->st_mode |= 0111;
  188.         }
  189.     }
  190.     if ( (st->st_mode & S_IFMT) == S_IFREG) {
  191.         if (_x_Bit_set_in_stat) {
  192.             if ((fd = (int) Fopen(path,0)) < 0) {
  193.                 errno = -fd;
  194.                 return -1;
  195.             }
  196.             magic = 0;
  197.             (void)Fread(fd,2,(char *)&magic);
  198.             (void)Fclose(fd);
  199.             if (magic == 0x601A    /* TOS executable */
  200.                 || magic == 0x2321) /* "#!" shell file */
  201.                 st->st_mode |= 0111;
  202.         }
  203.         st->st_size = d.dta_size;
  204.     /* in Unix, blocks are measured in 512 bytes */
  205.         st->st_blocks = (st->st_size + 511) / 512;
  206.         st->st_nlink = 1; /* we dont have hard links */
  207.     } else {
  208. fill_dir:
  209.         st->st_size = 1024;
  210.         st->st_blocks = 2;
  211.         st->st_nlink = 2;    /* "foo" && "foo/.." */
  212.     }
  213.  
  214.     st->st_rdev = 0;
  215.     st->st_uid = geteuid();    /* the current user owns every file */
  216.     st->st_gid = getegid();
  217.     st->st_blksize = 1024;
  218.     return 0;
  219. }
  220.  
  221.  
  222. /* 
  223.  * fstat: if we're not running under MiNT, this is pretty bogus.
  224.  * what we can really find is:
  225.  * modification time: via Fdatime()
  226.  * file size: via Fseek()
  227.  * fortunately, these are the things most programs are interested in.
  228.  * BUG: passing an invalid file descriptor gets back a stat structure for
  229.  * a tty.
  230.  */
  231.  
  232. int
  233. fstat(fd, st)
  234. int fd;
  235. struct stat *st;
  236. {
  237.     long oldplace, r;
  238.     _DOSTIME timeptr;
  239.     short magic;
  240.  
  241.     if (__mint >= 9) {        /* use FSTAT Fcntl */
  242.         r = Fcntl(fd, st, FSTAT);
  243.         if (r) {
  244.             errno = (int) -r;
  245.             return -1;
  246.         }
  247.         CONVERT(st->st_mtime);
  248.         CONVERT(st->st_atime);
  249.         CONVERT(st->st_ctime);
  250.         st->st_blocks = (st->st_blocks * st->st_blksize) / 512;
  251.         return 0;
  252.     }
  253.  
  254.     r = Fdatime(&timeptr, fd, 0);
  255.     if (r < 0) {            /* assume TTY */
  256.         st->st_mode = S_IFCHR | 0600;
  257.         st->st_attr = 0;
  258.         st->st_mtime = st->st_ctime = st->st_atime =
  259.             time((time_t *)0) - 2;
  260.         st->st_size = 0;
  261.     } else {
  262.         st->st_mtime = st->st_atime = st->st_ctime =
  263.             _unixtime(timeptr.time, timeptr.date);
  264.         st->st_mode = S_IFREG | 0644;        /* this may be false */
  265.         st->st_attr = 0;            /* because this is */
  266.  
  267.     /* get current file location */
  268.         oldplace = Fseek(0L, fd, SEEK_CUR);
  269.         if (oldplace < 0) {        /* can't seek -- must be pipe */
  270.             st->st_mode = S_IFIFO | 0644;
  271.             st->st_size = 1024;
  272.         } else {
  273.             r = Fseek(0L, fd, SEEK_END);    /* go to end of file */
  274.             st->st_size = r;
  275.             (void)Fseek(0L, fd, SEEK_SET);    /* go to start of file */
  276.             /* check for executable file */
  277.             if (Fread(fd, 2, (char *)&magic) == 2) {
  278.                 if (magic == 0x601a || magic == 0x2321)
  279.                     st->st_mode |= 0111;
  280.             }
  281.             (void)Fseek(oldplace, fd, SEEK_SET);
  282.         }
  283.     }
  284.  
  285. /* all this stuff is likely bogus as well. sigh. */
  286.     st->st_dev = Dgetdrv();
  287.     st->st_rdev = 0;
  288.     st->st_uid = getuid();
  289.     st->st_gid = getgid();
  290.     st->st_blksize = 1024;
  291. /* note: most Unixes measure st_blocks in 512 byte units */
  292.     st->st_blocks = (st->st_size + 511) / 512;
  293.     st->st_ino = ++__inode;
  294.     st->st_nlink = 1;
  295.     return 0;
  296. }
  297.  
  298. int
  299. lstat(path, st)
  300.     const char *path;
  301.     struct stat *st;
  302. {
  303.     return do_stat(path, st, 1);
  304. }
  305.  
  306. int
  307. stat(path, st)
  308.     const char *path;
  309.     struct stat *st;
  310. {
  311.     return do_stat(path, st, 0);
  312. }
  313.