		/* Copyright 2000-2004 The Apache Software Foundation
		 *
		 * Licensed under the Apache License, Version 2.0 (the "License");
		 * you may not use this file except in compliance with the License.
		 * You may obtain a copy of the License at
		 *
		 *     http://www.apache.org/licenses/LICENSE-2.0
		 *
		 * Unless required by applicable law or agreed to in writing, software
		 * distributed under the License is distributed on an "AS IS" BASIS,
		 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		 * See the License for the specific language governing permissions and
		 * limitations under the License.
		 */
		
		#include "apr_arch_file_io.h"
		#include "apr_strings.h"
		#include "apr_portable.h"
		#include "apr_thread_mutex.h"
		#include "apr_arch_inherit.h"
		
		#ifdef NETWARE
		#include "nks/dirio.h"
		#include "apr_hash.h"
		#include "fsio.h"
		#endif
		
		apr_status_t apr_unix_file_cleanup(void *thefile)
         166    {
         166        apr_file_t *file = thefile;
         166        apr_status_t flush_rv = APR_SUCCESS, rv = APR_SUCCESS;
		
         166        if (file->buffered) {
           2            flush_rv = apr_file_flush(file);
		    }
         166        if (close(file->filedes) == 0) {
         166            file->filedes = -1;
         166            if (file->flags & APR_DELONCLOSE) {
          11                unlink(file->fname);
		        }
		#if APR_HAS_THREADS
         166            if (file->thlock) {
      ######                rv = apr_thread_mutex_destroy(file->thlock);
		        }
		#endif
		    }
		    else {
		        /* Are there any error conditions other than EINTR or EBADF? */
      ######            rv = errno;
		    }
         166        return rv != APR_SUCCESS ? rv : flush_rv;
		}
		
		APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new, 
		                                        const char *fname, 
		                                        apr_int32_t flag, 
		                                        apr_fileperms_t perm, 
		                                        apr_pool_t *pool)
          55    {
          55        apr_os_file_t fd;
          55        int oflags = 0;
		#if APR_HAS_THREADS
          55        apr_thread_mutex_t *thlock;
          55        apr_status_t rv;
		#endif
		
          55        if ((flag & APR_READ) && (flag & APR_WRITE)) {
          11            oflags = O_RDWR;
		    }
          44        else if (flag & APR_READ) {
          22            oflags = O_RDONLY;
		    }
          22        else if (flag & APR_WRITE) {
          21            oflags = O_WRONLY;
		    }
		    else {
           1            return APR_EACCES; 
		    }
		
          54        if (flag & APR_CREATE) {
          24            oflags |= O_CREAT; 
          24            if (flag & APR_EXCL) {
           5                oflags |= O_EXCL;
		        }
		    }
          54        if ((flag & APR_EXCL) && !(flag & APR_CREATE)) {
      ######            return APR_EACCES;
		    }   
		
          54        if (flag & APR_APPEND) {
           1            oflags |= O_APPEND;
		    }
          54        if (flag & APR_TRUNCATE) {
           9            oflags |= O_TRUNC;
		    }
		#ifdef O_BINARY
		    if (flag & APR_BINARY) {
		        oflags |= O_BINARY;
		    }
		#endif
		    
		#if APR_HAS_THREADS
          54        if ((flag & APR_BUFFERED) && (flag & APR_XTHREAD)) {
      ######            rv = apr_thread_mutex_create(&thlock,
		                                     APR_THREAD_MUTEX_DEFAULT, pool);
      ######            if (rv) {
      ######                return rv;
		        }
		    }
		#endif
		
          54        if (perm == APR_OS_DEFAULT) {
          24            fd = open(fname, oflags, 0666);
		    }
		    else {
          30            fd = open(fname, oflags, apr_unix_perms2mode(perm));
		    } 
          54        if (fd < 0) {
           3           return errno;
		    }
		
          51        (*new) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
          51        (*new)->pool = pool;
          51        (*new)->flags = flag;
          51        (*new)->filedes = fd;
		
          51        (*new)->fname = apr_pstrdup(pool, fname);
		
          51        (*new)->blocking = BLK_ON;
          51        (*new)->buffered = (flag & APR_BUFFERED) > 0;
		
          51        if ((*new)->buffered) {
           2            (*new)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
		#if APR_HAS_THREADS
           2            if ((*new)->flags & APR_XTHREAD) {
      ######                (*new)->thlock = thlock;
		        }
		#endif
		    }
		    else {
          49            (*new)->buffer = NULL;
		    }
		
          51        (*new)->is_pipe = 0;
          51        (*new)->timeout = -1;
          51        (*new)->ungetchar = -1;
          51        (*new)->eof_hit = 0;
          51        (*new)->filePtr = 0;
          51        (*new)->bufpos = 0;
          51        (*new)->dataRead = 0;
          51        (*new)->direction = 0;
		#ifndef WAITIO_USES_POLL
		    /* Create a pollset with room for one descriptor. */
		    /* ### check return codes */
		    (void) apr_pollset_create(&(*new)->pollset, 1, pool, 0);
		#endif
          51        if (!(flag & APR_FILE_NOCLEANUP)) {
          51            apr_pool_cleanup_register((*new)->pool, (void *)(*new), 
		                                  apr_unix_file_cleanup, 
		                                  apr_unix_file_cleanup);
		    }
          51        return APR_SUCCESS;
		}
		
		APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
          83    {
          83        return apr_pool_cleanup_run(file->pool, file, apr_unix_file_cleanup);
		}
		
		APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool)
          19    {
          19        if (unlink(path) == 0) {
          14            return APR_SUCCESS;
		    }
		    else {
           5            return errno;
		    }
		}
		
		APR_DECLARE(apr_status_t) apr_file_rename(const char *from_path, 
		                                          const char *to_path,
		                                          apr_pool_t *p)
      ######    {
      ######        if (rename(from_path, to_path) != 0) {
      ######            return errno;
		    }
      ######        return APR_SUCCESS;
		}
		
		APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile, 
		                                          apr_file_t *file)
      ######    {
      ######        *thefile = file->filedes;
      ######        return APR_SUCCESS;
		}
		
		APR_DECLARE(apr_status_t) apr_os_file_put(apr_file_t **file, 
		                                          apr_os_file_t *thefile,
		                                          apr_int32_t flags, apr_pool_t *pool)
           7    {
           7        int *dafile = thefile;
		    
           7        (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
           7        (*file)->pool = pool;
           7        (*file)->eof_hit = 0;
           7        (*file)->blocking = BLK_UNKNOWN; /* in case it is a pipe */
           7        (*file)->timeout = -1;
           7        (*file)->ungetchar = -1; /* no char avail */
           7        (*file)->filedes = *dafile;
           7        (*file)->flags = flags | APR_FILE_NOCLEANUP;
           7        (*file)->buffered = (flags & APR_BUFFERED) > 0;
		
		#ifndef WAITIO_USES_POLL
		    /* Create a pollset with room for one descriptor. */
		    /* ### check return codes */
		    (void) apr_pollset_create(&(*file)->pollset, 1, pool, 0);
		#endif
		
           7        if ((*file)->buffered) {
      ######            (*file)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
		#if APR_HAS_THREADS
      ######            if ((*file)->flags & APR_XTHREAD) {
      ######                apr_status_t rv;
      ######                rv = apr_thread_mutex_create(&((*file)->thlock),
		                                         APR_THREAD_MUTEX_DEFAULT, pool);
      ######                if (rv) {
      ######                    return rv;
		            }
		        }
		#endif
		    }
           7        return APR_SUCCESS;
		}    
		
		APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
      ######    {
      ######        if (fptr->eof_hit == 1) {
      ######            return APR_EOF;
		    }
      ######        return APR_SUCCESS;
		}   
		
		APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile, 
		                                               apr_pool_t *pool)
           2    {
           2        int fd = STDERR_FILENO;
		
           2        return apr_os_file_put(thefile, &fd, 0, pool);
		}
		
		APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile, 
		                                               apr_pool_t *pool)
           1    {
           1        int fd = STDOUT_FILENO;
		
           1        return apr_os_file_put(thefile, &fd, 0, pool);
		}
		
		APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile, 
		                                              apr_pool_t *pool)
           1    {
           1        int fd = STDIN_FILENO;
		
           1        return apr_os_file_put(thefile, &fd, 0, pool);
		}
		
      ######    APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_unix_file_cleanup)
		
      ######    APR_IMPLEMENT_INHERIT_UNSET(file, flags, pool, apr_unix_file_cleanup)
		
           9    APR_POOL_IMPLEMENT_ACCESSOR(file)
