home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / ncsa_tel / contribu / byu_tel2.hqx / tcpip / macutil.c < prev    next >
Text File  |  1990-04-11  |  11KB  |  526 lines

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