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

📄 shell.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
  return link(filename, linkname);
}

/***********************************************************************/
/*       sh_ls: List the contents of a directory                       */
/*                                                                     */
/*       Input: cmd_line = rest of line from user                      */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
static int sh_ls(char *cmd_line)
{
  char flag[MAX_ARG], temp_time[14], permissions[11];
  struct dirent *entry;
  struct stat buf;
  DIR *curr_dir;
  int r_value = 0;

  /*-------------------------------------------------------------------*/
  /* If no flag entered, it's the same as the 'dir' command.           */
  /*-------------------------------------------------------------------*/
  if (!get_arg(cmd_line, flag))
    return sh_dir(cmd_line);

  /*-------------------------------------------------------------------*/
  /* Else, if it's the -l flag, display additional contents.           */
  /*-------------------------------------------------------------------*/
  else
  {
    /*-----------------------------------------------------------------*/
    /* Ignore all other flags.                                         */
    /*-----------------------------------------------------------------*/
    if (strcmp(flag, "-l"))
      return sh_dir(cmd_line);
    else
    {
      /*---------------------------------------------------------------*/
      /* If unable to get the current working directory, return error. */
      /*---------------------------------------------------------------*/
      if (!getcwd(flag, MAX_ARG - 1))
        return -1;

      /*---------------------------------------------------------------*/
      /* Open the current directory.                                   */
      /*---------------------------------------------------------------*/
      curr_dir = opendir(flag);
      if (curr_dir == NULL)
        return -1;

      /*---------------------------------------------------------------*/
      /* Loop to read every file in the directory.                     */
      /*---------------------------------------------------------------*/
      for (;;)
      {
        /*-------------------------------------------------------------*/
        /* Read one entry at a time from directory.                    */
        /*-------------------------------------------------------------*/
        entry = readdir(curr_dir);

        /*-------------------------------------------------------------*/
        /* If we've reached the end of the directory, stop.            */
        /*-------------------------------------------------------------*/
        if (!entry)
          break;

        /*-------------------------------------------------------------*/
        /* Output contents of entry.                                   */
        /*-------------------------------------------------------------*/
        if (!stat(entry->d_name, &buf))
        {
          /*-----------------------------------------------------------*/
          /* Check if the entry is a directory or a file.              */
          /*-----------------------------------------------------------*/
          permissions[10] = '\0';
          if (S_ISDIR(buf.st_mode))
            permissions[0] = 'd';
          else
            permissions[0] = '-';

          /*-----------------------------------------------------------*/
          /* Set string with permissions for entry.                    */
          /*-----------------------------------------------------------*/
          if (buf.st_mode & S_IRUSR)
            permissions[1] = 'r';
          else
            permissions[1] = '-';
          if (buf.st_mode & S_IWUSR)
            permissions[2] = 'w';
          else
            permissions[2] = '-';
          if (buf.st_mode & S_IXUSR)
            permissions[3] = 'x';
          else
            permissions[3] = '-';
          if (buf.st_mode & S_IRGRP)
            permissions[4] = 'r';
          else
            permissions[4] = '-';
          if (buf.st_mode & S_IWGRP)
            permissions[5] = 'w';
          else
            permissions[5] = '-';
          if (buf.st_mode & S_IXGRP)
            permissions[6] = 'x';
          else
            permissions[6] = '-';
          if (buf.st_mode & S_IROTH)
            permissions[7] = 'r';
          else
            permissions[7] = '-';
          if (buf.st_mode & S_IWOTH)
            permissions[8] = 'w';
          else
            permissions[8] = '-';
          if (buf.st_mode & S_IXOTH)
            permissions[9] = 'x';
          else
            permissions[9] = '-';

          /*-----------------------------------------------------------*/
          /* Create a string version of the access time for the entry. */
          /*-----------------------------------------------------------*/
          strftime(temp_time, 13,"%b %d %H:%M",localtime(&buf.st_atime));

          /*-----------------------------------------------------------*/
          /* Output contents of the entry.                             */
          /*-----------------------------------------------------------*/
          printf("%s %u %5u %5u %10d %s %.*s\n", permissions,
                 buf.st_nlink, buf.st_uid, buf.st_gid, buf.st_size,
                 temp_time, FILENAME_MAX, entry->d_name);
        }
        else
        {
          r_value = -1;
          break;
        }
      }

      /*---------------------------------------------------------------*/
      /* Reset the directory position indicator.                       */
      /*---------------------------------------------------------------*/
      rewinddir(curr_dir);

      /*---------------------------------------------------------------*/
      /* If failed to close directory, return error.                   */
      /*---------------------------------------------------------------*/
      if (closedir(curr_dir))
        return -1;
    }
  }
  return r_value;
}

