home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 074.lha / AssignDevice / misc.c < prev   
C/C++ Source or Header  |  1986-11-20  |  7KB  |  326 lines

  1. /* misc.c 
  2.  *      Misc. subroutines that like to live together...
  3.  *
  4.  * Phillip Lindsay (c) 1987 Commodore-Amiga - This code may be freely used 
  5.  * as long as the copyright notice is left intact.
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/nodes.h>
  10. #include <exec/lists.h>
  11. #include <exec/memory.h>
  12. #include <exec/ports.h>
  13. #include <exec/libraries.h>
  14. #include <exec/devices.h>
  15. #include <exec/io.h>
  16. #include <devices/console.h>
  17. #include <intuition/intuition.h>
  18. #include <libraries/dos.h>
  19. #include <libraries/dosextens.h>
  20. #include <libraries/filehandler.h>
  21.  
  22. #ifdef AZTEC_C
  23. #include <functions.h>
  24. #endif
  25.  
  26. #define MKBPTR(x)       ((BPTR)((long)x >> 2))
  27.  
  28. /* btoc() takes a pointer to a BSTR and converts it to a
  29.  * C string.
  30.  */
  31. char *btoc(bstring)
  32. CPTR bstring;
  33. {
  34.  register UBYTE len,count,*cstring=NULL;
  35.  
  36.  if(bstring)
  37.  {
  38.     cstring = (UBYTE *) bstring;
  39.     len = cstring[0];              
  40.     for(count=0;count < len;count++)
  41.         cstring[count] = cstring[count+1];
  42.         
  43.     cstring[count] = '\0';
  44.  }
  45.  
  46.  
  47. return((char *)cstring);
  48. }
  49.  
  50. /* ctob() takes a pointer to a C string and converts 
  51.  * it to a BSTR.
  52.  */
  53. BSTR ctob(cstring)
  54. UBYTE *cstring;
  55. {
  56.  extern   UBYTE strlen();
  57.  register UBYTE len,count; 
  58.  
  59.  if(cstring)
  60.  {
  61.     len = strlen(cstring);              
  62.     for(count=len;count > (UBYTE)0;count--)
  63.         cstring[count] = cstring[count-1];
  64.     cstring[count] = len;
  65.  }
  66.  
  67.  return((BSTR)MKBPTR(cstring));
  68. }
  69.  
  70. /*
  71.  * memcpy() - fast "C" byte memory copy.
  72.  */
  73. VOID memcpy(source,dest,size)
  74. UBYTE *source,*dest;
  75. ULONG size;
  76. {
  77.     register UBYTE  *msource=source,
  78.                     *mdest=dest;
  79.     register ULONG  msize=size;
  80.  
  81.     if(msize)
  82.     {
  83.         do { *mdest++ = *msource++; } while(--msize);
  84.     }
  85.         
  86. }
  87.  
  88. /*
  89.  * memfill() - fill memory with supplied byte value for "size" bytes
  90.  */
  91. VOID memfill(source,size,value)
  92. UBYTE *source;
  93. ULONG size;
  94. UBYTE value;    
  95. {
  96.     register UBYTE  *msource=source,
  97.                     mvalue=value,
  98.                     *mend;
  99.  
  100.  for(mend = &source[size]; msource < mend ;msource++)
  101.     *msource = mvalue;
  102. }
  103.  
  104.  
  105. /*
  106.  * strupper() - return given string in upper case;
  107.  *
  108.  */ 
  109.  
  110. char *strupper(astring)
  111. char *astring; 
  112. {
  113.     register char *astr = astring;
  114.     
  115.     if(astr)
  116.     {
  117.         do
  118.         {
  119.             *astr = toupper(*astr);
  120.         } while(*astr++);
  121.     }
  122.  
  123.     return(astring);    
  124. }
  125.  
  126. /*
  127.  * trunc() - truncate string at specified character
  128.  *
  129.  */
  130.  VOID trunc(astring,acharacter)
  131.  char *astring,acharacter;
  132.  {
  133.     register char *astr=astring,achar=acharacter;
  134.     
  135.     do
  136.     {
  137.         if( *astr == achar )
  138.         {
  139.             *astr = '\0';
  140.             break;
  141.         }
  142.     } while(*astr++);       
  143.  }
  144.  
  145.  
  146. /*
  147.  * allocstr() - copies a string into a newly allocated memory space.
  148.  *
  149.  */
  150. char *allocstr(astring,length) 
  151. char    *astring;
  152. ULONG   length;
  153. {
  154.     register    char    *astr=astring,
  155.                         *newstr=NULL;
  156.     register    ULONG   slength=length;
  157.     
  158.     if( (astr) && (slength) )
  159.     {
  160.         if( ( newstr = ( char * ) AllocMem( slength, MEMF_PUBLIC) ) )
  161.             memcpy(astr,newstr,slength);
  162.     }
  163.     
  164.     
  165.     return(newstr);
  166. }
  167.  
  168.  
  169. #define KEYWORD ( (ULONG) ('C' << 24) | ('O' << 16) | ('P' << 8) | ('Y') )
  170.  
  171. /*
  172.  * check to see if device node was created by dupdev().
  173.  */
  174.  BOOL ourdevnode(dnode)
  175.  struct DeviceNode *dnode;
  176.  {
  177.     register    UBYTE   length;
  178.     char                *bname;
  179.     ULONG               key;
  180.  
  181.     
  182.     bname   = (char *)  BADDR(dnode->dn_Name);
  183.     length  = (UBYTE)   bname[0] + 1;
  184.     key     =           KEYWORD;
  185.         
  186.     return( (BOOL) ( !strncmp(&key,&bname[length],(ULONG)sizeof(ULONG)) ? TRUE :
  187.  FALSE ) );
  188.  }
  189.     
  190.  
  191. /* 
  192.  * free memory we allocated for duplicate device node. 
  193.  */  
  194. VOID freedev(dnode)
  195. struct DeviceNode *dnode;
  196. {
  197.     register    char    *devname;
  198.     register    UBYTE   length;
  199.     
  200.     if(dnode->dn_Name)
  201.     {
  202.         devname =   btoc( BADDR(dnode->dn_Name) );
  203.         length  =   (UBYTE) strlen(devname);
  204.         length +=   5;                  
  205.         FreeMem(devname,(ULONG) length);
  206.     }
  207.         
  208.     FreeMem( dnode, (ULONG) sizeof(*dnode) );   
  209. }
  210.  
  211.  
  212. /*
  213.  *  Add a dos node to the system device list
  214.  *
  215.  */
  216. VOID adddosnode(adnode)
  217. struct DeviceNode *adnode;
  218. {
  219.     extern   struct DosLibrary *DOSBase;
  220.     register struct RootNode   *rnode;
  221.     register struct DosInfo    *dinfo;
  222.     register struct DeviceNode *dnode;  
  223.      
  224.     rnode   = (struct RootNode *)  DOSBase->dl_Root;        /* find root node */
  225.     dinfo   = (struct DosInfo  *)  BADDR(rnode->rn_Info);   /* now dos info   */
  226.     
  227.     adnode->dn_Next = NULL;
  228.  
  229.     Forbid();
  230.     for(dnode = (struct DeviceNode *) BADDR(dinfo->di_DevInfo);dnode->dn_Next;
  231.       dnode = (struct DeviceNode *) BADDR(dnode->dn_Next))
  232.         ; /* do nothing but traverse the list */
  233.                 
  234.     dnode->dn_Next = MKBPTR(adnode);
  235.     Permit();        
  236. }
  237.  
  238.  
  239. /*
  240.  *  find a dos node in the system device list
  241.  *
  242.  */
  243. BOOL finddosnode(lookfor)
  244. char    *lookfor;
  245. {
  246.     extern   struct DosLibrary *DOSBase;
  247.     register struct RootNode   *rnode;
  248.     register struct DosInfo    *dinfo;
  249.     register struct DeviceNode *dnode;
  250.     register char              *bname;  
  251.     char                        name1[81];
  252.     char                        name2[81];
  253.     BOOL                        found = FALSE;
  254.      
  255.     rnode   = (struct RootNode *)  DOSBase->dl_Root;        /* find root node */
  256.     dinfo   = (struct DosInfo  *)  BADDR(rnode->rn_Info);   /* now dos info   */
  257.     
  258.     strcpy(name1,lookfor);
  259.     strupper(name1);
  260.  
  261.     Forbid();
  262.     for(dnode = (struct DeviceNode *) BADDR(dinfo->di_DevInfo); ( dnode ) ;
  263.       dnode = (struct DeviceNode *) BADDR(dnode->dn_Next))
  264.     {
  265.         bname = (char *) BADDR(dnode->dn_Name);
  266.         memcpy(bname, name2, (ULONG)( bname[0] + 1 ) );
  267.         
  268.         if( !( strcmp( name1, strupper(btoc(name2)) ) ) )
  269.         {
  270.             found   =   TRUE;
  271.             break; 
  272.         }
  273.     }
  274.  
  275.     Permit(); 
  276.     
  277.     return(found);       
  278. }
  279.  
  280.  
  281. /* 
  282.  *  Duplicate the given device node and add it the system device list.
  283.  */
  284. VOID dupdev(dnode,newname)
  285. struct DeviceNode   *dnode;
  286. char                *newname;
  287. {
  288.     register struct DeviceNode  *newdnode;
  289.     register BSTR               newbname;
  290.     register UBYTE              length;
  291.     char                        tmpname[81];
  292.     ULONG                       tmplong;
  293.         
  294.     newdnode = (struct DeviceNode *)
  295.         AllocMem( (ULONG) sizeof(*newdnode), MEMF_PUBLIC | MEMF_CLEAR );
  296.     
  297.     memcpy(dnode,newdnode,(ULONG)sizeof(*newdnode));
  298.     
  299.     newdnode->dn_Name   = NULL; 
  300.   
  301.     strcpy(tmpname,newname);
  302.     
  303.     length              = (UBYTE) strlen(tmpname);
  304.      
  305.     tmplong             = KEYWORD;
  306.     
  307.     memcpy(&tmplong,&tmpname[length+1], (ULONG) sizeof(ULONG) );
  308.   
  309.     length             += 5;    /* include NULL and LONG WORD */
  310.     
  311.     newbname            = ctob( allocstr(tmpname, (ULONG) length) );
  312.     
  313.     newdnode->dn_Next   = NULL;
  314.    
  315.     if(!newbname)
  316.         freedev(newdnode);
  317.     else
  318.     {    
  319.         newdnode->dn_Name = newbname;
  320.         adddosnode(newdnode);
  321.     }
  322. }
  323.  
  324.  
  325. /* end of misc.c */
  326.