Mail Archives: djgpp-workers/1996/08/16/11:34:50
Eli asked me a while ago for a function that
converts UNC to a drv:/path representation.
For what it's worth, here is one. It requires ioctl.c to
check if a device is remote or not. The strategy is to
check all possible drive values if they point to a remote drive, then
do a _truename on the root of that drive and see if it matches to the
given UNC. The longest match is taken as the result.
I have tested it under NW3.11 with VLM. Maybe someone here has
another network redirector to check if it works there. XFS drives do
not use UNCs.
Have a nice weekend.
>----------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/ioctl.h>
#define DEBUG
/*
** tn2dr: Try to convert a UNC to drv:/path
** INPUT:
** truename : The UNC
** OUTPUT:
** drv_path : well, drive:/path/to/it
** RETURNS
** 0 : Success
** -1 : Could not map
** -2 : _truename failed.
** -3 : Not called with a UNC specifier.
** errno remains untouched.
**
*/
int tn2dr(const char *truename,char *drv_path)
{ char drv;
char template[16];
char realname[256];
char canon_path[128];
char found = 0;
int longest_match = -1;
if(!(*truename =='\\' && truename[1]=='\\'))
return -3;
if(!_truename(truename,realname))
return -2;
#ifdef DEBUG
printf("Got <%s>, will check <%s>\n",truename, realname);
#endif
strcpy(template,"x:\\");
for(drv='A'; drv <='Z'; drv++){
/* UNCs can only happen on remote drives? */
/* I doubt it. */
int t = ioctl(drv-'A'+1, _DOS_ISREDIRBLK);
template[0]=drv;
if(( t != -1) && (t & 0x1000 ) &&
_truename(template,canon_path)){
#ifdef DEBUG
printf("Truename<%s> == canonpath<%s>\n",
template,canon_path);
#endif
if(strstr(realname,canon_path)){
char *c= realname;
char *d= canon_path;
int i=0;
while(*c && *c++ == *d++) ++i;
if(i>longest_match){
longest_match = i;
found = drv;
}
#ifdef DEBUG
printf("Maybe it is %c, matches %d chars\n",drv,i);
#endif
}
}
}
#ifdef DEBUG
if(found)
printf("Best match is drive %c:, %d
chars\n",found,longest_match);
#endif
if(found){
printf("Realname = <%s>\nLongest
Match:%d\n",realname,longest_match);
sprintf(drv_path,"%c:%s",found,
longest_match< strlen(realname) ?
realname+longest_match : "/");
#ifdef DEBUG
printf("%s\n",drv_path);
getch();
#endif
return 0;
}
return -1;
}
int main(int argc, char **argv){
char drv_path[256];
int i;
for(; *argv; argv++){
if(!tn2dr(*argv,drv_path))
printf("%s really is %s\n",*argv,drv_path);
}
return 0;
}
<---------------------------------------------------------------------
Ciao
Tom
******************************************************************
* Thomas Demmer * Phone : +49 234 700 6434 *
* Universitaetsstr. 150 * Fax : +49 234 709 4162 *
* Lehrstuhl fuer Stroemungsmechanik * Fax/Voice Box: *
* D-44780 Bochum * +49 2561 91371 2056 *
******************************************************************
* Email: demmer AT LStM DOT Ruhr-Uni-Bochum DOT De *
* WWW: http://www.lstm.ruhr-uni-bochum.de/~demmer *
******************************************************************
Bagpipes (n): an octopus wearing a kilt.
- Raw text -