home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / fhopen.lha / fhopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-08  |  2.3 KB  |  98 lines

  1. /* fhopen/fhclose - build file pointer from file handle (DICE)
  2.  *
  3.  * Copyright (C) 1993,1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose and without fee is hereby granted, provided
  7.  * that the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation.  This software is provided "as is" without express or
  10.  * implied warranty.
  11.  *
  12.  *  $VER: fhopen.c 2.0 (8.12.94)
  13.  */
  14. #include <exec/libraries.h>
  15. #include <dos/dos.h>
  16. #include <proto/dos.h>
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include <lib/misc.h>
  22.  
  23.  
  24. /* fhopen -
  25.  * build stdio file pointer for an open AmigaDOS file handle
  26.  */
  27. FILE *
  28. fhopen(fh, modes)
  29.     BPTR fh;
  30.     const char *modes;
  31. {
  32.     int fd;
  33.     _IOFDS *d = _MakeFD(&fd);
  34.  
  35.     if( d ) {
  36.         char *p;
  37.         short fdmode = O_ISOPEN;
  38.  
  39.         for( p = modes; *p; p++ ) {
  40.             switch( *p ) {
  41.                 case 'r':
  42.                     fdmode |= O_RDONLY;
  43.                     break;
  44.                 case 'w':
  45.                     fdmode |= O_WRONLY; /* | O_CREAT | O_TRUNC */
  46.                     break;
  47.                 case '+':
  48.                     fdmode |= O_RDWR;
  49.                     fdmode &= ~(O_RDONLY|O_WRONLY);
  50.                     break;
  51.                 case 'b':
  52.                     fdmode |= O_BINARY;
  53.                     break;
  54.                 case 'a':
  55.                     fdmode |= O_APPEND; /* | O_CREAT */
  56.                     break;
  57.             }
  58.         }
  59.  
  60.         d->fd_Fh = fh;
  61.         d->fd_FileName = NULL;
  62.         d->fd_Flags |= fdmode;
  63.  
  64.         if( strlen(modes) < 14 ) {
  65.             FILE *fp;
  66.             char buf[16];
  67.  
  68.             buf[0] = 'F';
  69.             strcpy(buf + 1, modes);
  70.             if( fp = fopen((char *)fd, buf) ) {
  71.                 if( fp->sd_Flags & __SIF_APPEND )
  72.                     Seek(fh, 0L, OFFSET_END);
  73.             }
  74.             return fp;
  75.         }
  76.     }
  77.     return (FILE *)NULL;
  78. }
  79.  
  80.  
  81. /* fhclose -
  82.  * close file pointer from fhopen() without Close()ing file handle
  83.  */
  84. int
  85. fhclose(fp)
  86.     FILE *fp;
  87. {
  88.     int err = EOF;
  89.     _IOFDS *d;
  90.  
  91.     if( fp && (d = __getfh(fp->sd_Fd)) ) {
  92.         d->fd_Flags |= O_NOCLOSE;
  93.         err = fclose(fp);
  94.     }
  95.     return err;
  96. }
  97.  
  98.