Back to index

glibc  2.9
open.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994, 1995, 1996, 1997, 2002 Free Software Foundation, Inc.
00002    Ported to standalone by Joel Sherrill jsherril@redstone-emh2.army.mil,
00003      On-Line Applications Research Corporation.
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <fcntl.h>
00023 #include <stdarg.h>
00024 #include <stddef.h>
00025 
00026 #include <stdio.h>
00027 #include <bits/stdio_lim.h>
00028 #include <unistd.h>
00029 
00030 #define __DECLARE_FILE_DESCRIPTORS__
00031 
00032 #include "filedesc.h"
00033 
00034 /* Open FILE with access OFLAG.  If OFLAG includes O_CREAT,
00035    a third argument is the file protection.  */
00036 int
00037 __open (file, oflag)
00038      const char *file;
00039      int oflag;
00040 {
00041   int mode;
00042   int newfd;
00043   int index;
00044 
00045   if (file == NULL)
00046     {
00047       __set_errno (EINVAL);
00048       return -1;
00049     }
00050 
00051   if (oflag & O_CREAT)
00052     {
00053       va_list arg;
00054       va_start(arg, oflag);
00055       mode = va_arg(arg, int);
00056       va_end(arg);
00057     }
00058 
00059   /*
00060    *  Find an open slot.
00061    */
00062 
00063   newfd = -1;
00064 
00065   for ( index=0 ; index< FOPEN_MAX ; index++ )
00066     if ( !__FD_Table[ index ].in_use ) {
00067       newfd = index;
00068       break;
00069     }
00070 
00071   if ( newfd == -1 ) {
00072     __set_errno (ENFILE);
00073     return -1;
00074   }
00075 
00076   /*
00077    *  Initialize the open slot
00078    */
00079 
00080   __FD_Table[ newfd ].in_use = 1;
00081   __FD_Table[ newfd ].flags = oflag;
00082 
00083   return newfd;
00084 }
00085 libc_hidden_def (__open)
00086 
00087 /* Initialization Code for Console I/O */
00088 
00089 #ifdef HAVE_GNU_LD
00090 static
00091 #endif
00092 void
00093 __NONE_init_console_io (argc, argv, envp)
00094      int argc;
00095      char **argv;
00096      char **envp;
00097 {
00098   int index;
00099 
00100   for ( index=0 ; index< FOPEN_MAX ; index++ )
00101     __FD_Table[ index ].in_use = 0;
00102 
00103   stdin = fopen( "", "r" );
00104 
00105   stdout = fopen( "", "w" );
00106 
00107   stderr = fopen( "", "w" );
00108 
00109   /*
00110    *  Line buffer the standard input and output and use no buffering for
00111    *  standard error.
00112    */
00113 
00114   setvbuf( stdin,  NULL, _IOLBF, BUFSIZ );
00115   setvbuf( stdout, NULL, _IOLBF, BUFSIZ );
00116   setvbuf( stderr, NULL, _IONBF, BUFSIZ );
00117 
00118   (void) &__NONE_init_console_io;  /* Avoid "defined but not used" warning. */
00119 }
00120 
00121 #ifdef  HAVE_GNU_LD
00122 text_set_element (__libc_subinit, __NONE_init_console_io);
00123 #endif
00124 
00125 weak_alias (__open, open)