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>)
19 #ifdef FEATURE_DIRECTORY_STACK
29 typedef struct tagDIRENTRY
31 struct tagDIRENTRY
*prev
;
32 struct tagDIRENTRY
*next
;
34 } DIRENTRY
, *LPDIRENTRY
;
37 static INT nStackDepth
;
38 static LPDIRENTRY lpStackTop
;
39 static LPDIRENTRY lpStackBottom
;
43 PushDirectory (LPTSTR pszPath
)
47 lpDir
= (LPDIRENTRY
)malloc (sizeof (DIRENTRY
));
50 error_out_of_memory ();
55 if (lpStackTop
== NULL
)
58 lpStackBottom
= lpDir
;
62 lpDir
->next
= lpStackTop
;
63 lpStackTop
->prev
= lpDir
;
67 lpDir
->pszPath
= (LPTSTR
)malloc ((_tcslen(pszPath
)+1)*sizeof(TCHAR
));
71 error_out_of_memory ();
75 _tcscpy (lpDir
->pszPath
, pszPath
);
92 lpStackTop
= lpDir
->next
;
93 if (lpStackTop
!= NULL
)
94 lpStackTop
->prev
= NULL
;
98 free (lpDir
->pszPath
);
106 GetDirectoryStackTop (LPTSTR pszPath
)
109 _tcsncpy (pszPath
, lpStackTop
->pszPath
, MAX_PATH
);
116 * initialize directory stack
118 VOID
InitDirectoryStack (VOID
)
122 lpStackBottom
= NULL
;
127 * destroy directory stack
129 VOID
DestroyDirectoryStack (VOID
)
136 INT
GetDirectoryStackDepth (VOID
)
145 INT
CommandPushd (LPTSTR first
, LPTSTR rest
)
147 TCHAR curPath
[MAX_PATH
];
148 TCHAR newPath
[MAX_PATH
];
149 BOOL bChangePath
= FALSE
;
151 if (!_tcsncmp (rest
, _T("/?"), 2))
153 ConOutPuts (_T("Stores the current directory for use by the POPD command, then\n"
154 "changes to the specified directory.\n\n"
155 "PUSHD [path | ..]\n\n"
156 " path Specifies the directory to make the current directory"));
160 if (rest
[0] != _T('\0'))
162 GetFullPathName (rest
, MAX_PATH
, newPath
, NULL
);
163 bChangePath
= IsValidPathName (newPath
);
166 GetCurrentDirectory (MAX_PATH
, curPath
);
167 if (PushDirectory (curPath
))
171 SetCurrentDirectory (newPath
);
180 INT
CommandPopd (LPTSTR first
, LPTSTR rest
)
182 TCHAR szPath
[MAX_PATH
];
184 if (!_tcsncmp(rest
, _T("/?"), 2))
186 ConOutPuts (_T("Changes to the directory stored by the PUSHD command.\n\n"
191 if (GetDirectoryStackDepth () == 0)
194 GetDirectoryStackTop (szPath
);
197 SetCurrentDirectory (szPath
);
206 INT
CommandDirs (LPTSTR first
, LPTSTR rest
)
210 if (!_tcsncmp(rest
, _T("/?"), 2))
212 ConOutPuts (_T("Prints the contents of the directory stack.\n"
219 lpDir
= lpStackBottom
;
223 ConOutPuts (_T("Directory stack empty"));
227 while (lpDir
!= NULL
)
229 ConOutPuts (lpDir
->pszPath
);
237 #endif /* FEATURE_DIRECTORY_STACK */