Re: Determine if open(2) created or opened?

From: Casper H.S. Dik (Casper.Dik_at_Sun.COM)
Date: 12/27/03

  • Next message: Michael B Allen: "Re: Determine if open(2) created or opened?"
    Date: 27 Dec 2003 10:06:25 GMT
    
    

    Michael B Allen <mba2000@ioplex.com> writes:

    >O_CREAT specifies that if the file does not exist it should be created
    >whereas if the file already exists it should simply be opened. How can it
    >be determined if the file was created or just opened?

    You can't; what you can do, however, is this:

            fd = open(path, O_WHATEVER);
            if (fd == -1 && errno == ENOENT) {
                    /* open failed, file didn't exist */
                    fd = open(path, O_WHATEVER|O_CREAT|O_EXCL, 0666);
            }

    Of course, the open can now fail if it was created in between,
    you could iterate.

    Casper

    -- 
    Expressed in this posting are my opinions.  They are in no way related
    to opinions held by my employer, Sun Microsystems.
    Statements on Sun products included here are not gospel and may
    be fiction rather than truth.
    

  • Next message: Michael B Allen: "Re: Determine if open(2) created or opened?"