home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / DIRUTL / PWD_C.ZIP / PWD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-21  |  2.3 KB  |  116 lines

  1. /*-
  2.  * pwd.c  -- main program for `pwd' for MSDOS
  3.  * Barry Schwartz, 21 August 1990
  4.  *
  5.  * Command usage:
  6.  *    pwd [drive ...]
  7.  *
  8.  * Each drive may be specified with or without a trailing colon.
  9.  * If no drive is given, the default drive is assumed.
  10.  *
  11.  * If any of the `drive' specifications is invalid for some reason,
  12.  * the message "invalid drive specification: %s", where %s is replaced
  13.  * by the drive specification (or "<default drive>" if no arguments
  14.  * were given and for some bizarre and probably scary reason the default
  15.  * drive has been found to be "invalid"), is inserted in the output stream,
  16.  * and the exit status will be 2.  Otherwise the exit status will be 0.
  17.  * (The exit status 1 is reserved for possible future use to denote
  18.  * some kind of fatal error.)
  19.  */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <dos.h>
  25. #include <stdlib.h>
  26. #include "msdosmac.h"
  27.  
  28.  
  29. char           *get_working_directory();
  30.  
  31.  
  32. #if 0
  33. char           *progname;
  34. #endif
  35. int             exit_status;
  36.  
  37.  
  38.  
  39. void
  40. print_invalid_drive_spec(char *arg)
  41. {
  42.     printf("invalid drive specification: %s\n", arg);
  43.     exit_status = 2;
  44. }
  45.  
  46.  
  47.  
  48. void
  49. print_working_directory(int drive, char *arg)
  50. {
  51.     static char     buf[_MAX_DIR];
  52.  
  53.     if (!get_working_directory(buf, drive))
  54.     print_invalid_drive_spec(arg);
  55.     else
  56.     {
  57.     CONVERT_TO_SLASHES(buf);
  58.     printf("%c:/%s\n", drive + 'a' - 1, buf);
  59.     }
  60. }
  61.  
  62.  
  63. #if 0
  64.  
  65. void
  66. usage()
  67. {
  68.     fprintf(stderr, "usage: %s [drive ...]\n", progname);
  69.     exit(1);
  70. }
  71.  
  72. #endif
  73.  
  74.  
  75.  
  76. int
  77. main(argc, argv)
  78. int             argc;
  79. char          **argv;
  80. {
  81.     int             drive;
  82.  
  83.     exit_status = 0;
  84.  
  85. #if 0
  86.     EXTRACT_PROGRAM_NAME(argv[0]);
  87.     progname = argv[0];
  88. #endif
  89.  
  90.     if (argc == 1)
  91.     {
  92.     _dos_getdrive(&drive);
  93.     print_working_directory(drive, "<default drive>");
  94.     }
  95.     else
  96.     {
  97.     int             arg_index = 0;
  98.     char           *arg;
  99.  
  100.     while (++arg_index != argc)
  101.     {
  102.         arg = argv[arg_index];
  103.         drive = tolower(arg[0]);    /* Requires a `smart' tolower() */
  104.         if (!islower(drive) || (arg[1] && (arg[1] != ':' || arg[2])))
  105.         print_invalid_drive_spec(arg);
  106.         else
  107.         {
  108.         drive -= 'a' - 1;    /* Convert to 1, 2, 3, ... */
  109.         print_working_directory(drive, arg);
  110.         }
  111.     }
  112.     }
  113.  
  114.     return exit_status;
  115. }
  116.