$NetBSD: patch-ag,v 1.1.1.1 1999/12/04 12:56:31 scw Exp $

--- libaudiofile/audiofile.c.orig	Fri Nov 12 17:19:12 1999
+++ libaudiofile/audiofile.c	Tue Nov 30 21:55:15 1999
@@ -528,25 +528,39 @@
   const char *mode,
   AFfilesetup setup)
 {
-  int file;
+  fhandle file;
   AFfilehandle tempHandle;
 
   if (!mode) return AF_NULL_FILEHANDLE;
 
   if (!strcmp (mode,"r"))
   {
+#ifndef USE_STDIO
     file = open (name,O_RDONLY);
     if (file == -1) return AF_NULL_FILEHANDLE;
     if ((tempHandle = AFopenfd (file,mode,setup)) == AF_NULL_FILEHANDLE)
       close (file);
+#else
+    file = fopen (name,"rb");
+    if (file == NULL) return AF_NULL_FILEHANDLE;
+    if ((tempHandle = AFopenfd (file,mode,setup)) == AF_NULL_FILEHANDLE)
+      fclose (file);
+#endif
   }
   else if (!strcmp (mode,"w"))
   {
+#ifndef USE_STDIO
     // NB decimal 438 = octal 666
     file = open (name,O_RDWR | O_CREAT | O_TRUNC,438);
     if (file == -1) return AF_NULL_FILEHANDLE;
     if ((tempHandle = AFopenfd (file,mode,setup)) == AF_NULL_FILEHANDLE)
       close (file);
+#else
+    file = fopen (name,"wb");
+    if (file == NULL) return AF_NULL_FILEHANDLE;
+    if ((tempHandle = AFopenfd (file,mode,setup)) == AF_NULL_FILEHANDLE)
+      fclose (file);
+#endif
   }
   else
   {
@@ -558,7 +572,7 @@
 /*---------------------------------------------------------------------------
 | FUNCTION AFopenfd
 ---------------------------------------------------------------------------*/
-AFfilehandle AFopenfd (int file,const char *mode,AFfilesetup setup)
+AFfilehandle AFopenfd (fhandle file,const char *mode,AFfilesetup setup)
 {
   if (!mode) return AF_NULL_FILEHANDLE;
   if (!strcmp (mode,"r"))
@@ -628,7 +642,7 @@
 /*---------------------------------------------------------------------------
 | FUNCTION AFreadAIFF
 ---------------------------------------------------------------------------*/
-AFfilehandle AFreadAIFF (int file)
+AFfilehandle AFreadAIFF (fhandle file)
 {
   int i;
   int j;
@@ -692,7 +706,11 @@
   else
     fail ("AIFF header does not contain a valid form type\n")
 
+#ifndef USE_STDIO
   while (!done && !error) // && !feof (file))
+#else
+  while (!done && !error && !feof (file))
+#endif
   {
     if (!idRead (file,id))
     {
@@ -753,14 +771,22 @@
       uiread (blocksize)
 
       // Skip past data for now
+#ifndef USE_STDIO
       handle->ssndStart = lseek (file,0L,SEEK_CUR);
+#else
+      handle->ssndStart = ftell (file);
+#endif
       if (handle->ssndStart == -1) aiffError
       handle->ssndStart += offset;
       if (chunksize >= 8)
       {
         seeksize = chunksize - 8;
         if (seeksize % 2) seeksize++;
+#ifndef USE_STDIO
         if (lseek (file,seeksize,SEEK_CUR) == -1) aiffError
+#else
+        if (fseek (file,seeksize,SEEK_CUR) == -1) aiffError
+#endif
       }
       else
       {
@@ -817,7 +843,11 @@
           {
             comment [i].text =
               (char *) calloc (comment [i].count + 1,sizeof (char));
+#ifndef USE_STDIO
             if (read (file,comment [i].text,comment [i].count)
+#else
+            if (fread (comment [i].text,1,comment [i].count,file)
+#endif
               != comment [i].count)
             {
               if (comment)
@@ -902,7 +932,11 @@
       if (chunksize != 24) aiffError
 
       handle->aiff.aesdatapresent = TRUE;
+#ifndef USE_STDIO
       if (read (file,handle->aiff.aesdata,24) != 24) aiffError
+#else
+      if (fread (handle->aiff.aesdata,1,24,file) != 24) aiffError
+#endif
       aesd = TRUE;
     }
     else if (!strncmp (id,"NAME",4))
@@ -919,7 +953,11 @@
         handle->aiff.misc [0].current = 0;
         handle->aiff.misc [0].data    =
           (char *) calloc (chunksize,sizeof (char));
+#ifndef USE_STDIO
         if (read (file,handle->aiff.misc [0].data,chunksize)
+#else
+        if (fread (handle->aiff.misc [0].data,1,chunksize,file)
+#endif
           != chunksize)
           aiffError
         if (chunksize % 2) ucread (uctemp)
@@ -945,7 +983,11 @@
         handle->aiff.misc [1].current = 0;
         handle->aiff.misc [1].data    =
           (char *) calloc (chunksize,sizeof (char));
+#ifndef USE_STDIO
         if (read (file,handle->aiff.misc [1].data,chunksize)
+#else
+        if (fread (handle->aiff.misc [1].data,1,chunksize,file)
+#endif
           != chunksize)
           aiffError
         if (chunksize % 2) ucread (uctemp)
@@ -971,7 +1013,11 @@
         handle->aiff.misc [2].current = 0;
         handle->aiff.misc [2].data    =
           (char *) calloc (chunksize,sizeof (char));
+#ifndef USE_STDIO
         if (read (file,handle->aiff.misc [2].data,chunksize)
+#else
+        if (fread (handle->aiff.misc [2].data,1,chunksize,file)
+#endif
           != chunksize)
           aiffError
         if (chunksize % 2) ucread (uctemp)
@@ -1007,7 +1053,11 @@
         handle->aiff.misc [3].current = 0;
         handle->aiff.misc [3].data    =
           (char *) calloc (chunksize,sizeof (char));
+#ifndef USE_STDIO
         if (read (file,handle->aiff.misc [3].data,chunksize)
+#else
+        if (fread (handle->aiff.misc [3].data,1,chunksize,file)
+#endif
           != chunksize)
           aiffError
         if (chunksize % 2) ucread (uctemp)
@@ -1043,7 +1093,11 @@
         handle->aiff.misc [4].current = 0;
         handle->aiff.misc [4].data    =
           (char *) calloc (chunksize,sizeof (char));
+#ifndef USE_STDIO
         if (read (file,handle->aiff.misc [4].data,chunksize)
+#else
+        if (fread (handle->aiff.misc [4].data,1,chunksize,file)
+#endif
           != chunksize)
           aiffError
         if (chunksize % 2) ucread (uctemp)
@@ -1079,7 +1133,11 @@
         handle->aiff.misc [5].current = 0;
         handle->aiff.misc [5].data    =
           (char *) calloc (chunksize,sizeof (char));
+#ifndef USE_STDIO
         if (read (file,handle->aiff.misc [5].data,chunksize)
+#else
+        if (fread (handle->aiff.misc [5].data,1,chunksize,file)
+#endif
           != chunksize)
           aiffError
         if (chunksize % 2) ucread (uctemp)
@@ -1100,7 +1158,11 @@
       // Skip past data
       seeksize = chunksize;
       if (seeksize % 2) seeksize++;
+#ifndef USE_STDIO
       if (lseek (file,seeksize,SEEK_CUR) == -1) aiffError
+#else
+      if (fseek (file,seeksize,SEEK_CUR) == -1) aiffError
+#endif
     }
   }
 
@@ -1121,7 +1183,11 @@
   }
 
   // Read to start of sound data
+#ifndef USE_STDIO
   if (lseek (file,handle->ssndStart,SEEK_SET) == -1)
+#else
+  if (fseek (file,handle->ssndStart,SEEK_SET) == -1)
+#endif
     fail ("error seeking to start of sound data\n")
 
   handle = (AFfilehandle) calloc (1,sizeof (aiffHandle));
@@ -1132,7 +1198,7 @@
 /*---------------------------------------------------------------------------
 | FUNCTION AFwriteAIFF
 ---------------------------------------------------------------------------*/
-AFfilehandle AFwriteAIFF (int file,AFfilesetup setup)
+AFfilehandle AFwriteAIFF (fhandle file,AFfilesetup setup)
 {
   int i;
   int j;
@@ -1309,7 +1375,11 @@
     idwrite ("AESD")
     chunksize = 24;
     iwrite (chunksize)
+#ifndef USE_STDIO
     if (write (file,handle->aiff.aesdata,24) != 24)
+#else
+    if (fwrite (handle->aiff.aesdata,1,24,file) != 24)
+#endif
       return AF_NULL_FILEHANDLE;
     handle->actualBytes += 24;
   }
@@ -1339,7 +1409,11 @@
         iwrite (chunksize)
         if (chunksize)
         {
+#ifndef USE_STDIO
           if (write (file,handle->aiff.misc [i].data,chunksize)
+#else
+          if (fwrite (handle->aiff.misc [i].data,1,chunksize,file)
+#endif
             != chunksize)
             return AF_NULL_FILEHANDLE;
           handle->actualBytes += chunksize;
@@ -1364,7 +1438,10 @@
   // Offset and blocksize zero
   uiwrite (0)
   uiwrite (0)
-  // fflush (file); no flushing needed
+
+#ifdef USE_STDIO
+  fflush (file);
+#endif
 
   handle = (AFfilehandle) calloc (1,sizeof (aiffHandle));
   *handle = handleAct;
@@ -1374,7 +1451,7 @@
 /*---------------------------------------------------------------------------
 | FUNCTION AFgetfd
 ---------------------------------------------------------------------------*/
-int AFgetfd (AFfilehandle handle)
+fhandle AFgetfd (AFfilehandle handle)
 {
   if (!handle) return 0;
   return handle->file;
@@ -1427,7 +1504,7 @@
   double dtemp;
   char *tempString;
   char idtemp [4];
-  int file;
+  fhandle file;
   int byteWidth;
   int bytes;
 
@@ -1439,7 +1516,11 @@
   
   if (handle->mode == READONLY)
   {
+#ifndef USE_STDIO
     if (close (handle->file) == -1) return -1;
+#else
+    if (fclose (handle->file) == -1) return -1;
+#endif
     free (handle);
     return 0;
   }
@@ -1454,7 +1535,11 @@
       ucwrite (0)
     
     // Reset file descriptor
+#ifndef USE_STDIO
     if (lseek (file,0L,SEEK_SET) == -1)
+#else
+    if (fseek (file,0L,SEEK_SET) == -1)
+#endif
     {
       fprintf (stderr,"unable to seek on file\n");
       return -1;
@@ -1605,7 +1690,11 @@
       idupdate ("AESD")
       chunksize = 24;
       iupdate (chunksize)
+#ifndef USE_STDIO
       if (write (file,handle->aiff.aesdata,24) != 24)
+#else
+      if (fwrite (handle->aiff.aesdata,1,24,file) != 24)
+#endif
         return AF_NULL_FILEHANDLE;
     }
     
@@ -1634,7 +1723,11 @@
           iupdate (chunksize)
           if (chunksize)
           {
+#ifndef USE_STDIO
             if (write (file,handle->aiff.misc [i].data,chunksize)
+#else
+            if (fwrite (handle->aiff.misc [i].data,1,chunksize,file)
+#endif
               != chunksize)
               return AF_NULL_FILEHANDLE;
             
@@ -1664,7 +1757,11 @@
     uiupdate (0)
     uiupdate (0)
 
+#ifndef USE_STDIO
     if (close (handle->file) == -1) return -1;
+#else
+    if (fclose (handle->file) == -1) return -1;
+#endif
     free (handle);
     return 0;
   }
@@ -1693,7 +1790,7 @@
 /*---------------------------------------------------------------------------
 | FUNCTION AFidentifyfd
 ---------------------------------------------------------------------------*/
-int AFidentifyfd (int file)
+int AFidentifyfd (fhandle file)
 {
   char id [4];
   int bytes;
@@ -1702,13 +1799,21 @@
   if (!file) return AF_FILE_UNKNOWN;
     
   // Check FORM chunk
+#ifndef USE_STDIO
   if (read (file,id,4) != 4 || strncmp (id,"FORM",4))
+#else
+  if (fread (id,1,4,file) != 4 || strncmp (id,"FORM",4))
+#endif
     return AF_FILE_UNKNOWN;
 
   if (!iRead (file,&bytes))
     return AF_FILE_UNKNOWN;
 
+#ifndef USE_STDIO
   if (read (file,id,4) != 4)
+#else
+  if (fread (id,1,4,file) != 4)
+#endif
     return AF_FILE_UNKNOWN;
 
   if (!strncmp (id,"AIFF",4))
@@ -2379,7 +2484,7 @@
   int i;
   int j;
   int frameCount;
-  int file;
+  fhandle file;
   signed char ctemp;
   short stemp;
   int itemp;
@@ -2393,7 +2498,11 @@
   i = 0;
   frameCount = 0;
   while (handle->actualFrames < handle->aiff.framecount &&
+#ifndef USE_STDIO
     frameCount < count) // && !(feof (file)))
+#else
+    frameCount < count && !(feof (file)))
+#endif
   {
     for (j=0; j<handle->aiff.channels; j++)
     {
@@ -2468,7 +2577,7 @@
   int i;
   int j;
   int frameCount;
-  int file;
+  fhandle file;
   signed char ctemp;
   short stemp;
   int itemp;
@@ -2481,7 +2590,11 @@
 
   i = 0;
   frameCount = 0;
+#ifndef USE_STDIO
   while (frameCount < count) // && !(feof (file)))
+#else
+  while (frameCount < count && !(feof (file)))
+#endif
   {
     for (j=0; j<handle->aiff.channels; j++)
     {
@@ -2520,7 +2633,7 @@
 int AFseekframe (AFfilehandle handle,int trackid,int offset)
 {
   int seeksize;
-  int file;
+  fhandle file;
 
   if (!handle || trackid != AF_DEFAULT_TRACK) return 0;
   if (handle->mode != READONLY) return 0;
@@ -2554,7 +2667,11 @@
   
   seeksize *= handle->aiff.channels;
   handle->actualFrames = offset;
+#ifndef USE_STDIO
   if (lseek (file,seeksize,SEEK_CUR) == -1)
+#else
+  if (fseek (file,seeksize,SEEK_CUR) == -1)
+#endif
     return -1;
   else
     return offset;
