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

📄 sysioctl.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/***********************************************************************/
/*                                                                     */
/*   Module:  sysioctl.c                                               */
/*   Release: 2004.5                                                   */
/*   Version: 2004.6                                                   */
/*   Purpose: Implements functions related to the TTY drivers          */
/*                                                                     */
/*---------------------------------------------------------------------*/
/*                                                                     */
/*               Copyright 2004, Blunk Microsystems                    */
/*                      ALL RIGHTS RESERVED                            */
/*                                                                     */
/*   Licensees have the non-exclusive right to use, modify, or extract */
/*   this computer program for software development at a single site.  */
/*   This program may be resold or disseminated in executable format   */
/*   only. The source code may not be redistributed or resold.         */
/*                                                                     */
/***********************************************************************/
#include <stdarg.h>
#include "../include/libc/string.h"
#include "../include/libc/ctype.h"
#include "../include/libc/errno.h"
#include "../include/libc/stdlib.h"
#include "../posix.h"
#include "../include/targetos.h"
#include "../include/sys.h"
#include "../include/fsprivate.h"

/***********************************************************************/
/* Configuration Data                                                  */
/***********************************************************************/
/*
** List of software modules in this build
*/
const Module ModuleList[] =
{
#if NUM_RFS_VOLS
  RfsModule,
#endif
#if NUM_FFS_VOLS
  FfsModule,
#endif
#if INC_NAND_FS
  NandDriverModule,
#endif
#if INC_NOR_FS
  NorDriverModule,
#endif
#if NUM_FAT_VOLS
  FatModule,
#if NUM_NAND_FTLS
  FtlNandDriverModule,
#endif
#if INC_FAT_FIXED
  FatDriverModule,
#endif
#endif
#if PCCARD_SUPPORT
  pccModule,
  pccDvrModule,
  AppModule,
#endif
  NULL,
};

/***********************************************************************/
/* Global Variable Declarations                                        */
/***********************************************************************/
HeadEntry MountedList =
{
  NULL, NULL,
};
FILE        Files[FOPEN_MAX + 1];
SEM         FileSysSem;
const char *FSPath;
int CurrFixedVols = 0;

/***********************************************************************/
/* Function Prototypes                                                 */
/***********************************************************************/
int printf(const char *format, ...);

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

/***********************************************************************/
/*  do_nothing: A do-nothing acquire/release routine                   */
/*                                                                     */
/***********************************************************************/
static void do_nothing(const FILE *file, int rwcode)
{
}

/***********************************************************************/
/* dir_file_write: Error function that gets called when a file write   */
/*              is attempted on a directory control block              */
/*                                                                     */
/*     Returns: Error always                                           */
/*                                                                     */
/***********************************************************************/
static int dir_file_write(FILE *stream, const ui8 *buf, ui32 len)
{
  stream->errcode = EISDIR;
  set_errno(EISDIR);
  return -1;
}

/***********************************************************************/
/* dir_file_read: Error function that gets called when a file read is  */
/*              attempted on a directory control block                 */
/*                                                                     */
/*     Returns: Error always                                           */
/*                                                                     */
/***********************************************************************/
static int dir_file_read(FILE *stream, ui8 *buf, ui32 len)
{
  stream->errcode = EISDIR;
  set_errno(EISDIR);
  return -1;
} /*lint !e818*/

