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