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