msi: Only insert entries into listbox if property value matches.
[wine] / programs / cmd / builtins.c
1 /*
2  * CMD - Wine-compatible command line interface - built-in functions.
3  *
4  * Copyright (C) 1999 D A Pickles
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /*
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.
26  */
27
28 /*
29  * FIXME:
30  * - No support for pipes, shell parameters
31  * - Lots of functionality missing from builtins
32  * - Messages etc need international support
33  */
34
35 #define WIN32_LEAN_AND_MEAN
36
37 #include "wcmd.h"
38
39 void WCMD_execute (char *orig_command, char *parameter, char *substitution);
40
41 struct env_stack
42 {
43   struct env_stack *next;
44   WCHAR *strings;
45 };
46
47 struct env_stack *saved_environment;
48
49 extern HINSTANCE hinst;
50 extern char *inbuilt[];
51 extern int echo_mode, verify_mode;
52 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
53 extern BATCH_CONTEXT *context;
54 extern DWORD errorlevel;
55
56
57
58 /****************************************************************************
59  * WCMD_clear_screen
60  *
61  * Clear the terminal screen.
62  */
63
64 void WCMD_clear_screen (void) {
65
66   /* Emulate by filling the screen from the top left to bottom right with
67         spaces, then moving the cursor to the top left afterwards */
68   CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
69   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
70
71   if (GetConsoleScreenBufferInfo(hStdOut, &consoleInfo))
72   {
73       COORD topLeft;
74       DWORD screenSize;
75
76       screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
77
78       topLeft.X = 0;
79       topLeft.Y = 0;
80       FillConsoleOutputCharacter(hStdOut, ' ', screenSize, topLeft, &screenSize);
81       SetConsoleCursorPosition(hStdOut, topLeft);
82   }
83 }
84
85 /****************************************************************************
86  * WCMD_change_tty
87  *
88  * Change the default i/o device (ie redirect STDin/STDout).
89  */
90
91 void WCMD_change_tty (void) {
92
93   WCMD_output (nyi);
94
95 }
96
97 /****************************************************************************
98  * WCMD_copy
99  *
100  * Copy a file or wildcarded set.
101  * FIXME: No wildcard support
102  */
103
104 void WCMD_copy (void) {
105
106 DWORD count;
107 WIN32_FIND_DATA fd;
108 HANDLE hff;
109 BOOL force, status;
110 static const char overwrite[] = "Overwrite file (Y/N)?";
111 char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile;
112
113   if (param1[0] == 0x00) {
114     WCMD_output ("Argument missing\n");
115     return;
116   }
117
118   if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
119     WCMD_output ("Wildcards not yet supported\n");
120     return;
121   }
122
123   /* If no destination supplied, assume current directory */
124   if (param2[0] == 0x00) {
125       strcpy(param2, ".");
126   }
127
128   GetFullPathName (param2, sizeof(outpath), outpath, NULL);
129   if (outpath[strlen(outpath) - 1] == '\\')
130       outpath[strlen(outpath) - 1] = '\0';
131   hff = FindFirstFile (outpath, &fd);
132   if (hff != INVALID_HANDLE_VALUE) {
133     if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
134       GetFullPathName (param1, sizeof(inpath), inpath, &infile);
135       strcat (outpath, "\\");
136       strcat (outpath, infile);
137     }
138     FindClose (hff);
139   }
140
141   force = (strstr (quals, "/Y") != NULL);
142   if (!force) {
143     hff = FindFirstFile (outpath, &fd);
144     if (hff != INVALID_HANDLE_VALUE) {
145       FindClose (hff);
146       WCMD_output (overwrite);
147       ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
148       if (toupper(string[0]) == 'Y') force = TRUE;
149     }
150     else force = TRUE;
151   }
152   if (force) {
153     status = CopyFile (param1, outpath, FALSE);
154     if (!status) WCMD_print_error ();
155   }
156 }
157
158 /****************************************************************************
159  * WCMD_create_dir
160  *
161  * Create a directory.
162  *
163  * this works recursivly. so mkdir dir1\dir2\dir3 will create dir1 and dir2 if
164  * they do not already exist.
165  */
166
167 BOOL create_full_path(CHAR* path)
168 {
169     int len;
170     CHAR *new_path;
171     BOOL ret = TRUE;
172
173     new_path = HeapAlloc(GetProcessHeap(),0,strlen(path)+1);
174     strcpy(new_path,path);
175
176     while ((len = strlen(new_path)) && new_path[len - 1] == '\\')
177         new_path[len - 1] = 0;
178
179     while (!CreateDirectory(new_path,NULL))
180     {
181         CHAR *slash;
182         DWORD last_error = GetLastError();
183         if (last_error == ERROR_ALREADY_EXISTS)
184             break;
185
186         if (last_error != ERROR_PATH_NOT_FOUND)
187         {
188             ret = FALSE;
189             break;
190         }
191
192         if (!(slash = strrchr(new_path,'\\')) && ! (slash = strrchr(new_path,'/')))
193         {
194             ret = FALSE;
195             break;
196         }
197
198         len = slash - new_path;
199         new_path[len] = 0;
200         if (!create_full_path(new_path))
201         {
202             ret = FALSE;
203             break;
204         }
205         new_path[len] = '\\';
206     }
207     HeapFree(GetProcessHeap(),0,new_path);
208     return ret;
209 }
210
211 void WCMD_create_dir (void) {
212
213     if (param1[0] == 0x00) {
214         WCMD_output ("Argument missing\n");
215         return;
216     }
217     if (!create_full_path(param1)) WCMD_print_error ();
218 }
219
220 /****************************************************************************
221  * WCMD_delete
222  *
223  * Delete a file or wildcarded set.
224  *
225  */
226
227 void WCMD_delete (int recurse) {
228
229 WIN32_FIND_DATA fd;
230 HANDLE hff;
231 char fpath[MAX_PATH];
232 char *p;
233
234   if (param1[0] == 0x00) {
235     WCMD_output ("Argument missing\n");
236     return;
237   }
238   hff = FindFirstFile (param1, &fd);
239   if (hff == INVALID_HANDLE_VALUE) {
240     WCMD_output ("%s :File Not Found\n",param1);
241     return;
242   }
243   if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
244         && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
245     strcat (param1, "\\*");
246     FindClose(hff);
247     WCMD_delete (1);
248     return;
249   }
250   if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) {
251     strcpy (fpath, param1);
252     do {
253       p = strrchr (fpath, '\\');
254       if (p != NULL) {
255         *++p = '\0';
256         strcat (fpath, fd.cFileName);
257       }
258       else strcpy (fpath, fd.cFileName);
259       if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
260         if (!DeleteFile (fpath)) WCMD_print_error ();
261       }
262     } while (FindNextFile(hff, &fd) != 0);
263     FindClose (hff);
264   }
265   else {
266     if (!DeleteFile (param1)) WCMD_print_error ();
267     FindClose (hff);
268   }
269 }
270
271 /****************************************************************************
272  * WCMD_echo
273  *
274  * Echo input to the screen (or not). We don't try to emulate the bugs
275  * in DOS (try typing "ECHO ON AGAIN" for an example).
276  */
277
278 void WCMD_echo (const char *command) {
279
280 static const char eon[] = "Echo is ON\n", eoff[] = "Echo is OFF\n";
281 int count;
282
283   if ((command[0] == '.') && (command[1] == 0)) {
284     WCMD_output (newline);
285     return;
286   }
287   if (command[0]==' ')
288     command++;
289   count = strlen(command);
290   if (count == 0) {
291     if (echo_mode) WCMD_output (eon);
292     else WCMD_output (eoff);
293     return;
294   }
295   if (lstrcmpi(command, "ON") == 0) {
296     echo_mode = 1;
297     return;
298   }
299   if (lstrcmpi(command, "OFF") == 0) {
300     echo_mode = 0;
301     return;
302   }
303   WCMD_output_asis (command);
304   WCMD_output (newline);
305
306 }
307
308 /**************************************************************************
309  * WCMD_for
310  *
311  * Batch file loop processing.
312  * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
313  * will probably work here, but the reverse is not necessarily the case...
314  */
315
316 void WCMD_for (char *p) {
317
318 WIN32_FIND_DATA fd;
319 HANDLE hff;
320 char *cmd, *item;
321 char set[MAX_PATH], param[MAX_PATH];
322 int i;
323
324   if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
325         || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
326         || (param1[0] != '%')) {
327     WCMD_output ("Syntax error\n");
328     return;
329   }
330   lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
331   WCMD_parameter (p, 4, &cmd);
332   lstrcpy (param, param1);
333
334 /*
335  *      If the parameter within the set has a wildcard then search for matching files
336  *      otherwise do a literal substitution.
337  */
338
339   i = 0;
340   while (*(item = WCMD_parameter (set, i, NULL))) {
341     if (strpbrk (item, "*?")) {
342       hff = FindFirstFile (item, &fd);
343       if (hff == INVALID_HANDLE_VALUE) {
344         return;
345       }
346       do {
347         WCMD_execute (cmd, param, fd.cFileName);
348       } while (FindNextFile(hff, &fd) != 0);
349       FindClose (hff);
350 }
351     else {
352       WCMD_execute (cmd, param, item);
353     }
354     i++;
355   }
356 }
357
358 /*****************************************************************************
359  * WCMD_Execute
360  *
361  *      Execute a command after substituting variable text for the supplied parameter
362  */
363
364 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
365
366 char *new_cmd, *p, *s, *dup;
367 int size;
368
369   size = lstrlen (orig_cmd);
370   new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
371   dup = s = strdup (orig_cmd);
372
373   while ((p = strstr (s, param))) {
374     *p = '\0';
375     size += lstrlen (subst);
376     new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
377     strcat (new_cmd, s);
378     strcat (new_cmd, subst);
379     s = p + lstrlen (param);
380   }
381   strcat (new_cmd, s);
382   WCMD_process_command (new_cmd);
383   free (dup);
384   LocalFree ((HANDLE)new_cmd);
385 }
386
387
388 /**************************************************************************
389  * WCMD_give_help
390  *
391  *      Simple on-line help. Help text is stored in the resource file.
392  */
393
394 void WCMD_give_help (char *command) {
395
396 int i;
397 char buffer[2048];
398
399   command = WCMD_strtrim_leading_spaces(command);
400   if (lstrlen(command) == 0) {
401     LoadString (hinst, 1000, buffer, sizeof(buffer));
402     WCMD_output_asis (buffer);
403   }
404   else {
405     for (i=0; i<=WCMD_EXIT; i++) {
406       if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
407           param1, -1, inbuilt[i], -1) == 2) {
408         LoadString (hinst, i, buffer, sizeof(buffer));
409         WCMD_output_asis (buffer);
410         return;
411       }
412     }
413     WCMD_output ("No help available for %s\n", param1);
414   }
415   return;
416 }
417
418 /****************************************************************************
419  * WCMD_go_to
420  *
421  * Batch file jump instruction. Not the most efficient algorithm ;-)
422  * Prints error message if the specified label cannot be found - the file pointer is
423  * then at EOF, effectively stopping the batch file.
424  * FIXME: DOS is supposed to allow labels with spaces - we don't.
425  */
426
427 void WCMD_goto (void) {
428
429 char string[MAX_PATH];
430
431   if (param1[0] == 0x00) {
432     WCMD_output ("Argument missing\n");
433     return;
434   }
435   if (context != NULL) {
436     SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
437     while (WCMD_fgets (string, sizeof(string), context -> h)) {
438       if ((string[0] == ':') && (lstrcmpi (&string[1], param1) == 0)) return;
439     }
440     WCMD_output ("Target to GOTO not found\n");
441   }
442   return;
443 }
444
445
446 /****************************************************************************
447  * WCMD_if
448  *
449  * Batch file conditional.
450  * FIXME: Much more syntax checking needed!
451  */
452
453 void WCMD_if (char *p) {
454
455 int negate = 0, test = 0;
456 char condition[MAX_PATH], *command, *s;
457
458   if (!lstrcmpi (param1, "not")) {
459     negate = 1;
460     lstrcpy (condition, param2);
461 }
462   else {
463     lstrcpy (condition, param1);
464   }
465   if (!lstrcmpi (condition, "errorlevel")) {
466     if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
467     WCMD_parameter (p, 2+negate, &command);
468   }
469   else if (!lstrcmpi (condition, "exist")) {
470     if (GetFileAttributesA(WCMD_parameter (p, 1+negate, NULL)) != INVALID_FILE_ATTRIBUTES) {
471         test = 1;
472     }
473     WCMD_parameter (p, 2+negate, &command);
474   }
475   else if ((s = strstr (p, "=="))) {
476     s += 2;
477     if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
478     WCMD_parameter (s, 1, &command);
479   }
480   else {
481     WCMD_output ("Syntax error\n");
482     return;
483   }
484   if (test != negate) {
485     command = strdup (command);
486     WCMD_process_command (command);
487     free (command);
488   }
489 }
490
491 /****************************************************************************
492  * WCMD_move
493  *
494  * Move a file, directory tree or wildcarded set of files.
495  * FIXME: Needs input and output files to be fully specified.
496  */
497
498 void WCMD_move (void) {
499
500 int status;
501 char outpath[MAX_PATH], inpath[MAX_PATH], *infile;
502 WIN32_FIND_DATA fd;
503 HANDLE hff;
504
505   if (param1[0] == 0x00) {
506     WCMD_output ("Argument missing\n");
507     return;
508   }
509
510   if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
511     WCMD_output ("Wildcards not yet supported\n");
512     return;
513   }
514
515   /* If no destination supplied, assume current directory */
516   if (param2[0] == 0x00) {
517       strcpy(param2, ".");
518   }
519
520   /* If 2nd parm is directory, then use original filename */
521   GetFullPathName (param2, sizeof(outpath), outpath, NULL);
522   hff = FindFirstFile (outpath, &fd);
523   if (hff != INVALID_HANDLE_VALUE) {
524     if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
525       GetFullPathName (param1, sizeof(inpath), inpath, &infile);
526       strcat (outpath, "\\");
527       strcat (outpath, infile);
528     }
529     FindClose (hff);
530   }
531
532   status = MoveFile (param1, outpath);
533   if (!status) WCMD_print_error ();
534 }
535
536 /****************************************************************************
537  * WCMD_pause
538  *
539  * Wait for keyboard input.
540  */
541
542 void WCMD_pause (void) {
543
544 DWORD count;
545 char string[32];
546
547   WCMD_output (anykey);
548   ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
549 }
550
551 /****************************************************************************
552  * WCMD_remove_dir
553  *
554  * Delete a directory.
555  */
556
557 void WCMD_remove_dir (void) {
558
559   if (param1[0] == 0x00) {
560     WCMD_output ("Argument missing\n");
561     return;
562   }
563   if (!RemoveDirectory (param1)) WCMD_print_error ();
564 }
565
566 /****************************************************************************
567  * WCMD_rename
568  *
569  * Rename a file.
570  * FIXME: Needs input and output files to be fully specified.
571  */
572
573 void WCMD_rename (void) {
574
575 int status;
576
577   if (param1[0] == 0x00 || param2[0] == 0x00) {
578     WCMD_output ("Argument missing\n");
579     return;
580   }
581   if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
582     WCMD_output ("Wildcards not yet supported\n");
583     return;
584   }
585   status = MoveFile (param1, param2);
586   if (!status) WCMD_print_error ();
587 }
588
589 /*****************************************************************************
590  * WCMD_dupenv
591  *
592  * Make a copy of the environment.
593  */
594 static WCHAR *WCMD_dupenv( const WCHAR *env )
595 {
596   WCHAR *env_copy;
597   int len;
598
599   if( !env )
600     return NULL;
601
602   len = 0;
603   while ( env[len] )
604     len += (lstrlenW(&env[len]) + 1);
605
606   env_copy = LocalAlloc (LMEM_FIXED, (len+1) * sizeof (WCHAR) );
607   if (!env_copy)
608   {
609     WCMD_output ("out of memory\n");
610     return env_copy;
611   }
612   memcpy (env_copy, env, len*sizeof (WCHAR));
613   env_copy[len] = 0;
614
615   return env_copy;
616 }
617
618 /*****************************************************************************
619  * WCMD_setlocal
620  *
621  *  setlocal pushes the environment onto a stack
622  *  Save the environment as unicode so we don't screw anything up.
623  */
624 void WCMD_setlocal (const char *s) {
625   WCHAR *env;
626   struct env_stack *env_copy;
627
628   /* DISABLEEXTENSIONS ignored */
629
630   env_copy = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
631   if( !env_copy )
632   {
633     WCMD_output ("out of memory\n");
634     return;
635   }
636
637   env = GetEnvironmentStringsW ();
638
639   env_copy->strings = WCMD_dupenv (env);
640   if (env_copy->strings)
641   {
642     env_copy->next = saved_environment;
643     saved_environment = env_copy;
644   }
645   else
646     LocalFree (env_copy);
647
648   FreeEnvironmentStringsW (env);
649 }
650
651 /*****************************************************************************
652  * WCMD_strchrW
653  */
654 static inline WCHAR *WCMD_strchrW(WCHAR *str, WCHAR ch)
655 {
656    while(*str)
657    {
658      if(*str == ch)
659        return str;
660      str++;
661    }
662    return NULL;
663 }
664
665 /*****************************************************************************
666  * WCMD_endlocal
667  *
668  *  endlocal pops the environment off a stack
669  */
670 void WCMD_endlocal (void) {
671   WCHAR *env, *old, *p;
672   struct env_stack *temp;
673   int len, n;
674
675   if (!saved_environment)
676     return;
677
678   /* pop the old environment from the stack */
679   temp = saved_environment;
680   saved_environment = temp->next;
681
682   /* delete the current environment, totally */
683   env = GetEnvironmentStringsW ();
684   old = WCMD_dupenv (GetEnvironmentStringsW ());
685   len = 0;
686   while (old[len]) {
687     n = lstrlenW(&old[len]) + 1;
688     p = WCMD_strchrW(&old[len], '=');
689     if (p)
690     {
691       *p++ = 0;
692       SetEnvironmentVariableW (&old[len], NULL);
693     }
694     len += n;
695   }
696   LocalFree (old);
697   FreeEnvironmentStringsW (env);
698   
699   /* restore old environment */
700   env = temp->strings;
701   len = 0;
702   while (env[len]) {
703     n = lstrlenW(&env[len]) + 1;
704     p = WCMD_strchrW(&env[len], '=');
705     if (p)
706     {
707       *p++ = 0;
708       SetEnvironmentVariableW (&env[len], p);
709     }
710     len += n;
711   }
712   LocalFree (env);
713   LocalFree (temp);
714 }
715
716 /*****************************************************************************
717  * WCMD_setshow_attrib
718  *
719  * Display and optionally sets DOS attributes on a file or directory
720  *
721  * FIXME: Wine currently uses the Unix stat() function to get file attributes.
722  * As a result only the Readonly flag is correctly reported, the Archive bit
723  * is always set and the rest are not implemented. We do the Right Thing anyway.
724  *
725  * FIXME: No SET functionality.
726  *
727  */
728
729 void WCMD_setshow_attrib (void) {
730
731 DWORD count;
732 HANDLE hff;
733 WIN32_FIND_DATA fd;
734 char flags[9] = {"        "};
735
736   if (param1[0] == '-') {
737     WCMD_output (nyi);
738     return;
739   }
740
741   if (lstrlen(param1) == 0) {
742     GetCurrentDirectory (sizeof(param1), param1);
743     strcat (param1, "\\*");
744   }
745
746   hff = FindFirstFile (param1, &fd);
747   if (hff == INVALID_HANDLE_VALUE) {
748     WCMD_output ("%s: File Not Found\n",param1);
749   }
750   else {
751     do {
752       if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
753         if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
754           flags[0] = 'H';
755         }
756         if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
757           flags[1] = 'S';
758         }
759         if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
760           flags[2] = 'A';
761         }
762         if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
763           flags[3] = 'R';
764         }
765         if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
766           flags[4] = 'T';
767         }
768         if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
769           flags[5] = 'C';
770         }
771         WCMD_output ("%s   %s\n", flags, fd.cFileName);
772         for (count=0; count < 8; count++) flags[count] = ' ';
773       }
774     } while (FindNextFile(hff, &fd) != 0);
775   }
776   FindClose (hff);
777 }
778
779 /*****************************************************************************
780  * WCMD_setshow_default
781  *
782  *      Set/Show the current default directory
783  */
784
785 void WCMD_setshow_default (void) {
786
787 BOOL status;
788 char string[1024];
789
790   if (strlen(param1) == 0) {
791     GetCurrentDirectory (sizeof(string), string);
792     strcat (string, "\n");
793     WCMD_output (string);
794   }
795   else {
796     status = SetCurrentDirectory (param1);
797     if (!status) {
798       WCMD_print_error ();
799       return;
800     }
801    }
802   return;
803 }
804
805 /****************************************************************************
806  * WCMD_setshow_date
807  *
808  * Set/Show the system date
809  * FIXME: Can't change date yet
810  */
811
812 void WCMD_setshow_date (void) {
813
814 char curdate[64], buffer[64];
815 DWORD count;
816
817   if (lstrlen(param1) == 0) {
818     if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
819                 curdate, sizeof(curdate))) {
820       WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
821       ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
822       if (count > 2) {
823         WCMD_output (nyi);
824       }
825     }
826     else WCMD_print_error ();
827   }
828   else {
829     WCMD_output (nyi);
830   }
831 }
832
833 /****************************************************************************
834  * WCMD_compare
835  */
836 static int WCMD_compare( const void *a, const void *b )
837 {
838     int r;
839     const char * const *str_a = a, * const *str_b = b;
840     r = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
841           *str_a, -1, *str_b, -1 );
842     if( r == CSTR_LESS_THAN ) return -1;
843     if( r == CSTR_GREATER_THAN ) return 1;
844     return 0;
845 }
846
847 /****************************************************************************
848  * WCMD_setshow_sortenv
849  *
850  * sort variables into order for display
851  */
852 static void WCMD_setshow_sortenv(const char *s)
853 {
854   UINT count=0, len=0, i;
855   const char **str;
856
857   /* count the number of strings, and the total length */
858   while ( s[len] ) {
859     len += (lstrlen(&s[len]) + 1);
860     count++;
861   }
862
863   /* add the strings to an array */
864   str = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, count * sizeof (char*) );
865   if( !str )
866     return;
867   str[0] = s;
868   for( i=1; i<count; i++ )
869     str[i] = str[i-1] + lstrlen(str[i-1]) + 1;
870
871   /* sort the array */
872   qsort( str, count, sizeof (char*), WCMD_compare );
873
874   /* print it */
875   for( i=0; i<count; i++ ) {
876       WCMD_output_asis(str[i]);
877       WCMD_output_asis("\n");
878   }
879
880   LocalFree( str );
881 }
882
883 /****************************************************************************
884  * WCMD_setshow_env
885  *
886  * Set/Show the environment variables
887  */
888
889 void WCMD_setshow_env (char *s) {
890
891 LPVOID env;
892 char *p;
893 int status;
894 char buffer[1048];
895
896   if (strlen(param1) == 0) {
897     env = GetEnvironmentStrings ();
898     WCMD_setshow_sortenv( env );
899   }
900   else {
901     p = strchr (s, '=');
902     if (p == NULL) {
903
904       /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
905          environment variable C, whereas on NT it shows ALL variables
906          starting with C.
907        */
908       status = GetEnvironmentVariable(s, buffer, sizeof(buffer));
909       if (status) {
910         WCMD_output_asis( s);
911         WCMD_output_asis( "=");
912         WCMD_output_asis( buffer);
913         WCMD_output_asis( "\n");
914       } else {
915         WCMD_output ("Environment variable %s not defined\n", s);
916       }
917       return;
918     }
919     *p++ = '\0';
920
921     if (strlen(p) == 0) p = NULL;
922     status = SetEnvironmentVariable (s, p);
923     if ((!status) & (GetLastError() != ERROR_ENVVAR_NOT_FOUND)) WCMD_print_error();
924   }
925   /* WCMD_output (newline);   @JED*/
926 }
927
928 /****************************************************************************
929  * WCMD_setshow_path
930  *
931  * Set/Show the path environment variable
932  */
933
934 void WCMD_setshow_path (char *command) {
935
936 char string[1024];
937 DWORD status;
938
939   if (strlen(param1) == 0) {
940     status = GetEnvironmentVariable ("PATH", string, sizeof(string));
941     if (status != 0) {
942       WCMD_output_asis ( "PATH=");
943       WCMD_output_asis ( string);
944       WCMD_output_asis ( "\n");
945     }
946     else {
947       WCMD_output ("PATH not found\n");
948     }
949   }
950   else {
951     status = SetEnvironmentVariable ("PATH", command);
952     if (!status) WCMD_print_error();
953   }
954 }
955
956 /****************************************************************************
957  * WCMD_setshow_prompt
958  *
959  * Set or show the command prompt.
960  */
961
962 void WCMD_setshow_prompt (void) {
963
964 char *s;
965
966   if (strlen(param1) == 0) {
967     SetEnvironmentVariable ("PROMPT", NULL);
968   }
969   else {
970     s = param1;
971     while ((*s == '=') || (*s == ' ')) s++;
972     if (strlen(s) == 0) {
973       SetEnvironmentVariable ("PROMPT", NULL);
974     }
975     else SetEnvironmentVariable ("PROMPT", s);
976   }
977 }
978
979 /****************************************************************************
980  * WCMD_setshow_time
981  *
982  * Set/Show the system time
983  * FIXME: Can't change time yet
984  */
985
986 void WCMD_setshow_time (void) {
987
988 char curtime[64], buffer[64];
989 DWORD count;
990 SYSTEMTIME st;
991
992   if (strlen(param1) == 0) {
993     GetLocalTime(&st);
994     if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
995                 curtime, sizeof(curtime))) {
996       WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
997       ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
998       if (count > 2) {
999         WCMD_output (nyi);
1000       }
1001     }
1002     else WCMD_print_error ();
1003   }
1004   else {
1005     WCMD_output (nyi);
1006   }
1007 }
1008
1009 /****************************************************************************
1010  * WCMD_shift
1011  *
1012  * Shift batch parameters.
1013  */
1014
1015 void WCMD_shift (void) {
1016
1017   if (context != NULL) context -> shift_count++;
1018
1019 }
1020
1021 /****************************************************************************
1022  * WCMD_title
1023  *
1024  * Set the console title
1025  */
1026 void WCMD_title (char *command) {
1027   SetConsoleTitle(command);
1028 }
1029
1030 /****************************************************************************
1031  * WCMD_type
1032  *
1033  * Copy a file to standard output.
1034  */
1035
1036 void WCMD_type (void) {
1037
1038 HANDLE h;
1039 char buffer[512];
1040 DWORD count;
1041
1042   if (param1[0] == 0x00) {
1043     WCMD_output ("Argument missing\n");
1044     return;
1045   }
1046   h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1047                 FILE_ATTRIBUTE_NORMAL, NULL);
1048   if (h == INVALID_HANDLE_VALUE) {
1049     WCMD_print_error ();
1050     return;
1051   }
1052   while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
1053     if (count == 0) break;      /* ReadFile reports success on EOF! */
1054     buffer[count] = 0;
1055     WCMD_output_asis (buffer);
1056   }
1057   CloseHandle (h);
1058 }
1059
1060 /****************************************************************************
1061  * WCMD_verify
1062  *
1063  * Display verify flag.
1064  * FIXME: We don't actually do anything with the verify flag other than toggle
1065  * it...
1066  */
1067
1068 void WCMD_verify (char *command) {
1069
1070 static const char von[] = "Verify is ON\n", voff[] = "Verify is OFF\n";
1071 int count;
1072
1073   count = strlen(command);
1074   if (count == 0) {
1075     if (verify_mode) WCMD_output (von);
1076     else WCMD_output (voff);
1077     return;
1078   }
1079   if (lstrcmpi(command, "ON") == 0) {
1080     verify_mode = 1;
1081     return;
1082   }
1083   else if (lstrcmpi(command, "OFF") == 0) {
1084     verify_mode = 0;
1085     return;
1086   }
1087   else WCMD_output ("Verify must be ON or OFF\n");
1088 }
1089
1090 /****************************************************************************
1091  * WCMD_version
1092  *
1093  * Display version info.
1094  */
1095
1096 void WCMD_version (void) {
1097
1098   WCMD_output (version_string);
1099
1100 }
1101
1102 /****************************************************************************
1103  * WCMD_volume
1104  *
1105  * Display volume info and/or set volume label. Returns 0 if error.
1106  */
1107
1108 int WCMD_volume (int mode, char *path) {
1109
1110 DWORD count, serial;
1111 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
1112 BOOL status;
1113
1114   if (lstrlen(path) == 0) {
1115     status = GetCurrentDirectory (sizeof(curdir), curdir);
1116     if (!status) {
1117       WCMD_print_error ();
1118       return 0;
1119     }
1120     status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
1121         NULL, NULL, 0);
1122   }
1123   else {
1124     if ((path[1] != ':') || (lstrlen(path) != 2)) {
1125       WCMD_output_asis("Syntax Error\n\n");
1126       return 0;
1127     }
1128     wsprintf (curdir, "%s\\", path);
1129     status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
1130         NULL, NULL, 0);
1131   }
1132   if (!status) {
1133     WCMD_print_error ();
1134     return 0;
1135   }
1136   WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
1137         curdir[0], label, HIWORD(serial), LOWORD(serial));
1138   if (mode) {
1139     WCMD_output ("Volume label (11 characters, ENTER for none)?");
1140     ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
1141     if (count > 1) {
1142       string[count-1] = '\0';           /* ReadFile output is not null-terminated! */
1143       if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
1144     }
1145     if (lstrlen(path) != 0) {
1146       if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
1147     }
1148     else {
1149       if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();
1150     }
1151   }
1152   return 1;
1153 }