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 >
Text File  |  1992-03-13  |  16KB  |  464 lines

  1. /********************************************************************
  2. Source: RESETMAP.C - RESET MAP drives & search drives.
  3. Date  : 6/19/90      - original source
  4. -----------------------------------------------------------------------
  5.  
  6. Purpose:
  7.    demonstrate how mappings may be saved and restored for critical error
  8.    recovery.  It shows how to save all relevant information for any
  9.    mapped drive, including ROOT mappings, and how to restore them.
  10. -----------------------------------------------------------------------
  11.  
  12. Originally developed with MSC 5.1:
  13.  
  14. Compiler switches:
  15.  
  16.    cl /W3 /Zp /J /Gs /AS /DMSC /DLINT_ARGS /c
  17.  
  18. -----------------------------------------------------------------------
  19. 06/19/90 - vb - initial release.
  20. -----------------------------------------------------------------------
  21. 01/23/92 - vb - implemented map root support.
  22. ***********************************************************************/
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <conio.h>
  29. #include <direct.h>
  30. #include <dos.h>
  31.  
  32. /*-----------------------------------------------------------------------*/
  33.  
  34. #include <nit.h>
  35. #include <niterror.h>
  36.  
  37. /*-----------------------------------------------------------------------*/
  38.  
  39. #define DISPLAYSAVE        1
  40. #define DISPLAYRESET       1
  41.  
  42. #define NOTMAPPED       255
  43. #define LOCAL           128
  44. #define MAXMAPPINGS      32
  45. #define MAXSEARCHDRIVES  16
  46. #define SEARCHDRIVE       1
  47. #define ENDMARKER      0xFF
  48.  
  49. /*-----------------------------------------------------------------------*/
  50.  
  51. /* Mapping Control Structure:
  52. ** makes a copy of the current map drives.
  53. ** Thus in the event of losing a server a recovery process may be
  54. ** that will restore the map drives. */
  55.  
  56. struct {
  57.    BYTE vector[MAXSEARCHDRIVES]; /* stores search drive order (indexes) */
  58.    struct {
  59.       char path[255];
  60.       BYTE search;      /* Flag - marked if search drive                */
  61.       BYTE dtype;       /* drive type - used, network, local            */
  62.       BYTE rootEnd;     /* index into 'path' at end of the root portion */
  63.       WORD connID;      /* server connection for each mapped drive      */
  64.       } drive[MAXMAPPINGS];
  65.    }  maps;
  66.  
  67. int   cCode;
  68. BYTE  drivenum;
  69. BYTE  dirhandlenum;
  70. BYTE  sflags;
  71. WORD  connID, defaultConnID;
  72. char  dirPath[255];
  73. char  thePath[255];
  74. char  tmpPath[255];
  75. char  serverName[48];
  76. char  volume[48];
  77.  
  78. extern  void SaveDriveMappings(void );
  79. extern  void ResetDriveMappings(void );
  80. extern  void DisplayMappings(void );
  81.  
  82. extern  int main(void );
  83.  
  84. /********************************************************************
  85. ** saves all mapped drives, placing respective information into the
  86. ** control structure
  87. ********************************************************************/
  88. void SaveDriveMappings()
  89. {
  90.    int drv, I;
  91.    char *pntr, *pntr2, *tpntr, tchr;
  92.  
  93.    for (drv = 0; drv < MAXMAPPINGS; drv++) {
  94.       maps.drive[drv].dtype = NOTMAPPED;     /* is not being used       */
  95.       maps.drive[drv].rootEnd = 0;           /* ?? if mapped root yet   */
  96.  
  97.       cCode = GetDriveInformation((BYTE)drv,
  98.                                   &maps.drive[drv].connID,
  99.                                                       &dirhandlenum);
  100.  
  101.          /* determine if local drive, permament or temporary or such
  102.          ** a combination */
  103.  
  104.       if (cCode == LOCAL) {
  105.          maps.drive[drv].dtype = (BYTE)LOCAL;
  106.          maps.drive[drv].connID = 0;         /* no connection ID */
  107.          } /* if */
  108.  
  109.       if (dirhandlenum) {
  110.  
  111.             /* get filer server name if not local drv */
  112.  
  113.          GetFileServerName(maps.drive[drv].connID,maps.drive[drv].path);
  114.  
  115.             /* must set preferred connection to obtain the path   */
  116.             /* from the required server.                          */
  117.  
  118.          if (connID != maps.drive[drv].connID) {
  119.             connID = maps.drive[drv].connID;
  120.             SetPreferredConnectionID(maps.drive[drv].connID);
  121.             } /* if */
  122.  
  123.          if (GetDirectoryPath(dirhandlenum,dirPath) == 0) {
  124.             maps.drive[drv].dtype = (BYTE)cCode;
  125.             strcat(maps.drive[drv].path,"\\");
  126.             strcat(maps.drive[drv].path,dirPath);
  127.  
  128.                /* Check if MAPped ROOT */
  129.  
  130.             sprintf(thePath,"%c:",drv + 'A');      /* set drive letter */
  131.  
  132.                /* obtain the path for the drive. NOTE: if mapped root   */
  133.                /* the MAP ROOTed portion is not returned.               */
  134.  
  135.             ParsePath(thePath, serverName, volume, tmpPath);
  136.  
  137.                /* if a 'path' was returned then get rid of trailing '\' */
  138.  
  139.             if (tmpPath[0]) tmpPath[strlen(tmpPath) - 1] = NULL;
  140.  
  141.                /* To check if this is a MAPped ROOT, compare the paths  */
  142.                /* returned from the 'ParsePath' api and the path we got */
  143.                /* back from the 'GetDirectoryPath' api. If the paths do */
  144.                /* NOT match we found a MAPped ROOT.  We must also       */
  145.                /* determine the MAP ROOTed portion and any path off of  */
  146.                /* the root                                              */
  147.  
  148.             if (strcmp(tmpPath,(strchr(dirPath,':') + 1))) {
  149.  
  150.                   /* calculate index where the root ends ('\') */
  151.  
  152.                maps.drive[drv].rootEnd =
  153.                                (BYTE)(strstr(maps.drive[drv].path,tmpPath) -
  154.                                maps.drive[drv].path );
  155.  
  156.                   /* force NULL over the '\', the path off of the root  */
  157.                   /* can now be obtained via the path[rootEnd]          */
  158.  
  159.                maps.drive[drv].path[maps.drive[drv].rootEnd - 1] = NULL;
  160.                }
  161.             } /* if */
  162.          } /* if */
  163.       } /* for */
  164.  
  165.       /* use the PATH environment variable to obtain the             */
  166.       /* search drive order which also includes local search paths.  */
  167.  
  168.         strcpy(thePath,strupr(getenv("PATH")));
  169.  
  170.    if (thePath[0] != NULL) {
  171.       drv = 0;
  172.       pntr = thePath;
  173.  
  174.          /* loop all of PATH if processed */
  175.  
  176.       while (drv < MAXSEARCHDRIVES && (pntr = strchr(pntr,':')) ) {
  177.          tpntr = pntr;
  178.  
  179.             /* if character after ':' = '.' then it's a search mapping, */
  180.             /* otherwise it's a local drive specification               */
  181.  
  182.          if (*(pntr + 1) == '.') {
  183.             maps.vector[drv] = *(pntr - 1) - 'A';
  184.             maps.drive[maps.vector[drv]].search = SEARCHDRIVE;
  185.  
  186.             drv++;     /* advanced to next drive in path */
  187.             }
  188.          else {
  189.                /* Local search drive. Find next available location in the  */
  190.                /* drive table and assign the correct vector value          */
  191.  
  192.             for (I = 0; (maps.drive[I].dtype != NOTMAPPED) &&
  193.                          (I < MAXMAPPINGS); I++);
  194.  
  195.             if (I < MAXMAPPINGS) {
  196.                   /* check for the ';' following each drive specifier.  */
  197.                   /* NOTE: on last one there may not be one so check    */
  198.                   /* for   NULL instead                                 */
  199.  
  200.                     if ((pntr2 = strchr(pntr,';')) == NULL)
  201.                       pntr2 = strchr(pntr, '\0');
  202.  
  203.                if (pntr2) {
  204.                   tchr = *pntr2;
  205.                   *pntr2 = '\0';
  206.                   pntr--;
  207.  
  208.                      /* update the search drive order */
  209.  
  210.                   maps.vector[drv] = (BYTE)I;
  211.                   maps.drive[I].dtype = 1;
  212.                   maps.drive[I].search = SEARCHDRIVE;
  213.  
  214.                      /* copy the local drive specification */
  215.  
  216.                   strcpy(maps.drive[I].path,pntr);
  217.  
  218.                   if (tchr != NULL) *pntr2 = '|';
  219.  
  220.                   drv++;     /* advanced to next drive in path */
  221.                   }
  222.                }
  223.             }
  224.  
  225.             /* 0xFF marks end of search drive vector */
  226.  
  227.          if (drv <= MAXSEARCHDRIVES) maps.vector[drv] = ENDMARKER;
  228.  
  229.          *tpntr = ' ';    /* must STEP over current ':' for next search */
  230.          } /* while */
  231.       }
  232. } /* SaveDriveMappings */
  233.  
  234. /********************************************************************
  235. ** Maps the supplied drive and path as a Mapped Root
  236. ** Input Parameters:
  237. **    int drv - A = 0, B = 1...
  238. **    char far *rpath - must be FAR pointer to string
  239. ********************************************************************/
  240. int MapRoot(int drv, char far *rpath)
  241. {
  242.    union   REGS    regs;
  243.    struct  SREGS   sregs;
  244.  
  245.    regs.x.ax = 0xE905;             /* Map Root function code - AX = E905h  */
  246.    regs.x.bx = drv + 1;            /* A = 1, B = 2....                     */
  247.    regs.x.dx = FP_OFF(rpath);      /* DX - offset of path                  */
  248.    sregs.ds  = FP_SEG(rpath);      /* DS - segment of path                 */
  249.  
  250.    intdosx(®s,®s,&sregs);
  251.    return(regs.h.al);               /* error code return in AL */
  252. }  /* MapRoot */
  253.  
  254. /********************************************************************
  255. ** Restore the the drive mappings saved in the conrol structure
  256. ********************************************************************/
  257. void ResetDriveMappings()
  258. {
  259.    int drv, sdrv, dummy;
  260.    char drvl, *pntr;
  261.  
  262.       /* 1st reset a the non-search drives.  Actually all mapped drives */
  263.       /* are reset, since search drives are only mapped drives that are */
  264.       /* specified in the PATH environment variable.                    */
  265.  
  266.    for (drv = 0; drv < MAXMAPPINGS; drv++) {
  267.  
  268.          /* if connection ID specified for drive, then reset it,  */
  269.          /* otherwise it's a local mapping only                   */
  270.  
  271.       if (maps.drive[drv].connID) {
  272.             /* if MAPped ROOT, then set the root and then modify the */
  273.             /* path off of that root if necessary                    */
  274.  
  275.          if (maps.drive[drv].rootEnd) {
  276.  
  277.                /* display only non-search drives at this time */
  278.  
  279.             if (maps.drive[drv].search != SEARCHDRIVE)
  280.                printf("\nDrive:  %c := %s \\%s\\",'A' + (char)drv,
  281.                    maps.drive[drv].path,
  282.                    &maps.drive[drv].path[maps.drive[drv].rootEnd]
  283.                    );
  284.  
  285.             SetPreferredConnectionID(maps.drive[drv].connID);
  286.             cCode = MapRoot(drv, (char far *)maps.drive[drv].path);
  287.             if (cCode)  {
  288.                printf("\nError MapRoot: %d",cCode);
  289.                exit(1);
  290.                }
  291.  
  292.                /* if not at root, then set the path off of the root */
  293.  
  294.             if (maps.drive[drv].path[maps.drive[drv].rootEnd] != NULL) {
  295.                _dos_getdrive(&sdrv);
  296.                _dos_setdrive(drv, &dummy);
  297.                chdir(&maps.drive[drv].path[maps.drive[drv].rootEnd]);
  298.                _dos_setdrive(sdrv, &sdrv);
  299.                } /* if */
  300.             } /* if */
  301.          else {
  302.  
  303.                /* display only non-search drives at this time */
  304.  
  305.             if (maps.drive[drv].search != SEARCHDRIVE)
  306.                printf("\nDrive:  %c := %s ",'A' + (char)drv,
  307.                       maps.drive[drv].path);
  308.  
  309.             drvl = (char)(drv + 'A');
  310.             cCode = MapDrive(maps.drive[drv].connID,
  311.                              NO_BASE_DRIVE,
  312.                              maps.drive[drv].path,
  313.                              DRIVE_ADD,
  314.                              0,
  315.                              &drvl);
  316.  
  317.             if (cCode) {
  318.                printf("\nError MapDrive: %d",cCode);
  319.                exit(1);
  320.                } /* if */
  321.             } /* else */
  322.          } /* if */
  323.       else if (maps.drive[drv].dtype == LOCAL)
  324.          printf("\nDrive:  %c := maps to local drive ",'A' + (char)drv);
  325.       } /* for */
  326.  
  327.       /* now display the search drives & create the PATH variable */
  328.  
  329.    printf("\n-----");
  330.  
  331.    pntr = thePath;
  332.  
  333.    for (drv = 0; (maps.vector[drv] != ENDMARKER) &&
  334.                  (drv < MAXSEARCHDRIVES); drv++) {
  335.       if (maps.vector[drv] < MAXMAPPINGS) {
  336.  
  337.             /* only reset the a NetWare mapped drives */
  338.             /* NOTE: local search drives set only via */
  339.             /* the PATH variable                      */
  340.  
  341.          if (maps.drive[maps.vector[drv]].connID) {
  342.             drvl = maps.vector[drv] + 'A';
  343.  
  344.             printf("\nSearch %2d := [%c:%s",drv + 1, drvl,
  345.                    maps.drive[maps.vector[drv]].path);
  346.  
  347.             if (maps.drive[maps.vector[drv]].rootEnd)
  348.                printf(" \\%s\\ ",
  349.                       &maps.drive[drv].path[maps.drive[drv].rootEnd]);
  350.  
  351.             printf("]");
  352.  
  353.                /* place the drive letter into the path followed by ':.;' */
  354.             sprintf(pntr,"%c:.;",
  355.                     (char)(maps.vector[drv] + 'A'));
  356.             pntr += 4;
  357. /*
  358.             *pntr++ = drvl;
  359.             *pntr++ = ':';
  360.             *pntr++ = '.';
  361.             *pntr++ = ';';
  362.             *pntr = NULL;
  363. */
  364.             }
  365.          else  {
  366.                /* force local drive & path mapping into PATH */
  367.  
  368.             printf("\nSearch %2d := %s",drv + 1,
  369.                    maps.drive[maps.vector[drv]].path);
  370.  
  371.             strcpy(pntr,maps.drive[maps.vector[drv]].path);
  372.             if (pntr = strchr(pntr,NULL)) {
  373.                *pntr++ = ';';
  374.                *pntr = NULL;
  375.                }
  376.             } /* else */
  377.          } /* if */
  378.       } /* for */
  379.  
  380.    /*-- UPDATE THE DOS PATH VARIABLE --*/
  381.  
  382.    PutEnvironmentVariable( "PATH", thePath );
  383.  
  384.    printf("\nPath = %s\n",thePath);
  385. } /* ResetDriveMappings */
  386.  
  387. /***********************************************************************
  388. ** Displays the mappings saved in the control structure
  389. ***********************************************************************/
  390. void DisplayMappings()
  391. {
  392.    BYTE drv;
  393.  
  394.    for (drv = 0; drv < MAXMAPPINGS; drv++) {
  395.       if (maps.drive[drv].dtype == LOCAL)      /* local drive mappings */
  396.          printf("\nDrive %c: local drive",drv + 'A');
  397.       } /* for */
  398.  
  399.    printf("\n-----");
  400.  
  401.       /* Non search drive mappings */
  402.  
  403.    for (drv = 0; drv < MAXMAPPINGS; drv++) {
  404.       if (maps.drive[drv].dtype  != NOTMAPPED &&
  405.           maps.drive[drv].dtype  != LOCAL &&
  406.           maps.drive[drv].search != SEARCHDRIVE) {
  407.  
  408.             /* display mapped roots */
  409.  
  410.          if (maps.drive[drv].rootEnd)
  411.             printf("\nDrive:  %c := [ %s \\%s\\ ]",'A' + drv,
  412.                    maps.drive[drv].path,
  413.                    &maps.drive[drv].path[maps.drive[drv].rootEnd]
  414.                    );
  415.          else
  416.             printf("\nDrive %c:= %s",drv + 'A',maps.drive[drv].path);
  417.          }
  418.       } /* for */
  419.  
  420.    printf("\n-----");
  421.    for (drv = 0; (maps.vector[drv] < ENDMARKER) &&
  422.                  (drv < MAXSEARCHDRIVES); drv++) {
  423.  
  424.      printf("\nSearch %2d := ",drv + 1);
  425.  
  426.      if (maps.drive[maps.vector[drv]].connID) {
  427.          printf("[%c:%s", maps.vector[drv] + 'A',
  428.                 maps.drive[maps.vector[drv]].path);
  429.  
  430.          if (maps.drive[maps.vector[drv]].rootEnd)
  431.             printf(" \\%s\\ ",
  432.                    &maps.drive[drv].path[maps.drive[drv].rootEnd]);
  433.  
  434.          printf("]");
  435.          }
  436.       else
  437.          printf("[%s]", maps.drive[maps.vector[drv]].path);
  438.       } /* for */
  439.  
  440. } /* DisplayMappings */
  441.  
  442. /**************************************************************************/
  443. main()
  444. {
  445.    printf("\nRESETMAP: Saves and Restores mapped drives.\n");
  446.  
  447.       /* save the current default connection ID */
  448.  
  449.    defaultConnID = GetDefaultConnectionID();
  450.  
  451.    SaveDriveMappings();
  452.  
  453.    DisplayMappings();
  454.  
  455.    printf("\n\nPress Key to Restore the Drives...\n\n");
  456.    getch();
  457.    printf("\nRestoring drive MAP:\n");
  458.  
  459.    ResetDriveMappings();
  460.  
  461.    SetPreferredConnectionID(defaultConnID);
  462. }  /* main */
  463.  
  464.