/***********************************************************************/
/*    sh_mkdir: Create a directory                                     */
/*                                                                     */
/*       Input: cmd_line = rest of line from user                      */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
static int sh_mkdir(char *cmd_line)
{
  char dirname[MAX_ARG];

  /*-------------------------------------------------------------------*/
  /* Get directory name.                                               */
  /*-------------------------------------------------------------------*/
  if (!get_arg(cmd_line, dirname))
    return -1;

  /*-------------------------------------------------------------------*/
  /* Make directory and return result.                                 */
  /*-------------------------------------------------------------------*/
  return mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR);
}

/***********************************************************************/
/*     sh_more: Display contents of a file                             */
/*                                                                     */
/*       Input: cmd_line = rest of line from user                      */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
static int sh_more(char *cmd_line)
{
  char filename[MAX_ARG], buf[2 * PAGE_SIZE + 1];
  int num_bytes, fid;

  /*-------------------------------------------------------------------*/
  /* Get name of file.                                                 */
  /*-------------------------------------------------------------------*/
  if (!get_arg(cmd_line, filename))
    return -1;

  /*-------------------------------------------------------------------*/
  /* Open file in read only mode.                                      */
  /*-------------------------------------------------------------------*/
  fid = open(filename, O_RDONLY);
  if (fid == -1)
    return -1;

  /*-------------------------------------------------------------------*/
  /* Go through file and display it 2 * PAGE_SIZE bytes at a time.     */
  /*-------------------------------------------------------------------*/
  for (num_bytes = 2 * PAGE_SIZE; num_bytes == 2 * PAGE_SIZE;)
  {
    /*-----------------------------------------------------------------*/
    /* Read 2 * PAGE_SIZE bytes at a time from the file.               */
    /*-----------------------------------------------------------------*/
    num_bytes = read(fid, buf, 2 * PAGE_SIZE);

    /*-----------------------------------------------------------------*/
    /* Output the read bytes.                                          */
    /*-----------------------------------------------------------------*/
    buf[num_bytes] = '\0';
    printf("%s", buf);
  }
  putchar('\n');

  /*-------------------------------------------------------------------*/
  /* If closing the file fails, return error.                          */
  /*-------------------------------------------------------------------*/
  if (close(fid))
    return -1;

  return 0;
}

/***********************************************************************/
/*    sh_mount: Mount a file system volume                             */
/*                                                                     */
/*       Input: cmd_line = rest of line from user                      */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
static int sh_mount(char *cmd_line)
{
  char fsname[MAX_ARG];

  /*-------------------------------------------------------------------*/
  /* Get the file system name.                                         */
  /*-------------------------------------------------------------------*/
  if (!get_arg(cmd_line, fsname))
    return -1;

  /*-------------------------------------------------------------------*/
  /* Mount system and return result.                                   */
  /*-------------------------------------------------------------------*/
  return mount(fsname);
}

/***********************************************************************/
/* sh_mount_all: Mount all existing unmounted devices                  */
/*                                                                     */
/*       Input: cmd_line = rest of line from user                      */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
static int sh_mount_all(char *cmd_line)
{
  return mount_all(TRUE);
}

/***********************************************************************/
/*       sh_mv: Move file/dir to new location                          */
/*                                                                     */
/*       Input: cmd_line = rest of line from user                      */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
static int sh_mv(char *cmd_line)
{
  char old_location[MAX_ARG], new_location[MAX_ARG];
  int curr;

  /*-------------------------------------------------------------------*/
  /* Get the old name.                                                 */
  /*-------------------------------------------------------------------*/
  curr = get_arg(cmd_line, old_location);
  if (!curr)
    return -1;

  /*-------------------------------------------------------------------*/
  /* Get the new path name.                                            */
  /*-------------------------------------------------------------------*/
  if (!get_arg(&cmd_line[curr], new_location))
    return -1;

  /*-------------------------------------------------------------------*/
  /* Call rename() function, return its result code.                   */
  /*-------------------------------------------------------------------*/
  return renameFFS(old_location, new_location);

⌨️ 快捷键说明

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