[comp.sys.amiga] ASSIGN: solution

hsgj@batcomputer.UUCP (02/08/87)

If you remember, I had a problem where I needed to know whether
a name was ASSIGNed before I used it.  Mike Meyer suggested checking
the data structure Assign uses.  Following this advice, I was able to do
just what I wanted.  Anyways, since the solution was prompted by a
net suggestion, I thought I'd share it:
	Included is Assigned() and a test program main().  The way
to use this from a program is like this:
	...
	if (Assigned("HELP") != AS_UNKNOWN)
		/* Read help file from the help disk or directory */
		show_help_file("HELP:helpfile");
	else
		/* Read help file from the current directory */
		show_help_file("helpfile");
	...
The sample main() just lets you test it from the keyboard.  Note that
it is highly case-sensitive. In other words, df0 is unknown, but DF0
is a drive.  Note also that you should NOT add a colon: to the name
to be searched.

-- Dan Green (ps: beware of .signature at eof)

------------------------------ cut here -------------------------

#include "stdio.h"
#include "exec/types.h"
#include "libraries/dosextens.h"

#define	AS_DEVICE	0
#define	AS_DIR		1
#define	AS_DRIVE	2
#define	AS_UNKNOWN	3

int	Assigned(searchname)
char	*searchname;
{
	struct	DosLibrary	*DosLibrary;
	struct	RootNode	*RootNode;
	struct	DosInfo		*DosInfo;
	struct	DeviceList	*DevInfo;
	char	name[256];
	char	*astr;
	BSTR	bstr;
	long	type;
	int	i,rc;

	DosLibrary = (struct DosLibrary *)OpenLibrary("dos.library",0);
	RootNode = (struct RootNode *)DosLibrary->dl_Root;
	DosInfo = (struct DosInfo *)BADDR(RootNode->rn_Info);
	DevInfo = (struct DeviceList *)BADDR(DosInfo->di_DevInfo);

	rc = AS_UNKNOWN;
	while (DevInfo != NULL) {
		type = DevInfo->dl_Type;
		bstr = (BSTR)DevInfo->dl_Name;
		astr = (char *)BADDR(bstr);
		for (i = 0; i < astr[0]; i++)
			name[i] = astr[i+1];
		name[i] = '\0';
		if (strcmp(name,searchname) == 0) {
			rc = type;
			DevInfo = NULL;
		}
		else
			DevInfo = (struct DeviceList *)BADDR(DevInfo->dl_Next);
	}
	CloseLibrary(DosLibrary);
	return(rc);
}

main()
{
	char	cmd[80];

	printf("*** This program is Case Sensitive! ***\n");
	printf("*** Do not append a colon: to the searchname ***\n\n");

	for (;;) {
		printf("Enter search name or QUIT: ");
		scanf("%s",cmd);
		if (strcmp(cmd,"QUIT") == 0)
			return(0);
		switch ( Assigned(cmd) ) {
			case AS_DEVICE:
				printf("%s is a device\n",cmd);
				break;
			case AS_DIR:
				printf("%s is an assigned directory\n",cmd);
				break;
			case AS_DRIVE:
				printf("%s is a mounted drive\n",cmd);
				break;
			case AS_UNKNOWN:
				printf("%s is unknown\n",cmd);
				break;
			default:
				printf("%s is not defined???\n",cmd);
				break;
		}
	}
}

------------------------------ cut here -------------------------
-- Dan Green
-- 
ARPA:  hsgj%vax2.ccs.cornell.edu@cu-arpa.cs.cornell.edu
UUCP:  ihnp4!cornell!batcomputer!hsgj   BITNET:  hsgj@cornella

mic@ut-ngp.UUCP (02/09/87)

In article <131@batcomputer.tn.cornell.edu> hsgj@batcomputer.tn.cornell.edu (Dan Green) writes:
>If you remember, I had a problem where I needed to know whether
>a name was ASSIGNed before I used it.  Mike Meyer suggested checking
>the data structure Assign uses.  Following this advice, I was able to do
>just what I wanted.  Anyways, since the solution was prompted by a
>net suggestion, I thought I'd share it:
>
..... [ Program that searches the DOS device list to
	see if a particular name is ASSIGNED or not]
>
> -- Dan Green
>-- 
>ARPA:  hsgj%vax2.ccs.cornell.edu@cu-arpa.cs.cornell.edu
>UUCP:  ihnp4!cornell!batcomputer!hsgj   BITNET:  hsgj@cornella

If you go poking around system data structures, you should really use
Forbid() and Permit() to keep other processes from changing the data
while you're accessing it.

	get head of device list...
	Forbid();
	while (devinfo != NULL)
		use devinfo->whatever...
	Permit();
	return result...

Although it would be rare for this particular routine to get into
trouble (depends on how often you mount new devices, I guess), better
safe than sorry...

--mic--

...!ihnp4!ut-sally!ut-ngp!mic
mic@ngp.utexas.edu
ccep001@utadnx.bitnet