2006-02-16 23:23:37 +00:00
|
|
|
/*
|
|
|
|
* DIRSTACK.C - pushd / pop (directory stack) internal commands.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* History:
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 14-Dec-1998 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Implemented PUSHD and POPD command.
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 20-Jan-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Unicode and redirection safe!
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 20-Jan-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Added DIRS command.
|
|
|
|
*/
|
|
|
|
|
2013-01-24 23:00:42 +00:00
|
|
|
#include "precomp.h"
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
#ifdef FEATURE_DIRECTORY_STACK
|
|
|
|
|
|
|
|
typedef struct tagDIRENTRY
|
|
|
|
{
|
2013-06-30 00:08:43 +00:00
|
|
|
struct tagDIRENTRY *prev;
|
|
|
|
struct tagDIRENTRY *next;
|
|
|
|
TCHAR szPath[1];
|
2006-02-16 23:23:37 +00:00
|
|
|
} DIRENTRY, *LPDIRENTRY;
|
|
|
|
|
|
|
|
|
|
|
|
static INT nStackDepth;
|
|
|
|
static LPDIRENTRY lpStackTop;
|
|
|
|
static LPDIRENTRY lpStackBottom;
|
|
|
|
|
|
|
|
|
|
|
|
static INT
|
|
|
|
PushDirectory (LPTSTR pszPath)
|
|
|
|
{
|
2013-06-30 00:08:43 +00:00
|
|
|
LPDIRENTRY lpDir = cmd_alloc(FIELD_OFFSET(DIRENTRY, szPath[_tcslen(pszPath) + 1]));
|
|
|
|
if (!lpDir)
|
|
|
|
{
|
2017-12-03 17:49:41 +00:00
|
|
|
WARN("Cannot allocate memory for lpDir\n");
|
|
|
|
error_out_of_memory();
|
2013-06-30 00:08:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpDir->prev = NULL;
|
|
|
|
lpDir->next = lpStackTop;
|
|
|
|
if (lpStackTop == NULL)
|
|
|
|
lpStackBottom = lpDir;
|
|
|
|
else
|
|
|
|
lpStackTop->prev = lpDir;
|
|
|
|
lpStackTop = lpDir;
|
|
|
|
|
|
|
|
_tcscpy(lpDir->szPath, pszPath);
|
|
|
|
|
|
|
|
nStackDepth++;
|
|
|
|
|
|
|
|
return nErrorLevel = 0;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static VOID
|
|
|
|
PopDirectory (VOID)
|
|
|
|
{
|
2013-06-30 00:08:43 +00:00
|
|
|
LPDIRENTRY lpDir = lpStackTop;
|
|
|
|
lpStackTop = lpDir->next;
|
|
|
|
if (lpStackTop != NULL)
|
|
|
|
lpStackTop->prev = NULL;
|
|
|
|
else
|
|
|
|
lpStackBottom = NULL;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
cmd_free (lpDir);
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
nStackDepth--;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize directory stack
|
|
|
|
*/
|
|
|
|
VOID InitDirectoryStack (VOID)
|
|
|
|
{
|
2013-06-30 00:08:43 +00:00
|
|
|
nStackDepth = 0;
|
|
|
|
lpStackTop = NULL;
|
|
|
|
lpStackBottom = NULL;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* destroy directory stack
|
|
|
|
*/
|
|
|
|
VOID DestroyDirectoryStack (VOID)
|
|
|
|
{
|
2013-06-30 00:08:43 +00:00
|
|
|
while (nStackDepth)
|
|
|
|
PopDirectory ();
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
INT GetDirectoryStackDepth (VOID)
|
|
|
|
{
|
2013-06-30 00:08:43 +00:00
|
|
|
return nStackDepth;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pushd command
|
|
|
|
*/
|
2008-08-25 23:22:03 +00:00
|
|
|
INT CommandPushd (LPTSTR rest)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2013-06-30 00:08:43 +00:00
|
|
|
TCHAR curPath[MAX_PATH];
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
if (!_tcsncmp (rest, _T("/?"), 2))
|
|
|
|
{
|
|
|
|
ConOutResPuts(STRING_DIRSTACK_HELP1);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
GetCurrentDirectory (MAX_PATH, curPath);
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
if (rest[0] != _T('\0'))
|
|
|
|
{
|
|
|
|
if (!SetRootPath(NULL, rest))
|
|
|
|
return 1;
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
return PushDirectory(curPath);
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* popd command
|
|
|
|
*/
|
2008-08-25 23:22:03 +00:00
|
|
|
INT CommandPopd (LPTSTR rest)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2013-06-30 00:08:43 +00:00
|
|
|
INT ret = 0;
|
|
|
|
if (!_tcsncmp(rest, _T("/?"), 2))
|
|
|
|
{
|
|
|
|
ConOutResPuts(STRING_DIRSTACK_HELP2);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
if (nStackDepth == 0)
|
|
|
|
return 1;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
ret = _tchdir(lpStackTop->szPath) != 0;
|
|
|
|
PopDirectory ();
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
return ret;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dirs command
|
|
|
|
*/
|
2008-08-25 23:22:03 +00:00
|
|
|
INT CommandDirs (LPTSTR rest)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2013-06-30 00:08:43 +00:00
|
|
|
LPDIRENTRY lpDir;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
if (!_tcsncmp(rest, _T("/?"), 2))
|
|
|
|
{
|
|
|
|
ConOutResPuts(STRING_DIRSTACK_HELP3);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
nErrorLevel = 0;
|
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
lpDir = lpStackBottom;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
if (lpDir == NULL)
|
|
|
|
{
|
|
|
|
ConOutResPuts(STRING_DIRSTACK_HELP4);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
while (lpDir != NULL)
|
|
|
|
{
|
|
|
|
ConOutPuts(lpDir->szPath);
|
|
|
|
lpDir = lpDir->prev;
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 00:08:43 +00:00
|
|
|
return 0;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* FEATURE_DIRECTORY_STACK */
|