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