[comp.os.vms] testing if a file is open from pascal

saganich%frodo.DECnet@MGHCCC.HARVARD.EDU ("FRODO::SAGANICH") (05/27/88)

Hi Infovaxers,

    I am in a situation where I need to test whether a file is open for 
write by another users.  If so I would like to wait until I can open it.
Pascal doesn't seem to have any sort of is_it_already_open type of 
procedure.  What I was going to do is call c and use the access function.  
However, this doesn't seem to really fit the bill either.  As such I have 
two questions

    	1) What langauge/function can I use to test if a file is already
    	   open?  If none exist has someone already written something that 
    	   I could take a look at?

    	2) Any ideas why the following blurb of code results in an access
    	   violation??


Thanks in advance for any help

Al Saganich, systems analyst. MGH boston Ma.
saganich%frodo.decnet@mghccc.harvard.edu

/****************** cut here, is_it_open.c ********************/

#include <stdio.h>
#include <descrip.h>

is_it_open(str_ptr)
struct dsc$descriptor *str_ptr;

{


printf("the length is %d\n",str_ptr->dsc$w_length);
printf("the string is %s\n",str_ptr->dsc$a_pointer);
}




{--------------- cut here combine.pas -----------------------} 
program combine (input,output);
type
    string = varying [100] of char;
var
    
    test_str:string;
    status:integer;

[EXTERNAL] function Is_it_open(str:string):integer; extern;

begin
test_str :='THIS IS A TEST STRING';

status := is_it_open(test_str);
end.
------

sh1u+@ANDREW.CMU.EDU (Stuart Mark Hiser) (05/29/88)

>        2) Any ideas why the following blurb of code results in an access
>           violation??

I've been bitten by this myself: the problem is that in a pascal varying of
character, the length of the string is maintained explicitly in a separate
register, whereis C doesn't - it expects strings to end with a null byte.
Printf is looking down the string, trying to find the null byte (that isn't
there), and eventually reaches the end of its address space -> access
violation.  This worked for me:

str_ptr->dsc$a_pointer[length] = '\0';
(note: dsc$w_length is the maximum length, not necessarily the current length)

Two other things you should be aware of:

1: Pascal generally passes strings by reference, not by descriptor; to use
descriptors, change your declaration to:
[EXTERNAL] function Is_it_open(str: %descr string):integer; extern;

2: For a varying string, the first two bytes of dsc$a_pointer will contain the
current length of the string; the actual string data will start at third byte.

[ keep in mind this is all from memory - I don't have my manuals handy :-) ]


Stu Hiser                               R746SH1U@VB.CC.CMU.EDU
VAX/VMS system programmer               R746SH1U@CMCCVB.BITNET
Carnegie Mellon University                      CMCCVB::R7467SH1U (CCnet)