Skip to content

Commit b4766e6

Browse files
committed
Fix bug that prevented changing drives with a:
Fix to bug report #5 ( #5 ). Both `a:` and `cd /d a:` are made functional again. There is however no support of short paths such a `c:` to refer to the current directory of drive C.
1 parent ceffed1 commit b4766e6

6 files changed

Lines changed: 34 additions & 30 deletions

File tree

libpbat/file/pBat_Win32File.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ int pBat_DirExists(const char *ptrName)
102102
wchar_t* name;
103103
size_t conv;
104104

105+
105106
if ((name = (wchar_t*) libcu8_xconvert(LIBCU8_TO_U16, ptrName,
106107
strlen(ptrName)+1, &conv)) == NULL)
107108
return 0;

libpbat/libpBat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ LIBPBAT void pBat_GetMousePos(FILE* f, char on_move, CONSOLECOORD* co
399399
#define PBAT_TEST_SEPARATOR(p) (*(p) == '/' || *(p) == '\\')
400400
#define PBAT_TEST_UNC_PATH(p) (*(p) == '\\' && *(p+1) == '\\')
401401
#define PBAT_TEST_ROOT_PATH(p) (PBAT_TEST_SEPARATOR(p) && !PBAT_TEST_SEPARATOR(p+1))
402-
#define PBAT_TEST_DRIVE_PATH(p) (*(p) && *(p+1)==':' && PBAT_TEST_SEPARATOR(p+2))
402+
#define PBAT_TEST_DRIVE_PATH(p) (*(p) && *(p+1)==':' && (!*(p+2) || PBAT_TEST_SEPARATOR(p+2)))
403403
#define PBAT_TEST_ABSOLUTE_PATH(p) (PBAT_TEST_UNC_PATH(p) || PBAT_TEST_ROOT_PATH(p) \
404404
|| PBAT_TEST_DRIVE_PATH(p))
405405
#define PBAT_DEF_DELIMITER "\\"

pbat/command/pBat_Cd.c

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int __inline__ pBat_Canonicalize(char* path)
7676
/* If the path is UNC, just skip the very first part */
7777
if (PBAT_TEST_UNC_PATH(path)) {
7878
path += 2;
79-
79+
8080
while (*path && !PBAT_TEST_SEPARATOR(path))
8181
path ++;
8282

@@ -171,8 +171,9 @@ int pBat_SetCurrentDir(char* lpLine)
171171
if (*lpLine == '/') {
172172

173173
/* well, arguably lpCurrentDir[2] is a slash ...
174-
unless lpCurrentDir refers to a unc path, but this
175-
is not handled yet */
174+
unless lpCurrentDir refers to a unc path
175+
which is still unsupported */
176+
176177
strncpy(lpCurrentDir + 2, lpLine, FILENAME_MAX-2);
177178
lpCurrentDir[FILENAME_MAX-1] = '\0';
178179

@@ -402,40 +403,30 @@ int pBat_CmdCd_win(char* lpLine)
402403

403404
if (*pBat_SkipBlanks(lpLine+2) == '\0') {
404405

405-
/* only got a drive name */
406-
varname[1] = *lpLine;
407-
408-
if (!(lpNext = pBat_GetEnv(lpeEnv, varname))) {
409-
410-
lpNext = lpLine;
406+
/* If the only argument is a drive letter eg `x:` do the following
407+
steps :
408+
- check if `=x:` variable exists, if so change current directory to it
409+
- if no change directory to x:\ and set `=x:` accordingly */
411410

412-
}
411+
force = TRUE;
413412

414-
if (pBat_SetCurrentDir(lpNext)) {
413+
varname[1] = *lpLine;
415414

416-
pBat_ShowErrorMessage(PBAT_DIRECTORY_ERROR
417-
| PBAT_PRINT_C_ERROR,
418-
lpNext,
419-
FALSE
420-
);
415+
if (!(lpLine = pBat_GetEnv(lpeEnv, varname))) {
421416

422-
status = PBAT_DIRECTORY_ERROR;
417+
/* append a slash so this becomes a valid path */
418+
lpLine = pBat_SkipBlanks(lpesStr->str);
423419

424420
}
425-
426-
goto end;
427-
428421
}
429422

430-
431-
/* get current curent directory disk */
432423
passed = *lpLine;
433424

434425
}
435426

436427
if ((passed == 0)
437428
|| (toupper(passed) == toupper(current))
438-
|| (force == TRUE)) {
429+
|| force) {
439430

440431
/* change the current directory, yeah */
441432

@@ -454,8 +445,7 @@ int pBat_CmdCd_win(char* lpLine)
454445
}
455446

456447
varname[1] = (passed == 0) ? (current) : (passed);
457-
458-
pBat_SetEnv(lpeEnv, varname, lpLine);
448+
pBat_SetEnv(lpeEnv, varname, (passed == 0) ? lpCurrentDir : lpLine);
459449

460450
end:
461451
pBat_EsFree_Cached(lpesStr);

pbat/core/pBat_Globals.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ char* lpInitVar[]= {
2727
"PBAT_VERSION", PBAT_VERSION,
2828
"PBAT_OS", PBAT_OS,
2929
NULL, NULL, /* PBAT_PATH is dinamically generated */
30+
#ifdef WIN32
31+
NULL, NULL, /* used to initialize the %=x:% variable based on the current path*/
32+
#endif
3033
"PBAT_OS_TYPE", PBAT_OS_TYPE,
3134
"PBAT_START_SCRIPT", START_SCRIPT,
3235
"PROMPT","$P$G",

pbat/core/pBat_Run.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ int pBat_ExecOperators(PARSED_LINE** lpLine)
132132

133133

134134
/* If we get to this, it is because the first command of the
135-
pipe sequence has not be executed, so skip*/
135+
pipe sequence has not been executed, so skip
136+
eg. a || b | c and a fails.*/
136137
case PARSED_STREAM_NODE_PIPE:
137138
status |= EXECOPERATORS_SKIP;
138139

@@ -229,7 +230,7 @@ void pBat_LaunchPipe(struct pipe_launch_data_t* infos)
229230
230231
as this function executes the left hand side of a pipe.
231232
232-
*/
233+
*/
233234
pBat_ExecOutput(infos->stream);
234235

235236
pBat_RunCommand(infos->str, NULL);
@@ -328,6 +329,7 @@ void pBat_RunParsedLine(PARSED_LINE* line)
328329
int lock, ok;
329330
char *pch;
330331

332+
/* loop though operators */
331333
do {
332334

333335
lock = pBat_GetStreamStackLockState(lppsStreamStack);

pbat/init/pBat_Init.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,20 @@ void pBat_Init(void)
179179
Example if LC_NUMERIC is french, then 1.1 becomes 1,1 */
180180
setlocale(LC_NUMERIC, "C");
181181

182-
183-
184182
/* Set the value of %PBAT_PATH% */
185183
lpInitVar[4]="PBAT_PATH";
186184
lpInitVar[5]=lppBatPath;
187185

186+
#ifdef WIN32
187+
char drive_var[] = "=x:";
188+
189+
/* initialize the current %=x:% drive variable with current dir */
190+
drive_var[1] = *lpCurrentDir;
191+
lpInitVar[6] = drive_var;
192+
lpInitVar[7] = lpCurrentDir;
193+
194+
#endif // WIN32
195+
188196
/* Set the default pBat vars */
189197
pBat_InitVar(lpInitVar);
190198

0 commit comments

Comments
 (0)