home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff274.lzh / LookFor / source / getdisks.c next >
C/C++ Source or Header  |  1989-11-16  |  3KB  |  134 lines

  1. /* getdisks.c - Grab all available disk devices and return them to you in a
  2.  *       simple exec list. The list is made up of named nodes--the "names"
  3.  *       being the device name.
  4.  *
  5.  *     Phillip Lindsay (c) 1987 Commodore-Amiga Inc.
  6.  * You may use this source as long as the copyright notice is left intact.
  7.  */
  8. #include <exec/types.h>
  9. #include <exec/nodes.h>
  10. #include <exec/lists.h>
  11. #include <exec/memory.h>
  12. #include <libraries/dos.h>
  13. #include <libraries/dosextens.h>
  14. #include <libraries/filehandler.h>
  15. #include <stdio.h>
  16. #include <proto/exec.h>
  17. #include <proto/dos.h>
  18.  
  19. /*  protos  */
  20. void *btoc(char *);
  21. struct Node *GetNode(char *,UBYTE,UBYTE);
  22. void FreeNode(struct Node *);
  23. void getdisks(struct List *);
  24. void freedisks(struct List *);
  25.  
  26. extern struct DosLibrary *DOSBase;
  27.  
  28. /* btoc() takes a pointer to a string in BCPL format and converts it to a
  29.  * C string.
  30.  */
  31. void *btoc(bstring)
  32. char *bstring;
  33. {
  34.  register UBYTE len,count,*cstring;
  35.  
  36.  cstring = (UBYTE *) bstring;
  37.  len = cstring[0];
  38.  for(count=0;count < len;count++)
  39.   cstring[count] = cstring[count+1];
  40.  cstring[count] = '\0';
  41.  return;
  42. }
  43.  
  44. /* GetNode() will build a node structure for you. It will append memory to the
  45.  *    node structure for the node name passed.
  46.  */
  47. struct Node *GetNode(name,type,pri)
  48. char *name;
  49. UBYTE type,pri;
  50. {
  51.  register struct Node *mynode;
  52.  register char        *myname;
  53.  register UBYTE         *mymemory;
  54.  register ULONG         mynamesize;
  55.  
  56.  mynamesize =( ((ULONG)strlen(name)) ? (ULONG)strlen(name)+1 : 0L );
  57.  
  58.  mymemory = (UBYTE *)
  59.   AllocMem((ULONG)sizeof(*mynode)+mynamesize,MEMF_PUBLIC | MEMF_CLEAR);
  60.  
  61.  if(!mymemory) return((struct Node *)NULL);
  62.  
  63.  mynode = (struct Node *) mymemory;
  64.  if(mynamesize)
  65.   {
  66.    myname = (char *) mymemory+(ULONG)sizeof(*mynode);
  67.    strcpy(myname,name);
  68.    mynode->ln_Name = myname;
  69.   }
  70.  mynode->ln_Type = type;
  71.  mynode->ln_Pri  = pri;
  72.  
  73.  return(mynode);
  74. }
  75.  
  76. /* This function assumes you used GetNode() for node initialization. Will
  77.  *    free all memory used by node. Make sure you remove node from any list.
  78.  */
  79. void FreeNode(mynode)
  80. struct Node *mynode;
  81. {
  82.  register ULONG mymemsize;
  83.  
  84.  mymemsize = (ULONG) sizeof(*mynode);
  85.  mymemsize+= ((mynode->ln_Name) ? (ULONG)strlen(mynode->ln_Name)+1 : 0L);
  86.  
  87.  FreeMem(mynode,mymemsize);
  88. }
  89.  
  90.  
  91. /* getdisks() will grab all disk device names in the system device list and
  92.  *  append an exec node to a given list. The node being named in respect to the
  93.  *  device.
  94.  */
  95. void getdisks(dlist)
  96. struct List *dlist;   /* passed a pointer to a initialize exec list */
  97. {
  98.  extern struct DosLibrary      *DOSBase;
  99.  struct RootNode          *rnode;
  100.  struct DosInfo             *dinfo;
  101.  register struct DeviceNode     *dnode;
  102.  register struct Node         *adisk;
  103.  char     *bname,name[32];
  104.  
  105.  rnode   = (struct RootNode *)  DOSBase->dl_Root;
  106.  dinfo   = (struct DosInfo  *)  BADDR(rnode->rn_Info);
  107.  
  108.  Forbid();
  109.  for(dnode = (struct DeviceNode *) BADDR(dinfo->di_DevInfo);BADDR(dnode);
  110.     dnode = (struct DeviceNode *) BADDR(dnode->dn_Next))
  111.   {
  112.    if(!dnode->dn_Type && dnode->dn_Task && BADDR(dnode->dn_Name))
  113.     {
  114.      bname = (char *) BADDR(dnode->dn_Name);
  115.      movmem(bname,name,(ULONG)bname[0]+1L);
  116.      btoc(name);
  117.      if((adisk=GetNode(name,0,0))) AddTail(dlist,adisk);
  118.     }
  119.   }
  120.  Permit();
  121. }
  122.  
  123. /* freedisks() will free all nodes in a given list. Function assumes nodes where
  124.  *    initialized with GetNode().
  125.  */
  126. void freedisks(dlist)
  127. struct List *dlist;
  128. {
  129.  register struct Node *disk;
  130.  
  131.  while((disk=RemTail(dlist)))
  132.   FreeNode(disk);
  133. }
  134.