9f18f787588d76c09e128f0204af1cae98fc9eb0
[openafs.git] / src / WINNT / afsd / fs_utils.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <windows.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <errno.h>
17 #include <malloc.h>
18 #include <string.h>
19 #include <winioctl.h>
20 #include <winsock2.h>
21 #include <nb30.h>
22
23 #include <osi.h>
24 #include "afsd.h"
25 #include "smb.h"
26 #include "cmd.h"
27 #include <fs_utils.h>
28 #include <WINNT\afsreg.h>
29
30 long fs_ExtractDriveLetter(char *inPathp, char *outPathp)
31 {
32         if (inPathp[0] != 0 && inPathp[1] == ':') {
33                 /* there is a drive letter */
34                 *outPathp++ = *inPathp++;
35                 *outPathp++ = *inPathp++;
36                 *outPathp++ = 0;
37         }
38         else *outPathp = 0;
39
40         return 0;
41 }
42
43 /* strip the drive letter from a component */
44 long fs_StripDriveLetter(char *inPathp, char *outPathp, long outSize)
45 {
46         char tempBuffer[1000];
47         strcpy(tempBuffer, inPathp);
48         if (tempBuffer[0] != 0 && tempBuffer[1] == ':') {
49                 /* drive letter present */
50                 strcpy(outPathp, tempBuffer+2);
51         }
52         else {
53                 /* no drive letter present */
54                 strcpy(outPathp, tempBuffer);
55         }
56         return 0;
57 }
58
59 /* take a path with a drive letter, possibly relative, and return a full path
60  * without the drive letter.  This is the full path relative to the working
61  * dir for that drive letter.  The input and output paths can be the same.
62  */
63 long fs_GetFullPath(char *pathp, char *outPathp, long outSize)
64 {
65         char tpath[1000];
66         char origPath[1000];
67     char *firstp;
68     long code;
69     int pathHasDrive;
70     int doSwitch;
71     char newPath[3];
72
73         if (pathp[0] != 0 && pathp[1] == ':') {
74                 /* there's a drive letter there */
75         firstp = pathp+2;
76         pathHasDrive = 1;
77     } else {
78         firstp = pathp;
79                 pathHasDrive = 0;
80         }   
81         
82     if (*firstp == '\\' || *firstp == '/') {
83                 /* already an absolute pathname, just copy it back */
84         strcpy(outPathp, firstp);
85         return 0;
86     }
87         
88     GetCurrentDirectoryA(sizeof(origPath), origPath);
89         
90         doSwitch = 0;
91     if (pathHasDrive && (*pathp & ~0x20) != (origPath[0] & ~0x20)) {
92                 /* a drive has been specified and it isn't our current drive.
93          * to get path, switch to it first.  Must case-fold drive letters
94          * for user convenience.
95          */
96                 doSwitch = 1;
97         newPath[0] = *pathp;
98         newPath[1] = ':';
99         newPath[2] = 0;
100         if (!SetCurrentDirectoryA(newPath)) {
101                         code = GetLastError();
102             return code;
103         }
104     }
105         
106     /* now get the absolute path to the current wdir in this drive */
107     GetCurrentDirectoryA(sizeof(tpath), tpath);
108     strcpy(outPathp, tpath+2);  /* skip drive letter */
109         /* if there is a non-null name after the drive, append it */
110         if (*firstp != 0) {
111         strcat(outPathp, "\\");
112         strcat(outPathp, firstp);
113         }
114
115         /* finally, if necessary, switch back to our home drive letter */
116     if (doSwitch) {
117         SetCurrentDirectoryA(origPath);
118     }
119         
120     return 0;
121 }
122
123 /* is this a digit or a digit-like thing? */
124 static int ismeta(int abase, int ac) {
125 /*    if (ac == '-' || ac == 'x' || ac == 'X') return 1; */
126     if (ac >= '0' && ac <= '7') return 1;
127     if (abase <= 8) return 0;
128     if (ac >= '8' && ac <= '9') return 1;
129     if (abase <= 10) return 0;
130     if (ac >= 'a' && ac <= 'f') return 1;
131     if (ac >= 'A' && ac <= 'F') return 1;
132     return 0;
133 }
134
135 /* given that this is a digit or a digit-like thing, compute its value */
136 static int getmeta(int ac) {
137     if (ac >= '0' && ac <= '9') return ac - '0';
138     if (ac >= 'a' && ac <= 'f') return ac - 'a' + 10;
139     if (ac >= 'A' && ac <= 'F') return ac - 'A' + 10;
140     return 0;
141 }
142
143 char *cm_mount_root="afs"; 
144 char *cm_slash_mount_root="/afs";
145 char *cm_back_slash_mount_root="\\afs";
146
147 void fs_utils_InitMountRoot()
148 {
149     HKEY parmKey;
150     char mountRoot[MAX_PATH+1];
151     char *pmount=mountRoot;
152     DWORD len=sizeof(mountRoot)-1;
153     printf("int mountroot \n");
154     if ((RegOpenKeyExA(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY, 0, 
155                       (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey)!= ERROR_SUCCESS)
156         || (RegQueryValueExA(parmKey, "Mountroot", NULL, NULL,(LPBYTE)(mountRoot), &len)!= ERROR_SUCCESS)
157          || (len==sizeof(mountRoot)-1)
158          ) 
159         strcpy(mountRoot, "\\afs"); 
160     RegCloseKey(parmKey);
161     mountRoot[len]=0;       /*safety see ms-help://MS.MSDNQTR.2002OCT.1033/sysinfo/base/regqueryvalueex.htm*/
162     cm_mount_root=malloc(len+1);
163     cm_slash_mount_root=malloc(len+2);
164     cm_back_slash_mount_root=malloc(len+2);
165     if ((*pmount=='/') || (*pmount='\\'))
166         pmount++;
167     strcpy(cm_mount_root,pmount);
168     strcpy(cm_slash_mount_root+1,pmount);
169     cm_slash_mount_root[0]='/';
170     strcpy(cm_back_slash_mount_root+1,pmount);
171     cm_back_slash_mount_root[0]='\\';
172 }