HP3000-L Archives

July 2002, Week 1

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Jeff Woods <[log in to unmask]>
Reply To:
Jeff Woods <[log in to unmask]>
Date:
Mon, 1 Jul 2002 13:53:07 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (57 lines)
Hi, folks.

I'm trying to use O_EXCL with O_CREAT in a call to open() from an HPC/iX
[ie, CCXL] program on MPE 7.0(Exp1).  [Some background:  O_EXCL is an option
used on Unix systems in conjunction with O_CREAT to tell open() to create
the file being opened and fail if it already exists.  Without O_EXCL the
O_CREAT option indicates to create the file if it doesn't exist and open it
in either case.]  The O_EXCL option for open() doesn't seem to appear in the
HPC/iX Library Reference Manual (E1092) for some reason, though it's defined
in FCNTL.H.SYS if _POSIX_SOURCE is defined.

I'm trying to create and open a new permanent file as a FILE* stream for use
as an application log file, but I want the open to fail if the file already
exists so I can detect the presence of the existing log file.  Using
O_EXCL|O_CREAT should work and provides an atomic operation of testing for a
pre-existing file and creating/opening it.  I can check to see if the file
exists before opening it, but that opens the potential for a race condition.

The code I'm testing at the moment uses open() to create/open the file, then
uses dup() to create a second file descriptor handle which it then converts
to a FILE* stream via fdopen() so I can use fprintf() with it.  At the end
of access, I call fclose() on the stream and then call the FCLOSE()
intrinsic with the disposition set to "save" (011) or "delete" (4) depending
on whether any errors and/or warnings occurred during the application run
and finally call close() to clean up the open file-descriptor.  Something
like:

int fd = open(path, O_CREAT|O_EXCL|O_WRONLY|O_MPEOPTS, 0, "Df4 Ds1 R4092 V
X2");
FILE *fs = fdopen(dup(fd), "a");
....
fprintf(fs, "Various messages get written.\n");
....
fclose(fs);
FCLOSE(_mpe_fileno(fd), (errors ? 011 : 4), 0);
close(fd);

It seems the only way to get the behavior I need is to use HPFOPEN() with a
domain of 4 ["new permanent file"] but I don't see any way to turn the MPE
file number into a FILE* stream.  I'm about to the point of converting all
the fprintf() calls to sprintf() and FWRITE() calls.  But it seems to me
that there should be a way to do what I'm trying to accomplish in a more
portable way (especially since what I'm doing above should work on "real
Unix").

Any pointers on what's really happening and how best I might proceed
[especially if I receive them before I'm mostly finished converting to
sprintf() and FWRITE()] would be most appreciated.  Thanks.
--
Jeff Woods
[log in to unmask]
Quintessential School Systems
[log in to unmask]

* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *

ATOM RSS1 RSS2