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