summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/cdir.c
blob: 853cecbd54368b49e57a394beb789240cb9e9777 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
**                          CDIR.C
**
** Written By:    Lynn R. Lively
** Date Written:  9/18/91
**
** Purpose: To provide a change directory facility that will cross
**          drive/partition boundries. Never did understand why
**          MSDOS cd wouldn't do this already.
**
**-----------------------------------------------------------------
** I hereby place this work into the Public Domain. It may be used
** for any legal purpose public or private. Use this material at
** your own risk. I accept no responsibility for the accuracy or
** usability of the information contained herein. Neither do I
** accept liability for any possible damage caused by use of this
** material. However, should you have a problem, question, or
** suggestion I would be glad to help in any way that I can. You
** can reach me at (H) 713-893-7875 or (W) 713-591-6611 x 149.
**-----------------------------------------------------------------
*/

/*
**                          Change History
**
**  Rev #   Date       By      Description of change
**  1.00  | 09/18/91 | LRL | Original Version
**  1.01  | 09/18/91 | RBS | Added MSC, ZTC support for SNIPPETS
**-----------------------------------------------------------------
** Directory of initials:
** Initials          Name
** LRL        Lynn R. Lively
** RBS        Bob Stout
*/


#include <stdio.h>
#include <string.h>

#ifdef __TURBOC__
 #include <dir.h>
#else
 #include <dos.h>
 #include <direct.h>

 #ifdef __ZTC__
  #define _dos_getdrive(d) dos_getdrive(d)
  #define _dos_setdrive(d,m) dos_setdrive(d,m)
  #define drive_t unsigned
 #else
  #define drive_t int
 #endif

 drive_t getdisk(void)
 {
       drive_t drive;

       _dos_getdrive(&drive);
       return drive - 1;
 }

 drive_t setdisk(drive_t drive)
 {
       drive_t max_drives;

       _dos_setdrive(drive + 1, &max_drives);
       return max_drives - 1;
 }
#endif

main (int argc, char * argv[])
{
      int d;
      int max_d;

      char wk_str[128];

      if (argc > 1)
      {
            strupr (argv[1]);
            if (argv[1][1] == ':')
            {
                  /*
                  ** Find out what the maximum drive number can be.
                  */

                  max_d = getdisk ();
                  max_d = setdisk (max_d);

                  d = argv[1][0] - 'A';
                  if (d < max_d)
                  {
                        /*
                        ** If the drive specification was valid position to it
                        ** and then do a change directory.
                        */

                        setdisk (d);
                        chdir (argv[1]);
                  }
                  else
                  {
                        puts ("Invalid drive specification");
                        return -1;
                  }
            }
            else
            {
                  /*
                  ** If the argument has no drive spec just do a regular
                  ** change directory.
                  */

                  chdir (argv[1]);
            }
      }
      else
      {
            /*
            ** If no arguments are passed, return the current working
            ** directory path just like MSDOS cd does.
            */

            puts (getcwd (wk_str, sizeof (wk_str)));
      }
      return 0;
}