<commit_before>/* 
 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
 *
 * $Id: openFile.c,v 1.4 1999/01/07 15:48:40 simonm Exp $
 *
 * openFile Runtime Support
 */

/* We use lstat, which is sadly not POSIX */
#define NON_POSIX_SOURCE

#include "Rts.h"
#include "stgio.h"
#include "fileObject.h"

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

IOFileObject*
openStdFile(fd,flags,rd)
StgInt fd;
StgInt flags;
StgInt rd;
{
    IOFileObject* fo;

    if ((fo = malloc(sizeof(IOFileObject))) == NULL)
       return NULL;
    fo->fd       = fd;
    fo->buf      = NULL;
    fo->bufWPtr  = 0;
    fo->bufRPtr  = 0;
    fo->flags   = flags | FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
    fo->connectedTo = NULL;
    return fo;
}

#define OPENFILE_APPEND 0
#define OPENFILE_WRITE 1
#define OPENFILE_READ_ONLY 2
#define OPENFILE_READ_WRITE 3

IOFileObject*
openFile(file, how, binary, flags)
StgByteArray file;
StgInt how;
StgInt binary;
StgInt flags;
{
    FILE *fp;
    int fd;
    int oflags;
    int exclusive;
    int created = 0;
    struct stat sb;
    IOFileObject* fo;

#ifdef __CONCURRENT_HASKELL__
#warning FixMe: Ignoring bogus bit 7 in openFiles 2nd argument
    how = how & 0x7f;
#endif

    /*
     * Since we aren't supposed to succeed when we're opening for writing and
     * there's another writer, we can't just do an open() with O_WRONLY.
     */

    switch (how) {
      case OPENFILE_APPEND:
        oflags = O_WRONLY | O_NOCTTY | O_APPEND; 
	exclusive = 1;
	break;
      case OPENFILE_WRITE:
	oflags = O_WRONLY | O_NOCTTY;
	exclusive = 1;
	break;
    case OPENFILE_READ_ONLY:<commit_msg>[project @ 1999-01-23 17:44:40 by sof] Removed workaround for bug - fixed (a while ago.)<commit_after>/* 
 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
 *
 * $Id: openFile.c,v 1.5 1999/01/23 17:44:40 sof Exp $
 *
 * openFile Runtime Support
 */

/* We use lstat, which is sadly not POSIX */
#define NON_POSIX_SOURCE

#include "Rts.h"
#include "stgio.h"
#include "fileObject.h"

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

IOFileObject*
openStdFile(fd,flags,rd)
StgInt fd;
StgInt flags;
StgInt rd;
{
    IOFileObject* fo;

    if ((fo = malloc(sizeof(IOFileObject))) == NULL)
       return NULL;
    fo->fd       = fd;
    fo->buf      = NULL;
    fo->bufWPtr  = 0;
    fo->bufRPtr  = 0;
    fo->flags   = flags | FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
    fo->connectedTo = NULL;
    return fo;
}

#define OPENFILE_APPEND 0
#define OPENFILE_WRITE 1
#define OPENFILE_READ_ONLY 2
#define OPENFILE_READ_WRITE 3

IOFileObject*
openFile(file, how, binary, flags)
StgByteArray file;
StgInt how;
StgInt binary;
StgInt flags;
{
    FILE *fp;
    int fd;
    int oflags;
    int exclusive;
    int created = 0;
    struct stat sb;
    IOFileObject* fo;

    /*
     * Since we aren't supposed to succeed when we're opening for writing and
     * there's another writer, we can't just do an open() with O_WRONLY.
     */

    switch (how) {
      case OPENFILE_APPEND:
        oflags = O_WRONLY | O_NOCTTY | O_APPEND; 
	exclusive = 1;
	break;
      case OPENFILE_WRITE:
	oflags = O_WRONLY | O_NOCTTY;
	exclusive = 1;
	break;
    case OPENFILE_READ_ONLY:
{'fraction_non_alphanumeric': 0.11044691147167662, 'fraction_numerical': 0.02391118701964133, 'mean_word_length': 3.2134292565947242, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5189065', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1340, 'n_words': 373}