[comp.sys.mac.programmer] HELP with _OpenResFile

dvlmfs@zeus.umu.se (05/23/89)

I've got a big problem with an INIT I'm writing so I thought that maybe someone
out on the net could help me with this problem or explain why it doesn't work.
All I'm trying to do is to open my files' resources from the patch 
code so I can use the resources that I've made, but this doesn't work 
and I dont know why! It's written in LSC 3.0 and looks like this:

main()
{
	asm {
		.	;from here I patch traps and move code -
		.	;into the "right" place
		.

;here comes the pure code
@patchstart
	movem.l	a0-a1/d0-d3,-(sp)	;save registers
	clr.w	-(sp)			;clear for result
	pea	@fileName		;push adress of fileName
	_OpenResFile			;trap call
	move.w	(sp)+,d3		;save result in d3
	clr.w	-(sp)			;clear for result
	_ResError			;trap call
	move.w	(sp)+,d0		;get result from _ResError
	tst.w	d0			;check if it's equal to noErr
	bne	@error			;if error has occured - ErrorAlert
	.
	.
	.
	jmp	@exit			;jmp over error
@error
	move.w	#0x000A,-(sp)		;push 0x000A for _SysBeep
	_SysBeep			;trap call
@exit	movem.l	(sp)+,a0-a1/d0-d3	;restore registers
@fileName
	dc.b	0x0006			;length byte of Str255
	dc.b	'M','y','F','i','l','e'	;the file name
@patchend	

	} /* end asm */
} /* end main */

The error this program generates is ( according to _ResError ) 
an OSErr (tmfoErr) equal to -42 and it stands for 'too many files open' !!!
It is _OpenResFile that generates this error and I don't know what I shall
do with it. Could someone please point out the problem with this code !
If I do something similar in pure C it works!

main()
{
	int	refNum;

	.
	.
	.
	refNum = OpenResFile("\pMyFile");
	if (ResError())
		DebugStr("\pOpenResFile");
	.
	.
	.
}

This code doesn't generate an error so the ResError statement never occurs.

I have compared theese two programs in TMON and they look the same BUT
the first ( and most important ) program doesn't work.

Please correct what I'm doing wrong and explain to me what I shall do.

Thanks in advance.


Michael Forzelius            
dvlmfs@cs.umu.se

ksitze@nmsu.edu (Kevin Sitze) (05/23/89)

In article: <dvlmfs@zeus.umu.se's message of 23 May 89 07:16:24 GMT>
>I've got a big problem with an INIT I'm writing so I thought that
>maybe someone out on the net could help me with this problem or
>explain why it doesn't work.  All I'm trying to do is to open my files'
>resources from the patch code so I can use the resources that I've
>made, but this doesn't work and I dont know why! It's written in LSC
>3.0 and looks like this: 

First problem with the code I see right off is:

@error
	move.w	#0x000A,-(sp)		;push 0x000A for _SysBeep
	_SysBeep			;trap call
@exit	movem.l	(sp)+,a0-a1/d0-d3	;restore registers
>@fileName
>	dc.b	0x0006			;length byte of Str255
>	dc.b	'M','y','F','i','l','e'	;the file name
@patchend	

You have to 1) jump over the string, it's not code, it's data.
	    2) Set you lenght byte as 0x06 (don't have my machine (or
               memory) handy at the moment but I'm pretty sure that a
               zero is the first byte here and then comes a 0x06.
               e.g. memory dump:  0000: 00 06 ...)

Second piece of coding:

        movem.l	a0-a1/d0-d3,-(sp)	;save registers
	clr.w	-(sp)			;clear for result
	pea	@fileName		;push adress of fileName
	_OpenResFile			;trap call
	move.w	(sp)+,d3		;save result in d3
>	clr.w	-(sp)			;clear for result
>	_ResError			;trap call
>	move.w	(sp)+,d0		;get result from _ResError
>	tst.w	d0			;check if it's equal to noErr
	bne	@error			;if error has occured - ErrorAlert

If OpenResFile is incapable of actually opening the file wanted (for
whatever reason) then an error code will be returned & placed in
refNum.  e.g. the code with > in column one is unnecessary.

