Category : C++ Source Code
Archive   : VCCRT1.ZIP
Filename : STAT.C

 
Output of file : STAT.C contained in archive : VCCRT1.ZIP
/***
*dos/stat.c - get file status info
*
* Copyright (c) 1985-1992, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines stat() - get file status info
*
*******************************************************************************/

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

static int near _ValidDrive( unsigned drive );

#define ISSLASH(a) ((a) == '\\' || (a) == '/')

/***
*static unsigned near _dtoxmode(attr, name) -
*
*Purpose:
*
*Entry:
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/

static unsigned near _dtoxmode(int attr, const char *name)
{
REG1 unsigned uxmode;
unsigned dosmode;
REG2 const char *p;

dosmode = attr & 0xff;
if ((p = name)[1] == ':')
p += 2;

/* check to see if this is a directory - note we must make a special
* check for the root, which DOS thinks is not a directory
*/

uxmode = (( ISSLASH(*p) && !p[1]) || (dosmode & A_D)
|| !*p) ? S_IFDIR|S_IEXEC : S_IFREG;

/* If attribute byte does not have read-only bit, it is read-write */

uxmode |= (dosmode & A_RO) ? S_IREAD : (S_IREAD|S_IWRITE);

/* see if file appears to be executable - check extension of name */

if (p = strrchr(name, '.')) {
if ( !_stricmp(p, ".exe") ||
!_stricmp(p, ".bat") ||
!_stricmp(p, ".com") )
uxmode |= S_IEXEC;
}

/* propagate user read/write/execute bits to group/other fields */

uxmode |= (uxmode & 0700) >> 3;
uxmode |= (uxmode & 0700) >> 6;

return(uxmode);
}


/***
*int _stat(name, buf) - get file status info
*
*Purpose:
* stat obtains information about the file or directory specified
* by name and stores it in the structure pointed to by buf.
*
*Entry:
* char *name - pathname of file
* struct _stat *buf - pointer to structure to fill in
*
*Exit:
* fills in structure pointed to by buf
* returns 0 if successful
* returns -1 and sets errno if unsuccessful
*
*Exceptions:
*
*******************************************************************************/

int _stat( REG1 const char *name, REG2 struct _stat *buf)
{
_WINSTATIC struct _find_t findbuf;
int drive; /* A: = 1, B: = 2, etc. */
char *path;
_WINSTATIC char pathbuf[ _MAX_PATH ];

/* Don't allow wildcards to be interpreted by system */

if (_mbspbrk(name, "?*")) {
errno = ENOENT;
return(-1);
}

/* Try to get disk from name. If none, get current disk. */

if (name[1] == ':'){
if ( *name && !name[2] ) {
errno = ENOENT; /* return an error if name is */
return(-1); /* just drive letter then colon */
}
drive = tolower(*name) - 'a' + 1;
}
else
drive = _getdrive();

/* Get info from DOS about the file */

if ( _dos_findfirst(name, (_A_HIDDEN | _A_SYSTEM | _A_SUBDIR), &findbuf)
!= 0) {

/* Error on findfirst -- no matching file name */
/* See if we're looking for the root directory */
/* In the non-MBCS case, _mbspbrk is mapped to strpbrk by
* mbstring.h */

if ( !( _mbspbrk(name, "./\\") &&
(path = _fullpath( pathbuf, name, _MAX_PATH )) &&
(strlen( path ) == 3) &&
_ValidDrive( drive ) ) ) {
errno = ENOENT;
return(-1);
}

findbuf.attrib = _A_SUBDIR;
findbuf.size = 0L;
findbuf.wr_date = (1 << 5) + 1; /* 1 January 1980 */
findbuf.wr_time = 0; /* 00:00:00 */
}

/* Fill in the user's buffer */

buf->st_uid = buf->st_gid = buf->st_ino = 0;
buf->st_rdev = buf->st_dev = drive - 1;
buf->st_mode = _dtoxmode(findbuf.attrib, name);
buf->st_nlink = 1;
buf->st_size = findbuf.size;
buf->st_atime = buf->st_mtime = buf->st_ctime =
XTIME(findbuf.wr_date, findbuf.wr_time);

/* Good return */

return(0);

}


/***
*static int near _ValidDrive( unsigned dive )
*
*Purpose:
* Determine if the supplied drive is valid or not.
*
* NOTES:
* (1) Uses function 0x1C (get drive data)
* (2) This routine MUST save/restore DS since function 0x1C
* hammers that reg (compiler _loadds option should handle this).
*
*Entry:
* drive = 0 = default drive,
* 1 = a:, 2 => b: ...
*Exit:
* !0 = drive exists
* 0 = drive does not exist.
*Exceptions:
*
*******************************************************************************/

static int near _ValidDrive( unsigned drive )
{
_WINSTATIC union _REGS inregs;
_WINSTATIC union _REGS outregs;
_WINSTATIC struct _SREGS segregs;

inregs.h.ah = 0x1c;
inregs.h.dl = (unsigned char)drive;
segregs.ds = segregs.es = 0;

return( (_intdosx(&inregs, &outregs, &segregs) & 0xFF) != 0xFF);

}


  3 Responses to “Category : C++ Source Code
Archive   : VCCRT1.ZIP
Filename : STAT.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/