📄 shell.c
字号:
}
/***********************************************************************/
/* sh_pwd: Get the current working directory */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_pwd(char *cmd_line)
{
char *cwd;
/*-------------------------------------------------------------------*/
/* Get name of current working directory. */
/*-------------------------------------------------------------------*/
cwd = getcwd(NULL, 0);
if (cwd == NULL)
return -1;
/*-------------------------------------------------------------------*/
/* Display directory name and free memory allocated for name. */
/*-------------------------------------------------------------------*/
puts(cwd);
free(cwd);
return 0;
}
/***********************************************************************/
/* sh_rm: Delete a file */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_rm(char *cmd_line)
{
char filename[MAX_ARG];
/*-------------------------------------------------------------------*/
/* If no file name entered, return error. */
/*-------------------------------------------------------------------*/
if (!get_arg(cmd_line, filename))
return -1;
/*-------------------------------------------------------------------*/
/* Remove the file and return result. */
/*-------------------------------------------------------------------*/
return unlink(filename);
}
/***********************************************************************/
/* sh_rmdir: Delete a directory */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_rmdir(char *cmd_line)
{
char dirname[MAX_ARG];
/*-------------------------------------------------------------------*/
/* If no dir name entered, return error. */
/*-------------------------------------------------------------------*/
if (!get_arg(cmd_line, dirname))
return -1;
/*-------------------------------------------------------------------*/
/* Remove the directory and return result. */
/*-------------------------------------------------------------------*/
return rmdir(dirname);
}
/***********************************************************************/
/* sh_stat: Show statistics of a file */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_stat(char *cmd_line)
{
struct stat buf;
char filename[MAX_ARG];
/*-------------------------------------------------------------------*/
/* Get name of file. */
/*-------------------------------------------------------------------*/
if (!get_arg(cmd_line, filename))
return -1;
/*-------------------------------------------------------------------*/
/* Call stat() to get the file's statistics. */
/*-------------------------------------------------------------------*/
if (stat(filename, &buf))
return -1;
/*-------------------------------------------------------------------*/
/* Display some of the statistics. */
/*-------------------------------------------------------------------*/
printf("File Access time: %s", ctime(&buf.st_atime));
printf("File Modified time: %s", ctime(&buf.st_mtime));
printf("File Mode: %d\n", buf.st_mode);
printf("File Serial Number: %d\n", buf.st_ino);
printf("File Num Links: %d\n", buf.st_nlink);
printf("File Size (bytes): %u\n", buf.st_size);
return 0;
}
/***********************************************************************/
/* sortdir_cmp: Comparison function used by sortdir(). Comparisons */
/* are done by one of: name, file number, size, mod time */
/* */
/* Inputs: e1 = first entry to compare */
/* e2 = second entry to compare */
/* */
/* Returns: -1 if e1 < e2, 0 if e1 = e2, 1 if e1 > e2 */
/* */
/***********************************************************************/
static int sortdir_cmp(const DirEntry *e1, const DirEntry *e2)
{
switch (SortDir)
{
case 2:
{
if (e1->st_size < e2->st_size)
return -1;
else if (e1->st_size > e2->st_size)
return 1;
else
return 0;
}
case 3:
{
if (e1->st_ino < e2->st_ino)
return -1;
else if (e1->st_ino > e2->st_ino)
return 1;
else
return 0;
}
case 4:
{
if (e1->st_mtime < e2->st_mtime)
return -1;
else if (e1->st_mtime > e2->st_mtime)
return 1;
else
return 0;
}
default:
return strcmp(e1->st_name, e2->st_name);
}
}
/***********************************************************************/
/* sh_sortdir: Sort a directory */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_sortdir(char *cmd_line)
{
int curr;
char dirname[MAX_ARG], sortoption[MAX_ARG];
/*-------------------------------------------------------------------*/
/* Get name of directory. */
/*-------------------------------------------------------------------*/
curr = get_arg(cmd_line, dirname);
if (!curr)
return -1;
/*-------------------------------------------------------------------*/
/* If no sortoption assume its by name. */
/*-------------------------------------------------------------------*/
if (!get_arg(&cmd_line[curr], sortoption))
SortDir = 1;
else
{
if (!strcmp(sortoption, "2"))
SortDir = 2;
else if (!strcmp(sortoption, "3"))
SortDir = 3;
else if (!strcmp(sortoption, "4"))
SortDir = 4;
else
SortDir = 1;
}
/*-------------------------------------------------------------------*/
/* Sort the directory. */
/*-------------------------------------------------------------------*/
return sortdir(dirname, sortdir_cmp);
}
/***********************************************************************/
/* sh_time: Time any valid shell command */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_time(char *cmd_line)
{
clock_t delta, sample;
/*-------------------------------------------------------------------*/
/* Start time measurement for app. */
/*-------------------------------------------------------------------*/
sample = clock();
/*-------------------------------------------------------------------*/
/* Execute shell command. */
/*-------------------------------------------------------------------*/
process_cmd(cmd_line);
/*-------------------------------------------------------------------*/
/* Get ending time stamp and display result. */
/*-------------------------------------------------------------------*/
delta = clock() - sample;
printf("shell command took %u ticks (%u seconds)\n",
delta, delta / CLOCKS_PER_SEC);
return 0;
}
/***********************************************************************/
/* sh_trunc: Truncate a file */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_trunc(char *cmd_line)
{
int curr, size;
char fname[MAX_ARG], size_str[MAX_ARG];
/*-------------------------------------------------------------------*/
/* Get name of the file. */
/*-------------------------------------------------------------------*/
curr = get_arg(cmd_line, fname);
if (!curr)
return -1;
/*-------------------------------------------------------------------*/
/* Get the size of the file after truncation. */
/*-------------------------------------------------------------------*/
if (!get_arg(&cmd_line[curr], size_str))
return -1;
size = atoi(size_str);
if (size < 0)
return -1;
/*-------------------------------------------------------------------*/
/* Truncate file. */
/*-------------------------------------------------------------------*/
return truncate(fname, (off_t)size);
}
/***********************************************************************/
/* sh_unformat: Unformat a file system */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_unformat(char *cmd_line)
{
char fsname[MAX_ARG];
/*-------------------------------------------------------------------*/
/* Get the file system name. */
/*-------------------------------------------------------------------*/
if (!get_arg(cmd_line, fsname))
return -1;
/*-------------------------------------------------------------------*/
/* Unformat file system and return result. */
/*-------------------------------------------------------------------*/
return unformat(fsname);
}
/***********************************************************************/
/* sh_unmount: Unmount a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -