ok() does not support '%S'. Store the Ansi version, convert to Unicode
[wine] / programs / wcmd / builtins.c
1 /*
2  * WCMD - 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 #include "wcmd.h"
36
37 void WCMD_execute (char *orig_command, char *parameter, char *substitution);
38
39 extern HINSTANCE hinst;
40 extern char *inbuilt[];
41 extern char nyi[];
42 extern char newline[];
43 extern char version_string[];
44 extern char anykey[];
45 extern int echo_mode, verify_mode;
46 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
47 extern BATCH_CONTEXT *context;
48 extern DWORD errorlevel;
49
50
51
52 /****************************************************************************
53  * WCMD_clear_screen
54  *
55  * Clear the terminal screen.
56  */
57
58 void WCMD_clear_screen () {
59
60   /* Emulate by filling the screen from the top left to bottom right with
61         spaces, then moving the cursor to the top left afterwards */
62   COORD topLeft;
63   long screenSize;
64   CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
65   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
66
67   GetConsoleScreenBufferInfo(hStdOut, &consoleInfo);
68   screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
69
70   topLeft.X = 0;
71   topLeft.Y = 0;
72   FillConsoleOutputCharacter(hStdOut, ' ', screenSize, topLeft, &screenSize);
73   SetConsoleCursorPosition(hStdOut, topLeft);
74 }
75
76 /****************************************************************************
77  * WCMD_change_tty
78  *
79  * Change the default i/o device (ie redirect STDin/STDout).
80  */
81
82 void WCMD_change_tty () {
83
84   WCMD_output (nyi);
85
86 }
87
88 /****************************************************************************
89  * WCMD_copy
90  *
91  * Copy a file or wildcarded set.
92  * FIXME: No wildcard support
93  */
94
95 void WCMD_copy () {
96
97 DWORD count;
98 WIN32_FIND_DATA fd;
99 HANDLE hff;
100 BOOL force, status;
101 static char *overwrite = "Overwrite file (Y/N)?";
102 char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile;
103
104   if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
105     WCMD_output ("Wildcards not yet supported\n");
106     return;
107   }
108
109   /* If no destination supplied, assume current directory */
110   if (param2[0] == 0x00) {
111       strcpy(param2, ".");
112   }
113
114   GetFullPathName (param2, sizeof(outpath), outpath, NULL);
115   hff = FindFirstFile (outpath, &fd);
116   if (hff != INVALID_HANDLE_VALUE) {
117     if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
118       GetFullPathName (param1, sizeof(inpath), inpath, &infile);
119       strcat (outpath, "\\");
120       strcat (outpath, infile);
121     }
122     FindClose (hff);
123   }
124
125   force = (strstr (quals, "/Y") != NULL);
126   if (!force) {
127     hff = FindFirstFile (outpath, &fd);
128     if (hff != INVALID_HANDLE_VALUE) {
129       FindClose (hff);
130       WCMD_output (overwrite);
131       ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
132       if (toupper(string[0]) == 'Y') force = TRUE;
133     }
134     else force = TRUE;
135   }
136   if (force) {
137     status = CopyFile (param1, outpath, FALSE);
138     if (!status) WCMD_print_error ();
139   }
140 }
141
142 /****************************************************************************
143  * WCMD_create_dir
144  *
145  * Create a directory.
146  */
147
148 void WCMD_create_dir () {
149
150   if (!CreateDirectory (param1, NULL)) WCMD_print_error ();
151 }
152
153 /****************************************************************************
154  * WCMD_delete
155  *
156  * Delete a file or wildcarded set.
157  *
158  */
159
160 void WCMD_delete (int recurse) {
161
162 WIN32_FIND_DATA fd;
163 HANDLE hff;
164 char fpath[MAX_PATH];
165 char *p;
166
167   hff = FindFirstFile (param1, &fd);
168   if (hff == INVALID_HANDLE_VALUE) {
169     WCMD_output ("%s :File Not Found\n",param1);
170     return;
171   }
172   if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
173         && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
174     strcat (param1, "\\*");
175     FindClose(hff);
176     WCMD_delete (1);
177     return;
178   }
179   if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) {
180     strcpy (fpath, param1);
181     do {
182       p = strrchr (fpath, '\\');
183       if (p != NULL) {
184         *++p = '\0';
185         strcat (fpath, fd.cFileName);
186       }
187       else strcpy (fpath, fd.cFileName);
188       if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
189         if (!DeleteFile (fpath)) WCMD_print_error ();
190       }
191     } while (FindNextFile(hff, &fd) != 0);
192     FindClose (hff);
193   }
194   else {
195     if (!DeleteFile (param1)) WCMD_print_error ();
196     FindClose (hff);
197   }
198 }
199
200 /****************************************************************************
201  * WCMD_echo
202  *
203  * Echo input to the screen (or not). We don't try to emulate the bugs
204  * in DOS (try typing "ECHO ON AGAIN" for an example).
205  */
206
207 void WCMD_echo (char *command) {
208
209 static char *eon = "Echo is ON\n", *eoff = "Echo is OFF\n";
210 int count;
211
212   count = strlen(command);
213   if (count == 0) {
214     if (echo_mode) WCMD_output (eon);
215     else WCMD_output (eoff);
216     return;
217   }
218   if ((count == 1) && (command[0] == '.')) {
219     WCMD_output (newline);
220     return;
221   }
222   if (lstrcmpi(command, "ON") == 0) {
223     echo_mode = 1;
224     return;
225   }
226   if (lstrcmpi(command, "OFF") == 0) {
227     echo_mode = 0;
228     return;
229   }
230   WCMD_output_asis (command);
231   WCMD_output (newline);
232
233 }
234
235 /**************************************************************************
236  * WCMD_for
237  *
238  * Batch file loop processing.
239  * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
240  * will probably work here, but the reverse is not necessarily the case...
241  */
242
243 void WCMD_for (char *p) {
244
245 WIN32_FIND_DATA fd;
246 HANDLE hff;
247 char *cmd, *item;
248 char set[MAX_PATH], param[MAX_PATH];
249 int i;
250
251   if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
252         || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
253         || (param1[0] != '%')) {
254     WCMD_output ("Syntax error\n");
255     return;
256   }
257   lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
258   WCMD_parameter (p, 4, &cmd);
259   lstrcpy (param, param1);
260
261 /*
262  *      If the parameter within the set has a wildcard then search for matching files
263  *      otherwise do a literal substitution.
264  */
265
266   i = 0;
267   while (*(item = WCMD_parameter (set, i, NULL))) {
268     if (strpbrk (item, "*?")) {
269       hff = FindFirstFile (item, &fd);
270       if (hff == INVALID_HANDLE_VALUE) {
271         return;
272       }
273       do {
274         WCMD_execute (cmd, param, fd.cFileName);
275       } while (FindNextFile(hff, &fd) != 0);
276       FindClose (hff);
277 }
278     else {
279       WCMD_execute (cmd, param, item);
280     }
281     i++;
282   }
283 }
284
285 /*****************************************************************************
286  * WCMD_Execute
287  *
288  *      Execute a command after substituting variable text for the supplied parameter
289  */
290
291 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
292
293 char *new_cmd, *p, *s, *dup;
294 int size;
295
296   size = lstrlen (orig_cmd);
297   new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
298   dup = s = strdup (orig_cmd);
299
300   while ((p = strstr (s, param))) {
301     *p = '\0';
302     size += lstrlen (subst);
303     new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
304     strcat (new_cmd, s);
305     strcat (new_cmd, subst);
306     s = p + lstrlen (param);
307   }
308   strcat (new_cmd, s);
309   WCMD_process_command (new_cmd);
310   free (dup);
311   LocalFree ((HANDLE)new_cmd);
312 }
313
314
315 /**************************************************************************
316  * WCMD_give_help
317  *
318  *      Simple on-line help. Help text is stored in the resource file.
319  */
320
321 void WCMD_give_help (char *command) {
322
323 int i;
324 char buffer[2048];
325
326   command = WCMD_strtrim_leading_spaces(command);
327   if (lstrlen(command) == 0) {
328     LoadString (0, 1000, buffer, sizeof(buffer));
329     WCMD_output (buffer);
330   }
331   else {
332     for (i=0; i<=WCMD_EXIT; i++) {
333       if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
334           param1, -1, inbuilt[i], -1) == 2) {
335         LoadString (hinst, i, buffer, sizeof(buffer));
336         WCMD_output (buffer);
337         return;
338       }
339     }
340     WCMD_output ("No help available for %s\n", param1);
341   }
342   return;
343 }
344
345 /****************************************************************************
346  * WCMD_go_to
347  *
348  * Batch file jump instruction. Not the most efficient algorithm ;-)
349  * Prints error message if the specified label cannot be found - the file pointer is
350  * then at EOF, effectively stopping the batch file.
351  * FIXME: DOS is supposed to allow labels with spaces - we don't.
352  */
353
354 void WCMD_goto () {
355
356 char string[MAX_PATH];
357
358   if (context != NULL) {
359     SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
360     while (WCMD_fgets (string, sizeof(string), context -> h)) {
361       if ((string[0] == ':') && (strcmp (&string[1], param1) == 0)) return;
362     }
363     WCMD_output ("Target to GOTO not found\n");
364   }
365   return;
366 }
367
368
369 /****************************************************************************
370  * WCMD_if
371  *
372  * Batch file conditional.
373  * FIXME: Much more syntax checking needed!
374  */
375
376 void WCMD_if (char *p) {
377
378 HANDLE h;
379 int negate = 0, test = 0;
380 char condition[MAX_PATH], *command, *s;
381
382   if (!lstrcmpi (param1, "not")) {
383     negate = 1;
384     lstrcpy (condition, param2);
385 }
386   else {
387     lstrcpy (condition, param1);
388   }
389   if (!lstrcmpi (condition, "errorlevel")) {
390     if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
391     return;
392     WCMD_parameter (p, 2+negate, &command);
393   }
394   else if (!lstrcmpi (condition, "exist")) {
395     if ((h = CreateFile (WCMD_parameter (p, 1+negate, NULL), GENERIC_READ,
396                          FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
397         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) {
398       CloseHandle (h);
399       test = 1;
400     }
401     WCMD_parameter (p, 2+negate, &command);
402   }
403   else if ((s = strstr (p, "=="))) {
404     s += 2;
405     if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
406     WCMD_parameter (s, 1, &command);
407   }
408   else {
409     WCMD_output ("Syntax error\n");
410     return;
411   }
412   if (test != negate) {
413     command = strdup (command);
414     WCMD_process_command (command);
415     free (command);
416   }
417 }
418
419 /****************************************************************************
420  * WCMD_move
421  *
422  * Move a file, directory tree or wildcarded set of files.
423  * FIXME: Needs input and output files to be fully specified.
424  */
425
426 void WCMD_move () {
427
428 int status;
429 char outpath[MAX_PATH], inpath[MAX_PATH], *infile;
430 WIN32_FIND_DATA fd;
431 HANDLE hff;
432
433   if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
434     WCMD_output ("Wildcards not yet supported\n");
435     return;
436   }
437
438   /* If no destination supplied, assume current directory */
439   if (param2[0] == 0x00) {
440       strcpy(param2, ".");
441   }
442
443   /* If 2nd parm is directory, then use original filename */
444   GetFullPathName (param2, sizeof(outpath), outpath, NULL);
445   hff = FindFirstFile (outpath, &fd);
446   if (hff != INVALID_HANDLE_VALUE) {
447     if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
448       GetFullPathName (param1, sizeof(inpath), inpath, &infile);
449       strcat (outpath, "\\");
450       strcat (outpath, infile);
451     }
452     FindClose (hff);
453   }
454
455   status = MoveFile (param1, outpath);
456   if (!status) WCMD_print_error ();
457 }
458
459 /****************************************************************************
460  * WCMD_pause
461  *
462  * Wait for keyboard input.
463  */
464
465 void WCMD_pause () {
466
467 DWORD count;
468 char string[32];
469
470   WCMD_output (anykey);
471   ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
472 }
473
474 /****************************************************************************
475  * WCMD_remove_dir
476  *
477  * Delete a directory.
478  */
479
480 void WCMD_remove_dir () {
481
482   if (!RemoveDirectory (param1)) WCMD_print_error ();
483 }
484
485 /****************************************************************************
486  * WCMD_rename
487  *
488  * Rename a file.
489  * FIXME: Needs input and output files to be fully specified.
490  */
491
492 void WCMD_rename () {
493
494 int status;
495
496   if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
497     WCMD_output ("Wildcards not yet supported\n");
498     return;
499   }
500   status = MoveFile (param1, param2);
501   if (!status) WCMD_print_error ();
502 }
503
504 /*****************************************************************************
505  * WCMD_setshow_attrib
506  *
507  * Display and optionally sets DOS attributes on a file or directory
508  *
509  * FIXME: Wine currently uses the Unix stat() function to get file attributes.
510  * As a result only the Readonly flag is correctly reported, the Archive bit
511  * is always set and the rest are not implemented. We do the Right Thing anyway.
512  *
513  * FIXME: No SET functionality.
514  *
515  */
516
517 void WCMD_setshow_attrib () {
518
519 DWORD count;
520 HANDLE hff;
521 WIN32_FIND_DATA fd;
522 char flags[9] = {"        "};
523
524   if (param1[0] == '-') {
525     WCMD_output (nyi);
526     return;
527   }
528
529   if (lstrlen(param1) == 0) {
530     GetCurrentDirectory (sizeof(param1), param1);
531     strcat (param1, "\\*");
532   }
533
534   hff = FindFirstFile (param1, &fd);
535   if (hff == INVALID_HANDLE_VALUE) {
536     WCMD_output ("%s: File Not Found\n",param1);
537   }
538   else {
539     do {
540       if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
541         if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
542           flags[0] = 'H';
543         }
544         if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
545           flags[1] = 'S';
546         }
547         if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
548           flags[2] = 'A';
549         }
550         if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
551           flags[3] = 'R';
552         }
553         if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
554           flags[4] = 'T';
555         }
556         if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
557           flags[5] = 'C';
558         }
559         WCMD_output ("%s   %s\n", flags, fd.cFileName);
560         for (count=0; count < 8; count++) flags[count] = ' ';
561       }
562     } while (FindNextFile(hff, &fd) != 0);
563   }
564   FindClose (hff);
565 }
566
567 /*****************************************************************************
568  * WCMD_setshow_default
569  *
570  *      Set/Show the current default directory
571  */
572
573 void WCMD_setshow_default () {
574
575 BOOL status;
576 char string[1024];
577
578   if (strlen(param1) == 0) {
579     GetCurrentDirectory (sizeof(string), string);
580     strcat (string, "\n");
581     WCMD_output (string);
582   }
583   else {
584     status = SetCurrentDirectory (param1);
585     if (!status) {
586       WCMD_print_error ();
587       return;
588     }
589    }
590   return;
591 }
592
593 /****************************************************************************
594  * WCMD_setshow_date
595  *
596  * Set/Show the system date
597  * FIXME: Can't change date yet
598  */
599
600 void WCMD_setshow_date () {
601
602 char curdate[64], buffer[64];
603 DWORD count;
604
605   if (lstrlen(param1) == 0) {
606     if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
607                 curdate, sizeof(curdate))) {
608       WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
609       ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
610       if (count > 2) {
611         WCMD_output (nyi);
612       }
613     }
614     else WCMD_print_error ();
615   }
616   else {
617     WCMD_output (nyi);
618   }
619 }
620
621 /****************************************************************************
622  * WCMD_setshow_env
623  *
624  * Set/Show the environment variables
625  *
626  * FIXME: need to sort variables into order for display?
627  */
628
629 void WCMD_setshow_env (char *s) {
630
631 LPVOID env;
632 char *p;
633 int status;
634 char buffer[1048];
635
636   if (strlen(param1) == 0) {
637     env = GetEnvironmentStrings ();
638     p = (char *) env;
639     while (*p) {
640       WCMD_output ("%s\n", p);
641       p += lstrlen(p) + 1;
642     }
643   }
644   else {
645     p = strchr (s, '=');
646     if (p == NULL) {
647
648       /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
649          environment variable C, whereas on NT it shows ALL variables
650          starting with C.
651        */
652       status = GetEnvironmentVariable(s, buffer, sizeof(buffer));
653       if (status) {
654         WCMD_output("%s=%s\n", s, buffer);
655       } else {
656         WCMD_output ("Environment variable %s not defined\n", s);
657       }
658       return;
659     }
660     *p++ = '\0';
661
662     if (strlen(p) == 0) p = 0x00;
663     status = SetEnvironmentVariable (s, p);
664     if (!status) WCMD_print_error();
665   }
666   /* WCMD_output (newline);   @JED*/
667 }
668
669 /****************************************************************************
670  * WCMD_setshow_path
671  *
672  * Set/Show the path environment variable
673  */
674
675 void WCMD_setshow_path () {
676
677 char string[1024];
678 DWORD status;
679
680   if (strlen(param1) == 0) {
681     status = GetEnvironmentVariable ("PATH", string, sizeof(string));
682     if (status != 0) {
683       WCMD_output ("PATH=%s\n", string);
684     }
685     else {
686       WCMD_output ("PATH not found\n");
687     }
688   }
689   else {
690     status = SetEnvironmentVariable ("PATH", param1);
691     if (!status) WCMD_print_error();
692   }
693 }
694
695 /****************************************************************************
696  * WCMD_setshow_prompt
697  *
698  * Set or show the command prompt.
699  */
700
701 void WCMD_setshow_prompt () {
702
703 char *s;
704
705   if (strlen(param1) == 0) {
706     SetEnvironmentVariable ("PROMPT", NULL);
707   }
708   else {
709     s = param1;
710     while ((*s == '=') || (*s == ' ')) s++;
711     if (strlen(s) == 0) {
712       SetEnvironmentVariable ("PROMPT", NULL);
713     }
714     else SetEnvironmentVariable ("PROMPT", s);
715   }
716 }
717
718 /****************************************************************************
719  * WCMD_setshow_time
720  *
721  * Set/Show the system time
722  * FIXME: Can't change time yet
723  */
724
725 void WCMD_setshow_time () {
726
727 char curtime[64], buffer[64];
728 DWORD count;
729 SYSTEMTIME st;
730
731   if (strlen(param1) == 0) {
732     GetLocalTime(&st);
733     if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
734                 curtime, sizeof(curtime))) {
735       WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
736       ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
737       if (count > 2) {
738         WCMD_output (nyi);
739       }
740     }
741     else WCMD_print_error ();
742   }
743   else {
744     WCMD_output (nyi);
745   }
746 }
747
748 /****************************************************************************
749  * WCMD_shift
750  *
751  * Shift batch parameters.
752  */
753
754 void WCMD_shift () {
755
756   if (context != NULL) context -> shift_count++;
757
758 }
759
760 /****************************************************************************
761  * WCMD_title
762  *
763  * Set the console title
764  */
765 void WCMD_title (char *command) {
766   SetConsoleTitle(command);
767 }
768
769 /****************************************************************************
770  * WCMD_type
771  *
772  * Copy a file to standard output.
773  */
774
775 void WCMD_type () {
776
777 HANDLE h;
778 char buffer[512];
779 DWORD count;
780
781   h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
782                 FILE_ATTRIBUTE_NORMAL, NULL);
783   if (h == INVALID_HANDLE_VALUE) {
784     WCMD_print_error ();
785     return;
786   }
787   while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
788     if (count == 0) break;      /* ReadFile reports success on EOF! */
789     WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), buffer, count, &count, NULL);
790   }
791   CloseHandle (h);
792 }
793
794 /****************************************************************************
795  * WCMD_verify
796  *
797  * Display verify flag.
798  * FIXME: We don't actually do anything with the verify flag other than toggle
799  * it...
800  */
801
802 void WCMD_verify (char *command) {
803
804 static char *von = "Verify is ON\n", *voff = "Verify is OFF\n";
805 int count;
806
807   count = strlen(command);
808   if (count == 0) {
809     if (verify_mode) WCMD_output (von);
810     else WCMD_output (voff);
811     return;
812   }
813   if (lstrcmpi(command, "ON") == 0) {
814     verify_mode = 1;
815     return;
816   }
817   else if (lstrcmpi(command, "OFF") == 0) {
818     verify_mode = 0;
819     return;
820   }
821   else WCMD_output ("Verify must be ON or OFF\n");
822 }
823
824 /****************************************************************************
825  * WCMD_version
826  *
827  * Display version info.
828  */
829
830 void WCMD_version () {
831
832   WCMD_output (version_string);
833
834 }
835
836 /****************************************************************************
837  * WCMD_volume
838  *
839  * Display volume info and/or set volume label. Returns 0 if error.
840  */
841
842 int WCMD_volume (int mode, char *path) {
843
844 DWORD count, serial;
845 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
846 BOOL status;
847 static char syntax[] = "Syntax Error\n\n";
848
849   if (lstrlen(path) == 0) {
850     status = GetCurrentDirectory (sizeof(curdir), curdir);
851     if (!status) {
852       WCMD_print_error ();
853       return 0;
854     }
855     status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
856         NULL, NULL, 0);
857   }
858   else {
859     if ((path[1] != ':') || (lstrlen(path) != 2)) {
860       WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), syntax, strlen(syntax), &count, NULL);
861       return 0;
862     }
863     wsprintf (curdir, "%s\\", path);
864     status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
865         NULL, NULL, 0);
866   }
867   if (!status) {
868     WCMD_print_error ();
869     return 0;
870   }
871   WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
872         curdir[0], label, HIWORD(serial), LOWORD(serial));
873   if (mode) {
874     WCMD_output ("Volume label (11 characters, ENTER for none)?");
875     ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
876     if (count > 1) {
877       string[count-1] = '\0';           /* ReadFile output is not null-terminated! */
878       if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
879     }
880     if (lstrlen(path) != 0) {
881       if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
882     }
883     else {
884       if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();
885     }
886   }
887   return 1;
888 }