windows-afsifs-20050620
[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     }
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     GetCurrentDirectory(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 (!SetCurrentDirectory(newPath)) {
102                         code = GetLastError();
103             return code;
104         }
105     }
106         
107     /* now get the absolute path to the current wdir in this drive */
108     GetCurrentDirectory(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                 SetCurrentDirectory(origPath);
119     }
120         
121     return 0;
122 }
123
124 #ifdef COMMENT
125 struct hostent *hostutil_GetHostByName(char *namep)
126 {
127         struct hostent *thp;
128         
129         thp = gethostbyname(namep);
130         return thp;
131 }
132
133 /* get hostname or addr, given addr in network byte order */
134 char *hostutil_GetNameByINet(afs_uint32 addr)
135 {
136         static char hostNameBuffer[256];
137         struct hostent *thp;
138         
139         thp = gethostbyaddr((char *) &addr, sizeof(afs_uint32), AF_INET);
140         if (thp)
141                 strcpy(hostNameBuffer, thp->h_name);
142         else {
143                 sprintf(hostNameBuffer, "%d.%d.%d.%d",
144                         addr & 0xff,
145                         (addr >> 8) & 0xff,
146                         (addr >> 16) & 0xff,
147                         (addr >> 24) & 0xff);
148         }
149
150         /* return static buffer */
151         return hostNameBuffer;
152 }
153 #endif
154
155 /* is this a digit or a digit-like thing? */
156 static int ismeta(ac, abase)
157 register int abase;
158 register int ac; {
159 /*    if (ac == '-' || ac == 'x' || ac == 'X') return 1; */
160     if (ac >= '0' && ac <= '7') return 1;
161     if (abase <= 8) return 0;
162     if (ac >= '8' && ac <= '9') return 1;
163     if (abase <= 10) return 0;
164     if (ac >= 'a' && ac <= 'f') return 1;
165     if (ac >= 'A' && ac <= 'F') return 1;
166     return 0;
167 }
168
169 /* given that this is a digit or a digit-like thing, compute its value */
170 static int getmeta(ac)
171 register int ac; {
172     if (ac >= '0' && ac <= '9') return ac - '0';
173     if (ac >= 'a' && ac <= 'f') return ac - 'a' + 10;
174     if (ac >= 'A' && ac <= 'F') return ac - 'A' + 10;
175     return 0;
176 }
177
178 long util_GetInt32 (as, aval)
179 register char *as;
180 long *aval;
181 {
182     register long total;
183     register int tc;
184     int base;
185     int negative;
186
187     total = 0;  /* initialize things */
188     negative = 0;
189
190     /* skip over leading spaces */
191     while (tc = *as) {
192         if (tc != ' ' && tc != '\t') break;
193     }
194
195     /* compute sign */
196     if (*as == '-') {
197         negative = 1;
198         as++;   /* skip over character */
199     }
200
201     /* compute the base */
202     if (*as == '0') {
203         as++;
204         if (*as == 'x' || *as == 'X') {
205             base = 16;
206             as++;
207         }
208         else base = 8;
209     }
210     else base = 10;
211
212     /* compute the # itself */
213     while(tc = *as) {
214         if (!ismeta(tc, base)) return -1;
215         total *= base;
216         total += getmeta(tc);
217         as++;
218     }
219
220     if (negative) *aval = -total;
221     else *aval = total;
222     return 0;
223 }
224
225 char *cm_mount_root="afs"; 
226 char *cm_slash_mount_root="/afs";
227 char *cm_back_slash_mount_root="\\afs";
228
229 void fs_utils_InitMountRoot()
230 {
231     HKEY parmKey;
232     char mountRoot[MAX_PATH+1];
233     char *pmount=mountRoot;
234     DWORD len=sizeof(mountRoot)-1;
235     printf("int mountroot \n");
236     if ((RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY, 0, KEY_QUERY_VALUE, &parmKey)!= ERROR_SUCCESS) 
237          || (RegQueryValueEx(parmKey, "Mountroot", NULL, NULL,(LPBYTE)(mountRoot), &len)!= ERROR_SUCCESS)
238          || (len==sizeof(mountRoot)-1)
239          ) 
240         strcpy(mountRoot, "\\afs"); 
241     RegCloseKey(parmKey);
242     mountRoot[len]=0;       /*safety see ms-help://MS.MSDNQTR.2002OCT.1033/sysinfo/base/regqueryvalueex.htm*/
243     cm_mount_root=malloc(len+1);
244     cm_slash_mount_root=malloc(len+2);
245     cm_back_slash_mount_root=malloc(len+2);
246     if ((*pmount=='/') || (*pmount='\\'))
247         pmount++;
248     strcpy(cm_mount_root,pmount);
249     strcpy(cm_slash_mount_root+1,pmount);
250     cm_slash_mount_root[0]='/';
251     strcpy(cm_back_slash_mount_root+1,pmount);
252     cm_back_slash_mount_root[0]='\\';
253 }
254