View of xos/kernel/usr.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 "usr.h"
 
#include <sprintf.h>
 
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
 
int errno;
 
static int vfdprintf(int fd, const char *format, va_list arg)
{
  char buffer[1024];
  int n;
 
  if ((n = vsnprintf(buffer, sizeof buffer, format, arg)) < 0)
    return -1;
  return write(fd, buffer, n);
}
 
static int vprintf(const char *format, va_list arg)
{
  return vfdprintf(STDOUT_FILENO, format, arg);
}
 
static int __attribute__ ((format (printf, 2, 3))) fdprintf(int fd, const char *format, ...)
{
  va_list arg;
  int n;
 
  va_start(arg, format);
  n = vfdprintf(fd, format, arg);
  va_end(arg);
  return n;
}
 
int __attribute__ ((format (printf, 1, 2))) printf(const char *format, ...)
{
  va_list arg;
  int n;
 
  va_start(arg, format);
  n = vprintf(format, arg);
  va_end(arg);
  return n;
}
 
void perror(const char *s)
{
  if (s && *s)
    fdprintf(STDERR_FILENO, "%s: ", s);
  fdprintf(STDERR_FILENO, "failed with code=%d\n", errno);
}