ReactOS 0.4.15-dev-7846-g8ba6c66
dirstack.c
Go to the documentation of this file.
1/*
2 * DIRSTACK.C - pushd / pop (directory stack) internal commands.
3 *
4 *
5 * History:
6 *
7 * 14-Dec-1998 (Eric Kohl)
8 * Implemented PUSHD and POPD command.
9 *
10 * 20-Jan-1999 (Eric Kohl)
11 * Unicode and redirection safe!
12 *
13 * 20-Jan-1999 (Eric Kohl)
14 * Added DIRS command.
15 */
16
17#include "precomp.h"
18
19#ifdef FEATURE_DIRECTORY_STACK
20
21typedef struct tagDIRENTRY
22{
27
28
32
33
34static INT
36{
37 LPDIRENTRY lpDir = cmd_alloc(FIELD_OFFSET(DIRENTRY, szPath[_tcslen(pszPath) + 1]));
38 if (!lpDir)
39 {
40 WARN("Cannot allocate memory for lpDir\n");
42 return -1;
43 }
44
45 lpDir->prev = NULL;
46 lpDir->next = lpStackTop;
47 if (lpStackTop == NULL)
48 lpStackBottom = lpDir;
49 else
50 lpStackTop->prev = lpDir;
51 lpStackTop = lpDir;
52
53 _tcscpy(lpDir->szPath, pszPath);
54
56
57 return nErrorLevel = 0;
58}
59
60
61static VOID
63{
64 LPDIRENTRY lpDir = lpStackTop;
65 lpStackTop = lpDir->next;
66 if (lpStackTop != NULL)
68 else
70
71 cmd_free (lpDir);
72
74}
75
76
77/*
78 * initialize directory stack
79 */
81{
82 nStackDepth = 0;
85}
86
87
88/*
89 * destroy directory stack
90 */
92{
93 while (nStackDepth)
94 PopDirectory ();
95}
96
97
99{
100 return nStackDepth;
101}
102
103
104/*
105 * pushd command
106 */
108{
109 TCHAR curPath[MAX_PATH];
110
111 if (!_tcsncmp (rest, _T("/?"), 2))
112 {
114 return 0;
115 }
116
117 GetCurrentDirectory (MAX_PATH, curPath);
118
119 if (rest[0] != _T('\0'))
120 {
121 if (!SetRootPath(NULL, rest))
122 return 1;
123 }
124
125 return PushDirectory(curPath);
126}
127
128
129/*
130 * popd command
131 */
133{
134 INT ret = 0;
135 if (!_tcsncmp(rest, _T("/?"), 2))
136 {
138 return 0;
139 }
140
141 if (nStackDepth == 0)
142 return 1;
143
144 ret = _tchdir(lpStackTop->szPath) != 0;
145 PopDirectory ();
146
147 return ret;
148}
149
150
151/*
152 * dirs command
153 */
155{
156 LPDIRENTRY lpDir;
157
158 if (!_tcsncmp(rest, _T("/?"), 2))
159 {
161 return 0;
162 }
163
164 nErrorLevel = 0;
165
166 lpDir = lpStackBottom;
167
168 if (lpDir == NULL)
169 {
171 return 0;
172 }
173
174 while (lpDir != NULL)
175 {
176 ConOutPuts(lpDir->szPath);
177 lpDir = lpDir->prev;
178 }
179
180 return 0;
181}
182
183#endif /* FEATURE_DIRECTORY_STACK */
INT nErrorLevel
Definition: cmd.c:158
VOID error_out_of_memory(VOID)
Definition: error.c:138
BOOL SetRootPath(TCHAR *oldpath, TCHAR *InPath)
Definition: internal.c:182
#define ConOutPuts(szStr)
Definition: console.h:29
#define ConOutResPuts(uID)
Definition: console.h:35
#define STRING_DIRSTACK_HELP4
Definition: resource.h:118
#define STRING_DIRSTACK_HELP1
Definition: resource.h:115
#define STRING_DIRSTACK_HELP3
Definition: resource.h:117
#define STRING_DIRSTACK_HELP2
Definition: resource.h:116
#define WARN(fmt,...)
Definition: debug.h:112
#define cmd_free(ptr)
Definition: cmddbg.h:31
#define cmd_alloc(size)
Definition: cmddbg.h:29
VOID DestroyDirectoryStack(VOID)
Definition: dirstack.c:91
INT CommandDirs(LPTSTR rest)
Definition: dirstack.c:154
struct tagDIRENTRY * LPDIRENTRY
VOID InitDirectoryStack(VOID)
Definition: dirstack.c:80
static VOID PopDirectory(VOID)
Definition: dirstack.c:62
INT CommandPopd(LPTSTR rest)
Definition: dirstack.c:132
static LPDIRENTRY lpStackTop
Definition: dirstack.c:30
INT GetDirectoryStackDepth(VOID)
Definition: dirstack.c:98
static LPDIRENTRY lpStackBottom
Definition: dirstack.c:31
static INT nStackDepth
Definition: dirstack.c:29
static INT PushDirectory(LPTSTR pszPath)
Definition: dirstack.c:35
struct tagDIRENTRY DIRENTRY
INT CommandPushd(LPTSTR rest)
Definition: dirstack.c:107
#define NULL
Definition: types.h:112
#define MAX_PATH
Definition: compat.h:34
#define _tcscpy
Definition: tchar.h:623
#define _tcsncmp
Definition: tchar.h:1428
#define _tchdir
Definition: tchar.h:672
LPCWSTR szPath
Definition: env.c:37
Definition: fat.h:103
TCHAR szPath[1]
Definition: dirstack.c:25
struct tagDIRENTRY * next
Definition: dirstack.c:24
struct tagDIRENTRY * prev
Definition: dirstack.c:23
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
int32_t INT
Definition: typedefs.h:58
#define _T(x)
Definition: vfdio.h:22
int ret
#define GetCurrentDirectory
Definition: winbase.h:3740
char TCHAR
Definition: xmlstorage.h:189
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define _tcslen
Definition: xmlstorage.h:198