Subversion Repositories HelenOS

Rev

Rev 2374 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 1987,1997, Prentice Hall
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use of the MINIX operating system in source and
  6.  * binary forms, with or without modification, are permitted provided
  7.  * that the following conditions are met:
  8.  
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  
  12.  * - Redistributions in binary form must reproduce the above
  13.  *   copyright notice, this list of conditions and the following
  14.  *   disclaimer in the documentation and/or other materials provided
  15.  *   with the distribution.
  16.  
  17.  * - Neither the name of Prentice Hall nor the names of the software
  18.  *   authors or contributors may be used to endorse or promote
  19.  *   products derived from this software without specific prior
  20.  *   written permission.
  21.  
  22.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
  23.  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26.  * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
  27.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  32.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  33.  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34.  */
  35.  
  36.  
  37. /* This file contains the procedures for opening, closing, and seeking on files. */
  38.  
  39. /* Methods:
  40.  * do_open:   perform the OPEN system call
  41.  * do_close:  perform the CLOSE system call
  42.  * do_lseek:  perform the SEEK system call
  43.  */
  44.  
  45.  
  46. #include "fs.h"
  47. #include "block.h"
  48. #include "file.h"
  49. #include "fproc.h"
  50. #include "inode.h"
  51. #include "param.h"
  52.  
  53.    
  54. int do_open()
  55. {
  56.      
  57.     /* Perform the open(file_name) system call */
  58.      
  59.     int r;
  60.     register inode_t *rip;
  61.     filp_t *fil_ptr;
  62.        
  63.    
  64.     r = fetch_name(file_name, strlen(file_name)+1);
  65.     if (r != OK)
  66.         return err_code; /* name was bad */
  67.      
  68.     /* See if file descriptor and filp slots are available. */
  69.     if ( (r = get_fd(0, &fd, &fil_ptr)) != OK)
  70.         return r;
  71.  
  72.     /* Scan path name. */
  73.     if ( (rip = eat_path(user_path)) == NIL_INODE)
  74.         return err_code;
  75.      
  76.     /* Claim the file descriptor and filp slot and fill them in. */
  77.     fp->fp_filp[fd] = fil_ptr;
  78.     fil_ptr->filp_count = 1;
  79.     fil_ptr->filp_ino = rip;
  80.     fil_ptr->filp_flags = R_BIT;
  81.    
  82.     return fd;
  83. }
  84.      
  85. int do_close()
  86. {
  87.  
  88.     /* Perform the close(fd) system call. */
  89.    
  90.     register filp_t *rfilp;
  91.     register inode_t *rip;
  92.      
  93.      
  94.     /* First locate the inode that belongs to the file descriptor */
  95.     if ( (rfilp = get_filp(fd)) == NIL_FILP)
  96.         return err_code;
  97.  
  98.     rip = rfilp->filp_ino;        /* 'rip' points to the inode */
  99.  
  100.     if (--rfilp->filp_count == 0) {
  101.         put_inode(rip);
  102.     }
  103.  
  104.     fp->fp_filp[fd] = NIL_FILP;
  105.      
  106.     return OK;
  107. }
  108.    
  109. int do_lseek()
  110. {
  111.    
  112.     /* Perform the seek(ls_fd, offset, whence) system call. */
  113.    
  114.     register filp_t *rfilp;
  115.     register offset_t pos, new_pos;
  116.      
  117.      
  118.     /* Check to see if the file descriptor is valid. */
  119.     if ( (rfilp = get_filp(fd)) == NIL_FILP)
  120.         return err_code;
  121.    
  122.    
  123.     /* The value of 'whence' determines the start position to use. */
  124.     switch(whence) {
  125.         case 0: pos = 0;
  126.             break;
  127.             case 1: pos = rfilp->filp_pos;  
  128.             break;
  129.             case 2: pos = rfilp->filp_ino->i_size;  
  130.             break;
  131.             default: return FS_EINVAL;
  132.     }
  133.    
  134.     /* Check for overflow. */
  135.     if (((long)offset > 0) && ((long)(pos + offset) < (long)pos))
  136.         return FS_EINVAL;
  137.     if (((long)offset < 0) && ((long)(pos + offset) > (long)pos))
  138.         return FS_EINVAL;
  139.      
  140.     new_pos = pos + offset;
  141.    
  142.     /* Check for setting behind the end. */
  143.     if ((new_pos) > rfilp->filp_ino->i_size) {
  144.         pos = rfilp->filp_ino->i_size;
  145.     }
  146.     else if (new_pos < 0) {
  147.           pos = 0;
  148.     }
  149.     else {
  150.         pos = new_pos;
  151.     }
  152.     rfilp->filp_pos = pos;
  153.  
  154.     return pos;
  155. }
  156.  
  157.