2 * CMD - Wine-compatible command line interface - built-in functions.
4 * Copyright (C) 1999 D A Pickles
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * On entry to each function, global variables quals, param1, param2 contain
24 * the qualifiers (uppercased and concatenated) and parameters entered, with
25 * environment-variable and batch parameter substitution already done.
30 * - No support for pipes, shell parameters
31 * - Lots of functionality missing from builtins
32 * - Messages etc need international support
35 #define WIN32_LEAN_AND_MEAN
40 void WCMD_execute (char *orig_command, char *parameter, char *substitution);
44 struct env_stack *next;
48 struct env_stack *saved_environment;
49 struct env_stack *pushd_directories;
51 extern HINSTANCE hinst;
52 extern char *inbuilt[];
53 extern int echo_mode, verify_mode;
54 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
55 extern BATCH_CONTEXT *context;
56 extern DWORD errorlevel;
60 /****************************************************************************
63 * Clear the terminal screen.
66 void WCMD_clear_screen (void) {
68 /* Emulate by filling the screen from the top left to bottom right with
69 spaces, then moving the cursor to the top left afterwards */
70 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
71 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
73 if (GetConsoleScreenBufferInfo(hStdOut, &consoleInfo))
78 screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
82 FillConsoleOutputCharacter(hStdOut, ' ', screenSize, topLeft, &screenSize);
83 SetConsoleCursorPosition(hStdOut, topLeft);
87 /****************************************************************************
90 * Change the default i/o device (ie redirect STDin/STDout).
93 void WCMD_change_tty (void) {
99 /****************************************************************************
102 * Copy a file or wildcarded set.
103 * FIXME: No wildcard support
106 void WCMD_copy (void) {
112 static const char overwrite[] = "Overwrite file (Y/N)?";
113 char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile;
115 if (param1[0] == 0x00) {
116 WCMD_output ("Argument missing\n");
120 if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
121 WCMD_output ("Wildcards not yet supported\n");
125 /* If no destination supplied, assume current directory */
126 if (param2[0] == 0x00) {
130 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
131 if (outpath[strlen(outpath) - 1] == '\\')
132 outpath[strlen(outpath) - 1] = '\0';
133 hff = FindFirstFile (outpath, &fd);
134 if (hff != INVALID_HANDLE_VALUE) {
135 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
136 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
137 strcat (outpath, "\\");
138 strcat (outpath, infile);
143 force = (strstr (quals, "/Y") != NULL);
145 hff = FindFirstFile (outpath, &fd);
146 if (hff != INVALID_HANDLE_VALUE) {
148 WCMD_output (overwrite);
149 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
150 if (toupper(string[0]) == 'Y') force = TRUE;
155 status = CopyFile (param1, outpath, FALSE);
156 if (!status) WCMD_print_error ();
160 /****************************************************************************
163 * Create a directory.
165 * this works recursivly. so mkdir dir1\dir2\dir3 will create dir1 and dir2 if
166 * they do not already exist.
169 BOOL create_full_path(CHAR* path)
175 new_path = HeapAlloc(GetProcessHeap(),0,strlen(path)+1);
176 strcpy(new_path,path);
178 while ((len = strlen(new_path)) && new_path[len - 1] == '\\')
179 new_path[len - 1] = 0;
181 while (!CreateDirectory(new_path,NULL))
184 DWORD last_error = GetLastError();
185 if (last_error == ERROR_ALREADY_EXISTS)
188 if (last_error != ERROR_PATH_NOT_FOUND)
194 if (!(slash = strrchr(new_path,'\\')) && ! (slash = strrchr(new_path,'/')))
200 len = slash - new_path;
202 if (!create_full_path(new_path))
207 new_path[len] = '\\';
209 HeapFree(GetProcessHeap(),0,new_path);
213 void WCMD_create_dir (void) {
215 if (param1[0] == 0x00) {
216 WCMD_output ("Argument missing\n");
219 if (!create_full_path(param1)) WCMD_print_error ();
222 /****************************************************************************
225 * Delete a file or wildcarded set.
229 void WCMD_delete (int recurse) {
233 char fpath[MAX_PATH];
236 if (param1[0] == 0x00) {
237 WCMD_output ("Argument missing\n");
240 hff = FindFirstFile (param1, &fd);
241 if (hff == INVALID_HANDLE_VALUE) {
242 WCMD_output ("%s :File Not Found\n",param1);
245 if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
246 && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
247 strcat (param1, "\\*");
252 if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) {
253 strcpy (fpath, param1);
255 p = strrchr (fpath, '\\');
258 strcat (fpath, fd.cFileName);
260 else strcpy (fpath, fd.cFileName);
261 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
262 if (!DeleteFile (fpath)) WCMD_print_error ();
264 } while (FindNextFile(hff, &fd) != 0);
268 if (!DeleteFile (param1)) WCMD_print_error ();
273 /****************************************************************************
276 * Echo input to the screen (or not). We don't try to emulate the bugs
277 * in DOS (try typing "ECHO ON AGAIN" for an example).
280 void WCMD_echo (const char *command) {
282 static const char eon[] = "Echo is ON\n", eoff[] = "Echo is OFF\n";
285 if ((command[0] == '.') && (command[1] == 0)) {
286 WCMD_output (newline);
291 count = strlen(command);
293 if (echo_mode) WCMD_output (eon);
294 else WCMD_output (eoff);
297 if (lstrcmpi(command, "ON") == 0) {
301 if (lstrcmpi(command, "OFF") == 0) {
305 WCMD_output_asis (command);
306 WCMD_output (newline);
310 /**************************************************************************
313 * Batch file loop processing.
314 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
315 * will probably work here, but the reverse is not necessarily the case...
318 void WCMD_for (char *p) {
323 char set[MAX_PATH], param[MAX_PATH];
326 if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
327 || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
328 || (param1[0] != '%')) {
329 WCMD_output ("Syntax error\n");
332 lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
333 WCMD_parameter (p, 4, &cmd);
334 lstrcpy (param, param1);
337 * If the parameter within the set has a wildcard then search for matching files
338 * otherwise do a literal substitution.
342 while (*(item = WCMD_parameter (set, i, NULL))) {
343 if (strpbrk (item, "*?")) {
344 hff = FindFirstFile (item, &fd);
345 if (hff == INVALID_HANDLE_VALUE) {
349 WCMD_execute (cmd, param, fd.cFileName);
350 } while (FindNextFile(hff, &fd) != 0);
354 WCMD_execute (cmd, param, item);
360 /*****************************************************************************
363 * Execute a command after substituting variable text for the supplied parameter
366 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
368 char *new_cmd, *p, *s, *dup;
371 size = lstrlen (orig_cmd);
372 new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
373 dup = s = strdup (orig_cmd);
375 while ((p = strstr (s, param))) {
377 size += lstrlen (subst);
378 new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
380 strcat (new_cmd, subst);
381 s = p + lstrlen (param);
384 WCMD_process_command (new_cmd);
386 LocalFree ((HANDLE)new_cmd);
390 /**************************************************************************
393 * Simple on-line help. Help text is stored in the resource file.
396 void WCMD_give_help (char *command) {
401 command = WCMD_strtrim_leading_spaces(command);
402 if (lstrlen(command) == 0) {
403 LoadString (hinst, 1000, buffer, sizeof(buffer));
404 WCMD_output_asis (buffer);
407 for (i=0; i<=WCMD_EXIT; i++) {
408 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
409 param1, -1, inbuilt[i], -1) == 2) {
410 LoadString (hinst, i, buffer, sizeof(buffer));
411 WCMD_output_asis (buffer);
415 WCMD_output ("No help available for %s\n", param1);
420 /****************************************************************************
423 * Batch file jump instruction. Not the most efficient algorithm ;-)
424 * Prints error message if the specified label cannot be found - the file pointer is
425 * then at EOF, effectively stopping the batch file.
426 * FIXME: DOS is supposed to allow labels with spaces - we don't.
429 void WCMD_goto (void) {
431 char string[MAX_PATH];
433 if (param1[0] == 0x00) {
434 WCMD_output ("Argument missing\n");
437 if (context != NULL) {
438 char *paramStart = param1;
440 /* Handle special :EOF label */
441 if (lstrcmpi (":eof", param1) == 0) {
442 context -> skip_rest = TRUE;
446 /* Support goto :label as well as goto label */
447 if (*paramStart == ':') paramStart++;
449 SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
450 while (WCMD_fgets (string, sizeof(string), context -> h)) {
451 if ((string[0] == ':') && (lstrcmpi (&string[1], paramStart) == 0)) return;
453 WCMD_output ("Target to GOTO not found\n");
458 /*****************************************************************************
461 * Push a directory onto the stack
464 void WCMD_pushd (void) {
465 struct env_stack *curdir;
469 curdir = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
470 thisdir = LocalAlloc (LMEM_FIXED, 1024 * sizeof(WCHAR));
471 if( !curdir || !thisdir ) {
474 WCMD_output ("out of memory\n");
478 GetCurrentDirectoryW (1024, thisdir);
479 status = SetCurrentDirectoryA (param1);
486 curdir -> next = pushd_directories;
487 curdir -> strings = thisdir;
488 pushd_directories = curdir;
493 /*****************************************************************************
496 * Pop a directory from the stack
499 void WCMD_popd (void) {
500 struct env_stack *temp = pushd_directories;
502 if (!pushd_directories)
505 /* pop the old environment from the stack, and make it the current dir */
506 pushd_directories = temp->next;
507 SetCurrentDirectoryW(temp->strings);
508 LocalFree (temp->strings);
512 /****************************************************************************
515 * Batch file conditional.
516 * FIXME: Much more syntax checking needed!
519 void WCMD_if (char *p) {
521 int negate = 0, test = 0;
522 char condition[MAX_PATH], *command, *s;
524 if (!lstrcmpi (param1, "not")) {
526 lstrcpy (condition, param2);
529 lstrcpy (condition, param1);
531 if (!lstrcmpi (condition, "errorlevel")) {
532 if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
533 WCMD_parameter (p, 2+negate, &command);
535 else if (!lstrcmpi (condition, "exist")) {
536 if (GetFileAttributesA(WCMD_parameter (p, 1+negate, NULL)) != INVALID_FILE_ATTRIBUTES) {
539 WCMD_parameter (p, 2+negate, &command);
541 else if (!lstrcmpi (condition, "defined")) {
542 if (GetEnvironmentVariableA(WCMD_parameter (p, 1+negate, NULL), NULL, 0) > 0) {
545 WCMD_parameter (p, 2+negate, &command);
547 else if ((s = strstr (p, "=="))) {
549 if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
550 WCMD_parameter (s, 1, &command);
553 WCMD_output ("Syntax error\n");
556 if (test != negate) {
557 command = strdup (command);
558 WCMD_process_command (command);
563 /****************************************************************************
566 * Move a file, directory tree or wildcarded set of files.
567 * FIXME: Needs input and output files to be fully specified.
570 void WCMD_move (void) {
573 char outpath[MAX_PATH], inpath[MAX_PATH], *infile;
577 if (param1[0] == 0x00) {
578 WCMD_output ("Argument missing\n");
582 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
583 WCMD_output ("Wildcards not yet supported\n");
587 /* If no destination supplied, assume current directory */
588 if (param2[0] == 0x00) {
592 /* If 2nd parm is directory, then use original filename */
593 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
594 if (outpath[strlen(outpath) - 1] == '\\')
595 outpath[strlen(outpath) - 1] = '\0';
596 hff = FindFirstFile (outpath, &fd);
597 if (hff != INVALID_HANDLE_VALUE) {
598 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
599 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
600 strcat (outpath, "\\");
601 strcat (outpath, infile);
606 status = MoveFile (param1, outpath);
607 if (!status) WCMD_print_error ();
610 /****************************************************************************
613 * Wait for keyboard input.
616 void WCMD_pause (void) {
621 WCMD_output (anykey);
622 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
625 /****************************************************************************
628 * Delete a directory.
631 void WCMD_remove_dir (void) {
633 if (param1[0] == 0x00) {
634 WCMD_output ("Argument missing\n");
638 /* If subdirectory search not supplied, just try to remove
639 and report error if it fails (eg if it contains a file) */
640 if (strstr (quals, "/S") == NULL) {
641 if (!RemoveDirectory (param1)) WCMD_print_error ();
643 /* Otherwise use ShFileOp to recursively remove a directory */
646 SHFILEOPSTRUCT lpDir;
649 if (strstr (quals, "/Q") == NULL) {
651 char question[MAXSTRING];
653 /* Ask for confirmation */
654 sprintf(question, "%s, ", param1);
655 ok = WCMD_ask_confirm(question);
657 /* Abort if answer is 'N' */
664 lpDir.pFrom = param1;
665 lpDir.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
666 lpDir.wFunc = FO_DELETE;
667 if (SHFileOperationA(&lpDir)) WCMD_print_error ();
671 /****************************************************************************
675 * FIXME: Needs input and output files to be fully specified.
678 void WCMD_rename (void) {
682 if (param1[0] == 0x00 || param2[0] == 0x00) {
683 WCMD_output ("Argument missing\n");
686 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
687 WCMD_output ("Wildcards not yet supported\n");
690 status = MoveFile (param1, param2);
691 if (!status) WCMD_print_error ();
694 /*****************************************************************************
697 * Make a copy of the environment.
699 static WCHAR *WCMD_dupenv( const WCHAR *env )
709 len += (lstrlenW(&env[len]) + 1);
711 env_copy = LocalAlloc (LMEM_FIXED, (len+1) * sizeof (WCHAR) );
714 WCMD_output ("out of memory\n");
717 memcpy (env_copy, env, len*sizeof (WCHAR));
723 /*****************************************************************************
726 * setlocal pushes the environment onto a stack
727 * Save the environment as unicode so we don't screw anything up.
729 void WCMD_setlocal (const char *s) {
731 struct env_stack *env_copy;
733 /* DISABLEEXTENSIONS ignored */
735 env_copy = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
738 WCMD_output ("out of memory\n");
742 env = GetEnvironmentStringsW ();
744 env_copy->strings = WCMD_dupenv (env);
745 if (env_copy->strings)
747 env_copy->next = saved_environment;
748 saved_environment = env_copy;
751 LocalFree (env_copy);
753 FreeEnvironmentStringsW (env);
756 /*****************************************************************************
759 static inline WCHAR *WCMD_strchrW(WCHAR *str, WCHAR ch)
770 /*****************************************************************************
773 * endlocal pops the environment off a stack
775 void WCMD_endlocal (void) {
776 WCHAR *env, *old, *p;
777 struct env_stack *temp;
780 if (!saved_environment)
783 /* pop the old environment from the stack */
784 temp = saved_environment;
785 saved_environment = temp->next;
787 /* delete the current environment, totally */
788 env = GetEnvironmentStringsW ();
789 old = WCMD_dupenv (GetEnvironmentStringsW ());
792 n = lstrlenW(&old[len]) + 1;
793 p = WCMD_strchrW(&old[len], '=');
797 SetEnvironmentVariableW (&old[len], NULL);
802 FreeEnvironmentStringsW (env);
804 /* restore old environment */
808 n = lstrlenW(&env[len]) + 1;
809 p = WCMD_strchrW(&env[len], '=');
813 SetEnvironmentVariableW (&env[len], p);
821 /*****************************************************************************
822 * WCMD_setshow_attrib
824 * Display and optionally sets DOS attributes on a file or directory
826 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
827 * As a result only the Readonly flag is correctly reported, the Archive bit
828 * is always set and the rest are not implemented. We do the Right Thing anyway.
830 * FIXME: No SET functionality.
834 void WCMD_setshow_attrib (void) {
839 char flags[9] = {" "};
841 if (param1[0] == '-') {
846 if (lstrlen(param1) == 0) {
847 GetCurrentDirectory (sizeof(param1), param1);
848 strcat (param1, "\\*");
851 hff = FindFirstFile (param1, &fd);
852 if (hff == INVALID_HANDLE_VALUE) {
853 WCMD_output ("%s: File Not Found\n",param1);
857 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
858 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
861 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
864 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
867 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
870 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
873 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
876 WCMD_output ("%s %s\n", flags, fd.cFileName);
877 for (count=0; count < 8; count++) flags[count] = ' ';
879 } while (FindNextFile(hff, &fd) != 0);
884 /*****************************************************************************
885 * WCMD_setshow_default
887 * Set/Show the current default directory
890 void WCMD_setshow_default (void) {
895 if (strlen(param1) == 0) {
896 GetCurrentDirectory (sizeof(string), string);
897 strcat (string, "\n");
898 WCMD_output (string);
901 status = SetCurrentDirectory (param1);
910 /****************************************************************************
913 * Set/Show the system date
914 * FIXME: Can't change date yet
917 void WCMD_setshow_date (void) {
919 char curdate[64], buffer[64];
922 if (lstrlen(param1) == 0) {
923 if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
924 curdate, sizeof(curdate))) {
925 WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
926 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
931 else WCMD_print_error ();
938 /****************************************************************************
941 static int WCMD_compare( const void *a, const void *b )
944 const char * const *str_a = a, * const *str_b = b;
945 r = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
946 *str_a, -1, *str_b, -1 );
947 if( r == CSTR_LESS_THAN ) return -1;
948 if( r == CSTR_GREATER_THAN ) return 1;
952 /****************************************************************************
953 * WCMD_setshow_sortenv
955 * sort variables into order for display
956 * Optionally only display those who start with a stub
957 * returns the count displayed
959 static int WCMD_setshow_sortenv(const char *s, const char *stub)
961 UINT count=0, len=0, i, displayedcount=0, stublen=0;
964 if (stub) stublen = strlen(stub);
966 /* count the number of strings, and the total length */
968 len += (lstrlen(&s[len]) + 1);
972 /* add the strings to an array */
973 str = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, count * sizeof (char*) );
977 for( i=1; i<count; i++ )
978 str[i] = str[i-1] + lstrlen(str[i-1]) + 1;
981 qsort( str, count, sizeof (char*), WCMD_compare );
984 for( i=0; i<count; i++ ) {
985 if (!stub || CompareString (LOCALE_USER_DEFAULT,
986 NORM_IGNORECASE | SORT_STRINGSORT,
987 str[i], stublen, stub, -1) == 2) {
988 WCMD_output_asis(str[i]);
989 WCMD_output_asis("\n");
995 return displayedcount;
998 /****************************************************************************
1001 * Set/Show the environment variables
1004 void WCMD_setshow_env (char *s) {
1010 if (strlen(param1) == 0) {
1011 env = GetEnvironmentStrings ();
1012 WCMD_setshow_sortenv( env, NULL );
1015 p = strchr (s, '=');
1017 env = GetEnvironmentStrings ();
1018 if (WCMD_setshow_sortenv( env, s ) == 0) {
1019 WCMD_output ("Environment variable %s not defined\n", s);
1025 if (strlen(p) == 0) p = NULL;
1026 status = SetEnvironmentVariable (s, p);
1027 if ((!status) & (GetLastError() != ERROR_ENVVAR_NOT_FOUND)) WCMD_print_error();
1031 /****************************************************************************
1034 * Set/Show the path environment variable
1037 void WCMD_setshow_path (char *command) {
1042 if (strlen(param1) == 0) {
1043 status = GetEnvironmentVariable ("PATH", string, sizeof(string));
1045 WCMD_output_asis ( "PATH=");
1046 WCMD_output_asis ( string);
1047 WCMD_output_asis ( "\n");
1050 WCMD_output ("PATH not found\n");
1054 if (*command == '=') command++; /* Skip leading '=' */
1055 status = SetEnvironmentVariable ("PATH", command);
1056 if (!status) WCMD_print_error();
1060 /****************************************************************************
1061 * WCMD_setshow_prompt
1063 * Set or show the command prompt.
1066 void WCMD_setshow_prompt (void) {
1070 if (strlen(param1) == 0) {
1071 SetEnvironmentVariable ("PROMPT", NULL);
1075 while ((*s == '=') || (*s == ' ')) s++;
1076 if (strlen(s) == 0) {
1077 SetEnvironmentVariable ("PROMPT", NULL);
1079 else SetEnvironmentVariable ("PROMPT", s);
1083 /****************************************************************************
1086 * Set/Show the system time
1087 * FIXME: Can't change time yet
1090 void WCMD_setshow_time (void) {
1092 char curtime[64], buffer[64];
1096 if (strlen(param1) == 0) {
1098 if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
1099 curtime, sizeof(curtime))) {
1100 WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
1101 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
1106 else WCMD_print_error ();
1113 /****************************************************************************
1116 * Shift batch parameters.
1119 void WCMD_shift (void) {
1121 if (context != NULL) context -> shift_count++;
1125 /****************************************************************************
1128 * Set the console title
1130 void WCMD_title (char *command) {
1131 SetConsoleTitle(command);
1134 /****************************************************************************
1137 * Copy a file to standard output.
1140 void WCMD_type (void) {
1146 if (param1[0] == 0x00) {
1147 WCMD_output ("Argument missing\n");
1150 h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1151 FILE_ATTRIBUTE_NORMAL, NULL);
1152 if (h == INVALID_HANDLE_VALUE) {
1153 WCMD_print_error ();
1156 while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
1157 if (count == 0) break; /* ReadFile reports success on EOF! */
1159 WCMD_output_asis (buffer);
1164 /****************************************************************************
1167 * Display verify flag.
1168 * FIXME: We don't actually do anything with the verify flag other than toggle
1172 void WCMD_verify (char *command) {
1174 static const char von[] = "Verify is ON\n", voff[] = "Verify is OFF\n";
1177 count = strlen(command);
1179 if (verify_mode) WCMD_output (von);
1180 else WCMD_output (voff);
1183 if (lstrcmpi(command, "ON") == 0) {
1187 else if (lstrcmpi(command, "OFF") == 0) {
1191 else WCMD_output ("Verify must be ON or OFF\n");
1194 /****************************************************************************
1197 * Display version info.
1200 void WCMD_version (void) {
1202 WCMD_output (version_string);
1206 /****************************************************************************
1209 * Display volume info and/or set volume label. Returns 0 if error.
1212 int WCMD_volume (int mode, char *path) {
1214 DWORD count, serial;
1215 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
1218 if (lstrlen(path) == 0) {
1219 status = GetCurrentDirectory (sizeof(curdir), curdir);
1221 WCMD_print_error ();
1224 status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
1228 if ((path[1] != ':') || (lstrlen(path) != 2)) {
1229 WCMD_output_asis("Syntax Error\n\n");
1232 wsprintf (curdir, "%s\\", path);
1233 status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
1237 WCMD_print_error ();
1240 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
1241 curdir[0], label, HIWORD(serial), LOWORD(serial));
1243 WCMD_output ("Volume label (11 characters, ENTER for none)?");
1244 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
1246 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
1247 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
1249 if (lstrlen(path) != 0) {
1250 if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
1253 if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();
1259 /**************************************************************************
1262 * Exit either the process, or just this batch program
1266 void WCMD_exit (void) {
1268 int rc = atoi(param1); /* Note: atoi of empty parameter is 0 */
1270 if (context && lstrcmpi(quals, "/B") == 0) {
1272 context -> skip_rest = TRUE;
1278 /**************************************************************************
1281 * Issue a message and ask 'Are you sure (Y/N)', waiting on a valid
1284 * Returns True if Y answer is selected
1287 BOOL WCMD_ask_confirm (char *message) {
1289 char msgbuffer[MAXSTRING];
1290 char Ybuffer[MAXSTRING];
1291 char Nbuffer[MAXSTRING];
1292 char answer[MAX_PATH] = "";
1295 /* Load the translated 'Are you sure', plus valid answers */
1296 LoadString (hinst, WCMD_CONFIRM, msgbuffer, sizeof(msgbuffer));
1297 LoadString (hinst, WCMD_YES, Ybuffer, sizeof(Ybuffer));
1298 LoadString (hinst, WCMD_NO, Nbuffer, sizeof(Nbuffer));
1300 /* Loop waiting on a Y or N */
1301 while (answer[0] != Ybuffer[0] && answer[0] != Nbuffer[0]) {
1302 WCMD_output_asis (message);
1303 WCMD_output_asis (msgbuffer);
1304 WCMD_output_asis (" (");
1305 WCMD_output_asis (Ybuffer);
1306 WCMD_output_asis ("/");
1307 WCMD_output_asis (Nbuffer);
1308 WCMD_output_asis (")?");
1309 ReadFile (GetStdHandle(STD_INPUT_HANDLE), answer, sizeof(answer),
1311 answer[0] = toupper(answer[0]);
1314 /* Return the answer */
1315 return (answer[0] == Ybuffer[0]);