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

📄 shell.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
    /*-----------------------------------------------------------------*/
    /* Echo printable characters if line not full, else emit alert.    */
    /*-----------------------------------------------------------------*/
    else
    {
      if (i < (MAX_LINE - 1))
      {
        cmd_line[i++] = ch;
        putchar(ch);
      }
      else
        putchar('\a');
    }
  }
}

/***********************************************************************/
/*     get_arg: Get an argument from a command line                    */
/*                                                                     */
/*      Inputs: cmd_line = command line from which arg is taken        */
/*              arg = pointer to string to store arg                   */
/*                                                                     */
/*     Returns: Position in command line where we stopped              */
/*                                                                     */
/***********************************************************************/
static int get_arg(char *cmd_line, char *arg)
{
  char *start = cmd_line;

  /*-------------------------------------------------------------------*/
  /* Skip the leading spaces.                                          */
  /*-------------------------------------------------------------------*/
  while (isspace(*cmd_line))
    ++cmd_line;

  /*-------------------------------------------------------------------*/
  /* Store at most MAX_ARG chars into arg.                             */
  /*-------------------------------------------------------------------*/
  while ((cmd_line - start) < MAX_ARG - 1)
  {
    /*-----------------------------------------------------------------*/
    /* If we've reached another space or the end of line, stop.        */
    /*-----------------------------------------------------------------*/
    if (isspace(*cmd_line) || (*cmd_line == '\0'))
      break;

    /*-----------------------------------------------------------------*/
    /* Copy current char from command line into arg.                   */
    /*-----------------------------------------------------------------*/
    *arg++ = *cmd_line++;
  }

  /*-------------------------------------------------------------------*/
  /* Skip trailing spaces if any.                                      */
  /*-------------------------------------------------------------------*/
  while (isspace(*cmd_line))
    ++cmd_line;

  /*-------------------------------------------------------------------*/
  /* Terminate the arg string with '\0' and return location in line.   */
  /*-------------------------------------------------------------------*/
  *arg = '\0';
  return cmd_line - start;
}

/***********************************************************************/
/*   sh_append: Append a string to a file                              */
/*                                                                     */
/*       Input: cmd_line = rest of line from user                      */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
static int sh_append(char *cmd_line)
{
  int curr, num_bytes, r_value = 0, fid;
  char filename[MAX_ARG];

  /*-------------------------------------------------------------------*/
  /* Get the name of the file.                                         */
  /*-------------------------------------------------------------------*/
  curr = get_arg(cmd_line, filename);

  /*-------------------------------------------------------------------*/
  /* If no filename entered, return error.                             */
  /*-------------------------------------------------------------------*/
  if (!curr)
    return -1;

  /*-------------------------------------------------------------------*/
  /* Open file in append mode.                                         */
  /*-------------------------------------------------------------------*/
  fid = open(filename, O_RDWR | O_APPEND);

  /*-------------------------------------------------------------------*/
  /* If open failed, return error.                                     */
  /*-------------------------------------------------------------------*/
  if (fid == -1)
    return -1;

  /*-------------------------------------------------------------------*/
  /* If writing string to file fails, return error.                    */
  /*-------------------------------------------------------------------*/
  num_bytes = strlen(&cmd_line[curr]);
  if (write(fid, &cmd_line[curr], num_bytes) != num_bytes)
    r_value = -1;

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

  return r_value;
}

/***********************************************************************/
/*       sh_cd: Change working directory for the shell                 */
/*                                                                     */
/*       Input: cmd_line = rest of line from user                      */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
static int sh_cd(char *cmd_line)
{
  char dirname[MAX_ARG];

  /*-------------------------------------------------------------------*/
  /* If no dirname entered, return error.                              */
  /*-------------------------------------------------------------------*/
  if (!get_arg(cmd_line, dirname))
    return -1;

  /*-------------------------------------------------------------------*/
  /* Change directory.                                                 */
  /*-------------------------------------------------------------------*/
  return chdir(dirname);
}

