Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/src/runtime/Builtin/cOpen.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "haskell2c.h"


/* foreign import openFileC :: CString -> IOMode -> IO ForeignObj */
/*   Note: the return value is a ForeignObj, which is not strictly
 *   legal by the FFI standard.  However, it makes sense here, because
 *   the finaliser is in C, not in Haskell.
 */
void* openFileC (char* filename, int iom)
{
  char *type;
  FILE *fp;
  ForeignObj *fo;

  switch (iom) {
    case ReadMode:      type = "r";  break;
    case WriteMode:     type = "w";  break;
    case AppendMode:    type = "a";  break;
    case ReadWriteMode: type = "r+"; break;
  }
/*fprintf(stderr,"fopen: attempting to open file %s for %s\n",filename,type);*/

  fp = fopen(filename,type);
  if(fp) {
    FileDesc *a;
    /*fprintf(stderr,"fopen: succeeded\n");*/
    a = (FileDesc *)malloc(sizeof(FileDesc));
    a->fp = fp;
    a->bm = _IOFBF;
    a->size = -1;
    a->path = strdup(filename);
    fo = allocForeignObj(a,gcFile,gcNow);
    /*fprintf(stderr,"[openFileC: succeeded %x %x]\n",a,a->fp);*/
  } else {
    fo = allocForeignObj((void*)0,(void*)0,gcNone);
    /*fprintf(stderr,"fopen: failed to open file %s for %s\n",filename,type);*/
  }
  return (void*)nhc_mkCInt((int)fo);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.