Esempio n. 1
0
void _ps2sdk_libc_init()
{
    _ps2sdk_alloc_init();
    _ps2sdk_stdio_init();
    _ps2sdk_stdlib_init();
    _ps2sdk_time_init();
}
Esempio n. 2
0
/*
**
**  [func] - fopen.
**  [desc] - attempts to open the fname file using the mode file mode. if able
**           open the fname then returns the pointer to the FILE stream. else
**           returns NULL.
**  [entr] - const char *fname; the filename string pointer.
**           const char *mode; the file mode string pointer.
**  [exit] - FILE *; the pointer the fname FILE stream. else NULL.
**  [prec] - fname and mode are valid string pointers.
**  [post] - the fname file is opened.
**
*/
FILE *fopen(const char *fname, const char *mode)
{
  FILE *ret = NULL;
  int  fd = 0, flag = 0, i, iomode = 0;

  // some people won't use our crt0...
  if (!__stdio_initialised)
    _ps2sdk_stdio_init();

  /* ensure file name and mode are not NULL strings. */
  if ((fname != NULL) && (*fname != '\0')) {
    if ((mode != NULL) && (*mode != '\0')) {
      /* test the file mode. */
      switch(*mode++) {
        case 'r':
          flag = _IOREAD;
          iomode = O_RDONLY;
          break;
        case 'w':
          flag = _IOWRT;
          iomode = (O_WRONLY | O_CREAT | O_TRUNC);
          break;
        case 'a':
          flag = _IORW;
          iomode = O_APPEND|O_WRONLY;
          break;
      } // switch
      /* test the extended file mode. */
      for (; (*mode++ != '\0'); ) {
        switch(*mode) {
          case 'b':
            continue;
          case '+':
            flag |= (_IOREAD | _IOWRT);
            iomode |= (O_RDWR);
            continue;
          default:
            break;
        } // switch
      } // for
      /* search for an available fd slot. */
      for (i = 3; i < _NFILE; ++i) if (__iob[i].fd < 0) break;
      if (i < _NFILE) {
        char * t_fname = __ps2_normalize_path((char *)fname);
        char b_fname[FILENAME_MAX];
        __iob[i].type = __stdio_get_fd_type(fname);
        if (!strchr(fname, ':')) { // filename doesn't contain device
          t_fname = b_fname;
          if (fname[0] == '/' || fname[0] == '\\' ) {   // does it contain root ?
            char * device_end = strchr(__direct_pwd, ':');
            if (device_end) {      // yes, let's strip pwd a bit to keep device only
              strncpy(b_fname, __direct_pwd, device_end - __direct_pwd);
              strcpy(b_fname + (device_end - __direct_pwd), fname);
            } else {               // but pwd doesn't contain any device, let's default to host
              strcpy(b_fname, "host:");
              strcpy(b_fname + 5, fname);
            }
          } else {                 // otherwise, it's relative directory, let's copy pwd straight
            int b_fname_len = strlen(__direct_pwd);
            if (!strchr(__direct_pwd, ':')) { // check if pwd contains device name
              strcpy(b_fname, "host:");
              strcpy(b_fname + 5, __direct_pwd);
              if (!(__direct_pwd[b_fname_len - 1] == '/' || __direct_pwd[b_fname_len - 1] == '\\')) { // does it has trailing slash ?
                if(__iob[i].type == STD_IOBUF_TYPE_CDROM)
              	  b_fname[b_fname_len + 5] = '\\';
                else
                  b_fname[b_fname_len + 5] = '/';
                  // b_fname[b_fname_len + 6] = 0;
                  // b_fname_len += 2;
                b_fname_len++;
              }
              b_fname_len += 5;
              strcpy(b_fname + b_fname_len, fname);
            } else {                          // device name is here
              if (b_fname_len) {
                strcpy(b_fname, __direct_pwd);
                if (!(b_fname[b_fname_len - 1] == '/' || b_fname[b_fname_len - 1] == '\\')) {
                  if(__iob[i].type == STD_IOBUF_TYPE_CDROM)
                  	b_fname[b_fname_len] = '\\';
                  else
                    b_fname[b_fname_len] = '/';
                  // b_fname[b_fname_len + 1] = 0; // #neofar
                  // b_fname_len += 2;
                  b_fname_len++;
                }
                strcpy(b_fname + b_fname_len, fname);
              }
            }
          }
        }
        if ((fd = _ps2sdk_open((char *)t_fname, iomode)) >= 0) {
          __iob[i].fd = fd;
          __iob[i].cnt = 0;
          __iob[i].flag = flag;
          __iob[i].has_putback = 0;
          ret = (__iob + i);
        } else if ((__iob[i].type == STD_IOBUF_TYPE_CDROM)) {
          int fname_len = strlen(t_fname);
          if (!((t_fname[fname_len - 2] == ';') && (t_fname[fname_len - 1] == '1'))) {
            char cd_fname[fname_len + 3];
            strcpy(cd_fname, t_fname);
            cd_fname[fname_len + 0] = ';';
            cd_fname[fname_len + 1] = '1';
            cd_fname[fname_len + 2] = 0;
            if ((fd = _ps2sdk_open((char *)cd_fname, iomode)) >= 0) {
              __iob[i].fd = fd;
              __iob[i].cnt = 0;
              __iob[i].flag = flag;
              __iob[i].has_putback = 0;
              ret = (__iob + i);
            }
          }
        }
      } else {
        errno = ENFILE;
      }
    } else {
      errno = EINVAL;
    }
  } else {
    errno = EINVAL;
  }
  if (fd < 0) {
	errno = (fd * -1);
  }
  return (ret);
}