"Fossies" - the Fresh Open Source Software Archive

Member "alsa-oss-1.1.8/alsa/stdioemu.c" (7 Jan 2019, 3049 Bytes) of package /linux/misc/alsa-oss-1.1.8.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stdioemu.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.1.6_vs_1.1.8.

    1 /*
    2 
    3     Copyright (C) 2000 Stefan Westerfeld
    4                        stefan@space.twc.de
    5 
    6     This library is free software; you can redistribute it and/or
    7     modify it under the terms of the GNU Library General Public
    8     License as published by the Free Software Foundation; either
    9     version 2 of the License, or (at your option) any later version.
   10   
   11     This library is distributed in the hope that it will be useful,
   12     but WITHOUT ANY WARRANTY; without even the implied warranty of
   13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   14     Library General Public License for more details.
   15    
   16     You should have received a copy of the GNU Library General Public License
   17     along with this library; see the file COPYING.LIB.  If not, write to
   18     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   19     Boston, MA  02110-1301, USA
   20 
   21     Modified to add support for 64 bit fopen by Mike Hearn <mike@navi.cx>
   22 */
   23 
   24 /*
   25  * This source only exists because some very special programs think that
   26  * it is a very special idea to access /dev/dsp by the means of stdio, so
   27  * we need to fake FILE* access for artsdsp as well.
   28  *
   29  * To do so, it relies on glibc internals, so that it will probably not work
   30  * on other systems - but then again, it might not be necessary on other
   31  * systems, when fopen properly calls open, it might as well work unchanged.
   32  */
   33 
   34 
   35 #ifndef _GNU_SOURCE
   36 #define _GNU_SOURCE
   37 #endif
   38 
   39 #include <stdio.h>
   40 
   41 struct fd_cookie {
   42     int fd;
   43 };
   44 
   45 static ssize_t fdc_read (void *cookie, char *buffer, size_t size)
   46 {
   47     struct fd_cookie *fdc = (struct fd_cookie *)cookie;
   48     return read(fdc->fd, buffer, size);
   49 }
   50 
   51 static ssize_t fdc_write (void *cookie, const char *buffer, size_t size)
   52 {
   53     struct fd_cookie *fdc = (struct fd_cookie *)cookie;
   54     return write(fdc->fd, buffer, size);
   55 }
   56 
   57 static int fdc_seek (void* cookie ATTRIBUTE_UNUSED,
   58              off64_t* position ATTRIBUTE_UNUSED,
   59              int whence ATTRIBUTE_UNUSED)
   60 {
   61     return -1;
   62 }
   63 
   64 static int fdc_clean (void *cookie)
   65 {
   66     struct fd_cookie *fdc = (struct fd_cookie *)cookie;
   67     int result = close(fdc->fd);
   68     free(cookie);
   69     return result;
   70 }
   71 
   72 static FILE *fake_fopen(const char *path, const char *mode, int flags)
   73 {
   74     cookie_io_functions_t fns = { fdc_read, fdc_write, fdc_seek, fdc_clean };
   75     struct fd_cookie *fdc = (struct fd_cookie *)malloc(sizeof(struct fd_cookie));
   76     const char *mptr;
   77     int open_mode = 0;
   78     FILE *result = 0;
   79 
   80     for(mptr = mode; *mptr; mptr++) {
   81         if(*mptr == 'r') open_mode |= 1; /* 1 = read */
   82         if(*mptr == 'w') open_mode |= 2; /* 2 = write */
   83         if(*mptr == '+') open_mode |= 3; /* 3 = readwrite */
   84         if(*mptr == 'a') open_mode |= 2; /* append -> write */
   85     }
   86 
   87     switch (open_mode) {
   88     case 1:
   89         fdc->fd = open(path, O_RDONLY | flags, 0666);
   90         break;
   91     case 2:
   92         fdc->fd = open(path, O_WRONLY | flags, 0666);
   93         break;
   94     default:
   95         fdc->fd = open(path, O_RDWR | flags, 0666);
   96         break;
   97     }
   98 
   99     if (open_mode && fdc->fd > 0) {
  100         result = fopencookie (fdc,"w", fns);
  101         result->_fileno = fdc->fd;      /* ugly patchy slimy kludgy hack */
  102     }
  103     return result;
  104 }