Finally, I expect that the problem lies in the @fileName field.  You
might check to make sure that there is only _one_ byte before the
first letter ('M').  As this field lies directly after the
restore-registers at @exit, this will definantly cause problems. (say,
various system errors, etc.)

Hope this helps (wish I had my mac up & running...)

				-Kelesi
--
------------------------------------+-------------------------------
From the Macintosh of: Kevin Sitze  | Disclamer: Who the heck needs
                                    |   a disclamer?  After all, Dan
EMail: ksitze%NMSU.edu              |   Quayle doesn't.
SMail: 601 S. Melendres             +-------------------------------
       Las Cruces, NM  88005        | "We have the answers, the
------------------------------------+  trouble lies in finding the
"The difference between intelligence|  questions..."
and stupidity is that intelligence  | "The information is there,
has a limit."           - anonymous |  finding it is another story."
The dolt confuses you -- more --    |	   	    - Any consultant
------------------------------------+-------------------------------

thecloud@dhw68k.cts.com (Ken McLeod) (05/25/89)

In article <863@umecs.cs.umu.se> dvlmfs@zeus.umu.se () writes:
>I've got a big problem with an INIT I'm writing so I thought that maybe someone
>out on the net could help me with this problem or explain why it doesn't work.
>All I'm trying to do is to open my files' resources from the patch 
>code so I can use the resources that I've made, but this doesn't work 
>and I dont know why! It's written in LSC 3.0 and looks like this:
>
> [previous lines of code deleted...]
>       jmp     @exit                   ;jmp over error
>@error
>       move.w  #0x000A,-(sp)           ;push 0x000A for _SysBeep
>       _SysBeep                        ;trap call
>@exit  movem.l (sp)+,a0-a1/d0-d3       ;restore registers
>@fileName
>       dc.b    0x0006                  ;length byte of Str255
>       dc.b    'M','y','F','i','l','e' ;the file name
>@patchend
>
>       } /* end asm */
>} /* end main */


  Your code, as posted, will get real confused after it reaches the @exit
label, and falls through to your data! Hopefully, you actually did this:

@exit  movem.l (sp)+,a0-a1/d0-d3        ;restore registers
       bra.s   @patchend                ;skip following data!
@fileName
       . (inline data here)
       .
@patchend

  Another potential problem is that you define the length "byte" inline
as 0x0006 (word-length), not 0x06 (char-length). This *may* be generating
two bytes, 00 and 06. If so, then OpenResFile would think you were passing
a zero-length string, and would definitely signal an error.

-ken


-- 
==========     .......     =============================================
Ken McLeod    :.     .:    UUCP: ...{spsd,zardoz,felix}!dhw68k!thecloud
==========   :::.. ..:::   INTERNET: thecloud@dhw68k.cts.com
                ////       =============================================

zben@umd5.umd.edu (Ben Cranston) (05/27/89)

I think these two lines of code are exactly equivalent:

    DC.B   0x0006
    DC.B   0x06

because the DC directive is told to make exactly one byte.  Chances are this
is not the problem.


Consider these two facts:
1. Lack of a branch around the literal string means control is going to fall
   into the string and execute it as instructions.
2. User complained of a return code that says "Too many files open".

Suppose part of the string, as interpreted as instructions, is actually a
short branch back into the top of the code!  It will try to open the same
file a second time.  Two possibilities here:

A. The Mac file system notices it is the same file, and issues an error code
   that can also mean "tried to open the same file more than once".

B. The Mac file system does NOT notice it, and opens another connection to
   the same file.  Control falls into the short branch back.  Opens another
   connection to the same file.  Control falls into the short branch back...
   Eventually some system file table overflows, and the "too many files open"
   condition is diagnosed.

I would either put a sysbeep into the putative loop and see if the Mac makes
a lot of noise, or MacNosy the resource and see if there is indeed a branch
back.
-- 
Ben Cranston <zben@umd2.UMD.EDU>    (Kingdom of Merryland UniSys 1100/92)
Copyright 1989 (you may redistribute ONLY if your recipients can).