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

📄 main.c

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

/***********************************************************************/
/* Configuration                                                       */
/***********************************************************************/
#define ID_REG          0
#define CWD_WD1         1
#define CWD_WD2         2
#define BUF_LEN         1024
#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
};
static char Buf[BUF_LEN];

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

/***********************************************************************/
/*  lseek_test: lseek() test                                           */
/*                                                                     */
/*      Return: 0 if successful, else -1                               */
/*                                                                     */
/***********************************************************************/
static int lseek_test(const char *vol_name)
{
  int fid, words, offset, prevOffset, res;
  char *buf;
  FILE_FFS *stream;

  /*-------------------------------------------------------------------*/
  /* Initialize the buffer we will be using.                           */
  /*-------------------------------------------------------------------*/
  buf = (char *)calloc(64 * 1024, 1);

  /*-------------------------------------------------------------------*/
  /* Create file and write 64 KBytes using one write() call.           */
  /*-------------------------------------------------------------------*/
  fid = open("test.dat", O_RDWR | O_CREAT | O_EXCL, 0777);
  words = write(fid, buf, 64 * 1024);
  if (words != 64 * 1024)
  {
    perror("write()");
    free(buf);
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Check whether lseek() to current position returns file size.      */
  /*-------------------------------------------------------------------*/
  offset = lseek(fid, (off_t)0, SEEK_CUR);
  if (words != offset)
  {
    printf("Wrote %d bytes, but lseek() returned %d for current\n"
           "position\n", words, offset);
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Close the first file.                                             */
  /*-------------------------------------------------------------------*/
  if (close(fid))
  {
    perror("close");
    res = -1;
    goto exit;
  }

  /*-------------------------------------------------------------------*/
  /* Create file and write 64 KBytes using putc() in a loop.           */
  /*-------------------------------------------------------------------*/
  fid = open("test2.dat", O_RDWR | O_CREAT | O_EXCL, 0777);
  stream = fdopenFFS(fid, "r+");
  for (words = 0; words < 64 * 1024; words++)
    if (putcFFS(words & 0x7F, stream) < 0)
      return -1;

  /*-------------------------------------------------------------------*/
  /* Check whether lseek() to current position returns file size.      */
  /*-------------------------------------------------------------------*/
  offset = lseek(fid, (off_t)0, SEEK_CUR);
  if (words != offset)
  {
    printf("Wrote %d bytes, but lseek() returned %d for the current\n"
           "position\n", words, offset);
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Close the second file.                                            */
  /*-------------------------------------------------------------------*/
  if (close(fid))
  {
    perror("close");
    res = -1;
    goto exit;
  }

  /*-------------------------------------------------------------------*/
  /* Re-open the second file for reading and writing.                  */
  /*-------------------------------------------------------------------*/
  fid = open("test2.dat", O_RDWR, 0777);
  if (fid < 0)
  {
    perror("open");
    res = -1;
    goto exit;
  }

  /*-------------------------------------------------------------------*/
  /* Read BUF_LEN bytes.                                               */
  /*-------------------------------------------------------------------*/
  words = read(fid, buf, BUF_LEN);

  /*-------------------------------------------------------------------*/
  /* Move the file position backwards BUF_LEN bytes, write BUF_LEN     */
  /* bytes, and then read BUF_LEN bytes. Loop until all read.          */
  /*-------------------------------------------------------------------*/
  for (prevOffset = 0; words == BUF_LEN;)
  {
    /*-----------------------------------------------------------------*/
    /* Seek backwards BUF_LEN bytes and then write BUF_LEN bytes.      */
    /*-----------------------------------------------------------------*/
    lseek(fid, (off_t) -BUF_LEN, SEEK_CUR);
    words = write(fid, buf, BUF_LEN);
    if (words != BUF_LEN)
    {
      printf("write() returned %d, expected %d\n", words, BUF_LEN);
      res = -1;
      goto exit;
    }

    /*-----------------------------------------------------------------*/
    /* Get current position via lseek().                               */
    /*-----------------------------------------------------------------*/
    offset = lseek(fid, (off_t)0, SEEK_CUR);

    /*-----------------------------------------------------------------*/
    /* Ensure lseek() delta before and after write() equals BUF_LEN.   */
    /*-----------------------------------------------------------------*/
    if (BUF_LEN != offset - prevOffset)
    {
      printf("Wrote %d bytes, but lseek() delta equals %d\n", BUF_LEN,
             offset - prevOffset);
      res = -1;
      goto exit;
    }

    /*-----------------------------------------------------------------*/
    /* Read BUF_LEN bytes.                                             */
    /*-----------------------------------------------------------------*/
    words = read(fid, buf, BUF_LEN);

    /*-----------------------------------------------------------------*/
    /* Save position before read() and get current position.           */
    /*-----------------------------------------------------------------*/
    prevOffset = offset;
    offset = lseek(fid, (off_t)0, SEEK_CUR);

    /*-----------------------------------------------------------------*/
    /* Ensure lseek() delta before and after read() equals amount read.*/
    /*-----------------------------------------------------------------*/
    if (words != offset - prevOffset)
    {
      printf("Read %d bytes, but lseek() delta equals %d\n", words,
             offset - prevOffset);
      res = -1;
      goto exit;
    }
  }

  /*-------------------------------------------------------------------*/
  /* Close second file and delete both files.                          */
  /*-------------------------------------------------------------------*/
  close(fid);
  res = unlink("test.dat");
  res |= unlink("test2.dat");
  if (res)
    perror("unlink");

  /*-------------------------------------------------------------------*/
  /* Free the buffer and return success.                               */
  /*-------------------------------------------------------------------*/
exit:
  free(buf);
  return res;
}

/***********************************************************************/
/*  stdio_test: Test various stdio calls in file system                */
/*                                                                     */
/*       Input: vol_name = name of file system volume                  */
/*                                                                     */
/***********************************************************************/
static int stdio_test(char *vol_name)
{
  FILE_FFS *stream;
  int i;
  struct stat abuf;
  fpos_tFFS pos;

  /*-------------------------------------------------------------------*/
  /* Fill buffer with data.                                            */
  /*-------------------------------------------------------------------*/
  for (i = 0; i < BUF_LEN; ++i)
    Buf[i] = 'a' + i % 50;

  /*-------------------------------------------------------------------*/
  /* Open write.txt for writing/reading.                               */
  /*-------------------------------------------------------------------*/
  stream = fopenFFS("write.txt", "w+");
  if (stream == NULL)
  {
    perror("error opening the file!");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Write 678 bytes from buffer to "write.txt".                       */
  /*-------------------------------------------------------------------*/
  if (fwriteFFS(Buf, 1, 678, stream) != 678)
  {
    perror("error writing to file!");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Seek back 352 bytes from the end of the file.                     */
  /*-------------------------------------------------------------------*/
  if (fseekFFS(stream, -352, SEEK_CUR))
  {
    perror("error seeking in file!");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Write another 797 bytes, 352 of which will be over old data.      */
  /*-------------------------------------------------------------------*/
  if (fwriteFFS(Buf, 1, 797, stream) != 797)
  {
    perror("error writing to file!");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Do a stat on the file to check its size is the correct one.       */
  /*-------------------------------------------------------------------*/
  if (stat("write.txt", &abuf))
  {
    perror("error getting stats for file!");
    return -1;
  }
  if (abuf.st_size != 1123)
  {
    printf("TEST FAILED!\n");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Seek past file's end and ensure that its length hasn't changed.   */
  /*-------------------------------------------------------------------*/
  if (fseekFFS(stream, 100, SEEK_END))
  {
    perror("error seeking in file!");
    return -1;
  }
  if (fstat(filenoFFS(stream), &abuf))
  {
    perror("error getting stats for file!");
    return -1;
  }
  if (abuf.st_size != 1123)
  {
    printf("TEST FAILED!\n");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Write one additional byte and verify that file length has grown.  */
  /*-------------------------------------------------------------------*/
  if (fwriteFFS(Buf, 1, 1, stream) != 1)
  {
    perror("error writing to file!");
    return -1;
  }
  if (fstat(filenoFFS(stream), &abuf))
  {
    perror("error getting stats for file!");
    return -1;
  }
  if (abuf.st_size != 1123 + 101)
  {
    printf("TEST FAILED!\n");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Verify that the gap has been filled with zeros.                   */
  /*-------------------------------------------------------------------*/
  if (fseekFFS(stream, -101, SEEK_CUR))
  {
    perror("error seeking in file!");
    return -1;
  }
  for (i = 0; i < 100; ++i)
  {
    char c;

    if (freadFFS(&c, 1, 1, stream) != 1)
    {
      perror("error reading from file!");
      return -1;
    }
    if (c)
    {
      printf("TEST FAILED!\n");
      return -1;
    }
  }

  /*-------------------------------------------------------------------*/
  /* Do a set and get pos to check them.                               */
  /*-------------------------------------------------------------------*/
  rewindFFS(stream);
  if (fgetposFFS(stream, &pos))
  {
    perror("error getting position for file!");
    return -1;
  }
  if (fseekFFS(stream, 1120, SEEK_CUR))
  {
    perror("error seeking in file!");
    return -1;
  }
  if (freadFFS(Buf, 1, 3, stream) != 3)
  {
    perror("error reading from file!");
    return -1;
  }
  if (fsetposFFS(stream, &pos))
  {
    perror("error setting position for file!");

⌨️ 快捷键说明

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