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

 
Output of file : GETCWD.C contained in archive : VCCRT1.ZIP
/***
*getcwd.c - get current working directory
*
* Copyright (c) 1985-1992, Microsoft Corporation. All rights reserved.
*
*Purpose:
*
* contains functions getcwd and _getdcwd for getting the current
* working directory. getcwd gets the c.w.d. for the default disk
* drive, whereas _getdcwd allows one to get the c.w.d. for whatever
* disk drive is specified.
*
*******************************************************************************/


/*
* DRIVE is the length of the return string taken up by the drive
* designator and PATH is the maximum length of string taken up by
* the rest of the path name.
*/

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

/***
*char *_getcwd(pnbuf, maxlen) - get current working directory for default drive
*
*Purpose:
* getcwd gets the current working directory for the user,

* placing it in the buffer pointed to by pnbuf. If the length
* of the string exceeds the length of the buffer, maxlen,
* then NULL is returned. If pnbuf = NULL, maxlen is ignored.
* An entry point "_getdcwd()" is defined with takes the above
* parameters, plus a drive number. "getcwd()" is implemented
* as a call to "_getcwd()" with the default drive (0).
*
* If pnbuf = NULL, maxlen is ignored, and a buffer is automatically
* allocated using malloc() -- a pointer to which is returned by getcwd().
*
* side effects: no global data is used or affected
*
*Entry:
* char *pnbuf - pointer to a buffer maintained by the user;
* int maxlen - length of the buffer pointed to by pnbuf;
*
*Exit:
* Returns pointer to the buffer containing the c.w.d. name
* (same as pnbuf if non-NULL; otherwise, malloc is
* used to allocate a buffer)
* NULL = error return
*Exceptions:
*******************************************************************************/

/*
** _getcwd() is just a call to _getdcwd() with the default drive
*/

char *_getcwd (pnbuf, maxlen)
char *pnbuf;
int maxlen;
{
return(_getdcwd(0, pnbuf, maxlen));
}


/***
*char *_getdcwd(drive, pnbuf, maxlen) - get c.w.d. for given drive
*
*Purpose:
* _getdcwd gets the current working directory for the user,
* placing it in the buffer pointed to by pnbuf. It returns
* the length of the string put in the buffer. If the length
* of the string exceeds the length of the buffer, maxlen,
* then NULL is returned. If pnbuf = NULL, maxlen is ignored,
* and a buffer is automatically allocated using malloc() --
* a pointer to which is returned by _getdcwd().
*
* side effects: no global data is used or affected
*
*Entry:
* int drive - number of the drive being inquired about
* 0 = default, 1 = 'a:', 2 = 'b:', etc.
* char *pnbuf - pointer to a buffer maintained by the user;
* int maxlen - length of the buffer pointed to by pnbuf;
*
*Exit:
* Returns pointer to the buffer containing the c.w.d. name
* (same as pnbuf if non-NULL; otherwise, malloc is
* used to allocate a buffer)
*
*Exceptions:
*
*******************************************************************************/

char *_getdcwd (drive, pnbuf, maxlen)
int drive;
char *pnbuf;
REG2 int maxlen;
{
_WINSTATIC char buf[_MAX_PATH];
_WINSTATIC union _REGS inregs;
_WINSTATIC union _REGS outregs;
#ifdef SIZED
struct _SREGS segregs;
char *p = buf; /* cannot be register, need to use FP_SEG(p) */
#else
REG1 char *p = buf; /* can be register, FP_SEG(p) is not used */
#endif
int buflen;

/* set up string - prepend drive letter + colon to the path name
*/
if (drive == 0)
drive = _getdrive();

*p++ = (char)(drive + 'A' - 1); /* drive specifier */
*p++ = ':';
*p++ = '\\';

/* get root relative path name */

inregs.h.ah = DOS_curdir;
inregs.h.dl = (unsigned char)drive;
#ifdef SIZED
segregs.ds = segregs.es = FP_SEG(p);
inregs.x.si = FP_OFF(p);
_intdosx(&inregs,&outregs,&segregs);
#else
inregs.x.si = (unsigned int)p;
_intdos(&inregs,&outregs);
#endif

if (outregs.x.cflag) {
/* error on system call */
errno = EACCES;
_doserrno = outregs.x.ax;
return(NULL);
}


/* see if need to try to allocate buffer in heap */

buflen = strlen(buf)+1;

if (!(p=pnbuf)) {
if (maxlen < buflen)
maxlen = buflen; /* ignore original maxlen */
if (!(p = malloc(maxlen))) {
errno = ENOMEM;
return(p);
}
pnbuf = p;
}

/* make sure it all fits in the supplied (or created) buffer, else fail
*/

if (buflen > maxlen) {
errno = ERANGE;
return(NULL);
}

/* copy string to user buffer from internal buffer
* and return pointer to user buffer
*/

return(strcpy(p, buf));
}


  3 Responses to “Category : C++ Source Code
Archive   : VCCRT1.ZIP
Filename : GETCWD.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/