| 1 | 
         2 | 
         alfik | 
         /******************************************************************************
  | 
      
      
         | 2 | 
          | 
          | 
         *                                                                             *
  | 
      
      
         | 3 | 
          | 
          | 
         * License Agreement                                                           *
  | 
      
      
         | 4 | 
          | 
          | 
         *                                                                             *
  | 
      
      
         | 5 | 
          | 
          | 
         * Copyright (c) 2004 Altera Corporation, San Jose, California, USA.           *
  | 
      
      
         | 6 | 
          | 
          | 
         * All rights reserved.                                                        *
  | 
      
      
         | 7 | 
          | 
          | 
         *                                                                             *
  | 
      
      
         | 8 | 
          | 
          | 
         * Permission is hereby granted, free of charge, to any person obtaining a     *
  | 
      
      
         | 9 | 
          | 
          | 
         * copy of this software and associated documentation files (the "Software"),  *
  | 
      
      
         | 10 | 
          | 
          | 
         * to deal in the Software without restriction, including without limitation   *
  | 
      
      
         | 11 | 
          | 
          | 
         * the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
  | 
      
      
         | 12 | 
          | 
          | 
         * and/or sell copies of the Software, and to permit persons to whom the       *
  | 
      
      
         | 13 | 
          | 
          | 
         * Software is furnished to do so, subject to the following conditions:        *
  | 
      
      
         | 14 | 
          | 
          | 
         *                                                                             *
  | 
      
      
         | 15 | 
          | 
          | 
         * The above copyright notice and this permission notice shall be included in  *
  | 
      
      
         | 16 | 
          | 
          | 
         * all copies or substantial portions of the Software.                         *
  | 
      
      
         | 17 | 
          | 
          | 
         *                                                                             *
  | 
      
      
         | 18 | 
          | 
          | 
         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
  | 
      
      
         | 19 | 
          | 
          | 
         * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
  | 
      
      
         | 20 | 
          | 
          | 
         * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  | 
      
      
         | 21 | 
          | 
          | 
         * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
  | 
      
      
         | 22 | 
          | 
          | 
         * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
  | 
      
      
         | 23 | 
          | 
          | 
         * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
  | 
      
      
         | 24 | 
          | 
          | 
         * DEALINGS IN THE SOFTWARE.                                                   *
  | 
      
      
         | 25 | 
          | 
          | 
         *                                                                             *
  | 
      
      
         | 26 | 
          | 
          | 
         * This agreement shall be governed in all respects by the laws of the State   *
  | 
      
      
         | 27 | 
          | 
          | 
         * of California and by the laws of the United States of America.              *
  | 
      
      
         | 28 | 
          | 
          | 
         *                                                                             *
  | 
      
      
         | 29 | 
          | 
          | 
         * Altera does not recommend, suggest or require that this reference design    *
  | 
      
      
         | 30 | 
          | 
          | 
         * file be used in conjunction or combination with any other product.          *
  | 
      
      
         | 31 | 
          | 
          | 
         ******************************************************************************/
  | 
      
      
         | 32 | 
          | 
          | 
          
  | 
      
      
         | 33 | 
          | 
          | 
         #include <unistd.h>
  | 
      
      
         | 34 | 
          | 
          | 
         #include <fcntl.h>
  | 
      
      
         | 35 | 
          | 
          | 
          
  | 
      
      
         | 36 | 
          | 
          | 
         #include <stdio.h>
  | 
      
      
         | 37 | 
          | 
          | 
         #include <stdarg.h>
  | 
      
      
         | 38 | 
          | 
          | 
          
  | 
      
      
         | 39 | 
          | 
          | 
         #include "sys/alt_errno.h"
  | 
      
      
         | 40 | 
          | 
          | 
         #include "priv/alt_file.h"
  | 
      
      
         | 41 | 
          | 
          | 
         #include "alt_types.h"
  | 
      
      
         | 42 | 
          | 
          | 
         #include "os/alt_syscall.h"
  | 
      
      
         | 43 | 
          | 
          | 
          
  | 
      
      
         | 44 | 
          | 
          | 
         #define ALT_FCNTL_FLAGS_MASK ((alt_u32) (O_APPEND | O_NONBLOCK))
  | 
      
      
         | 45 | 
          | 
          | 
          
  | 
      
      
         | 46 | 
          | 
          | 
         /*
  | 
      
      
         | 47 | 
          | 
          | 
          * fcntl() is a limited implementation of the standard fcntl() system call.
  | 
      
      
         | 48 | 
          | 
          | 
          * It can be used to change the state of the flags associated with an open
  | 
      
      
         | 49 | 
          | 
          | 
          * file descriptor. Normally these flags are set during the call to
  | 
      
      
         | 50 | 
          | 
          | 
          * open(). It is anticipated that the main use of this function will be to
  | 
      
      
         | 51 | 
          | 
          | 
          * change the state of a device from blocking to non-blocking (where this is
  | 
      
      
         | 52 | 
          | 
          | 
          * supported).
  | 
      
      
         | 53 | 
          | 
          | 
          *
  | 
      
      
         | 54 | 
          | 
          | 
          * The input argument "fd" is the file descriptor to be manipulated. "cmd"
  | 
      
      
         | 55 | 
          | 
          | 
          * is the command to execute. This can be either F_GETFL (return the
  | 
      
      
         | 56 | 
          | 
          | 
          * current value of the flags) or F_SETFL (set the value of the flags).
  | 
      
      
         | 57 | 
          | 
          | 
          *
  | 
      
      
         | 58 | 
          | 
          | 
          * If "cmd" is F_SETFL then the argument "arg" is the new value of flags,
  | 
      
      
         | 59 | 
          | 
          | 
          * otherwise "arg" is ignored. Only the flags: O_APPEND and O_NONBLOCK
  | 
      
      
         | 60 | 
          | 
          | 
          * can be updated by a call to fcntl(). All other flags remain
  | 
      
      
         | 61 | 
          | 
          | 
          * unchanged.
  | 
      
      
         | 62 | 
          | 
          | 
          *
  | 
      
      
         | 63 | 
          | 
          | 
          * ALT_FCNTL is mapped onto the fcntl() system call in alt_syscall.h
  | 
      
      
         | 64 | 
          | 
          | 
          */
  | 
      
      
         | 65 | 
          | 
          | 
          
  | 
      
      
         | 66 | 
          | 
          | 
         int ALT_FCNTL (int file, int cmd, ...)
  | 
      
      
         | 67 | 
          | 
          | 
         {
  | 
      
      
         | 68 | 
          | 
          | 
           alt_fd*  fd;
  | 
      
      
         | 69 | 
          | 
          | 
           long     flags;
  | 
      
      
         | 70 | 
          | 
          | 
           va_list  argp;
  | 
      
      
         | 71 | 
          | 
          | 
          
  | 
      
      
         | 72 | 
          | 
          | 
           /*
  | 
      
      
         | 73 | 
          | 
          | 
            * A common error case is that when the file descriptor was created, the call
  | 
      
      
         | 74 | 
          | 
          | 
            * to open() failed resulting in a negative file descriptor. This is trapped
  | 
      
      
         | 75 | 
          | 
          | 
            * below so that we don't try and process an invalid file descriptor.
  | 
      
      
         | 76 | 
          | 
          | 
            */
  | 
      
      
         | 77 | 
          | 
          | 
          
  | 
      
      
         | 78 | 
          | 
          | 
           fd = (file < 0) ? NULL : &alt_fd_list[file];
  | 
      
      
         | 79 | 
          | 
          | 
          
  | 
      
      
         | 80 | 
          | 
          | 
           if (fd)
  | 
      
      
         | 81 | 
          | 
          | 
           {
  | 
      
      
         | 82 | 
          | 
          | 
             switch (cmd)
  | 
      
      
         | 83 | 
          | 
          | 
             {
  | 
      
      
         | 84 | 
          | 
          | 
             case F_GETFL:
  | 
      
      
         | 85 | 
          | 
          | 
               return fd->fd_flags & ~((alt_u32) ALT_FD_FLAGS_MASK);
  | 
      
      
         | 86 | 
          | 
          | 
             case F_SETFL:
  | 
      
      
         | 87 | 
          | 
          | 
               va_start(argp, cmd);
  | 
      
      
         | 88 | 
          | 
          | 
               flags = va_arg(argp, long);
  | 
      
      
         | 89 | 
          | 
          | 
               fd->fd_flags &= ~ALT_FCNTL_FLAGS_MASK;
  | 
      
      
         | 90 | 
          | 
          | 
               fd->fd_flags |= (flags & ALT_FCNTL_FLAGS_MASK);
  | 
      
      
         | 91 | 
          | 
          | 
               va_end(argp);
  | 
      
      
         | 92 | 
          | 
          | 
               return 0;
  | 
      
      
         | 93 | 
          | 
          | 
             default:
  | 
      
      
         | 94 | 
          | 
          | 
               ALT_ERRNO = EINVAL;
  | 
      
      
         | 95 | 
          | 
          | 
               return -1;
  | 
      
      
         | 96 | 
          | 
          | 
             }
  | 
      
      
         | 97 | 
          | 
          | 
           }
  | 
      
      
         | 98 | 
          | 
          | 
          
  | 
      
      
         | 99 | 
          | 
          | 
           ALT_ERRNO = EBADFD;
  | 
      
      
         | 100 | 
          | 
          | 
           return -1;
  | 
      
      
         | 101 | 
          | 
          | 
         }
  |