Use futimes() instead of utime() to implement SetFileTime, so that it
[wine.git] / programs / wcmd / builtins.c
blob060fc21b37dae099d8a88c4c9e6a11ec36fc10b0
1 /*
2 * WCMD - 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * NOTES:
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.
29 * FIXME:
30 * - No support for pipes, shell parameters
31 * - Lots of functionality missing from builtins
32 * - Messages etc need international support
35 #include "wcmd.h"
37 void WCMD_execute (char *orig_command, char *parameter, char *substitution);
39 struct env_stack
41 struct env_stack *next;
42 WCHAR *strings;
45 struct env_stack *saved_environment;
47 extern HINSTANCE hinst;
48 extern char *inbuilt[];
49 extern char nyi[];
50 extern char newline[];
51 extern char version_string[];
52 extern char anykey[];
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 /****************************************************************************
61 * WCMD_clear_screen
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))
75 COORD topLeft;
76 long screenSize;
78 screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
80 topLeft.X = 0;
81 topLeft.Y = 0;
82 FillConsoleOutputCharacter(hStdOut, ' ', screenSize, topLeft, &screenSize);
83 SetConsoleCursorPosition(hStdOut, topLeft);
87 /****************************************************************************
88 * WCMD_change_tty
90 * Change the default i/o device (ie redirect STDin/STDout).
93 void WCMD_change_tty (void) {
95 WCMD_output (nyi);
99 /****************************************************************************
100 * WCMD_copy
102 * Copy a file or wildcarded set.
103 * FIXME: No wildcard support
106 void WCMD_copy (void) {
108 DWORD count;
109 WIN32_FIND_DATA fd;
110 HANDLE hff;
111 BOOL force, status;
112 static const char *overwrite = "Overwrite file (Y/N)?";
113 char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile;
115 if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
116 WCMD_output ("Wildcards not yet supported\n");
117 return;
120 /* If no destination supplied, assume current directory */
121 if (param2[0] == 0x00) {
122 strcpy(param2, ".");
125 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
126 hff = FindFirstFile (outpath, &fd);
127 if (hff != INVALID_HANDLE_VALUE) {
128 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
129 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
130 strcat (outpath, "\\");
131 strcat (outpath, infile);
133 FindClose (hff);
136 force = (strstr (quals, "/Y") != NULL);
137 if (!force) {
138 hff = FindFirstFile (outpath, &fd);
139 if (hff != INVALID_HANDLE_VALUE) {
140 FindClose (hff);
141 WCMD_output (overwrite);
142 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
143 if (toupper(string[0]) == 'Y') force = TRUE;
145 else force = TRUE;
147 if (force) {
148 status = CopyFile (param1, outpath, FALSE);
149 if (!status) WCMD_print_error ();
153 /****************************************************************************
154 * WCMD_create_dir
156 * Create a directory.
159 void WCMD_create_dir (void) {
161 if (!CreateDirectory (param1, NULL)) WCMD_print_error ();
164 /****************************************************************************
165 * WCMD_delete
167 * Delete a file or wildcarded set.
171 void WCMD_delete (int recurse) {
173 WIN32_FIND_DATA fd;
174 HANDLE hff;
175 char fpath[MAX_PATH];
176 char *p;
178 hff = FindFirstFile (param1, &fd);
179 if (hff == INVALID_HANDLE_VALUE) {
180 WCMD_output ("%s :File Not Found\n",param1);
181 return;
183 if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
184 && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
185 strcat (param1, "\\*");
186 FindClose(hff);
187 WCMD_delete (1);
188 return;
190 if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) {
191 strcpy (fpath, param1);
192 do {
193 p = strrchr (fpath, '\\');
194 if (p != NULL) {
195 *++p = '\0';
196 strcat (fpath, fd.cFileName);
198 else strcpy (fpath, fd.cFileName);
199 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
200 if (!DeleteFile (fpath)) WCMD_print_error ();
202 } while (FindNextFile(hff, &fd) != 0);
203 FindClose (hff);
205 else {
206 if (!DeleteFile (param1)) WCMD_print_error ();
207 FindClose (hff);
211 /****************************************************************************
212 * WCMD_echo
214 * Echo input to the screen (or not). We don't try to emulate the bugs
215 * in DOS (try typing "ECHO ON AGAIN" for an example).
218 void WCMD_echo (const char *command) {
220 static const char *eon = "Echo is ON\n", *eoff = "Echo is OFF\n";
221 int count;
223 if ((command[0] == '.') && (command[1] == 0)) {
224 WCMD_output (newline);
225 return;
227 if (command[0]==' ')
228 command++;
229 count = strlen(command);
230 if (count == 0) {
231 if (echo_mode) WCMD_output (eon);
232 else WCMD_output (eoff);
233 return;
235 if (lstrcmpi(command, "ON") == 0) {
236 echo_mode = 1;
237 return;
239 if (lstrcmpi(command, "OFF") == 0) {
240 echo_mode = 0;
241 return;
243 WCMD_output_asis (command);
244 WCMD_output (newline);
248 /**************************************************************************
249 * WCMD_for
251 * Batch file loop processing.
252 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
253 * will probably work here, but the reverse is not necessarily the case...
256 void WCMD_for (char *p) {
258 WIN32_FIND_DATA fd;
259 HANDLE hff;
260 char *cmd, *item;
261 char set[MAX_PATH], param[MAX_PATH];
262 int i;
264 if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
265 || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
266 || (param1[0] != '%')) {
267 WCMD_output ("Syntax error\n");
268 return;
270 lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
271 WCMD_parameter (p, 4, &cmd);
272 lstrcpy (param, param1);
275 * If the parameter within the set has a wildcard then search for matching files
276 * otherwise do a literal substitution.
279 i = 0;
280 while (*(item = WCMD_parameter (set, i, NULL))) {
281 if (strpbrk (item, "*?")) {
282 hff = FindFirstFile (item, &fd);
283 if (hff == INVALID_HANDLE_VALUE) {
284 return;
286 do {
287 WCMD_execute (cmd, param, fd.cFileName);
288 } while (FindNextFile(hff, &fd) != 0);
289 FindClose (hff);
291 else {
292 WCMD_execute (cmd, param, item);
294 i++;
298 /*****************************************************************************
299 * WCMD_Execute
301 * Execute a command after substituting variable text for the supplied parameter
304 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
306 char *new_cmd, *p, *s, *dup;
307 int size;
309 size = lstrlen (orig_cmd);
310 new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
311 dup = s = strdup (orig_cmd);
313 while ((p = strstr (s, param))) {
314 *p = '\0';
315 size += lstrlen (subst);
316 new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
317 strcat (new_cmd, s);
318 strcat (new_cmd, subst);
319 s = p + lstrlen (param);
321 strcat (new_cmd, s);
322 WCMD_process_command (new_cmd);
323 free (dup);
324 LocalFree ((HANDLE)new_cmd);
328 /**************************************************************************
329 * WCMD_give_help
331 * Simple on-line help. Help text is stored in the resource file.
334 void WCMD_give_help (char *command) {
336 int i;
337 char buffer[2048];
339 command = WCMD_strtrim_leading_spaces(command);
340 if (lstrlen(command) == 0) {
341 LoadString (hinst, 1000, buffer, sizeof(buffer));
342 WCMD_output_asis (buffer);
344 else {
345 for (i=0; i<=WCMD_EXIT; i++) {
346 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
347 param1, -1, inbuilt[i], -1) == 2) {
348 LoadString (hinst, i, buffer, sizeof(buffer));
349 WCMD_output (buffer);
350 return;
353 WCMD_output ("No help available for %s\n", param1);
355 return;
358 /****************************************************************************
359 * WCMD_go_to
361 * Batch file jump instruction. Not the most efficient algorithm ;-)
362 * Prints error message if the specified label cannot be found - the file pointer is
363 * then at EOF, effectively stopping the batch file.
364 * FIXME: DOS is supposed to allow labels with spaces - we don't.
367 void WCMD_goto (void) {
369 char string[MAX_PATH];
371 if (context != NULL) {
372 SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
373 while (WCMD_fgets (string, sizeof(string), context -> h)) {
374 if ((string[0] == ':') && (strcmp (&string[1], param1) == 0)) return;
376 WCMD_output ("Target to GOTO not found\n");
378 return;
382 /****************************************************************************
383 * WCMD_if
385 * Batch file conditional.
386 * FIXME: Much more syntax checking needed!
389 void WCMD_if (char *p) {
391 HANDLE h;
392 int negate = 0, test = 0;
393 char condition[MAX_PATH], *command, *s;
395 if (!lstrcmpi (param1, "not")) {
396 negate = 1;
397 lstrcpy (condition, param2);
399 else {
400 lstrcpy (condition, param1);
402 if (!lstrcmpi (condition, "errorlevel")) {
403 if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
404 return;
405 WCMD_parameter (p, 2+negate, &command);
407 else if (!lstrcmpi (condition, "exist")) {
408 if ((h = CreateFile (WCMD_parameter (p, 1+negate, NULL), GENERIC_READ,
409 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
410 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) {
411 CloseHandle (h);
412 test = 1;
414 WCMD_parameter (p, 2+negate, &command);
416 else if ((s = strstr (p, "=="))) {
417 s += 2;
418 if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
419 WCMD_parameter (s, 1, &command);
421 else {
422 WCMD_output ("Syntax error\n");
423 return;
425 if (test != negate) {
426 command = strdup (command);
427 WCMD_process_command (command);
428 free (command);
432 /****************************************************************************
433 * WCMD_move
435 * Move a file, directory tree or wildcarded set of files.
436 * FIXME: Needs input and output files to be fully specified.
439 void WCMD_move (void) {
441 int status;
442 char outpath[MAX_PATH], inpath[MAX_PATH], *infile;
443 WIN32_FIND_DATA fd;
444 HANDLE hff;
446 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
447 WCMD_output ("Wildcards not yet supported\n");
448 return;
451 /* If no destination supplied, assume current directory */
452 if (param2[0] == 0x00) {
453 strcpy(param2, ".");
456 /* If 2nd parm is directory, then use original filename */
457 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
458 hff = FindFirstFile (outpath, &fd);
459 if (hff != INVALID_HANDLE_VALUE) {
460 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
461 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
462 strcat (outpath, "\\");
463 strcat (outpath, infile);
465 FindClose (hff);
468 status = MoveFile (param1, outpath);
469 if (!status) WCMD_print_error ();
472 /****************************************************************************
473 * WCMD_pause
475 * Wait for keyboard input.
478 void WCMD_pause (void) {
480 DWORD count;
481 char string[32];
483 WCMD_output (anykey);
484 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
487 /****************************************************************************
488 * WCMD_remove_dir
490 * Delete a directory.
493 void WCMD_remove_dir (void) {
495 if (!RemoveDirectory (param1)) WCMD_print_error ();
498 /****************************************************************************
499 * WCMD_rename
501 * Rename a file.
502 * FIXME: Needs input and output files to be fully specified.
505 void WCMD_rename (void) {
507 int status;
509 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
510 WCMD_output ("Wildcards not yet supported\n");
511 return;
513 status = MoveFile (param1, param2);
514 if (!status) WCMD_print_error ();
517 /*****************************************************************************
518 * WCMD_dupenv
520 * Make a copy of the environment.
522 WCHAR *WCMD_dupenv( const WCHAR *env )
524 WCHAR *env_copy;
525 int len;
527 if( !env )
528 return NULL;
530 len = 0;
531 while ( env[len] )
532 len += (lstrlenW(&env[len]) + 1);
534 env_copy = LocalAlloc (LMEM_FIXED, (len+1) * sizeof (WCHAR) );
535 if (!env_copy)
537 WCMD_output ("out of memory\n");
538 return env_copy;
540 memcpy (env_copy, env, len*sizeof (WCHAR));
541 env_copy[len] = 0;
543 return env_copy;
546 /*****************************************************************************
547 * WCMD_setlocal
549 * setlocal pushes the environment onto a stack
550 * Save the environment as unicode so we don't screw anything up.
552 void WCMD_setlocal (const char *s) {
553 WCHAR *env;
554 struct env_stack *env_copy;
556 /* DISABLEEXTENSIONS ignored */
558 env_copy = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
559 if( !env_copy )
561 WCMD_output ("out of memory\n");
562 return;
565 env = GetEnvironmentStringsW ();
567 env_copy->strings = WCMD_dupenv (env);
568 if (env_copy->strings)
570 env_copy->next = saved_environment;
571 saved_environment = env_copy;
573 else
574 LocalFree (env_copy);
576 FreeEnvironmentStringsW (env);
579 /*****************************************************************************
580 * WCMD_strchrW
582 inline WCHAR *WCMD_strchrW(WCHAR *str, WCHAR ch)
584 while(*str)
586 if(*str == ch)
587 return str;
588 str++;
590 return NULL;
593 /*****************************************************************************
594 * WCMD_endlocal
596 * endlocal pops the environment off a stack
598 void WCMD_endlocal (void) {
599 WCHAR *env, *old, *p;
600 struct env_stack *temp;
601 int len, n;
603 if (!saved_environment)
604 return;
606 /* pop the old environment from the stack */
607 temp = saved_environment;
608 saved_environment = temp->next;
610 /* delete the current environment, totally */
611 env = GetEnvironmentStringsW ();
612 old = WCMD_dupenv (GetEnvironmentStringsW ());
613 len = 0;
614 while (old[len]) {
615 n = lstrlenW(&old[len]) + 1;
616 p = WCMD_strchrW(&old[len], '=');
617 if (p)
619 *p++ = 0;
620 SetEnvironmentVariableW (&old[len], NULL);
622 len += n;
624 LocalFree (old);
625 FreeEnvironmentStringsW (env);
627 /* restore old environment */
628 env = temp->strings;
629 len = 0;
630 while (env[len]) {
631 n = lstrlenW(&env[len]) + 1;
632 p = WCMD_strchrW(&env[len], '=');
633 if (p)
635 *p++ = 0;
636 SetEnvironmentVariableW (&env[len], p);
638 len += n;
640 LocalFree (env);
641 LocalFree (temp);
644 /*****************************************************************************
645 * WCMD_setshow_attrib
647 * Display and optionally sets DOS attributes on a file or directory
649 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
650 * As a result only the Readonly flag is correctly reported, the Archive bit
651 * is always set and the rest are not implemented. We do the Right Thing anyway.
653 * FIXME: No SET functionality.
657 void WCMD_setshow_attrib (void) {
659 DWORD count;
660 HANDLE hff;
661 WIN32_FIND_DATA fd;
662 char flags[9] = {" "};
664 if (param1[0] == '-') {
665 WCMD_output (nyi);
666 return;
669 if (lstrlen(param1) == 0) {
670 GetCurrentDirectory (sizeof(param1), param1);
671 strcat (param1, "\\*");
674 hff = FindFirstFile (param1, &fd);
675 if (hff == INVALID_HANDLE_VALUE) {
676 WCMD_output ("%s: File Not Found\n",param1);
678 else {
679 do {
680 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
681 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
682 flags[0] = 'H';
684 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
685 flags[1] = 'S';
687 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
688 flags[2] = 'A';
690 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
691 flags[3] = 'R';
693 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
694 flags[4] = 'T';
696 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
697 flags[5] = 'C';
699 WCMD_output ("%s %s\n", flags, fd.cFileName);
700 for (count=0; count < 8; count++) flags[count] = ' ';
702 } while (FindNextFile(hff, &fd) != 0);
704 FindClose (hff);
707 /*****************************************************************************
708 * WCMD_setshow_default
710 * Set/Show the current default directory
713 void WCMD_setshow_default (void) {
715 BOOL status;
716 char string[1024];
718 if (strlen(param1) == 0) {
719 GetCurrentDirectory (sizeof(string), string);
720 strcat (string, "\n");
721 WCMD_output (string);
723 else {
724 status = SetCurrentDirectory (param1);
725 if (!status) {
726 WCMD_print_error ();
727 return;
730 return;
733 /****************************************************************************
734 * WCMD_setshow_date
736 * Set/Show the system date
737 * FIXME: Can't change date yet
740 void WCMD_setshow_date (void) {
742 char curdate[64], buffer[64];
743 DWORD count;
745 if (lstrlen(param1) == 0) {
746 if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
747 curdate, sizeof(curdate))) {
748 WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
749 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
750 if (count > 2) {
751 WCMD_output (nyi);
754 else WCMD_print_error ();
756 else {
757 WCMD_output (nyi);
761 /****************************************************************************
762 * WCMD_compare
764 int WCMD_compare( const void *a, const void *b )
766 int r;
767 const char * const *str_a = a, * const *str_b = b;
768 r = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
769 *str_a, -1, *str_b, -1 );
770 if( r == CSTR_LESS_THAN ) return -1;
771 if( r == CSTR_GREATER_THAN ) return 1;
772 return 0;
775 /****************************************************************************
776 * WCMD_setshow_sortenv
778 * sort variables into order for display
780 void WCMD_setshow_sortenv(const char *s)
782 UINT count=0, len=0, i;
783 const char **str;
785 /* count the number of strings, and the total length */
786 while ( s[len] ) {
787 len += (lstrlen(&s[len]) + 1);
788 count++;
791 /* add the strings to an array */
792 str = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, count * sizeof (char*) );
793 if( !str )
794 return;
795 str[0] = s;
796 for( i=1; i<count; i++ )
797 str[i] = str[i-1] + lstrlen(str[i-1]) + 1;
799 /* sort the array */
800 qsort( str, count, sizeof (char*), WCMD_compare );
802 /* print it */
803 for( i=0; i<count; i++ )
804 WCMD_output("%s\n", str[i] );
806 LocalFree( str );
809 /****************************************************************************
810 * WCMD_setshow_env
812 * Set/Show the environment variables
815 void WCMD_setshow_env (char *s) {
817 LPVOID env;
818 char *p;
819 int status;
820 char buffer[1048];
822 if (strlen(param1) == 0) {
823 env = GetEnvironmentStrings ();
824 WCMD_setshow_sortenv( env );
826 else {
827 p = strchr (s, '=');
828 if (p == NULL) {
830 /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
831 environment variable C, whereas on NT it shows ALL variables
832 starting with C.
834 status = GetEnvironmentVariable(s, buffer, sizeof(buffer));
835 if (status) {
836 WCMD_output("%s=%s\n", s, buffer);
837 } else {
838 WCMD_output ("Environment variable %s not defined\n", s);
840 return;
842 *p++ = '\0';
844 if (strlen(p) == 0) p = 0x00;
845 status = SetEnvironmentVariable (s, p);
846 if (!status) WCMD_print_error();
848 /* WCMD_output (newline); @JED*/
851 /****************************************************************************
852 * WCMD_setshow_path
854 * Set/Show the path environment variable
857 void WCMD_setshow_path (char *command) {
859 char string[1024];
860 DWORD status;
862 if (strlen(param1) == 0) {
863 status = GetEnvironmentVariable ("PATH", string, sizeof(string));
864 if (status != 0) {
865 WCMD_output ("PATH=%s\n", string);
867 else {
868 WCMD_output ("PATH not found\n");
871 else {
872 status = SetEnvironmentVariable ("PATH", command);
873 if (!status) WCMD_print_error();
877 /****************************************************************************
878 * WCMD_setshow_prompt
880 * Set or show the command prompt.
883 void WCMD_setshow_prompt (void) {
885 char *s;
887 if (strlen(param1) == 0) {
888 SetEnvironmentVariable ("PROMPT", NULL);
890 else {
891 s = param1;
892 while ((*s == '=') || (*s == ' ')) s++;
893 if (strlen(s) == 0) {
894 SetEnvironmentVariable ("PROMPT", NULL);
896 else SetEnvironmentVariable ("PROMPT", s);
900 /****************************************************************************
901 * WCMD_setshow_time
903 * Set/Show the system time
904 * FIXME: Can't change time yet
907 void WCMD_setshow_time (void) {
909 char curtime[64], buffer[64];
910 DWORD count;
911 SYSTEMTIME st;
913 if (strlen(param1) == 0) {
914 GetLocalTime(&st);
915 if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
916 curtime, sizeof(curtime))) {
917 WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
918 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
919 if (count > 2) {
920 WCMD_output (nyi);
923 else WCMD_print_error ();
925 else {
926 WCMD_output (nyi);
930 /****************************************************************************
931 * WCMD_shift
933 * Shift batch parameters.
936 void WCMD_shift (void) {
938 if (context != NULL) context -> shift_count++;
942 /****************************************************************************
943 * WCMD_title
945 * Set the console title
947 void WCMD_title (char *command) {
948 SetConsoleTitle(command);
951 /****************************************************************************
952 * WCMD_type
954 * Copy a file to standard output.
957 void WCMD_type (void) {
959 HANDLE h;
960 char buffer[512];
961 DWORD count;
963 h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
964 FILE_ATTRIBUTE_NORMAL, NULL);
965 if (h == INVALID_HANDLE_VALUE) {
966 WCMD_print_error ();
967 return;
969 while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
970 if (count == 0) break; /* ReadFile reports success on EOF! */
971 buffer[count] = 0;
972 WCMD_output_asis (buffer);
974 CloseHandle (h);
977 /****************************************************************************
978 * WCMD_verify
980 * Display verify flag.
981 * FIXME: We don't actually do anything with the verify flag other than toggle
982 * it...
985 void WCMD_verify (char *command) {
987 static const char *von = "Verify is ON\n", *voff = "Verify is OFF\n";
988 int count;
990 count = strlen(command);
991 if (count == 0) {
992 if (verify_mode) WCMD_output (von);
993 else WCMD_output (voff);
994 return;
996 if (lstrcmpi(command, "ON") == 0) {
997 verify_mode = 1;
998 return;
1000 else if (lstrcmpi(command, "OFF") == 0) {
1001 verify_mode = 0;
1002 return;
1004 else WCMD_output ("Verify must be ON or OFF\n");
1007 /****************************************************************************
1008 * WCMD_version
1010 * Display version info.
1013 void WCMD_version (void) {
1015 WCMD_output (version_string);
1019 /****************************************************************************
1020 * WCMD_volume
1022 * Display volume info and/or set volume label. Returns 0 if error.
1025 int WCMD_volume (int mode, char *path) {
1027 DWORD count, serial;
1028 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
1029 BOOL status;
1030 static char syntax[] = "Syntax Error\n\n";
1032 if (lstrlen(path) == 0) {
1033 status = GetCurrentDirectory (sizeof(curdir), curdir);
1034 if (!status) {
1035 WCMD_print_error ();
1036 return 0;
1038 status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
1039 NULL, NULL, 0);
1041 else {
1042 if ((path[1] != ':') || (lstrlen(path) != 2)) {
1043 WCMD_output_asis(syntax);
1044 return 0;
1046 wsprintf (curdir, "%s\\", path);
1047 status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
1048 NULL, NULL, 0);
1050 if (!status) {
1051 WCMD_print_error ();
1052 return 0;
1054 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
1055 curdir[0], label, HIWORD(serial), LOWORD(serial));
1056 if (mode) {
1057 WCMD_output ("Volume label (11 characters, ENTER for none)?");
1058 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
1059 if (count > 1) {
1060 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
1061 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
1063 if (lstrlen(path) != 0) {
1064 if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
1066 else {
1067 if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();
1070 return 1;