View of xos/tests/vfat_write.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/>. */
 
/* un fichier test.txt contenant "hello world !\n" doit etre present dans le
 * repertoire. */
 
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
 
static void die(const char *msg)
{
  fprintf(stderr, "%s\n", msg);
  _exit(1);
}
 
int main()
{
  int fd;
  struct stat statbuf;
  char buf[16];
 
  if ((fd = open("test.txt", O_RDWR)) < 0)
    die("open failed");
  if (lseek(fd, 100, SEEK_SET) != 100)
    die("seek failed");
  if (write(fd, "youpi", 5) != 5)
    die("write failed");
  if (fstat(fd, &statbuf) < 0)
    die("stat failed");
  printf("%ld\n", statbuf.st_size);
  if (lseek(fd, 0, SEEK_SET) != 0)
    die("seek failed");
  if (read(fd, buf, 13) != 13)
    die("read failed");
  printf("%s\n", buf);
  if (read(fd, buf, 3) != 3)
    die("read failed");
  printf("%02x %02x %02x\n", buf[0], buf[1], buf[2]);
  if (lseek(fd, 98, SEEK_SET) != 98)
    die("seek failed");
  if (read(fd, buf, 2) != 2)
    die("read failed");
  printf("%02x %02x\n", buf[0], buf[1]);
  if (read(fd, buf, 5) != 5)
    die("read failed");
  printf("%.5s\n", buf);
  close(fd);
 
  return 0;
}