View of xos/usr/lib/libc/stat.c


XOS | Parent Directory | View | Download

/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
#include "stat.h"
 
#include "xos.h"
 
#include <errno.h>
 
static int fstat_internal(int fd, struct stat *buf)
{
  struct _xos_stat_struct xos_statbuf;
 
  if (__syscall_stat(fd, &xos_statbuf) == -1)
    return -1;
  buf->st_dev = xos_statbuf.dev;
  buf->st_ino = xos_statbuf.id;
  buf->st_mode = 0;
  switch (xos_statbuf.type) {
  case _XOS_FT_DIR:
    buf->st_mode |= S_IFDIR;
    buf->st_blksize = 4096;
    break;
  case _XOS_FT_CHR:
    buf->st_mode |= S_IFCHR;
    buf->st_blksize = 1024;
    break;
  case _XOS_FT_BLK:
    buf->st_mode |= S_IFBLK;
    buf->st_blksize = 4096;
    break;
  case _XOS_FT_REG:
    buf->st_mode |= S_IFREG;
    buf->st_blksize = 4096;
    break;
  case _XOS_FT_PIPE:
    buf->st_mode |= S_IFIFO;
    buf->st_blksize = 4096;
    break;
  case _XOS_FT_NONE:
  default:
    buf->st_blksize = 4096;
  }
  buf->st_rdev = xos_statbuf.rdev;
  buf->st_size = xos_statbuf.size;
  buf->st_mtime = xos_statbuf.mtime;
  return 0;
}
 
static int stat_internal(const char *path, struct stat *buf)
{
  int fd;
  int errnum;
 
  if ((fd = __syscall_open(path, _XOS_O_READ)) == -1) {
    errnum = errno;
    goto error;
  }
  if (fstat_internal(fd, buf) == -1) {
    errnum = errno;
    goto error_close;
  }
  if (__syscall_close(fd) == -1) {
    errnum = errno;
    goto error;
  }
  return 0;
 
 error_close:
  __syscall_close(fd);
 error:
  errno = errnum;
  return -1;
}
 
int __attribute__ ((weak, visibility ("default"))) stat(const char *path, struct stat *buf)
{
  return stat_internal(path, buf);
}
typeof (stat) __stat __attribute__ ((alias ("stat")));
 
int __attribute__ ((weak, visibility ("default"))) fstat(int fd, struct stat *buf)
{
  return fstat_internal(fd, buf);
}
typeof (fstat) __fstat __attribute__ ((alias ("fstat")));
 
int __attribute__ ((weak, visibility ("default"))) lstat(const char *path, struct stat *buf)
{
  return stat_internal(path, buf);
}
typeof (lstat) __lstat __attribute__ ((alias ("lstat")));