home *** CD-ROM | disk | FTP | other *** search
/ Windows NT Super Tune-Up Kit / PIE-WindowsNTSuperTuneUpKit-1997.iso / DISK_UTL / MOUNT / MNT.C next >
C/C++ Source or Header  |  1995-02-25  |  4KB  |  163 lines

  1. // MNT - lists and/or mounts Drives in Windows NT
  2. // Version 1.00 (c) 1995, Christoph HochstΣtter
  3.  
  4. #include <windows.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <winioctl.h>
  8. #include <io.h>
  9. #include <conio.h>
  10. #include "mnt.h"
  11.  
  12. char zap = FALSE;
  13.  
  14. void __fastcall chmalloc(void **chpointer, const int bytes)
  15. {
  16.     if (!(*chpointer=malloc(bytes))) {
  17.         fputs("Not enough memory.\n",stderr);
  18.         ExitProcess(94);
  19.     }
  20. }
  21.  
  22. export char __fastcall ValidDrive(const char *drive)
  23. {
  24.     if ((drive[2] == 0) && (drive[1]==':') \
  25.     && ((*drive|0x20) > 0x60) && ((*drive|0x20) < 0x7b))
  26.         return TRUE;
  27.     else
  28.         return FALSE;
  29. }
  30.  
  31. export void __fastcall index(char *stringbuffer, char *stringlist[], register unsigned short *count)
  32. {
  33.     for (*count=0;;(*count)++) {
  34.         stringlist[*count]=stringbuffer;
  35.         while (*(++stringbuffer)!=0);
  36.         if (*(++stringbuffer) == 0) break;
  37.     }
  38. }
  39.  
  40. export int __fastcall sort(char *stringlist[],const unsigned short count)
  41. {
  42. register char dirty;
  43. register unsigned short i,j;
  44. void *help;
  45.  
  46.     do {
  47.         dirty=FALSE;
  48.         for (i=1;i<=count;i++) {
  49.             if (!ValidDrive(stringlist[i])) 
  50.                 continue;
  51.             else
  52.                 j=i;
  53.             if ((!ValidDrive(stringlist[i-1]))||*stringlist[i-1]>*stringlist[i]) {
  54.                 help=stringlist[i-1];
  55.                 stringlist[i-1]=stringlist[i];
  56.                 stringlist[i]=help;
  57.                 dirty=TRUE;
  58.             }
  59.         }
  60.     } while (dirty);
  61.     return j;
  62. }
  63.  
  64. void __fastcall MyQueryDosDevice(const LPCTSTR s1,const LPTSTR s2,const DWORD i)
  65. {
  66.     if (!QueryDosDevice(s1,s2,i)) {
  67.         fputs("QueryDosDevice failed.\n",stderr);
  68.         ExitProcess(95);
  69.     }
  70. }
  71.  
  72. void __fastcall show(void)
  73. {
  74. register unsigned short i;
  75. char *DeviceBuffer;
  76. unsigned short MaxDevicelist;
  77. char *Devicelist[MaxDosDevices];
  78. char *DeviceItem;
  79. char *CurrentItem;
  80.  
  81.     chmalloc(&DeviceBuffer,MaxDeviceBuffer);
  82.     chmalloc(&DeviceItem,MaxDeviceItem);
  83.  
  84.     MyQueryDosDevice(NULL,DeviceBuffer,MaxDeviceBuffer);
  85.  
  86.     index(DeviceBuffer,Devicelist,&MaxDevicelist);
  87.     MaxDevicelist=sort(Devicelist,MaxDevicelist);
  88.     for (i=0;i<=MaxDevicelist;i++) {
  89.         MyQueryDosDevice(Devicelist[i],DeviceItem,MaxDeviceItem);
  90.         CurrentItem=DeviceItem;
  91.         while (CurrentItem[0] != 0) {
  92.             if (DeviceItem == CurrentItem)
  93.                 printf("%s => %s\n",Devicelist[i],CurrentItem);
  94.             else
  95.                 printf("      %s\n",CurrentItem);
  96.             CurrentItem+=lstrlen(CurrentItem)+1;
  97.         }
  98.     }
  99.     free(DeviceBuffer);
  100.     free(DeviceItem);
  101. }
  102.  
  103. __inline void __fastcall DeleteDefinition(const char *drive)
  104. {
  105. char *DeviceItem;
  106.  
  107.     chmalloc(&DeviceItem,MaxDeviceItem);
  108.     do {
  109.         if (!DefineDosDevice(DDD_REMOVE_DEFINITION,drive,NULL)) {
  110.             fputs("Remove definition failed.\n",stderr);
  111.             ExitProcess(5);
  112.         }
  113.     } while (zap && QueryDosDevice(drive,DeviceItem,MaxDeviceItem));
  114.     free(DeviceItem);
  115. }
  116.  
  117. void __fastcall Usage(const char *a0)
  118. {
  119.     fprintf(stderr,"Usage: %s [-u | -z] [<drive>: [<Device>]]\n\n",a0);
  120.     fprintf(stderr,"Examples: %s J: \\Dosdevices\\C:\\DOS\n",a0);
  121.     fprintf(stderr,"          %s K: \\Device\\Harddisk0\\Partition1\n",a0);
  122.     fprintf(stderr,"          %s -u L: (deletes current definition of L:)\n",a0);
  123.     fprintf(stderr,"          %s -z M: (deletes all definitions of M:\n",a0);
  124.     ExitProcess(87);
  125. }
  126.  
  127. int main(int argc, char *argv[], char *envp[])
  128. {
  129.  
  130.     switch (argc) {
  131.         case 1:
  132.             show();
  133.             break;
  134.         case 3:
  135.             switch (*argv[1]) {
  136.                 case '-':
  137.                 case '/':
  138.                     if (argv[1][2] != 0) Usage(argv[0]);
  139.                     switch (argv[1][1]|0x20) {
  140.                         case 'z':
  141.                             zap = TRUE;
  142.                             break;
  143.                         case 'u':
  144.                             break;
  145.                         default:
  146.                             Usage(argv[0]);
  147.                             break;
  148.                     }
  149.                     if (!ValidDrive(argv[2])) Usage(argv[0]);
  150.                     DeleteDefinition(argv[2]);
  151.                     break;
  152.                 default:
  153.                     if (!ValidDrive(argv[1])) Usage(argv[0]);
  154.                     DefineDosDevice(DDD_RAW_TARGET_PATH,argv[1],argv[2]);
  155.                     break;
  156.             }
  157.             break;
  158.         default:
  159.             Usage(argv[0]);
  160.         }
  161.     return 0;
  162. }
  163.