/***********************************************************************/
/*       sh_cp: Copy a file into another file                          */
/*                                                                     */
/*       Input: cmd_line = rest of line from user                      */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
static int sh_cp(char *cmd_line)
{
  char orig_file[MAX_ARG], new_file[MAX_ARG], buf[300];
  int curr, num_bytes, orig_fid, new_fid, r_value = 0;

  /*-------------------------------------------------------------------*/
  /* Get name of original file.                                        */
  /*-------------------------------------------------------------------*/
  curr = get_arg(cmd_line, orig_file);
  if (!curr)
    return -1;

  /*-------------------------------------------------------------------*/
  /* Get name of new file.                                             */
  /*-------------------------------------------------------------------*/
  if (!get_arg(&cmd_line[curr], new_file))
    return -1;

  /*-------------------------------------------------------------------*/
  /* Open original file as read only.                                  */
  /*-------------------------------------------------------------------*/
  orig_fid = open(orig_file, O_RDONLY);
  if (orig_fid == -1)
    return -1;

  /*-------------------------------------------------------------------*/
  /* Open new file as write-only.                                      */
  /*-------------------------------------------------------------------*/
  new_fid = open(new_file, O_WRONLY | O_CREAT | O_EXCL,
                 S_IRUSR | S_IWUSR | S_IXUSR);
  if (new_fid == -1)
  {
    close(orig_fid);
    return  -1;
  }

  /*-------------------------------------------------------------------*/
  /* Copy original file into new file (300 bytes at a time).           */
  /*-------------------------------------------------------------------*/
  for (num_bytes = 300; num_bytes == 300;)
  {
    /*-----------------------------------------------------------------*/
    /* Read 300 bytes (or less) from original file.                    */
    /*-----------------------------------------------------------------*/
    num_bytes = read(orig_fid, buf, 300);

    /*-----------------------------------------------------------------*/
    /* If error occurred while writing to file, stop.                  */
    /*-----------------------------------------------------------------*/
    if (write(new_fid, buf, num_bytes) != num_bytes)
    {
      r_value = -1;
      break;
    }
  }

  /*-------------------------------------------------------------------*/
  /* Close both files.                                                 */
  /*-------------------------------------------------------------------*/
  if (close(new_fid))
    r_value = -1;
  if (close(orig_fid))
    r_value = -1;

  return r_value;
}

/***********************************************************************/
/*   sh_create: Create a new file and adds a string to it              */
/*                                                                     */
/*       Input: cmd_line = rest of line from user                      */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
static int sh_create(char *cmd_line)
{
  char filename[MAX_ARG];
  int curr, num_bytes, r_value = 0, fid;

  /*-------------------------------------------------------------------*/
  /* Get name of file to be created.                                   */
  /*-------------------------------------------------------------------*/
  curr = get_arg(cmd_line, filename);
  if (!curr)
    return -1;

  /*-------------------------------------------------------------------*/
  /* Open the file for write only.                                     */
  /*-------------------------------------------------------------------*/
  fid = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR);
  if (fid == -1)
    return -1;

  /*-------------------------------------------------------------------*/
  /* Write the command line string to the file.                        */
  /*-------------------------------------------------------------------*/
  num_bytes = strlen(&cmd_line[curr]);
  if (write(fid, &cmd_line[curr], num_bytes) != num_bytes)
    r_value = -1;

  /*-------------------------------------------------------------------*/
  /* If failed to close file, return error.                            */
  /*-------------------------------------------------------------------*/
  if (close(fid))
    r_value = -1;

  return r_value;
}

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

  /*-------------------------------------------------------------------*/
  /* If unable to get the current working directory, return error.     */
  /*-------------------------------------------------------------------*/
  if (!getcwd(dirname, MAX_ARG - 1))
    return -1;

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

  /*-------------------------------------------------------------------*/
  /* Loop to read every file in the current directory.                 */
  /*-------------------------------------------------------------------*/
  for (;;)
  {
    /*-----------------------------------------------------------------*/
    /* Read an entry at a time from directory until end is reached.    */
    /*-----------------------------------------------------------------*/
    entry = readdir(curr_dir);
    if (entry == NULL)
      break;

⌨️ 快捷键说明

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