home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / DropFTP / MacTCP files / dnr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-19  |  6.6 KB  |  296 lines  |  [TEXT/MPS ]

  1. /*     DNR.c - DNR library for MPW
  2.  
  3.     (c) Copyright 1988 by Apple Computer.  All rights reserved
  4.     
  5.     Modifications by Jim Matthews, Dartmouth College, 5/91
  6.     
  7. */
  8.  
  9. /* Following two lines added by JLN to make this file compile with Think C 6.0.1 */
  10. #pragma options (!require_protos)
  11. #define _GestaltDispatch                0xA0AD
  12.  
  13. #include <OSUtils.h>
  14. #include <Errors.h>
  15. #include <Files.h>
  16. #include <Resources.h>
  17. #include <Memory.h>
  18. #include <Traps.h>
  19. #include <GestaltEqu.h>
  20. #include <Folders.h>
  21. #include <ToolUtils.h>
  22.  
  23. #define OPENRESOLVER    1L
  24. #define CLOSERESOLVER    2L
  25. #define STRTOADDR        3L
  26. #define    ADDRTOSTR        4L
  27. #define    ENUMCACHE        5L
  28. #define ADDRTONAME        6L
  29. #define    HINFO            7L
  30. #define MXINFO            8L
  31.  
  32. Handle codeHndl = nil;
  33.  
  34. typedef OSErr (*OSErrProcPtr)(long,...);
  35. OSErrProcPtr dnr = nil;
  36.  
  37.  
  38. TrapType GetTrapType(theTrap)
  39. unsigned long theTrap;
  40. {
  41.     if (BitAnd(theTrap, 0x0800) > 0)
  42.         return(ToolTrap);
  43.     else
  44.         return(OSTrap);
  45.     }
  46.     
  47. Boolean TrapAvailable(trap)
  48. unsigned long trap;
  49. {
  50. TrapType trapType = ToolTrap;
  51. unsigned long numToolBoxTraps;
  52.  
  53.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  54.         numToolBoxTraps = 0x200;
  55.     else
  56.         numToolBoxTraps = 0x400;
  57.  
  58.     trapType = GetTrapType(trap);
  59.     if (trapType == ToolTrap) {
  60.         trap = BitAnd(trap, 0x07FF);
  61.         if (trap >= numToolBoxTraps)
  62.             trap = _Unimplemented;
  63.         }
  64.     return(NGetTrapAddress(trap, trapType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  65.  
  66. }
  67.  
  68. void GetSystemFolder(short *vRefNumP, long *dirIDP)
  69. {
  70.     SysEnvRec info;
  71.     long wdProcID;
  72.     
  73.     SysEnvirons(1, &info);
  74.     if (GetWDInfo(info.sysVRefNum, vRefNumP, dirIDP, &wdProcID) != noErr) {
  75.         *vRefNumP = 0;
  76.         *dirIDP = 0;
  77.         }
  78.     }
  79.  
  80. void GetCPanelFolder(short *vRefNumP, long *dirIDP)
  81. {
  82.     Boolean hasFolderMgr = false;
  83.     long feature;
  84.     
  85.     if (TrapAvailable(_GestaltDispatch)) if (Gestalt(gestaltFindFolderAttr, &feature) == noErr) hasFolderMgr = true;
  86.     if (!hasFolderMgr) {
  87.         GetSystemFolder(vRefNumP, dirIDP);
  88.         return;
  89.         }
  90.     else {
  91.         if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, vRefNumP, dirIDP) != noErr) {
  92.             *vRefNumP = 0;
  93.             *dirIDP = 0;
  94.             }
  95.         }
  96.     }
  97.     
  98. /* SearchFolderForDNRP is called to search a folder for files that might 
  99.     contain the 'dnrp' resource */
  100. short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID)
  101. {
  102.     HParamBlockRec fi;
  103.     Str255 filename;
  104.     short refnum;
  105.     
  106.     fi.fileParam.ioCompletion = nil;
  107.     fi.fileParam.ioNamePtr = filename;
  108.     fi.fileParam.ioVRefNum = vRefNum;
  109.     fi.fileParam.ioDirID = dirID;
  110.     fi.fileParam.ioFDirIndex = 1;
  111.     
  112.     while (PBHGetFInfo(&fi, false) == noErr) {
  113.         /* scan system folder for driver resource files of specific type & creator */
  114.         if (fi.fileParam.ioFlFndrInfo.fdType == targetType &&
  115.             fi.fileParam.ioFlFndrInfo.fdCreator == targetCreator) {
  116.             /* found the MacTCP driver file? */
  117.             refnum = HOpenResFile(vRefNum, dirID, filename, fsRdPerm);
  118.             if (GetIndResource('dnrp', 1) == NULL)
  119.                 CloseResFile(refnum);
  120.             else
  121.                 return refnum;
  122.             }
  123.         /* check next file in system folder */
  124.         fi.fileParam.ioFDirIndex++;
  125.         fi.fileParam.ioDirID = dirID;    /* PBHGetFInfo() clobbers ioDirID */
  126.         }
  127.     return(-1);
  128.     }    
  129.  
  130. /* OpenOurRF is called to open the MacTCP driver resources */
  131.  
  132. short OpenOurRF()
  133. {
  134.     short refnum;
  135.     short vRefNum;
  136.     long dirID;
  137.     
  138.     /* first search Control Panels for MacTCP 1.1 */
  139.     GetCPanelFolder(&vRefNum, &dirID);
  140.     refnum = SearchFolderForDNRP('cdev', 'ztcp', vRefNum, dirID);
  141.     if (refnum != -1) return(refnum);
  142.         
  143.     /* next search System Folder for MacTCP 1.0.x */
  144.     GetSystemFolder(&vRefNum, &dirID);
  145.     refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
  146.     if (refnum != -1) return(refnum);
  147.         
  148.     /* finally, search Control Panels for MacTCP 1.0.x */
  149.     GetCPanelFolder(&vRefNum, &dirID);
  150.     refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
  151.     if (refnum != -1) return(refnum);
  152.         
  153.     return -1;
  154.     }    
  155.  
  156.  
  157. OSErr OpenResolver(fileName)
  158. char *fileName;
  159. {
  160.     short refnum;
  161.     OSErr rc;
  162.     
  163.     if (dnr != nil)
  164.         /* resolver already loaded in */
  165.         return(noErr);
  166.         
  167.     /* open the MacTCP driver to get DNR resources. Search for it based on
  168.        creator & type rather than simply file name */    
  169.     refnum = OpenOurRF();
  170.  
  171.     /* ignore failures since the resource may have been installed in the 
  172.        System file if running on a Mac 512Ke */
  173.        
  174.     /* load in the DNR resource package */
  175.     codeHndl = GetIndResource('dnrp', 1);
  176.     if (codeHndl == nil) {
  177.         /* can't open DNR */
  178.         return(ResError());
  179.         }
  180.     
  181.     DetachResource(codeHndl);
  182.     if (refnum != -1) {
  183.         CloseWD(refnum);
  184.         CloseResFile(refnum);
  185.         }
  186.         
  187.     /* lock the DNR resource since it cannot be reloated while opened */
  188.     HLock(codeHndl);
  189.     dnr = (OSErrProcPtr) *codeHndl;
  190.     
  191.     /* call open resolver */
  192.     rc = (*dnr)(OPENRESOLVER, fileName);
  193.     if (rc != noErr) {
  194.         /* problem with open resolver, flush it */
  195.         HUnlock(codeHndl);
  196.         DisposHandle(codeHndl);
  197.         dnr = nil;
  198.         }
  199.     return(rc);
  200.     }
  201.  
  202.  
  203. OSErr CloseResolver()
  204. {
  205.     if (dnr == nil)
  206.         /* resolver not loaded error */
  207.         return(notOpenErr);
  208.         
  209.     /* call close resolver */
  210.     (void) (*dnr)(CLOSERESOLVER);
  211.  
  212.     /* release the DNR resource package */
  213.     HUnlock(codeHndl);
  214.     DisposHandle(codeHndl);
  215.     dnr = nil;
  216.     return(noErr);
  217.     }
  218.  
  219. OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
  220. char *hostName;
  221. struct hostInfo *rtnStruct;
  222. long resultproc;
  223. char *userDataPtr;
  224. {
  225.     if (dnr == nil)
  226.         /* resolver not loaded error */
  227.         return(notOpenErr);
  228.         
  229.     return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  230.     }
  231.     
  232. OSErr AddrToStr(addr, addrStr)
  233. unsigned long addr;
  234. char *addrStr;                                    
  235. {
  236.     if (dnr == nil)
  237.         /* resolver not loaded error */
  238.         return(notOpenErr);
  239.         
  240.     (*dnr)(ADDRTOSTR, addr, addrStr);
  241.     return(noErr);
  242.     }
  243.     
  244. OSErr EnumCache(resultproc, userDataPtr)
  245. long resultproc;
  246. char *userDataPtr;
  247. {
  248.     if (dnr == nil)
  249.         /* resolver not loaded error */
  250.         return(notOpenErr);
  251.         
  252.     return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
  253.     }
  254.     
  255.     
  256. OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
  257. unsigned long addr;
  258. struct hostInfo *rtnStruct;
  259. long resultproc;
  260. char *userDataPtr;                                    
  261. {
  262.     if (dnr == nil)
  263.         /* resolver not loaded error */
  264.         return(notOpenErr);
  265.         
  266.     return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  267.     }
  268.  
  269.  
  270. extern OSErr HInfo(hostName, returnRecPtr, resultProc, userDataPtr)
  271. char *hostName;
  272. struct returnRec *returnRecPtr;
  273. long resultProc;
  274. char *userDataPtr;
  275. {
  276.     if (dnr == nil)
  277.         /* resolver not loaded error */
  278.         return(notOpenErr);
  279.         
  280.     return((*dnr)(HINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  281.  
  282.     }
  283.     
  284. extern OSErr MXInfo(hostName, returnRecPtr, resultProc, userDataPtr)
  285. char *hostName;
  286. struct returnRec *returnRecPtr;
  287. long resultProc;
  288. char *userDataPtr;
  289. {
  290.     if (dnr == nil)
  291.         /* resolver not loaded error */
  292.         return(notOpenErr);
  293.         
  294.     return((*dnr)(MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  295.  
  296.     }