⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
/***********************************************************************/
/*                                                                     */
/*   Module:  main.c                                                   */
/*   Release: 2004.5                                                   */
/*   Version: 2004.3                                                   */
/*   Purpose: Implement the file test application                      */
/*                                                                     */
/***********************************************************************/
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <targetos.h>
#include <kernel.h>
#include <sys.h>
#include <pccard.h>
#include "..\..\posix.h"
#include "..\..\ffs_stdio.h"

/***********************************************************************/
/* Configuration                                                       */
/***********************************************************************/
#define ID_REG          0
#define CWD_WD1         1
#define CWD_WD2         2
#define BUF_SIZE        100
#define RFS_NAME        "rfs"

/***********************************************************************/
/* Global Data Declarations                                            */
/***********************************************************************/
static char *Volume[] =
{
#if NUM_RFS_VOLS
  RFS_NAME,
#endif
#if PCCARD_SUPPORT
  "ata",
#endif
#if INC_FAT_FIXED
  "fat",
#endif
#if NUM_NAND_FTLS
  "ftld",
#endif
#if INC_NAND_FS
  "nand",
#endif
#if INC_NOR_FS
  "nor",
#endif
};

/***********************************************************************/
/* Local Function Definitions                                          */
/***********************************************************************/

/***********************************************************************/
/*   file_test: Test assorted file system calls                        */
/*                                                                     */
/*       Input: vol_name = name of file system volume                  */
/*                                                                     */
/***********************************************************************/
static int file_test(char *vol_name)
{
  int i, k, fid, fid2;
  char hello[] = "Hello World, I'm testing the cool FFS!\n";
  const char *fname = "hello.c", *fname2 = "rhello.c";
  struct stat stbuf;
  char buf[BUF_SIZE];
  union vstat stats;
  off_t offset;

  /*-------------------------------------------------------------------*/
  /* Test stat() on volume name.                                       */
  /*-------------------------------------------------------------------*/
  snprintf(buf, BUF_SIZE, "/%s", vol_name);
  if (stat(buf, &stbuf) && S_ISREG(stbuf.st_mode))
  {
    perror("Error running stat() on volume name");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* The next tests are not compatible with FAT.                       */
  /*-------------------------------------------------------------------*/
  vstat(vol_name, &stats);
  if (stats.fat.vol_type != FAT_VOL)
  {
    /*-----------------------------------------------------------------*/
    /* Test re-opening a previously opened and deleted file.           */
    /*-----------------------------------------------------------------*/
    sprintf(buf, "//%s//bar", vol_name);
    fid = open(buf, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IXUSR);
    if (fid < 0)
    {
      perror("open()");
      return -1;
    }
    if (unlink(buf))
    {
      perror("unlink()");
      return -1;
    }
    fid2 = open(buf, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IXUSR);
    if (fid2 < 0)
    {
      perror("open()");
      return -1;
    }
    if (unlink(buf))
    {
      perror("unlink()");
      return -1;
    }
    if (close(fid))
    {
      perror("close()");
      return -1;
    }
    if (close(fid2))
    {
      perror("close()");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Check that open descriptor on a link still functions correctly  */
    /* until closed even after the actual link is removed.             */
    /*-----------------------------------------------------------------*/
    sprintf(buf, "//%s//foo", vol_name);
    fid = creat(buf, 0666);
    if (fid == -1)
    {
      perror("Error creating file");
      return -1;
    }
    if (unlink("foo"))
    {
      perror("Error removing file");
      return -1;
    }
    if (write(fid, buf, 100) != 100)
    {
      perror("Error writing to file");
      return -1;
    }
    if (close(fid))
    {
      perror("Error closing file");
      return -1;
    }
    fid = open("foo", O_RDONLY);
    if (fid != -1)
    {
      perror("File shouldn't exist");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Check that an open descriptor on a link works across a rename   */
    /* call for that link.                                             */
    /*-----------------------------------------------------------------*/
    fid = creat("foo", 0666);
    if (fid == -1)
    {
      perror("Error creating file");
      return -1;
    }
    if (renameFFS("foo", "foo2"))
    {
      perror("Error renaming file");
      return -1;
    }
    if (write(fid, buf, 700) != 700)
    {
      perror("Error writing to file");
      return -1;
    }
    if (close(fid))
    {
      perror("Error closing file");
      return -1;
    }
    if (unlink("foo2"))
    {
      perror("Error removing file");
      return -1;
    }
  }

  /*-------------------------------------------------------------------*/
  /* Successfully rename a file to its existing name.                  */
  /*-------------------------------------------------------------------*/
  fid = open("snork", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IXUSR);
  if (write(fid, buf, BUF_SIZE) != BUF_SIZE)
  {
    perror("Error writing to file");
    return -1;
  }
  if (close(fid))
  {
    perror("close()");
    return -1;
  }
  if (renameFFS("snork", "snork"))
  {
    perror("Error renaming file to its existing name");
    return -1;
  }
  if (unlink("snork"))
  {
    perror("unlink()");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Successfully rename an open file.                                 */
  /*-------------------------------------------------------------------*/
  fid = open("snit", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IXUSR);
  if (write(fid, buf, BUF_SIZE) != BUF_SIZE)
  {
    perror("Error writing to file");
    return -1;
  }
  if (renameFFS("snit", "snout"))
  {
    perror("Error renaming file to its existing name");
    return -1;
  }
  if (close(fid))
  {
    perror("close()");
    return -1;
  }
  if (unlink("snout"))
  {
    perror("unlink()");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Other renaming tests.                                             */
  /*-------------------------------------------------------------------*/
  if (mkdir("dira", S_IRUSR | S_IWUSR | S_IXUSR))
  {
    perror("Error making directory");
    return -1;
  }
  if (renameFFS("dira", "dirb"))
  {
    perror("Error renaming directory");
    return -1;
  }
  if (renameFFS("dirb", "dira"))
  {
    perror("Error renaming directory");
    return -1;
  }
  if (rmdir("dira"))
  {
    perror("Error removing directory");
    return -1;
  }
  fid = creat("filea.txt", S_IRUSR | S_IWUSR | S_IXUSR);
  if (fid == -1)
  {
    perror("Error creating the file");
    return -1;
  }
  if (write(fid, buf, 500) != 500)
  {
    perror("Error writing to file");
    return -1;
  }
  if (close(fid))
  {
    perror("Error closing file");
    return -1;
  }
  if (renameFFS("filea.txt", "fileb.txt"))
  {
    perror("Error renaming file");
    return -1;
  }
  if (renameFFS("fileb.txt", "filea.txt"))
  {
    perror("Error renaming file");
    return -1;
  }
  if (unlink("filea.txt"))
  {
    perror("Error removing file");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Test the seek routines.                                           */
  /*-------------------------------------------------------------------*/
  fid = creat("write.txt", S_IRUSR | S_IWUSR | S_IXUSR);
  if (fid == -1)
  {
    perror("Error creating the file");
    return -1;
  }
  if (write(fid, buf, 678) != 678)
  {
    perror("Error writing to file");
    return -1;
  }
  offset = lseek(fid, -352, SEEK_CUR);
  if (offset == (off_t)-1)
  {
    perror("Error seeking in file");
    return -1;
  }
  if (write(fid, buf, 797) != 797)
  {
    perror("Error writing to file");
    return -1;
  }
  if (fstat(fid, &stbuf))
  {
    perror("Error getting stats for file");
    return -1;
  }
  if (stbuf.st_size != 1123)
  {
    printf("fstat() st_size is unexpected!\n");
    return -1;
  }
  if (close(fid))
  {
    perror("Error closing file");
    return -1;
  }
  if (unlink("write.txt"))
  {
    perror("Error removing file");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* File creation, deletion loop.                                     */
  /*-------------------------------------------------------------------*/
  for (k = 0; k < 3; ++k)
  {
    /*-----------------------------------------------------------------*/
    /* Open two text files.                                            */
    /*-----------------------------------------------------------------*/
    fid = open(fname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IXUSR);
    if (fid == -1)
    {
      perror("Error opening the file");
      return -1;
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -