dlls/msvcrt/file.c
/* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
/* caller must hold the files lock */
Allocate an fd slot from a Win32 HANDLE, starting from fd
caller must hold the files lock
Allocate an fd slot from a Win32 HANDLE starting from fd caller must hold the files lock
static int msvcrt_set_fd(HANDLE hand, int flag, int fd)
{
  ioinfo *fdinfo;
  if (fd >= MSVCRT_MAX_FILES)
  {
    WARN(":files exhausted!\n");
    *MSVCRT__errno() = MSVCRT_ENFILE;
    return -1;
  }
  fdinfo = msvcrt_get_ioinfo(fd);
  if(fdinfo == &MSVCRT___badioinfo) {
    int i;
    MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
    if(!MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) {
      WARN(":out of memory!\n");
      *MSVCRT__errno() = MSVCRT_ENOMEM;
      return -1;
    }
    for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
      MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE;
    fdinfo = msvcrt_get_ioinfo(fd);
  }
  fdinfo->handle = hand;
  fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE));
  fdinfo->lookahead[0] = '\n';
  fdinfo->lookahead[1] = '\n';
  fdinfo->lookahead[2] = '\n';
  fdinfo->exflag = 0;
  if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
    MSVCRT_fdstart = MSVCRT_fdend + 1;
  else
    while (MSVCRT_fdstart < MSVCRT_fdend &&
      msvcrt_get_ioinfo(MSVCRT_fdstart)->handle != INVALID_HANDLE_VALUE)
      MSVCRT_fdstart++;
  if (fd >= MSVCRT_fdend)
    MSVCRT_fdend = fd + 1;
  TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
  switch (fd)
  {
  case 0: SetStdHandle(STD_INPUT_HANDLE,  hand); break;
  case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
  case 2: SetStdHandle(STD_ERROR_HANDLE,  hand); break;
  }
  return fd;
}
static int msvcrt_set_fd ( HANDLE hand , int flag , int fd ) { ioinfo * fdinfo ; if ( fd >= MSVCRT_MAX_FILES ) { WARN ( " \n " ) ; * MSVCRT__errno ( ) = MSVCRT_ENFILE ; return -1 ; } fdinfo = msvcrt_get_ioinfo ( fd ) ; if ( fdinfo == & MSVCRT___badioinfo ) { int i ; MSVCRT___pioinfo [ fd / MSVCRT_FD_BLOCK_SIZE ] = MSVCRT_calloc ( MSVCRT_FD_BLOCK_SIZE , sizeof ( ioinfo ) ) ; if ( ! MSVCRT___pioinfo [ fd / MSVCRT_FD_BLOCK_SIZE ] ) { WARN ( " \n " ) ; * MSVCRT__errno ( ) = MSVCRT_ENOMEM ; return -1 ; } for ( i = 0 ; i < MSVCRT_FD_BLOCK_SIZE ; i ++ ) MSVCRT___pioinfo [ fd / MSVCRT_FD_BLOCK_SIZE ] [ i ] . handle = INVALID_HANDLE_VALUE ; fdinfo = msvcrt_get_ioinfo ( fd ) ; } fdinfo -> handle = hand ; fdinfo -> wxflag = WX_OPEN | ( flag & ( WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE ) ) ; fdinfo -> lookahead [ 0 ] = ' \n ' ; fdinfo -> lookahead [ 1 ] = ' \n ' ; fdinfo -> lookahead [ 2 ] = ' \n ' ; fdinfo -> exflag = 0 ; if ( fd == MSVCRT_fdstart && fd == MSVCRT_fdend ) MSVCRT_fdstart = MSVCRT_fdend + 1 ; else while ( MSVCRT_fdstart < MSVCRT_fdend && msvcrt_get_ioinfo ( MSVCRT_fdstart ) -> handle != INVALID_HANDLE_VALUE ) MSVCRT_fdstart ++ ; if ( fd >= MSVCRT_fdend ) MSVCRT_fdend = fd + 1 ; TRACE ( " \n " , MSVCRT_fdstart , MSVCRT_fdend ) ; switch ( fd ) { case 0 : SetStdHandle ( STD_INPUT_HANDLE , hand ) ; break ; case 1 : SetStdHandle ( STD_OUTPUT_HANDLE , hand ) ; break ; case 2 : SetStdHandle ( STD_ERROR_HANDLE , hand ) ; break ; } return fd ; }
INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd
caller must hold the files lock
INTERNAL : Allocate an fd slot from a Win32 HANDLE starting from fd caller must hold the files lock
/* locate next free slot */ /* update last fd in use */