Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!rice!sun-spots-request From: frist@ccu.umanitoba.ca Newsgroups: comp.sys.sun Subject: Re: SUN Pascal: Determining if a file exists before opening Keywords: SunOS Message-ID: <5982@brazos.Rice.edu> Date: 21 Mar 90 17:42:00 GMT Sender: root@rice.edu Organization: Sun-Spots Lines: 47 Approved: Sun-Spots@rice.edu X-Refs: Original: v9n88 X-Sun-Spots-Digest: Volume 9, Issue 93, message 1 Last week, I posted the question In SUN Pascal, how do you test to see if a file exists, before 'reset'ting or 'rewrite'ing that file? Many of the replies suggested calling the c access() procedure from Pascal, while others sent c subroutines, or ideas for c subroutines, that could be compiled separately and linked by the pc command. Below is what I think is the simplest solution. Thanks a lot to all who replied to my query. const MAXLINE= 132; type (* LINE simulates dynamic character strings in standard Pascal *) CHARARRAY = packed array[1..MAXLINE] of char; LINE = record STR:CHARARRAY; LEN:integer end; (* The c function access is found in sys/file.h. access must be declared in the outermost scope of the Pascal program. See access (2) manual pages *) function access(PATH:CHARARRAY; MODE:integer):integer; external c; (* The following function may appear in any scope: *) (* - - - - - - - - - - - - - - - - - - - - - - - - - - - *) (* =true if file exists, =false if it doesn't *) (* A comparable procedure is provided with ATT System V Pascal *) (* A special thank you to Mark Foster, CIS Rearch Computing, Univ. of Penn. and Glenn Gribble of Synaptics Inc. for showing me how to use access() within Pascal programs. B.F. *) function EXISTS(FILENAME:LINE):boolean; const F_OK=0; (* checks if file exists *) begin (* EXISTS *) with FILENAME do begin STR[LEN+1]:=chr(0); (* c strings terminate with null *) if access(STR,F_OK)=-1 then EXISTS:=false else EXISTS:=true end (* with FILENAME *) end; (* EXISTS *) Brian Fristensky frist@ccu.umanitoba.ca Assistant Professor Dept. of Plant Science University of Manitoba Winnipeg, MB R3T 2N2 CANADA Office phone: 204-474-6085 FAX: 204-275-5128