[comp.sys.amiga] Grab disks from system device list.

phillip@cbmvax.UUCP (02/17/87)

Here is something I cooked up when I found a program that hard coded
devices in a menu.
==============================================================================
  Phillip Lindsay - Commodore Business Machines - Amiga Technical Support
  UUCP: {ihnp4|seismo|caip}!cbmvax!phillip      - Phone: (215) 431-9180
  No warranty is implied or otherwise given in the form of suggestion or 
  example. Any opinions found here are of my making. 	
*********************** CUT HERE
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	makefile
#	getdisks.c
# This archive created: Tue Feb 17 11:13:27 1987
export PATH; PATH=/bin:$PATH
echo shar: extracting "'makefile'" '(68 characters)'
if test -f 'makefile'
then
	echo shar: will not over-write existing file "'makefile'"
else
sed 's/^	X//' << \SHAR_EOF > 'makefile'
	X
	XCFLAGS = -dMANX +lcd
	X
	X
	Xgetdisks: getdisks.o
	X	ln -w getdisks.o -lc32
SHAR_EOF
echo shar: a missing newline was added to "'makefile'"
if test 68 -ne "`wc -c < 'makefile'`"
then
	echo shar: error transmitting "'makefile'" '(should have been 68 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'getdisks.c'" '(3670 characters)'
if test -f 'getdisks.c'
then
	echo shar: will not over-write existing file "'getdisks.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'getdisks.c'
	X/* getdisks.c - Grab all available disk devices and return them to you in a
	X * 		simple exec list. The list is made up of named nodes--the "names"
	X *	 	being the device name. 
	X *
	X * 	 Phillip Lindsay (c) 1987 Commodore-Amiga Inc. 
	X * You may use this source as long as the copyright notice is left intact.
	X */
	X#include <exec/types.h>
	X#include <exec/nodes.h>
	X#include <exec/lists.h>
	X#include <exec/memory.h>
	X#include <libraries/dos.h>
	X#include <libraries/dosextens.h>
	X#include <libraries/filehandler.h>
	X
	X#ifdef MANX
	X#include <functions.h>
	X#include <stdio.h>
	X#else
	X#include <lattice/stdio.h>
	X#endif
	X
	Xextern struct DosLibrary *DOSBase;
	X
	X/* btoc() takes a pointer to a string in BCPL format and converts it to a
	X * C string.
	X */
	Xvoid *btoc(bstring)
	Xchar *bstring;
	X{
	X register UBYTE len,count,*cstring; 
	X 
	X cstring = (UBYTE *) bstring;
	X len = cstring[0];              
	X for(count=0;count < len;count++)
	X  cstring[count] = cstring[count+1];
	X cstring[count] = '\0';
	X}
	X
	X/* GetNode() will build a node structure for you. It will append memory to the
	X * 	node structure for the node name passed.
	X */   	
	Xstruct Node *GetNode(name,type,pri)
	Xchar *name;
	XUBYTE type,pri;
	X{
	X register struct Node *mynode;
	X register char        *myname;
	X register UBYTE	      *mymemory;
	X register ULONG	      mynamesize;
	X
	X mynamesize =( ((ULONG)strlen(name)) ? (ULONG)strlen(name)+1 : 0L );   
	X
	X mymemory = (UBYTE *) 
	X  AllocMem((ULONG)sizeof(*mynode)+mynamesize,MEMF_PUBLIC | MEMF_CLEAR); 
	X
	X if(!mymemory) return((struct Node *)NULL);
	X 
	X mynode = (struct Node *) mymemory; 
	X if(mynamesize)
	X  {
	X   myname = (char *) mymemory+(ULONG)sizeof(*mynode);
	X   strcpy(myname,name);
	X   mynode->ln_Name = myname;
	X  }
	X mynode->ln_Type = type;
	X mynode->ln_Pri  = pri;
	X
	X return(mynode);
	X}
	X
	X/* This function assumes you used GetNode() for node initialization. Will
	X * 	free all memory used by node. Make sure you remove node from any list. 
	X */
	Xvoid FreeNode(mynode)
	Xstruct Node *mynode;
	X{
	X register ULONG mymemsize;
	X
	X mymemsize = (ULONG) sizeof(*mynode);
	X mymemsize+= ((mynode->ln_Name) ? (ULONG)strlen(mynode->ln_Name)+1 : 0L);
	X 
	X FreeMem(mynode,mymemsize);
	X}
	X
	X
	X/* getdisks() will grab all disk device names in the system device list and
	X *  append an exec node to a given list. The node being named in respect to the
	X *  device.  
	X */
	Xvoid getdisks(dlist)
	Xstruct List *dlist;	/* passed a pointer to a initialize exec list */ 
	X{
	X extern struct DosLibrary 	  *DOSBase;
	X struct RootNode	  	  *rnode; 
	X struct DosInfo	  		  *dinfo;
	X register struct DeviceNode	  *dnode;
	X register struct Node 		  *adisk;
	X char	  *bname,name[32];
	X
	X rnode   = (struct RootNode *)  DOSBase->dl_Root;
	X dinfo   = (struct DosInfo  *)  BADDR(rnode->rn_Info);
	X 
	X Forbid();
	X for(dnode = (struct DeviceNode *) BADDR(dinfo->di_DevInfo);BADDR(dnode);
	X 	dnode = (struct DeviceNode *) BADDR(dnode->dn_Next))
	X  {
	X   if(!dnode->dn_Type && dnode->dn_Task && BADDR(dnode->dn_Name)) 
	X    {
	X     bname = (char *) BADDR(dnode->dn_Name);    
	X     movmem(bname,name,(ULONG)bname[0]+1L);
	X     btoc(name);
	X     if((adisk=GetNode(name,0,0))) AddTail(dlist,adisk);    
	X    }
	X  }
	X Permit(); 
	X}
	X
	X/* freedisks() will free all nodes in a given list. Function assumes nodes where
	X *    initialized with GetNode().
	X */
	Xvoid freedisks(dlist)
	Xstruct List *dlist;
	X{
	X register struct Node *disk;
	X
	X while((disk=RemTail(dlist)))
	X  FreeNode(disk);
	X}
	X
	X
	Xmain()
	X{
	X struct List disks;
	X struct Node *disk;
	X  
	X NewList(&disks);	/* Initialize list header */
	X getdisks(&disks);	/* Fill list */
	X 
	X/* print any devices in list.... if any */
	X if(disks.lh_TailPred != &disks)
	X   for(disk = disks.lh_Head;disk->ln_Succ;disk=disk->ln_Succ)
	X     puts(disk->ln_Name);
	X
	X freedisks(&disks);
	X}
SHAR_EOF
if test 3670 -ne "`wc -c < 'getdisks.c'`"
then
	echo shar: error transmitting "'getdisks.c'" '(should have been 3670 characters)'
fi
fi # end of overwriting check
#	End of shell archive
exit 0
-- 
==============================================================================
  Phillip Lindsay - Commodore Business Machines - Amiga Technical Support
  UUCP: {ihnp4|seismo|caip}!cbmvax!phillip      - Phone: (215) 431-9180
  No warranty is implied or otherwise given in the form of suggestion or 
  example. Any opinions found here are of my making. 	/* eof */