Example #1
0
FBFILE *
fbopen(const char *filename, const char *mode)
{
	int openmode = 0;
	int pmode = 0;
	FBFILE *fb = NULL;
	int fd;
	s_assert(filename);
	s_assert(mode);

	if(filename == NULL || mode == NULL)
	{
		errno = EINVAL;
		return NULL;
	}
	while (*mode)
	{
		switch (*mode)
		{
		case 'r':
			openmode = O_RDONLY;
			break;
		case 'w':
			openmode = O_WRONLY | O_CREAT | O_TRUNC;
			pmode = 0644;
			break;
		case 'a':
			openmode = O_WRONLY | O_CREAT | O_APPEND;
			pmode = 0644;
			break;
		case '+':
			openmode &= ~(O_RDONLY | O_WRONLY);
			openmode |= O_RDWR;
			break;
		default:
			break;
		}
		++mode;
	}

	if((fd = file_open(filename, openmode, pmode)) == -1)
	{
		return fb;
	}

	if(NULL == (fb = fdbopen(fd, NULL)))
		file_close(fd);
	return fb;
}
Example #2
0
/** Open a new FBFILE.
 * @param[in] filename Name of file to open.
 * @param[in] mode fopen()-style mode string.
 * @return Pointer to newly allocated FBFILE.
 */
FBFILE* fbopen(const char *filename, const char *mode)
{
  int openmode = 0;
  int pmode = 0;
  FBFILE *fb = NULL;
  int fd;
  assert(filename);
  assert(mode);

  while (*mode) {
    switch (*mode) {
    case 'r':
      openmode = O_RDONLY;
      break;
    case 'w':
      openmode = O_WRONLY | O_CREAT | O_TRUNC;
      pmode = S_IRUSR | S_IWUSR;
      break;
    case 'a':
      openmode = O_WRONLY | O_CREAT | O_APPEND;
      pmode = S_IRUSR | S_IWUSR;
      break;
    case '+':
      openmode &= ~(O_RDONLY | O_WRONLY);
      openmode |= O_RDWR;
      break;
    default:
      break;
    }
    ++mode;
  }
  /*
   * stop NFS hangs...most systems should be able to open a file in
   * 3 seconds. -avalon (courtesy of wumpus)
   */
  alarm(3);
  if ((fd = open(filename, openmode, pmode)) == -1) {
    alarm(0);
    return fb;
  }
  alarm(0);

  if (NULL == (fb = fdbopen(fd, NULL)))
    close(fd);
  return fb;
}