1 /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
2 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
3 /* This is file STAT.C */
4 /*
5  *   Almost a 100% U**X-compatible stat() substitute.
6  *
7  * Usage:
8  *
9  *   That's easy: put this into libc.a, then just call stat() as usual.
10  *
11  * Rationale:
12  *
13  *   Many Unix-born programs make heavy use of stat() library
14  *   function to make decisions on files' equality, size, access
15  *   attributes etc.  In the MS-DOS environment, many implementations
16  *   of stat() are crippled, because DOS makes it very hard to get to
17  *   certain pieces of information about files and directories.  Thus
18  *   porting a program to DOS is usually an exercise in #ifdef'ing.
19  *   This implementation facilitates porting Unix programs to MS-DOS
20  *   by providing stat() which is much more Unix-compatible than those
21  *   of most DOS-based C compilers (e.g., Borland's).
22  *   Specifically, the following issues are taken care of:
23  *
24  *      1. This stat() doesn't fail for root directories, returning
25  *         valid information.
26  *      2. Directory size is not reported zero; the number of used
27  *         directory entries multiplied by entry size is returned instead.
28  *      3. Mode bits are set for all 3 groups (user, group, other).
29  *      4. Directories are NOT reported read-only, unless one of R, H or S
30  *         attributes is set.
31  *      5. Directories have their execute bit set, as they do under Unix.
32  *      6. Device names (such as /dev/con, lpt1, aux etc.) are treated as
33  *         if they were on a special drive called `@:' (st_dev = -1).
34  *         The "character special" mode bit is set for these devices.
35  *      7. The inode number (st_ino) is taken from the starting cluster
36  *         number of the file.  If the cluster number is unavailable, it
37  *         is invented using the file's name in a manner that minimizes
38  *         the possibility of inventing an inode which already belongs
39  *         to another file.  See below for details.
40  *      8. Executable files are found based on files' extensions and
41  *         magic numbers present at their beginning, and their execute
42  *         bits are set.
43  *
44  *   Lossage:
45  *
46  *      Beautiful as the above sounds, this implementation does fail
47  *      under certain circumstances.  The following is a list of known
48  *      problems:
49  *
50  *      1. The time fields for a root directory cannot be obtained, so
51  *         they are set to the beginning of the Epoch.
52  *      2. For files which reside on networked drives, the inode number
53  *         is invented, because network redirectors usually do not
54  *         bring that info with them.  This is not a total lossage, but
55  *         it could get us a different inode for each program run.
56  *      3. Empty files do not have a starting cluster number, because
57  *         DOS doesn't allocate one until you actually write something
58  *         to a file.  For these the inode is also invented.
59  *      4. If the st_ino field is a 16 bit number, the invented inode
60  *         numbers are from 65535 and down, assuming that most disks have
61  *         unused portions near their end.  Valid cluster numbers are 16-bit
62  *         unsigned integers, so a possibility of a clash exists, although
63  *         the last 80 or more cluster numbers are unused on all drives
64  *         I've seen.  If the st_ino is 32 bit, then invented inodes are
65  *         all greater than 64k, which totally eliminates a possibility
66  *         of a clash with an actual cluster number.
67  *      5. The method of computing directory size is an approximation:
68  *         a directory might consume much more space, if it has many
69  *         deleted entries.  Still, this is a close approximation, and
70  *         it does follow the logic of reporting size for a regular file:
71  *         only the actually used space is returned.
72  *      6. As this implementation relies heavily on undocumented DOS
73  *         features, it will fail to get actual file info in environments
74  *         other than native DOS, such as DR-DOS, OS/2 etc.  For these,
75  *         the function will return whatever info is available with
76  *         conventional DOS calls, which is no less than any other
77  *         implementation could do.  This stat() might also fail for
78  *         future DOS versions, if the layout of internal DOS data
79  *         area is changed; however, this seems unlikely.
80  *
81  * Copyright (c) 1994-96 Eli Zaretskii <eliz@is.elta.co.il>
82  *
83  * This software may be used freely so long as this copyright notice is
84  * left intact.  There is no warranty on this software.
85  *
86  */
87 
88 /*
89  * Tested with DJGPP port of GNU C compiler, versions 1.11maint5 and 1.12,
90  * under MS-DOS 3.3, 4.01, 5.0, 6.20 (with and without DoubleSpace) and
91  * with networked drives under XFS 1.86, Novell Netware 3.22, and
92  * TSoft NFS 0.24Beta.
93  *
94  */
95 
96 #include <libc/stubs.h>
97 #include <stdlib.h>
98 #include <stddef.h>
99 #include <unistd.h>
100 #include <time.h>
101 #include <stdio.h>
102 #include <string.h>
103 #include <ctype.h>
104 #include <errno.h>
105 #include <fcntl.h>
106 #include <sys/types.h>
107 #include <sys/stat.h>
108 #include <dos.h>
109 #include <dir.h>
110 
111 #include <dpmi.h>
112 #include <go32.h>
113 #include <libc/farptrgs.h>
114 #include <libc/bss.h>
115 #include <libc/dosio.h>
116 
117 #include "xstat.h"
118 
119 int __getdisk(void);
120 int __findfirst(const char *, struct ffblk *, int);
121 int __findnext(struct ffblk *);
122 
123 #define ALL_FILES   (FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_DIREC|FA_ARCH)
124 
125 #define _STAT_INODE         1   /* should we bother getting inode numbers? */
126 #define _STAT_EXEC_EXT      2   /* get execute bits from file extension? */
127 #define _STAT_EXEC_MAGIC    4   /* get execute bits from magic signature? */
128 #define _STAT_DIRSIZE       8   /* compute directory size? */
129 #define _STAT_ROOT_TIME  0x10   /* try to get root dir time stamp? */
130 #define _STAT_WRITEBIT   0x20   /* fstat() needs write bit? */
131 
132 /* Should we bother about executables at all? */
133 #define _STAT_EXECBIT       (_STAT_EXEC_EXT | _STAT_EXEC_MAGIC)
134 
135 /* The structure of the full directory entry.  This is the 32-byte
136    record present for each file/subdirectory in a DOS directory.
137    Although the ``packed'' attribute seems to be unnecessary, I use
138    it to be sure it will still work for future versions of GCC.  */
139 
140 struct full_dirent {
141   char           fname[8]      __attribute__ ((packed));
142   char           fext[3]       __attribute__ ((packed));
143   unsigned char  fattr         __attribute__ ((packed));
144   unsigned char  freserved[10] __attribute__ ((packed));
145   unsigned short ftime         __attribute__ ((packed));
146   unsigned short fdate         __attribute__ ((packed));
147   unsigned short fcluster      __attribute__ ((packed));
148   unsigned int   fsize         __attribute__ ((packed));
149 };
150 
151 
152 /* Static variables to speed up SDA DOS Swappable Data Area access on
153    subsequent calls.  */
154 
155 /* The count of number of SDA's we have.  It is more than 1 for DOS
156    4.x only.  If it has a value of 0, the function init_dirent_table()
157    will be called to compute the addresses where we are to look for
158    directory entry of our file.  A value of -1 means this method is
159    unsupported for this version of DOS.  */
160 static int  dirent_count;
161 
162 /* The table of places to look for our directory entry.
163    Each entry in the table is a linear offset from the beginning of
164    conventional memory which points to a particular location within
165    one of the SDA's, where the entry of a file being stat()'ed could
166    appear.  The offsets are computed once (when the routine is first
167    called) and then reused for other calls.  The actual storage for
168    the table is malloc()'ed when this function is first called.  */
169 static unsigned int * dirent_table;
170 
171 /* When we have only one SDA, this is where its only place to look for
172    directory entry is stored.  */
173 static unsigned int   dirent_place;
174 
175 /* This holds the fail bits from the last call to init_dirent_table(),
176    so we can return them every time get_inode_from_sda() is called.  */
177 static unsigned short init_dirent_table_bits;
178 
179 /* Holds the last seen value of __bss_count, to be safe for
180    restarted programs (emacs).  */
181 static int stat_count = -1;
182 
183 /*
184  * Parts of the following code is derived from file DOSSWAP.C,
185  * which came with ``Undocumented DOS'', 1st edition.
186  */
187 
188 /* Compute table of pointers to look for directory entry of a file.  */
189 static int
init_dirent_table(void)190 init_dirent_table (void)
191 {
192   short          get_sda_func;
193   unsigned short dirent_offset;
194   unsigned short true_dos_version;
195   unsigned short dos_major, dos_minor;
196   __dpmi_regs    regs;
197 
198   if (dirent_count == -1)     /* we already tried and found we can't */
199     return 0;
200 
201   /* Compute INT 21h function number and offset of directory entry
202      from start of SDA.  These depend on the DOS version.
203      We need exact knowledge about DOS internals, so we need the
204      TRUE DOS version (not the simulated one by SETVER), if that's
205      available.  */
206   true_dos_version = _get_dos_version(1);
207   dos_major = true_dos_version >> 8;
208   dos_minor = true_dos_version & 0xff;
209 
210   if ((dos_major == 3) && (dos_minor >= 10))
211     {
212       get_sda_func  = 0x5d06;
213       dirent_offset = 0x1a7;
214     }
215   else if (dos_major == 4)
216     {
217       /* According to ``Undocumented DOS, 2nd edition'', I could have
218          used 5d06 here, as for DOS 5 and above, but I like to be
219          defensive.  In fact, the above book itself uses 5d0b, contrary
220          to its own recommendation.  */
221       get_sda_func  = 0x5d0b;
222       dirent_offset = 0x1b3;
223     }
224   else if (dos_major >= 5)
225     {
226       get_sda_func  = 0x5d06;
227       dirent_offset = 0x1b3;
228     }
229   else
230     {
231       _djstat_fail_bits |= _STFAIL_OSVER;
232       dirent_count = -1;
233       return 0;
234     }
235 
236   _djstat_fail_bits &= ~_STFAIL_OSVER;  /* version is OK */
237 
238   /* Get the pointer to SDA by calling undocumented function 5dh of INT 21. */
239   regs.x.ax = get_sda_func;
240   __dpmi_int(0x21, &regs);
241   if (regs.x.flags & 1)
242     {
243       _djstat_fail_bits |= _STFAIL_SDA;
244       dirent_count = -1;      /* if the call failed, never try this later */
245       return 0;
246     }
247 
248   _djstat_fail_bits &= ~_STFAIL_SDA;    /* Get SDA succeeded */
249 
250   /* DOS 4.x might have several SDA's, which means we might have more
251      than one place to look into.  (It is typical of DOS 4 to complicate
252      things.)
253      Compute all the possible addresses where we will have to look.  */
254   if (dos_major == 4)
255     {
256       /* The pointer returned by INT 21h, AX=5D0b points to a header
257          which holds a number of SDA's and then an array of that number
258          of records each one of which includes address of an SDA (DWORD)
259          and its length and type (encoded in a WORD).
260          While walking this list of SDA's, we add to each pointer the
261          offset of directory entry and stash the resulting address in
262          our table for later use.  */
263 
264       int  sda_list_walker = MK_FOFF(regs.x.ds, regs.x.si);
265       int  i;
266       int *tbl;
267 
268       dirent_count = _farpeekw(_dos_ds, sda_list_walker); /* number of SDA's */
269 
270       /* Allocate storage for table.  */
271       tbl = dirent_table = (int *)malloc(dirent_count*sizeof(int));
272       if (!dirent_table)
273         {
274           /* If malloc() failed, maybe later it will succeed, so don't
275              store -1 in dirent_count.  */
276           dirent_count = 0;
277           _djstat_fail_bits |= _STFAIL_DCOUNT;
278           return 0;
279         }
280 
281       memset(dirent_table, 0, dirent_count*sizeof(int));
282       _djstat_fail_bits &= ~_STFAIL_DCOUNT; /* dirent_count seems OK */
283 
284       /* Walk the array of pointers, computing addresses of directory
285          entries and stashing them in our table.  */
286       _farsetsel(_dos_ds);
287       for (i = dirent_count, sda_list_walker += 2; i--; sda_list_walker += 6)
288         {
289           int            sda_start = _farnspeekl(sda_list_walker);
290           unsigned short sda_len   = _farnspeekw(sda_list_walker + 4) & 0x7fff;
291 
292           /* Let's be defensive here: if this SDA is too short to have
293              place for directory entry, we won't use it.  */
294           if (sda_len > dirent_offset)
295             *tbl++ = sda_start + dirent_offset;
296           else
297             dirent_count--;
298         }
299     }
300 
301   /* DOS 3.1 and 5.0 or later.  We have only one SDA pointed to by
302      whatever INT 21h, AH=5d returns.  */
303   else
304     {
305       dirent_count = 1;
306       dirent_place = MK_FOFF(regs.x.ds, regs.x.si) + dirent_offset;
307       dirent_table = &dirent_place;
308     }
309 
310   return 1;
311 }
312 
313 /* Get inode number by searching DOS Swappable Data Area.
314    The entire directory entry for a file found by FindFirst/FindNext
315    appears at a certain (version-dependent) offset in the SDA after
316    one of those function is called.
317    Should be called immediately after calling DOS FindFirst function,
318    before the info is overwritten by somebody who calls it again.  */
319 static unsigned int
get_inode_from_sda(const char * basename)320 get_inode_from_sda(const char *basename)
321 {
322   int            count          = dirent_count;
323   unsigned int * dirent_p       = dirent_table;
324   unsigned short dos_mem_base   = _dos_ds;
325   unsigned short our_mem_base   = _my_ds();
326   char  * dot                   = strchr(basename, '.');
327   size_t  total_len             = strlen(basename);
328   int     name_len              = dot ? dot - basename : total_len;
329   int     ext_len               = dot ? total_len - name_len - 1 : 0;
330   int     cluster_offset        = offsetof(struct full_dirent, fcluster);
331 
332   /* Restore failure bits set by last call to init_dirent_table(), so
333      they will be reported as if it were called now.  */
334   _djstat_fail_bits |= init_dirent_table_bits;
335 
336   /* Force reinitialization in restarted programs (emacs).  */
337   if (stat_count != __bss_count)
338     {
339       stat_count = __bss_count;
340       dirent_count = 0;
341     }
342 
343   /* Initialize the table of SDA entries where we are to look for
344      our file.  */
345   if (!dirent_count && !init_dirent_table())
346     {
347       /* Don't save the truename failure bit.  */
348       init_dirent_table_bits = (_djstat_fail_bits & ~_STFAIL_TRUENAME);
349       return 0;
350     }
351   init_dirent_table_bits = (_djstat_fail_bits & ~_STFAIL_TRUENAME);
352   if (dirent_count == -1)
353     return 0;
354 
355   count = dirent_count;
356   dirent_p = dirent_table;
357 
358   _farsetsel(dos_mem_base);
359 
360   /* This is DOS 4.x lossage: this loop might execute many times.
361      For other DOS versions it is executed exactly once.  */
362   while (count--)
363     {
364       unsigned int  src_address = *dirent_p & 0x000fffff;
365       char          cmp_buf[sizeof(struct full_dirent)];
366 
367       /* Copy the directory entry from the SDA to local storage.
368          The filename is stored there in infamous DOS format: name and
369          extension blank-padded to 8/3 characters, no dot between them.  */
370       movedata(dos_mem_base, src_address, our_mem_base, (unsigned int)cmp_buf,
371                sizeof(struct full_dirent));
372 
373       /* If this is the filename we are looking for, return
374          its starting cluster. */
375       if (!strncmp(cmp_buf, basename, name_len) &&
376           (ext_len == 0 || !strncmp(cmp_buf + 8, dot + 1, ext_len)))
377         return (unsigned int)_farnspeekw(*dirent_p + cluster_offset);
378 
379       /* This is not our file.  Search more, if more addresses left. */
380       dirent_p++;
381     }
382 
383   /* If not found, give up.  */
384   _djstat_fail_bits |= _STFAIL_BADSDA;
385   return 0;
386 }
387 
388 int _ioctl_get_first_cluster(const char *);
389 
390 /* Get the number of the first cluster of PATHNAME using
391    the IOCTL call Int 21h/AX=440Dh/CX=0871h, if that call
392    is supported by the OS.  Return the cluster number, or
393    a negative number if this service isn't supported.  */
394 
395 int
_ioctl_get_first_cluster(const char * pathname)396 _ioctl_get_first_cluster(const char *pathname)
397 {
398   __dpmi_regs r;
399 
400   /* See if the IOCTL GetFirstCluster call is supported.  */
401   r.x.ax = 0x4411;	       /* query generic IOCTL capability by drive */
402   r.h.bl = pathname[0] & 0x1f; /* drive number (1=A:) */
403   r.x.cx = 0x871;
404   __dpmi_int(0x21, &r);
405   if ((r.x.flags & 1) == 0 && r.x.ax == 0)
406     {
407       r.x.ax = 0x440d;	       /* Generic IOCTL */
408       r.x.cx = 0x0871;	       /* category code 08h, minor code 71h */
409       r.x.bx = 1;	       /* pathname uses current OEM character set */
410       r.x.ds = __tb >> 4;
411       r.x.dx = __tb & 0x0f;
412       _put_path(pathname);
413       __dpmi_int(0x21, &r);
414       if ((r.x.flags & 1) == 0)
415 	return ( ((int)r.x.dx << 16) + r.x.ax );
416     }
417   return -1;
418 }
419 
420 static char blanks_8[] = "        ";
421 
422 static int
stat_assist(const char * path,struct stat * statbuf)423 stat_assist(const char *path, struct stat *statbuf)
424 {
425   struct   ffblk ff_blk;
426   char     canon_path[MAX_TRUE_NAME];
427   char     pathname[MAX_TRUE_NAME];
428   short    drv_no;
429   unsigned dos_ftime;
430 
431   _djstat_fail_bits = 0;
432 
433   memset(statbuf, 0, sizeof(struct stat));
434   memset(&dos_ftime, 0, sizeof(dos_ftime));
435 
436   /* Fields which are constant under DOS.  */
437   statbuf->st_uid     = getuid();
438   statbuf->st_gid     = getgid();
439   statbuf->st_nlink   = 1;
440 #ifndef  NO_ST_BLKSIZE
441   statbuf->st_blksize = _go32_info_block.size_of_transfer_buffer;
442 #endif
443 
444   /* Make the path explicit.  This makes the rest of our job much
445      easier by getting rid of some constructs which, if present,
446      confuse `_truename' and/or `findfirst'.  In particular, it
447      deletes trailing slashes, makes "d:" explicit, and allows us
448      to make an illusion of having a ".." entry in root directories.  */
449   _fixpath (path, pathname);
450 
451   /* Get the drive number.  It is always explicit, since we
452      called `_fixpath' on the original pathname.  */
453   drv_no = toupper(pathname[0]) - 'A';
454 
455   /* Produce canonical pathname, with all the defaults resolved and
456      all redundant parts removed.  This calls undocumented DOS
457      function 60h.  */
458   if (_truename(path, canon_path) || _truename(pathname, canon_path))
459     {
460       /* Detect character device names which must be treated specially.
461          We could simply call FindFirst and test the 6th bit, but some
462          versions of DOS have trouble with this (see Ralph Brown's
463          Interrupt List, ``214E'', under `Bugs').  Instead we use
464          truename() which calls INT 21/AX=6000H.  For character devices
465          it returns X:/DEVNAME, where ``X'' is the current drive letter
466          (note the FORWARD slash!).  E.g., for CON or \dev\con it will
467          return C:/CON.
468          We will pretend that devices all reside on a special drive
469          called `@', which corresponds to st_dev = -1.  This is because
470          these devices have no files, and we must invent inode numbers
471          for them; this scheme allows to lower a risk of clash between
472          invented inode and one which belongs to a real file.  This is
473          also compatible with what our fstat() does.
474       */
475     char_dev:
476       if (canon_path[2] == '/')
477         {
478           char dev_name[9];     /* devices are at most 8 characters long */
479 
480           strncpy(dev_name, canon_path + 3, 8); /* the name without `X:/' */
481           dev_name[8] = '\0';
482           strcpy(canon_path, "@:\\dev\\");
483           strcat(canon_path, dev_name);
484           strncat(canon_path, blanks_8, 8 - strlen(dev_name)); /* blank-pad */
485           canon_path[15] = '\0';   /* ensure zero-termination */
486 
487           /* Invent inode */
488           statbuf->st_ino = _invent_inode(canon_path, 0, 0);
489 
490           /* Device code. */
491           statbuf->st_dev = -1;
492 #ifdef  HAVE_ST_RDEV
493           statbuf->st_rdev = -1;
494 #endif
495 
496           /* Set mode bits, including character special bit.
497              Should we treat printer devices as write-only?  */
498           statbuf->st_mode |= (S_IFCHR | READ_ACCESS | WRITE_ACCESS);
499 
500           /* We will arrange things so that devices have current time in
501              the access-time and modified-time fields of struct stat, and
502              zero (the beginning of times) in creation-time field.  This
503              is consistent with what DOS FindFirst function returns for
504              character device names (if it succeeds--see above).  */
505           statbuf->st_atime = statbuf->st_mtime = time(0);
506           statbuf->st_ctime = _file_time_stamp(dos_ftime);
507 
508           return 0;
509         }
510       else if (canon_path[0] >= 'A' && canon_path[0] <= 'z' &&
511                canon_path[1] == ':' && canon_path[2] == '\\')
512         {
513           /* _truename() returned a name with a drive letter.  (This is
514              always so for local drives, but some network redirectors
515              also do this.)  We will take this to be the TRUE drive
516              letter, because _truename() knows about SUBST and JOIN.
517              If the canonicalized path returns in the UNC form (which
518              means the drive is remote), it cannot be SUBSTed or JOINed,
519              because SUBST.EXE and JOIN.EXE won't let you do it; so, for
520              these cases, there is no problem in believing the drive
521              number we've got from the original path (or is there?).  */
522           drv_no = toupper(canon_path[0]) - 'A';
523         }
524     }
525   else
526     {
527       /* _truename() failed.  (This really shouldn't happen, but who knows?)
528          At least uppercase all letters, convert forward slashes to backward
529          ones, and pray... */
530       register const char *src = pathname;
531       register       char *dst = canon_path;
532 
533       while ( (*dst = (*src > 'a' && *src < 'z'
534                        ? *src++ - ('a' - 'A')
535                        : *src++)) != '\0')
536         {
537           if (*dst == '/')
538             *dst = '\\';
539           dst++;
540         }
541 
542       _djstat_fail_bits |= _STFAIL_TRUENAME;
543     }
544 
545   /* Call DOS FindFirst function, which will bring us most of the info.  */
546   if (!__findfirst(pathname, &ff_blk, ALL_FILES))
547     {
548       /* Time fields. */
549       dos_ftime =
550         ( (unsigned short)ff_blk.ff_fdate << 16 ) +
551           (unsigned short)ff_blk.ff_ftime;
552 
553       /* If the IOCTL GetFirstCluster call is available, try it first.  */
554       if ( (_djstat_flags & _STAT_INODE) == 0
555 	   && (statbuf->st_ino = _ioctl_get_first_cluster(pathname)) <= 0)
556         {
557 
558           /* For networked drives, don't believe the starting cluster
559              that the network redirector feeds us; always invent inode.
560              This is because network redirectors leave bogus values there,
561              and we don't have enough info to decide if the starting
562              cluster value is real or just a left-over from previous call.
563              For local files, try first using DOS SDA to get the inode from
564              the file's starting cluster number; if that fails, invent inode.
565              Note that the if clause below tests for non-zero value returned
566              by is_remote_drive(), which includes possible failure (-1).
567              This is because findfirst() already succeeded for our pathname,
568              and therefore the drive is a legal one; the only possibility that
569              is_remote_drive() fails is that some network redirector takes
570              over IOCTL functions in an incompatible way, which means the
571              drive is remote.  QED.  */
572           if (statbuf->st_ino == 0  /* don't try SDA if IOCTL call succeeded */
573 	      || _is_remote_drive(drv_no)
574               || (statbuf->st_ino = get_inode_from_sda(ff_blk.ff_name)) == 0)
575             {
576               _djstat_fail_bits |= _STFAIL_HASH;
577               statbuf->st_ino =
578                 _invent_inode(canon_path, dos_ftime, ff_blk.ff_fsize);
579             }
580 	  else if (toupper (canon_path[0]) != toupper (pathname[0])
581 		   && canon_path[1] == ':'
582 		   && canon_path[2] == '\\'
583 		   && canon_path[3] == '\0')
584 	    /* The starting cluster in SDA for the root of JOINed drive
585 	       actually belongs to the directory where that drive is
586 	       ``mounted''.  This can potentially be the cluster of
587 	       another file on the JOINed drive.  We cannot allow this.  */
588 	    statbuf->st_ino = 1;
589         }
590 
591       /* File size. */
592       statbuf->st_size = ff_blk.ff_fsize;
593 
594       /* Mode bits. */
595       statbuf->st_mode |= READ_ACCESS;
596       if ( !(ff_blk.ff_attrib & 0x07) )  /* no R, H or S bits set */
597         statbuf->st_mode |= WRITE_ACCESS;
598 
599       /* Sometimes `_truename' doesn't return X:/FOO for character
600 	 devices.  However, FindFirst returns attribute 40h for them.  */
601       if (ff_blk.ff_attrib == 0x40)
602 	{
603 	  size_t cplen = strlen (canon_path);
604 	  char *pslash = canon_path + cplen - 1;
605 
606 	  while (pslash > canon_path + 2 && *pslash != '\\')
607 	    pslash--;
608 
609 	  /* Force it into X:/FOO form.  */
610 	  if (canon_path[1] == ':')
611 	    {
612 	      if (pslash > canon_path + 2)
613 		memmove (canon_path + 2, pslash,
614 			 cplen - (pslash - canon_path) + 1);
615 	      canon_path[2] = '/';
616 	      goto char_dev;
617 	    }
618 	}
619 
620       /* Directories should have Execute bits set. */
621       if (ff_blk.ff_attrib & 0x10)
622         statbuf->st_mode |= (S_IFDIR | EXEC_ACCESS);
623 
624       else
625         {
626           /* This is a regular file. */
627           char *extension  = strrchr(ff_blk.ff_name, '.');
628 
629           /* Set regular file bit.  */
630           statbuf->st_mode |= S_IFREG;
631 
632           if ((_djstat_flags & _STAT_EXECBIT) != _STAT_EXECBIT)
633             {
634               /* Set execute bits based on file's extension and
635                  first 2 bytes. */
636               if (extension)
637                 extension++;    /* get past the dot */
638               if (_is_executable(pathname, -1, extension))
639                 statbuf->st_mode |= EXEC_ACCESS;
640             }
641         }
642     }
643   else if ((_djstat_fail_bits & _STFAIL_TRUENAME))
644     {
645       /* If both `findfirst' and `_truename' failed, this must
646 	 be a non-existent file or an illegal/inaccessible drive.  */
647       if (errno == ENMFILE)
648 	errno = ENODEV;
649       return -1;
650     }
651   else if (pathname[3] == '\0')
652     {
653       /* Detect root directories.  These are special because, unlike
654 	 subdirectories, FindFirst fails for them.  We look at PATHNAME
655 	 because a network redirector could tweak what `_truename'
656 	 returns to be utterly unrecognizable as root directory.  PATHNAME
657 	 always begins with "d:/", so it is root if PATHNAME[3] = 0.  */
658 
659       /* Mode bits. */
660       statbuf->st_mode |= (S_IFDIR|READ_ACCESS|WRITE_ACCESS|EXEC_ACCESS);
661 
662       /* Root directory will have an inode = 1.  Valid cluster numbers
663          for real files under DOS start with 2. */
664       statbuf->st_ino = 1;
665 
666       /* Simulate zero size.  This is what FindFirst returns for every
667          sub-directory.  Later we might compute a better approximation
668          (see below).  */
669       ff_blk.ff_fsize = 0L;
670 
671       /* The time fields are left to be zero, unless the user wants us
672          to try harder.  In the latter case, we check if the root has
673          a volume label entry, and use its time if it has. */
674 
675       if ( (_djstat_flags & _STAT_ROOT_TIME) == 0 )
676 	{
677 	  char buf[7];
678 
679 	  strcpy(buf, pathname);
680 	  strcat(buf, "*.*");
681 	  if (!__findfirst(buf, &ff_blk, FA_LABEL))
682 	    dos_ftime = ( (unsigned)ff_blk.ff_fdate << 16 ) + ff_blk.ff_ftime;
683 	  else
684 	    _djstat_fail_bits |= _STFAIL_LABEL;
685 	}
686     }
687   else
688     {
689       int e = errno;	/* errno value from original FindFirst on PATHNAME */
690       int i = 0;
691       int j = strlen (pathname) - 1;
692 
693       /* Check for volume labels.  We did not mix FA_LABEL with
694 	 other attributes in the call to `__findfirst' above,
695 	 because some environments will return bogus info in
696 	 that case.  For instance, Win95 and WinNT seem to
697 	 ignore `pathname' and return the volume label even if it
698 	 doesn't fit the name in `pathname'.  This fools us to
699 	 think that a non-existent file exists and is a volume
700 	 label.  Hence we test the returned name to be PATHNAME.  */
701       if (!__findfirst(pathname, &ff_blk, FA_LABEL))
702 	{
703 	  i = strlen (ff_blk.ff_name) - 1;
704 
705 	  if (j >= i)
706 	    {
707 	      for ( ; i >= 0 && j >= 0; i--, j--)
708 		if (toupper (ff_blk.ff_name[i]) != toupper (pathname[j]))
709 		  break;
710 	    }
711 	}
712 
713       if (i < 0 && pathname[j] == '/')
714 	{
715 	  /* Indeed a label.  */
716 	  statbuf->st_mode = READ_ACCESS;
717 #ifdef  S_IFLABEL
718 	  statbuf->st_mode |= S_IFLABEL;
719 #endif
720 	  statbuf->st_ino = 1;
721 	  statbuf->st_size = 0;
722 	  dos_ftime = ( (unsigned)ff_blk.ff_fdate << 16 ) + ff_blk.ff_ftime;
723 	}
724       else
725 	{
726 	  /* FindFirst on volume labels might set errno to ENMFILE or even
727 	     to something more strange like EINVAl; correct that.  */
728 	  errno = e;	/* restore errno from the original FindFirst */
729 	  if (errno == ENMFILE)
730 	    errno = ENOENT;
731 	  return -1;
732 	}
733     }
734 
735   /* Device code. */
736   statbuf->st_dev = drv_no;
737 #ifdef  HAVE_ST_RDEV
738   statbuf->st_rdev = drv_no;
739 #endif
740 
741   /* Time fields. */
742   statbuf->st_atime = statbuf->st_mtime = statbuf->st_ctime =
743     _file_time_stamp(dos_ftime);
744 
745   if ( ! strcmp(ff_blk.lfn_magic,"LFN32") )
746     {
747       unsigned xtime;
748       xtime = *(unsigned *)&ff_blk.lfn_ctime;
749       if(xtime)			/* May be zero if file written w/o lfn active */
750         statbuf->st_ctime = _file_time_stamp(xtime);
751       xtime = *(unsigned *)&ff_blk.lfn_atime;
752       if(xtime > dos_ftime)	/* Accessed time is date only, no time */
753         statbuf->st_atime = _file_time_stamp(xtime);
754     }
755 
756   if ( (statbuf->st_mode & S_IFMT) == S_IFDIR
757        && (_djstat_flags & _STAT_DIRSIZE) == 0 )
758     {
759       /* Under DOS, directory entries for subdirectories have
760          zero size.  Therefore, FindFirst brings us zero size
761          when called on a directory.  (Some network redirectors
762          might do a better job, thus below we also test for zero size
763          actually being returned.)  If we have zero-size directory,
764          we compute here the actual directory size by reading its
765          entries, then multiply their number by 32 (the size of a
766          directory entry under DOS).  This might lose in the case
767          that many files were deleted from a once huge directory,
768          because AFAIK, directories don't return unused clusters to
769          the disk pool.  Still, it is a good approximation of the
770          actual directory size.
771 
772          We also take this opportunity to return the number of links
773          for directories as Unix programs expect it to be: the number
774          of subdirectories, plus 2 (the directory itself and the ``.''
775          entry).
776 
777          The (max) size of the root directory could also be taken from
778          the disk BIOS Parameter Block (BPB) which can be obtained
779          by calling IOCTL (INT 21/AH=44H), subfunction 0DH, minor
780          function 60H.  But we will treat all directories the same,
781          even at performance cost, because it's more robust for
782          networked drives.  */
783 
784       size_t pathlen = strlen (pathname);
785       char   lastc   = pathname[pathlen - 1];
786       char  *search_spec = (char *)alloca (pathlen + 10); /* need only +5 */
787       int nfiles = 0, nsubdirs = 0, done;
788       size_t extra = 0;
789       int add_extra = 0;
790 
791       strcpy(search_spec, pathname);
792       if (lastc == '/')
793         strcat(search_spec, "*.*");
794       else
795         strcat(search_spec, "/*.*");
796 
797       if (statbuf->st_size == 0 && _USE_LFN)
798 	{
799 	  /* VFAT filesystems use additional directory entries to
800 	     store the long filenames.  */
801 	  char fstype[40];
802 
803 	  if ((_get_volume_info(pathname,0,0,fstype) & _FILESYS_LFN_SUPPORTED)
804 	      && strncmp(fstype, "FAT", 4) == 0)
805 	    add_extra = 1;
806 	}
807 
808       /* Count files and subdirectories.  */
809       for (done = __findfirst(search_spec, &ff_blk, ALL_FILES);
810 	   !done;
811 	   done = __findnext(&ff_blk))
812 	{
813 	  register char *fname = ff_blk.ff_name;
814 
815 	  /* Don't count "." and ".." entries.  This will show empty
816 	     directories as size 0.  */
817 	  if (! (fname[0] == '.'
818 		 && (fname[1] == '\0'
819 		     || (fname[1] == '.'
820 			 && fname[2] == '\0'))))
821 	    {
822 	      char fn[13];
823 
824 	      nfiles++;
825 	      if (ff_blk.ff_attrib & 0x10)
826 		nsubdirs++;
827 	      /* For each 13 characters of the long filename, a
828 		 32-byte directory entry is used.  */
829 	      if (add_extra && strcmp(_lfn_gen_short_fname(fname, fn), fname))
830 		extra += (strlen(fname) + 12) / 13;
831 	    }
832 	}
833 
834       statbuf->st_nlink = nsubdirs + 2;
835       if (statbuf->st_size == 0)
836 	statbuf->st_size  = (nfiles + extra) * sizeof(struct full_dirent);
837     }
838 
839   return 0;
840 }
841 
842 /* Main entry point.  This is library stat() function.
843  */
844 
845 int
stat(const char * path,struct stat * statbuf)846 stat(const char *path, struct stat *statbuf)
847 {
848   int  e = errno;
849   int  pathlen;
850 
851   if (!path || !statbuf)
852     {
853       errno = EFAULT;
854       return -1;
855     }
856 
857   if ((pathlen = strlen (path)) >= MAX_TRUE_NAME)
858     {
859       errno = ENAMETOOLONG;
860       return -1;
861     }
862 
863   /* Fail if PATH includes wildcard characters supported by FindFirst.  */
864   if (memchr(path, '*', pathlen) || memchr(path, '?', pathlen))
865     {
866       errno = ENOENT;	/* since no such filename is possible */
867       return -1;
868     }
869 
870   if (stat_assist(path, statbuf) == -1)
871     {
872       return -1;      /* errno set by stat_assist() */
873     }
874   else
875     {
876       errno = e;
877       return 0;
878     }
879 }
880 
881 #ifdef  TEST
882 
883 unsigned short _djstat_flags = 0;
884 
885 void
main(int argc,char * argv[])886 main(int argc, char *argv[])
887 {
888   struct stat stat_buf;
889   char *endp;
890 
891   if (argc < 2)
892     {
893       fprintf (stderr, "Usage: %s <_djstat_flags> <file...>\n", argv[0]);
894       exit(0);
895     }
896 
897   if (stat(*argv, &stat_buf) != 0)
898     perror ("stat failed on argv[0]");
899   else
900     fprintf(stderr, "DOS %d.%d (%s)\n", _osmajor, _osminor, _os_flavor);
901   argc--; argv++;
902 
903   _djstat_flags = (unsigned short)strtoul(*argv, &endp, 0);
904   argc--; argv++;
905 
906   while (argc--)
907     {
908       if (!stat(*argv, &stat_buf))
909         {
910           fprintf(stderr, "%s: %d %6u %o %d %d %ld %lu %s", *argv,
911                   stat_buf.st_dev,
912                   (unsigned)stat_buf.st_ino,
913                   stat_buf.st_mode,
914                   stat_buf.st_nlink,
915                   stat_buf.st_uid,
916                   (long)stat_buf.st_size,
917                   (unsigned long)stat_buf.st_mtime,
918                   ctime(&stat_buf.st_mtime));
919           _djstat_describe_lossage(stderr);
920         }
921       else
922         {
923           fprintf(stderr, "%s: lossage", *argv);
924           perror(" ");
925           _djstat_describe_lossage(stderr);
926         }
927 
928       ++argv;
929     }
930 
931     exit (0);
932 }
933 
934 #endif
935 
936