读书人

求getcwd函数源码解决办法

发布时间: 2013-03-27 11:22:42 作者: rapoo

求getcwd函数源码
不需要全路径,只需要获得当前运行程序的目录名就行,
想看一下源码怎么实现的,system调用?
[解决办法]
引用赵老师的话“单步调试和设断点调试是程序员必须掌握的技能之一。”

打断点,跟进去。windows下就是,主要就是调用GetFullPathName这个api
[解决办法]

http://www.microsoft.com/visualstudio/chs/downloads#d-2010-express
点开Visual C++ 2010 Express下面的语言选‘简体中文’,再点立即安装

再参考C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\getcwd.c

...
/***
*_TSCHAR *_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.
* _TSCHAR *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:
*
*******************************************************************************/

_TSCHAR * __cdecl _tgetdcwd (
int drive,
_TSCHAR *pnbuf,
int maxlen
)
{
_TSCHAR *retval;

_mlock( _ENV_LOCK );
__try {
retval = _tgetdcwd_nolock(drive, pnbuf, maxlen);
}
_finally {
_munlock( _ENV_LOCK );
}



return retval;
}

#ifdef _DEBUG

_TSCHAR * __cdecl _tgetdcwd_dbg (
int drive,
_TSCHAR *pnbuf,
int maxlen,
int nBlockUse,
const char * szFileName,
int nLine
)
{
_TSCHAR *retval;

_mlock( _ENV_LOCK );
__try {
retval = _tgetdcwd_lk_dbg(drive, pnbuf, maxlen, nBlockUse, szFileName, nLine);
}
_finally {
_munlock( _ENV_LOCK );
}

return retval;
}

#endif /* _DEBUG */

#ifdef _DEBUG

_TSCHAR * __cdecl _tgetdcwd_nolock (
int drive,
_TSCHAR *pnbuf,
int maxlen
)
{
return _tgetdcwd_lk_dbg(drive, pnbuf, maxlen, _NORMAL_BLOCK, NULL, 0);
}

_TSCHAR * __cdecl _tgetdcwd_lk_dbg (
int drive,
_TSCHAR *pnbuf,
int maxlen,
int nBlockUse,
const char * szFileName,
int nLine
)

#else /* _DEBUG */

_TSCHAR * __cdecl _tgetdcwd_nolock (
int drive,
_TSCHAR *pnbuf,
int maxlen
)

#endif /* _DEBUG */
{
_TSCHAR *p;
_TSCHAR drvstr[4];
int ret, count;
_TSCHAR *pname; /* only used as argument to GetFullPathName */

if ( drive != 0 ) {
/*
* Not the default drive - make sure it's valid.
*/
if ( !_validdrive(drive) ) {
_doserrno = ERROR_INVALID_DRIVE;
_VALIDATE_RETURN(("Invalid Drive",0), EACCES, NULL);
}
} else {
/* Get the drive index of the default drive */


drive = _getdrive();

}

/* If pnbuf is NULL, we pass 0 in both the 2nd & 3rd parameter
to find required length to allocate */

if(pnbuf)
{
_VALIDATE_RETURN( (maxlen > 0), EINVAL, NULL);
count = maxlen;
pnbuf[0] = 0;
}
else
count = 0;

/*
* Get the current directory string on that drive and its length
*/
if(drive!=0)
{
drvstr[0] = _T('A') - 1 + drive;
drvstr[1] = _T(':');
drvstr[2] = _T('.');
drvstr[3] = _T('\0');
}
else
{
drvstr[0] = _T('.');
drvstr[1] = _T('\0');
}

ret = GetFullPathName( drvstr, count, pnbuf, &pname );

if(ret == 0) {
_dosmaperr( GetLastError() );
return NULL;
}

if( pnbuf != NULL ) {
if(ret < count) {
/* GetFullPathName always returns a value less than that
passed in if successful i.e. we got the required dir */
return pnbuf;
}

errno = ERANGE;
*pnbuf = 0;
return NULL;
}

/* The code comes here only if pnbuf was NULL
Allocate the required memory & call GetFullPathName again*/

maxlen = __max(ret, maxlen) ;
#ifdef _DEBUG
if( (p = (_TSCHAR *)_calloc_dbg( maxlen, sizeof(_TSCHAR), nBlockUse, szFileName, nLine)) == NULL) {
#else /* _DEBUG */
if( (p = (_TSCHAR *)calloc( maxlen, sizeof(_TSCHAR))) == NULL) {
#endif /* _DEBUG */
errno = ENOMEM;


_doserrno = E_nomem;
return NULL;
}


ret = GetFullPathName( drvstr, maxlen, p, &pname );

if(ret == 0
[解决办法]
ret >= maxlen) {
_dosmaperr( GetLastError() );
return NULL;
}

return p;

}
...

读书人网 >C语言

热点推荐