2 * DIRSTACK.C - pushd / pop (directory stack) internal commands.
7 * 14-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
8 * Implemented PUSHD and POPD command.
10 * 20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
11 * Unicode and redirection safe!
13 * 20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
20 #ifdef FEATURE_DIRECTORY_STACK
22 typedef struct tagDIRENTRY
24 struct tagDIRENTRY
*prev
;
25 struct tagDIRENTRY
*next
;
27 } DIRENTRY
, *LPDIRENTRY
;
30 static INT nStackDepth
;
31 static LPDIRENTRY lpStackTop
;
32 static LPDIRENTRY lpStackBottom
;
36 PushDirectory (LPTSTR pszPath
)
40 lpDir
= (LPDIRENTRY
)malloc (sizeof (DIRENTRY
));
43 error_out_of_memory ();
48 if (lpStackTop
== NULL
)
51 lpStackBottom
= lpDir
;
55 lpDir
->next
= lpStackTop
;
56 lpStackTop
->prev
= lpDir
;
60 lpDir
->pszPath
= (LPTSTR
)malloc ((_tcslen(pszPath
)+1)*sizeof(TCHAR
));
64 error_out_of_memory ();
68 _tcscpy (lpDir
->pszPath
, pszPath
);
85 lpStackTop
= lpDir
->next
;
86 if (lpStackTop
!= NULL
)
87 lpStackTop
->prev
= NULL
;
91 free (lpDir
->pszPath
);
99 GetDirectoryStackTop (LPTSTR pszPath
)
102 _tcsncpy (pszPath
, lpStackTop
->pszPath
, MAX_PATH
);
109 * initialize directory stack
111 VOID
InitDirectoryStack (VOID
)
115 lpStackBottom
= NULL
;
120 * destroy directory stack
122 VOID
DestroyDirectoryStack (VOID
)
129 INT
GetDirectoryStackDepth (VOID
)
138 INT
CommandPushd (LPTSTR first
, LPTSTR rest
)
140 TCHAR curPath
[MAX_PATH
];
141 TCHAR newPath
[MAX_PATH
];
142 BOOL bChangePath
= FALSE
;
144 if (!_tcsncmp (rest
, _T("/?"), 2))
146 ConOutResPuts(STRING_DIRSTACK_HELP1
);
150 if (rest
[0] != _T('\0'))
152 GetFullPathName (rest
, MAX_PATH
, newPath
, NULL
);
153 bChangePath
= IsValidPathName (newPath
);
156 GetCurrentDirectory (MAX_PATH
, curPath
);
157 if (PushDirectory (curPath
))
161 SetCurrentDirectory (newPath
);
170 INT
CommandPopd (LPTSTR first
, LPTSTR rest
)
172 TCHAR szPath
[MAX_PATH
];
174 if (!_tcsncmp(rest
, _T("/?"), 2))
176 ConOutResPuts(STRING_DIRSTACK_HELP2
);
180 if (GetDirectoryStackDepth () == 0)
183 GetDirectoryStackTop (szPath
);
186 SetCurrentDirectory (szPath
);
195 INT
CommandDirs (LPTSTR first
, LPTSTR rest
)
199 if (!_tcsncmp(rest
, _T("/?"), 2))
201 ConOutResPuts(STRING_DIRSTACK_HELP3
);
206 lpDir
= lpStackBottom
;
210 ConOutResPuts(STRING_DIRSTACK_HELP4
);
214 while (lpDir
!= NULL
)
216 ConOutPuts (lpDir
->pszPath
);
224 #endif /* FEATURE_DIRECTORY_STACK */