home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / DataScope 2.0.3 / DataScope2l / TCPSource / macutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  9.8 KB  |  506 lines  |  [TEXT/MPS ]

  1. /*  MACUTIL.C
  2. *   Utilities for the network library that are Macintosh specific
  3. */
  4. #include "stdio.h"
  5. #include "memory.h"
  6.  
  7. #ifdef UNDERNOCIRCUMSTANCES
  8. #include "protocol.h"
  9. #include "data.h"
  10. #endif
  11.  
  12. #ifdef MPW
  13. #include "mpw.h"
  14. #endif
  15.  
  16. #include <Files.h>
  17. #include <Types.h>
  18. #include <appletalk.h>
  19. #include <events.h>
  20. #include <errors.h>
  21. #include <OSutils.h>
  22. #include "whatami.h"
  23.  
  24. /* Some globals for file lookup */
  25. int defaultv;
  26.  
  27. ParamBlockRec FLpb;
  28. CInfoPBRec HFLpb, tHFLpb;
  29. char FileName[256], FileTemplate[256];
  30. int DirTree[256];
  31. char *iobufptr=0L,*iobuf=0L,*iobufmax=0L;
  32. int iobufsize,lastcr;
  33.  
  34. int getWDnum()
  35. {
  36.     WDPBRec pb;
  37.  
  38.     pb.ioNamePtr=0L;
  39.  
  40.     if (isHFS())  PBHGetVol(&pb,FALSE);
  41.         else PBGetVol( (ParmBlkPtr) &pb,FALSE);
  42.     return(pb.ioWDVRefNum);        /* This proc returns the Vol reference */
  43. }
  44.  
  45. Str255 *getVname()
  46. {
  47.     WDPBRec pb;
  48.     char *temp;
  49.  
  50.     temp= (char *)NewPtr(256);
  51.  
  52.     pb.ioNamePtr=temp;
  53.     PBGetVol( (ParmBlkPtr) &pb,FALSE);
  54.     (*temp)++;
  55.     temp[*temp]=':';
  56.     temp[*temp+1]=0;
  57.     return( (Str255 *)temp);        /* This proc returns the Vol reference */
  58. }
  59.  
  60. Str255 *getWDname()
  61. {
  62.     WDPBRec pb;
  63.     CInfoPBRec cpb;
  64.     char tempst[100],*wdtemp,*wdout,*start,*store,*trav;
  65.     int i,j;
  66.  
  67.     if (!isHFS()) return(getVname()); /* if not HFS then only volume exists */
  68.  
  69.     wdout= (char *) NewPtr(256);
  70.     wdtemp= (char *) NewPtr(256);
  71.  
  72.     sprintf(tempst,"ioNamePtr = %lx",wdout);
  73.     putln (tempst);
  74.  
  75.     pb.ioNamePtr=wdout;
  76.     PBHGetVol(&pb,FALSE);
  77.  
  78. #ifdef WANNACOLON
  79.     (*wdout)++;
  80.     wdout[*wdout]=':';
  81. #endif WANNACOLON
  82.  
  83.     cpb.dirInfo.ioCompletion=0L;
  84.     cpb.dirInfo.ioVRefNum=pb.ioWDVRefNum;
  85.     cpb.dirInfo.ioDrDirID=pb.ioWDDirID;
  86.     trav=wdtemp;
  87.     DirTree[ (j=0) ]=pb.ioVRefNum;
  88.  
  89.     while(cpb.dirInfo.ioDrDirID!=0) {
  90.         cpb.hFileInfo.ioNamePtr=(StringPtr) trav;
  91.         cpb.hFileInfo.ioFDirIndex=(short) -1;
  92.         if (PBGetCatInfo(&cpb,FALSE)!=0)  {cpb.dirInfo.ioDrDirID=0; break;}
  93.         if (*trav==0) {cpb.dirInfo.ioDrDirID=0;break;}
  94.         i=*trav; *trav=0; start=trav+1;
  95.         trav+=i+1; *trav=0;
  96.         DirTree[j++]=cpb.dirInfo.ioDrDirID;
  97.         cpb.dirInfo.ioDrDirID=cpb.dirInfo.ioDrParID;
  98.         }
  99.  
  100.     *wdtemp=0;
  101.     store=wdout+*wdout+1;
  102.     if (trav-wdtemp) *wdout=(trav-wdtemp);
  103.     if (trav!=wdtemp) start=trav-1; else start=wdtemp;
  104.  
  105.     if (start!=wdtemp) {
  106.         while(*start) start--;        /* Go to beginning of string */
  107.         if (start!=wdtemp) start--;
  108.         }
  109.  
  110.     while(start!=wdtemp) {
  111.         while(*start) start--;        /* Go to beginning of string */
  112.         trav=start+1; *store='/';    /* Ready to move directory name */
  113.         store++;
  114.         while (*trav) {*store=*trav; store++;trav++; } /* store it */
  115.         if (start!=wdtemp) start --;
  116.         *store=0;
  117.         }
  118.     DisposPtr ((Ptr)wdtemp);
  119.     return( (Str255 *)wdout);
  120. }
  121.  
  122. setWDnum( num)
  123. int num;
  124. {
  125.     WDPBRec pb;
  126.  
  127.     pb.ioNamePtr=0L; 
  128.     pb.ioVRefNum=num;
  129.     pb.ioWDDirID=0L;
  130.     PBHSetVol(&pb,FALSE);
  131. }
  132.  
  133. /*
  134. *  setSFdir
  135. *  trick to set up the working directory from the last SFgetfile. TK
  136. */
  137.  
  138. #define SFSaveDisk  0x214
  139. #define CurDirStore 0x398
  140.  
  141. setSFdir( )
  142. {
  143.     WDPBRec pb;
  144.  
  145.     if (!isHFS()) {
  146.         defaultv = - (* (short *) SFSaveDisk);
  147.         return;
  148.     }
  149.     
  150.     pb.ioNamePtr=0L; 
  151.     pb.ioWDProcID=0L; 
  152.     pb.ioVRefNum=  - (* (short *) SFSaveDisk);        /* saved from last SF package call */
  153.     pb.ioWDDirID= (* (int *) CurDirStore);
  154.     PBOpenWD(&pb,FALSE);
  155.     
  156.     defaultv = pb.ioVRefNum;
  157.     setvol(NULL, defaultv);            /* make working directory */
  158.  
  159. }
  160.  
  161. setWDname( num, name)
  162. int num;
  163. Str255 *name;
  164. {
  165.     WDPBRec pb;
  166.  
  167.     pb.ioNamePtr=0L; 
  168.     pb.ioVRefNum=num;
  169.     pb.ioWDDirID=0L;
  170.     PBHSetVol(&pb,FALSE);
  171. }
  172.  
  173. wccheck(file,template)
  174. char *template,*file;
  175. {
  176.     while(*template) {
  177.         if (*template=='*') {
  178.             template++;
  179.             if (*template) {
  180.                 while((*file) && (*file!=*template)) file++;
  181.                 if ((*file)==0) return(0);
  182.                 }
  183.             else return(1);
  184.             continue;
  185.             }
  186.         else
  187.             if ((*template!='?') && (*template!=*file)) return(0);
  188.         template++;file++;
  189.         }
  190.     return(1);
  191. }
  192.  
  193. /**********************************************************************/
  194. /*
  195. *   Find directory name -- return a code that indicates whether the
  196. *   directory exists or not.
  197. *   0 = dir name ok
  198. *   -1 = error
  199. *   > 0 = dos error code, no dir by this name
  200. *
  201. *   Accept certain unix conventions, like '/' for separator
  202. *
  203. *   Also, append a '\' to the name before returning
  204. *  Note:  There must be enough room in the string to append the '\'
  205. */
  206.  
  207. direxist(dirname)
  208.     char dirname[];
  209.     {
  210.     return(-1);
  211.     }
  212.  
  213. /**********************************************************************/
  214. /* firstname
  215. *  find the first name in the given directory which matches the wildcard
  216. *  specification
  217. *
  218. *  must NewPtr enough space for the path plus a full filename
  219. *
  220. *  expand '*' (unix) to '*.*' (dos)
  221. */
  222. char *firstname(spec)
  223. char *spec;
  224. {
  225.     char *loader;
  226.     int HFS;
  227.  
  228.     setvol(0L, defaultv);                            /* Go to default Directory */
  229.  
  230.     if ( (HFS=isHFS()) )
  231.         PBHGetVol( (WDPBPtr) &HFLpb, FALSE);
  232.     else
  233.          PBGetVol(&FLpb,FALSE);
  234.  
  235.     loader=FileTemplate;
  236.     while(*loader++=*spec++);
  237.     putln(spec);
  238.     putln(FileTemplate);
  239.     if ( HFS) {
  240.         HFLpb.hFileInfo.ioCompletion=(ProcPtr) NULL;
  241.         HFLpb.hFileInfo.ioNamePtr=(StringPtr) FileName;
  242.         HFLpb.hFileInfo.ioFVersNum=(char) 0;
  243.         HFLpb.hFileInfo.ioFDirIndex=(short) 1;
  244.         tHFLpb=HFLpb;
  245.         if (PBGetCatInfo(&HFLpb,FALSE)!=0)  return(0L);
  246.         }
  247.     else {
  248.         FLpb.fileParam.ioCompletion=0;
  249.         FLpb.fileParam.ioNamePtr=FileName;
  250.         FLpb.fileParam.ioFVersNum=0;
  251.         FLpb.fileParam.ioFDirIndex=1;
  252.         if(PBGetFInfo(&FLpb,FALSE)!=0) return(0L);
  253.         }
  254.     FileName[FileName[0]+1]=0;
  255.     while(!wccheck(&FileName[1],FileTemplate)) {
  256.         if (HFS) {
  257.             tHFLpb.hFileInfo.ioFDirIndex++;
  258.             HFLpb=tHFLpb;
  259.             if (PBGetCatInfo(&HFLpb,FALSE)!=0)  return(0L);
  260.             }
  261.         else {
  262.             FLpb.fileParam.ioFDirIndex++;
  263.             if(PBGetFInfo(&FLpb,FALSE)!=0) return(0L);
  264.             }
  265.         FileName[FileName[0]+1]=0;
  266.         }
  267.  
  268.     if (isHFS() && ((HFLpb.hFileInfo.ioFlAttrib) & 16))  {
  269.         FileName[++FileName[0]]='/';
  270.         FileName[FileName[0]+1]=0;
  271.         }
  272.  
  273.     return(&FileName[1]);
  274. }
  275.  
  276. /**********************************************************************/
  277. /* nextname
  278. *  modify the path spec to contain the next file name in the
  279. *  sequence as given by DOS
  280. *
  281. *  if at the end of the sequence, return NULL
  282. */
  283. char *nextname()
  284. {
  285.  
  286.     if (isHFS() ) {
  287.         tHFLpb.hFileInfo.ioFDirIndex++;
  288.         HFLpb=tHFLpb;
  289.         if (HFLpb.hFileInfo.ioNamePtr!=FileName) return(0L);
  290.         if (PBGetCatInfo(&HFLpb,FALSE)!=0)  return(0L);
  291.         FileName[FileName[0]+1]=0;
  292.         while(!wccheck(&FileName[1],FileTemplate)) {
  293.             putln(&FileName[1]);
  294.             tHFLpb.hFileInfo.ioFDirIndex++;
  295.             HFLpb=tHFLpb;
  296.             if (PBGetCatInfo(&HFLpb,FALSE)!=0)  return(0L);
  297.             FileName[FileName[0]+1]=0;
  298.             }
  299.         }
  300.     else {
  301.         if (FLpb.fileParam.ioNamePtr!=FileName) return(0L);
  302.         FLpb.fileParam.ioFDirIndex++;
  303.         if(PBGetFInfo(&FLpb,FALSE)!=0) return(0L);
  304.         FileName[FileName[0]+1]=0;
  305.         while(!wccheck(&FileName[1],FileTemplate)) {
  306.             FLpb.fileParam.ioFDirIndex++;
  307.             if(PBGetFInfo(&FLpb,FALSE)!=0) return(0L);
  308.             FileName[FileName[0]+1]=0;
  309.             }
  310.         }
  311.  
  312.     if (isHFS() && (HFLpb.hFileInfo.ioFlAttrib & 16))  {
  313.         FileName[++FileName[0]]='/';
  314.         FileName[FileName[0]+1]=0;
  315.         }
  316.  
  317.     return(&FileName[1]);
  318. }
  319.  
  320.  
  321. /************************************************************************/
  322. /* userabort
  323. *  check to see if the user wants to abort
  324. *  For the PC, just see if the user has pressed ESC
  325. *  return true if the user wants to abort
  326. */
  327. userabort()
  328. {
  329.     EventRecord theEvent;
  330.  
  331.     while (GetNextEvent(24,&theEvent)) {
  332.         if ((theEvent.modifiers & cmdKey) &&
  333.             (theEvent.message & 0xff) =='.') 
  334.             return(-1);
  335.         }
  336.     return(0);
  337. }
  338.  
  339. dopwd(where,howmuch)
  340. char *where;
  341. int howmuch;
  342. {
  343.     char *temp;
  344.     int i;
  345.  
  346.     if (howmuch<255) return(-1);
  347.     setvol(0L, defaultv);                            /* Go to default Directory */
  348.     temp= (char *)getWDname();
  349.     where[0]='/';
  350.     for( i=1;i<=temp[0];i++) where[i]=temp[i];
  351.     where[temp[0]]=0;
  352.     putln(&temp[1]);
  353.     DisposPtr ((Ptr)temp);
  354.     putln(where);
  355.     return(0);
  356. }
  357.  
  358.  
  359. chgdir( modifier)
  360. char *modifier;
  361. {
  362.     WDPBRec pb;
  363.     char tempst[256], *nSlash, *start;
  364.     int i=0, j=0;
  365.  
  366.     if (! isHFS()) return(-1);
  367.  
  368.     setvol(0L, defaultv);                            /* Go to default Directory */
  369.  
  370.     start= (char *)getWDname();
  371.     DisposPtr ((Ptr)start);
  372.  
  373.     start=modifier;
  374.     tempst[0]=':';
  375.  
  376.     putln(start);
  377.  
  378.     if ( strcmp("..",modifier)==0) {
  379.         modifier[0]=0;
  380.         j++;
  381.         }
  382.     else
  383.     while ( (nSlash = (char *)index(modifier,'/') ) !=0L) {
  384.         if (( (nSlash -modifier) ==2) && (*modifier=='.') && (*(modifier+1) =='.') ) {
  385.             modifier +=3L;
  386.             start=modifier;
  387.             j++;
  388.             }
  389.         else {
  390.             *nSlash=':';
  391.             modifier=nSlash+1L;
  392.             }
  393.         }
  394.  
  395.     if ( *start==':') {
  396.         strcpy( tempst, start+1L);
  397.         j= strlen( tempst);
  398.         if (tempst[j-1] !=':') {
  399.             tempst[j] = ':';
  400.             tempst[j+1]='\000';
  401.             }
  402.         }
  403.     else
  404.         strcpy( tempst+1L, start);
  405.  
  406.     putln(tempst);
  407.  
  408.     pb.ioNamePtr=ctop(tempst);
  409. #ifdef MPW
  410.     c2pstr(tempst);
  411. #endif
  412.     pb.ioVRefNum=0;
  413.     pb.ioWDDirID=DirTree[j];
  414.     pb.ioCompletion=0L;
  415.     pb.ioWDProcID='NCSA';
  416.  
  417.     j = PBOpenWD( &pb,0);
  418.  
  419.     if ( (j==-35) || (j==-43) ) return(1);
  420.  
  421.     j = setvol(0L, (defaultv=pb.ioVRefNum));
  422.  
  423.     if ( (j==-35) || (j==-43) ) return(1);
  424.  
  425.     return(0);
  426. }
  427.  
  428. /*
  429.  *
  430.  */
  431.  
  432. Scolorset( ip, s)
  433. char *s;
  434. unsigned int *ip;
  435. {
  436.     if (3==sscanf(s,"{%d,%d,%d}", &ip[0], &ip[1],&ip[2]) )
  437.         return(1);
  438.     return(0);
  439. }
  440.  
  441. long time(x)
  442. long x;
  443. {
  444.     return( TickCount()/60);
  445. }
  446.  
  447. int memcmp( a, b, len)
  448. char *a, *b;
  449. int len;
  450. {
  451.     while (len --)
  452.         if (*a!=*b) 
  453.             return(1);
  454.     return(0);
  455. }
  456.  
  457. /**************************************************************************/
  458. /*  defdir and sysdir
  459. *   Use to change to and from the system folder directory.
  460. */
  461. #define CurrentVersion 1            /* Last known SysEnvirons version */
  462.  
  463. sysdir()
  464. {
  465.     int err;
  466.     char s[50];
  467.     SysEnvRec theWorld;                /* System Environment record */
  468.     
  469.     err = SysEnvirons(CurrentVersion, &theWorld);
  470.     if (err == envVersTooBig) {
  471.         /* should post a message saying we need to be updated */
  472.         putln("SysEnvirons out of date - macutil.c");
  473.     }
  474.     if (err != noErr)                /* possibly not available assume nothing here! */
  475.         return(0);
  476.  
  477.     (void) setvol(NULL, theWorld.sysVRefNum);    /* change default volume */
  478.     sprintf(s,"set sysdir: %d",theWorld.sysVRefNum);
  479.     putln(s);
  480.     
  481. }
  482.  
  483. setmydir()
  484. {
  485.     char s[50];
  486.     
  487.     (void) setvol(NULL, defaultv);
  488.     sprintf(s,"set defdir: %d",defaultv);
  489.     putln(s);
  490.     return(defaultv);
  491. }
  492.  
  493. makemydir(dir)
  494.     int dir;
  495. {
  496.     char s[50];
  497.     
  498.     defaultv = dir;
  499.     setvol(NULL, defaultv);
  500.     sprintf(s,"make dir: %d",defaultv);
  501.     putln(s);
  502.     return(defaultv);
  503.     
  504. }
  505.