/***********************************************************************/
/*  root_ioctl: Do POSIX functions as the root directory               */
/*                                                                     */
/*      Inputs: dir = holds dir ctrl block ptr                         */
/*              code = selects what function to do                     */
/*                                                                     */
/*     Returns: Depends on the function that was done                  */
/*                                                                     */
/***********************************************************************/
static void *root_ioctl(DIR *dir, int code, ...)
{
  void *r_value = NULL;
  static struct dirent read_dir;

  switch (code)
  {
    case GETCWD:
    {
      va_list list_of_args;
      char *buf;

      /*---------------------------------------------------------------*/
      /* Use the va_arg mechanism to fetch the args for fsearch.       */
      /*---------------------------------------------------------------*/
      va_start(list_of_args, code);
      buf = (va_arg(list_of_args, char *)); /*lint !e415 !e416 */
      va_end(list_of_args);

      /*---------------------------------------------------------------*/
      /* Malloc buffer if needed, and simply fill it with "/".         */
      /*---------------------------------------------------------------*/
      if (buf == NULL)
      {
        buf = malloc(2);
        if (buf == NULL)
          return NULL;
      }
      strcpy(buf, "/");
      r_value = buf;
      break;
    }

    case READDIR:
      /*---------------------------------------------------------------*/
      /* If there are entries in the mounted list, display the first   */
      /* mounted file system.                                          */
      /*---------------------------------------------------------------*/
      if (dir->pos == NULL)
      {
        if (MountedList.head == NULL)
          break;
        dir->pos = MountedList.head;
        strncpy(read_dir.d_name, ((FileSys *)dir->pos)->name,
                FILENAME_MAX);
        r_value = &read_dir;
      }

      /*---------------------------------------------------------------*/
      /* Else if it's not the last entry, advance to next one.         */
      /*---------------------------------------------------------------*/
      else if (((FileSys *)dir->pos)->next)
      {
        dir->pos = ((FileSys *)dir->pos)->next;
        strncpy(read_dir.d_name, ((FileSys *)dir->pos)->name,
                FILENAME_MAX);
        r_value = &read_dir;
      }

      break;

    case OPENDIR:
      /*---------------------------------------------------------------*/
      /* Set argument as pointer to root directory.                    */
      /*---------------------------------------------------------------*/
      dir->ioctl = Files[ROOT_DIR_INDEX].ioctl;
      dir->pos = NULL;
      r_value = dir;
      break;

    case CLOSEDIR:
      return NULL;

    case CHDIR:
    {
      ui32 cwd;
      FileSys *fsys;

      /*---------------------------------------------------------------*/
      /* Decrement previous working directory count if any.            */
      /*---------------------------------------------------------------*/
      FsReadCWD((void *)&fsys, &cwd);
      if (fsys)
        fsys->ioctl(NULL, CHDIR_DEC, cwd); /*lint !e522*/

      /*---------------------------------------------------------------*/
      /* Assign current working directory.                             */
      /*---------------------------------------------------------------*/
      FsSaveCWD((ui32)NULL, (ui32)NULL);
      break;
    }

    /*-----------------------------------------------------------------*/
    /* All unimplemented API functions return error.                   */
    /*-----------------------------------------------------------------*/
    case FOPEN:
    case TMPNAM:
    case TMPFILE:
      set_errno(EINVAL);
      return NULL;

    default:
      set_errno(EINVAL);
      return (void *)-1;
  }
  return r_value;
}

/***********************************************************************/
/* sw_mod_loop: Poll all installed modules with a request              */
/*                                                                     */
/*       Input: req = one of the module request codes defined in sys.h */
/*                                                                     */
/*     Returns: -1 if any module returns non-NULL, else 0              */
/*                                                                     */
/***********************************************************************/
static int sw_mod_loop(int req)
{
  int i;

  for (i = 0; ModuleList[i]; ++i)
    if (ModuleList[i](req))
      return -1;

  return 0;
}

/***********************************************************************/
/* fill_dir_entry: Fill in fields for DirEntry for a directory entry   */
/*                                                                     */
/*      Inputs: entry = pointer to directory entry                     */
/*              dir_entry = pointer to DirEntry to use with comparison */
/*                                                                     */
/***********************************************************************/
static void fill_dir_entry(const FDIR_T *entry, DirEntry *dir_entry)
{
  dir_entry->st_name = entry->name;
  dir_entry->st_ino = entry->comm->fileno;
  dir_entry->st_mode = entry->comm->mode;
  dir_entry->st_nlink = entry->comm->links;
  dir_entry->st_uid = entry->comm->user_id;
  dir_entry->st_gid = entry->comm->group_id;
  dir_entry->st_size = entry->comm->size;
  dir_entry->st_atime = entry->comm->ac_time;
  dir_entry->st_mtime = entry->comm->mod_time;
  dir_entry->st_ctime = entry->comm->mod_time;
}

/***********************************************************************/
/* Global Function Definitions                                         */
/***********************************************************************/

/***********************************************************************/
/*   QuickSort: Perform quick sort on a list of directory entries      */
/*                                                                     */
/*      Inputs: head = start of list                                   */
/*              tail = end of list                                     */
/*              cmp = user comparison function                         */
/*                                                                     */
/***********************************************************************/
void QuickSort(FFSEnt **head, FFSEnt **tail, DirEntry *e1,
               DirEntry *e2, int (*cmp) (const DirEntry *,
                                         const DirEntry *))
{
  FFSEnt *less_head = NULL, *less_tail = NULL;
  FFSEnt *greater_head = NULL, *greater_tail = NULL;
  FFSEnt *pivot, *curr_ent;

  /*-------------------------------------------------------------------*/
  /* If list has one element, just return.                             */
  /*-------------------------------------------------------------------*/
  if (head == tail)
    return;

  /*-------------------------------------------------------------------*/
  /* Select the first element as the pivot.                            */

⌨️ 快捷键说明

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