home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Network Support Encyclopedia 96-1
/
novell-nsepro-1996-1-cd2.iso
/
download
/
netware
/
mapit.exe
/
RESETMAP.C
< prev
next >
Wrap
Text File
|
1992-03-13
|
16KB
|
464 lines
/********************************************************************
Source: RESETMAP.C - RESET MAP drives & search drives.
Date : 6/19/90 - original source
-----------------------------------------------------------------------
Purpose:
demonstrate how mappings may be saved and restored for critical error
recovery. It shows how to save all relevant information for any
mapped drive, including ROOT mappings, and how to restore them.
-----------------------------------------------------------------------
Originally developed with MSC 5.1:
Compiler switches:
cl /W3 /Zp /J /Gs /AS /DMSC /DLINT_ARGS /c
-----------------------------------------------------------------------
06/19/90 - vb - initial release.
-----------------------------------------------------------------------
01/23/92 - vb - implemented map root support.
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <direct.h>
#include <dos.h>
/*-----------------------------------------------------------------------*/
#include <nit.h>
#include <niterror.h>
/*-----------------------------------------------------------------------*/
#define DISPLAYSAVE 1
#define DISPLAYRESET 1
#define NOTMAPPED 255
#define LOCAL 128
#define MAXMAPPINGS 32
#define MAXSEARCHDRIVES 16
#define SEARCHDRIVE 1
#define ENDMARKER 0xFF
/*-----------------------------------------------------------------------*/
/* Mapping Control Structure:
** makes a copy of the current map drives.
** Thus in the event of losing a server a recovery process may be
** that will restore the map drives. */
struct {
BYTE vector[MAXSEARCHDRIVES]; /* stores search drive order (indexes) */
struct {
char path[255];
BYTE search; /* Flag - marked if search drive */
BYTE dtype; /* drive type - used, network, local */
BYTE rootEnd; /* index into 'path' at end of the root portion */
WORD connID; /* server connection for each mapped drive */
} drive[MAXMAPPINGS];
} maps;
int cCode;
BYTE drivenum;
BYTE dirhandlenum;
BYTE sflags;
WORD connID, defaultConnID;
char dirPath[255];
char thePath[255];
char tmpPath[255];
char serverName[48];
char volume[48];
extern void SaveDriveMappings(void );
extern void ResetDriveMappings(void );
extern void DisplayMappings(void );
extern int main(void );
/********************************************************************
** saves all mapped drives, placing respective information into the
** control structure
********************************************************************/
void SaveDriveMappings()
{
int drv, I;
char *pntr, *pntr2, *tpntr, tchr;
for (drv = 0; drv < MAXMAPPINGS; drv++) {
maps.drive[drv].dtype = NOTMAPPED; /* is not being used */
maps.drive[drv].rootEnd = 0; /* ?? if mapped root yet */
cCode = GetDriveInformation((BYTE)drv,
&maps.drive[drv].connID,
&dirhandlenum);
/* determine if local drive, permament or temporary or such
** a combination */
if (cCode == LOCAL) {
maps.drive[drv].dtype = (BYTE)LOCAL;
maps.drive[drv].connID = 0; /* no connection ID */
} /* if */
if (dirhandlenum) {
/* get filer server name if not local drv */
GetFileServerName(maps.drive[drv].connID,maps.drive[drv].path);
/* must set preferred connection to obtain the path */
/* from the required server. */
if (connID != maps.drive[drv].connID) {
connID = maps.drive[drv].connID;
SetPreferredConnectionID(maps.drive[drv].connID);
} /* if */
if (GetDirectoryPath(dirhandlenum,dirPath) == 0) {
maps.drive[drv].dtype = (BYTE)cCode;
strcat(maps.drive[drv].path,"\\");
strcat(maps.drive[drv].path,dirPath);
/* Check if MAPped ROOT */
sprintf(thePath,"%c:",drv + 'A'); /* set drive letter */
/* obtain the path for the drive. NOTE: if mapped root */
/* the MAP ROOTed portion is not returned. */
ParsePath(thePath, serverName, volume, tmpPath);
/* if a 'path' was returned then get rid of trailing '\' */
if (tmpPath[0]) tmpPath[strlen(tmpPath) - 1] = NULL;
/* To check if this is a MAPped ROOT, compare the paths */
/* returned from the 'ParsePath' api and the path we got */
/* back from the 'GetDirectoryPath' api. If the paths do */
/* NOT match we found a MAPped ROOT. We must also */
/* determine the MAP ROOTed portion and any path off of */
/* the root */
if (strcmp(tmpPath,(strchr(dirPath,':') + 1))) {
/* calculate index where the root ends ('\') */
maps.drive[drv].rootEnd =
(BYTE)(strstr(maps.drive[drv].path,tmpPath) -
maps.drive[drv].path );
/* force NULL over the '\', the path off of the root */
/* can now be obtained via the path[rootEnd] */
maps.drive[drv].path[maps.drive[drv].rootEnd - 1] = NULL;
}
} /* if */
} /* if */
} /* for */
/* use the PATH environment variable to obtain the */
/* search drive order which also includes local search paths. */
strcpy(thePath,strupr(getenv("PATH")));
if (thePath[0] != NULL) {
drv = 0;
pntr = thePath;
/* loop all of PATH if processed */
while (drv < MAXSEARCHDRIVES && (pntr = strchr(pntr,':')) ) {
tpntr = pntr;
/* if character after ':' = '.' then it's a search mapping, */
/* otherwise it's a local drive specification */
if (*(pntr + 1) == '.') {
maps.vector[drv] = *(pntr - 1) - 'A';
maps.drive[maps.vector[drv]].search = SEARCHDRIVE;
drv++; /* advanced to next drive in path */
}
else {
/* Local search drive. Find next available location in the */
/* drive table and assign the correct vector value */
for (I = 0; (maps.drive[I].dtype != NOTMAPPED) &&
(I < MAXMAPPINGS); I++);
if (I < MAXMAPPINGS) {
/* check for the ';' following each drive specifier. */
/* NOTE: on last one there may not be one so check */
/* for NULL instead */
if ((pntr2 = strchr(pntr,';')) == NULL)
pntr2 = strchr(pntr, '\0');
if (pntr2) {
tchr = *pntr2;
*pntr2 = '\0';
pntr--;
/* update the search drive order */
maps.vector[drv] = (BYTE)I;
maps.drive[I].dtype = 1;
maps.drive[I].search = SEARCHDRIVE;
/* copy the local drive specification */
strcpy(maps.drive[I].path,pntr);
if (tchr != NULL) *pntr2 = '|';
drv++; /* advanced to next drive in path */
}
}
}
/* 0xFF marks end of search drive vector */
if (drv <= MAXSEARCHDRIVES) maps.vector[drv] = ENDMARKER;
*tpntr = ' '; /* must STEP over current ':' for next search */
} /* while */
}
} /* SaveDriveMappings */
/********************************************************************
** Maps the supplied drive and path as a Mapped Root
** Input Parameters:
** int drv - A = 0, B = 1...
** char far *rpath - must be FAR pointer to string
********************************************************************/
int MapRoot(int drv, char far *rpath)
{
union REGS regs;
struct SREGS sregs;
regs.x.ax = 0xE905; /* Map Root function code - AX = E905h */
regs.x.bx = drv + 1; /* A = 1, B = 2.... */
regs.x.dx = FP_OFF(rpath); /* DX - offset of path */
sregs.ds = FP_SEG(rpath); /* DS - segment of path */
intdosx(®s,®s,&sregs);
return(regs.h.al); /* error code return in AL */
} /* MapRoot */
/********************************************************************
** Restore the the drive mappings saved in the conrol structure
********************************************************************/
void ResetDriveMappings()
{
int drv, sdrv, dummy;
char drvl, *pntr;
/* 1st reset a the non-search drives. Actually all mapped drives */
/* are reset, since search drives are only mapped drives that are */
/* specified in the PATH environment variable. */
for (drv = 0; drv < MAXMAPPINGS; drv++) {
/* if connection ID specified for drive, then reset it, */
/* otherwise it's a local mapping only */
if (maps.drive[drv].connID) {
/* if MAPped ROOT, then set the root and then modify the */
/* path off of that root if necessary */
if (maps.drive[drv].rootEnd) {
/* display only non-search drives at this time */
if (maps.drive[drv].search != SEARCHDRIVE)
printf("\nDrive: %c := %s \\%s\\",'A' + (char)drv,
maps.drive[drv].path,
&maps.drive[drv].path[maps.drive[drv].rootEnd]
);
SetPreferredConnectionID(maps.drive[drv].connID);
cCode = MapRoot(drv, (char far *)maps.drive[drv].path);
if (cCode) {
printf("\nError MapRoot: %d",cCode);
exit(1);
}
/* if not at root, then set the path off of the root */
if (maps.drive[drv].path[maps.drive[drv].rootEnd] != NULL) {
_dos_getdrive(&sdrv);
_dos_setdrive(drv, &dummy);
chdir(&maps.drive[drv].path[maps.drive[drv].rootEnd]);
_dos_setdrive(sdrv, &sdrv);
} /* if */
} /* if */
else {
/* display only non-search drives at this time */
if (maps.drive[drv].search != SEARCHDRIVE)
printf("\nDrive: %c := %s ",'A' + (char)drv,
maps.drive[drv].path);
drvl = (char)(drv + 'A');
cCode = MapDrive(maps.drive[drv].connID,
NO_BASE_DRIVE,
maps.drive[drv].path,
DRIVE_ADD,
0,
&drvl);
if (cCode) {
printf("\nError MapDrive: %d",cCode);
exit(1);
} /* if */
} /* else */
} /* if */
else if (maps.drive[drv].dtype == LOCAL)
printf("\nDrive: %c := maps to local drive ",'A' + (char)drv);
} /* for */
/* now display the search drives & create the PATH variable */
printf("\n-----");
pntr = thePath;
for (drv = 0; (maps.vector[drv] != ENDMARKER) &&
(drv < MAXSEARCHDRIVES); drv++) {
if (maps.vector[drv] < MAXMAPPINGS) {
/* only reset the a NetWare mapped drives */
/* NOTE: local search drives set only via */
/* the PATH variable */
if (maps.drive[maps.vector[drv]].connID) {
drvl = maps.vector[drv] + 'A';
printf("\nSearch %2d := [%c:%s",drv + 1, drvl,
maps.drive[maps.vector[drv]].path);
if (maps.drive[maps.vector[drv]].rootEnd)
printf(" \\%s\\ ",
&maps.drive[drv].path[maps.drive[drv].rootEnd]);
printf("]");
/* place the drive letter into the path followed by ':.;' */
sprintf(pntr,"%c:.;",
(char)(maps.vector[drv] + 'A'));
pntr += 4;
/*
*pntr++ = drvl;
*pntr++ = ':';
*pntr++ = '.';
*pntr++ = ';';
*pntr = NULL;
*/
}
else {
/* force local drive & path mapping into PATH */
printf("\nSearch %2d := %s",drv + 1,
maps.drive[maps.vector[drv]].path);
strcpy(pntr,maps.drive[maps.vector[drv]].path);
if (pntr = strchr(pntr,NULL)) {
*pntr++ = ';';
*pntr = NULL;
}
} /* else */
} /* if */
} /* for */
/*-- UPDATE THE DOS PATH VARIABLE --*/
PutEnvironmentVariable( "PATH", thePath );
printf("\nPath = %s\n",thePath);
} /* ResetDriveMappings */
/***********************************************************************
** Displays the mappings saved in the control structure
***********************************************************************/
void DisplayMappings()
{
BYTE drv;
for (drv = 0; drv < MAXMAPPINGS; drv++) {
if (maps.drive[drv].dtype == LOCAL) /* local drive mappings */
printf("\nDrive %c: local drive",drv + 'A');
} /* for */
printf("\n-----");
/* Non search drive mappings */
for (drv = 0; drv < MAXMAPPINGS; drv++) {
if (maps.drive[drv].dtype != NOTMAPPED &&
maps.drive[drv].dtype != LOCAL &&
maps.drive[drv].search != SEARCHDRIVE) {
/* display mapped roots */
if (maps.drive[drv].rootEnd)
printf("\nDrive: %c := [ %s \\%s\\ ]",'A' + drv,
maps.drive[drv].path,
&maps.drive[drv].path[maps.drive[drv].rootEnd]
);
else
printf("\nDrive %c:= %s",drv + 'A',maps.drive[drv].path);
}
} /* for */
printf("\n-----");
for (drv = 0; (maps.vector[drv] < ENDMARKER) &&
(drv < MAXSEARCHDRIVES); drv++) {
printf("\nSearch %2d := ",drv + 1);
if (maps.drive[maps.vector[drv]].connID) {
printf("[%c:%s", maps.vector[drv] + 'A',
maps.drive[maps.vector[drv]].path);
if (maps.drive[maps.vector[drv]].rootEnd)
printf(" \\%s\\ ",
&maps.drive[drv].path[maps.drive[drv].rootEnd]);
printf("]");
}
else
printf("[%s]", maps.drive[maps.vector[drv]].path);
} /* for */
} /* DisplayMappings */
/**************************************************************************/
main()
{
printf("\nRESETMAP: Saves and Restores mapped drives.\n");
/* save the current default connection ID */
defaultConnID = GetDefaultConnectionID();
SaveDriveMappings();
DisplayMappings();
printf("\n\nPress Key to Restore the Drives...\n\n");
getch();
printf("\nRestoring drive MAP:\n");
ResetDriveMappings();
SetPreferredConnectionID(defaultConnID);
} /* main */