ReactOS  0.4.13-dev-249-gcba1a2f
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 
21 typedef struct tagDIRENTRY
22 {
23  struct tagDIRENTRY *prev;
24  struct tagDIRENTRY *next;
27 
28 
32 
33 
34 static 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 
55  nStackDepth++;
56 
57  return nErrorLevel = 0;
58 }
59 
60 
61 static VOID
63 {
64  LPDIRENTRY lpDir = lpStackTop;
65  lpStackTop = lpDir->next;
66  if (lpStackTop != NULL)
67  lpStackTop->prev = NULL;
68  else
70 
71  cmd_free (lpDir);
72 
73  nStackDepth--;
74 }
75 
76 
77 /*
78  * initialize directory stack
79  */
81 {
82  nStackDepth = 0;
83  lpStackTop = NULL;
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:157
#define STRING_DIRSTACK_HELP4
Definition: resource.h:119
INT CommandDirs(LPTSTR rest)
Definition: dirstack.c:154
struct tagDIRENTRY * prev
Definition: dirstack.c:23
#define WARN(fmt,...)
Definition: debug.h:111
_TCHAR * _tcscpy(_TCHAR *to, const _TCHAR *from)
Definition: tcscpy.h:8
int32_t INT
Definition: typedefs.h:56
CHAR * LPTSTR
Definition: xmlstorage.h:192
struct tagDIRENTRY * LPDIRENTRY
static VOID PopDirectory(VOID)
Definition: dirstack.c:62
INT GetDirectoryStackDepth(VOID)
Definition: dirstack.c:98
#define GetCurrentDirectory
Definition: winbase.h:3619
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
smooth NULL
Definition: ftsmooth.c:416
struct tagDIRENTRY DIRENTRY
VOID error_out_of_memory(VOID)
Definition: error.c:136
static INT nStackDepth
Definition: dirstack.c:29
struct tagDIRENTRY * next
Definition: dirstack.c:24
static LPDIRENTRY lpStackBottom
Definition: dirstack.c:31
VOID InitDirectoryStack(VOID)
Definition: dirstack.c:80
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
VOID DestroyDirectoryStack(VOID)
Definition: dirstack.c:91
#define MAX_PATH
Definition: compat.h:26
int ret
TCHAR szPath[1]
Definition: dirstack.c:25
Definition: fat.h:102
INT CommandPopd(LPTSTR rest)
Definition: dirstack.c:132
#define STRING_DIRSTACK_HELP3
Definition: resource.h:118
BOOL SetRootPath(TCHAR *oldpath, TCHAR *InPath)
Definition: internal.c:182
#define STRING_DIRSTACK_HELP2
Definition: resource.h:117
static INT PushDirectory(LPTSTR pszPath)
Definition: dirstack.c:35
#define cmd_alloc(size)
Definition: cmddbg.h:29
#define cmd_free(ptr)
Definition: cmddbg.h:31
LPCWSTR szPath
Definition: env.c:35
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:254
#define ConOutResPuts(uID)
Definition: console.h:36
INT CommandPushd(LPTSTR rest)
Definition: dirstack.c:107
static LPDIRENTRY lpStackTop
Definition: dirstack.c:30
int _tcsncmp(const _TCHAR *s1, const _TCHAR *s2, size_t n)
Definition: tcsncmp.h:9
int _tchdir(const _TCHAR *_path)
Definition: chdir.c:8
#define STRING_DIRSTACK_HELP1
Definition: resource.h:116
VOID ConOutPuts(LPTSTR szText)
Definition: tee.c:27