📄 shell.c
字号:
/*-----------------------------------------------------------------*/
/* Output contents of entry. */
/*-----------------------------------------------------------------*/
printf("%s\n", entry->d_name);
}
/*-------------------------------------------------------------------*/
/* If failed to close directory, return error. */
/*-------------------------------------------------------------------*/
if (closedir(curr_dir))
return -1;
return r_value;
}
/***********************************************************************/
/* sh_display: Display the contents of a file */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_display(char *cmd_line)
{
char filename[MAX_ARG], output[DISPLAY_WIDTH], c;
int i, j, count = 0, s_out = 0, e_out = 0, fid, len;
/*-------------------------------------------------------------------*/
/* If no file name entered, return error. */
/*-------------------------------------------------------------------*/
if (!get_arg(cmd_line, filename))
return -1;
/*-------------------------------------------------------------------*/
/* Open file in read only mode, returning error if unable. */
/*-------------------------------------------------------------------*/
fid = open(filename, O_RDONLY);
if (fid == -1)
return -1;
/*-------------------------------------------------------------------*/
/* Loop to display a line at a time. */
/*-------------------------------------------------------------------*/
for (;;)
{
/*-----------------------------------------------------------------*/
/* Read into buf a line worth of characters. */
/*-----------------------------------------------------------------*/
for (i = count; i < DISPLAY_WIDTH; ++i)
{
/*---------------------------------------------------------------*/
/* Read character at a time, breaking if unable. */
/*---------------------------------------------------------------*/
len = read(fid, &c, 1);
if (len != 1)
break;
/*---------------------------------------------------------------*/
/* Remember the last ' ' encountered. */
/*---------------------------------------------------------------*/
if (c == ' ')
e_out = s_out + i;
/*---------------------------------------------------------------*/
/* Store the character read, c, in the output buffer. */
/*---------------------------------------------------------------*/
output[(s_out + i) % DISPLAY_WIDTH] = c;
}
/*-----------------------------------------------------------------*/
/* If -1, display output buffer and quit. */
/*-----------------------------------------------------------------*/
if (len != 1)
{
for (j = 0; j < i; ++j)
putchar(output[(s_out + j) % DISPLAY_WIDTH]);
putchar('\n');
break;
}
/*-----------------------------------------------------------------*/
/* If there is a space in the output buffer, display from */
/* the beginning (s_out) until last space (e_out). */
/*-----------------------------------------------------------------*/
if (e_out > s_out)
{
for (j = s_out; j < e_out; ++j)
putchar(output[j % DISPLAY_WIDTH]);
count = DISPLAY_WIDTH - e_out + s_out - 1;
s_out = e_out + 1;
}
/*-----------------------------------------------------------------*/
/* Else there is no space so display the whole thing. */
/*-----------------------------------------------------------------*/
else
{
for (j = 0; j < i; ++j)
putchar(output[(s_out + j) % DISPLAY_WIDTH]);
count = 0;
s_out += i;
}
putchar('\n');
}
/*-------------------------------------------------------------------*/
/* close the file. */
/*-------------------------------------------------------------------*/
if (close(fid))
return -1;
return 0;
}
/***********************************************************************/
/* sh_format: Format storage media for the file system */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_format(char *cmd_line)
{
char fsname[MAX_ARG];
/*-------------------------------------------------------------------*/
/* Get name of volume. */
/*-------------------------------------------------------------------*/
if (!get_arg(cmd_line, fsname))
return -1;
/*-------------------------------------------------------------------*/
/* Format and return the result. */
/*-------------------------------------------------------------------*/
return format(fsname);
}
/***********************************************************************/
/* sh_fstat: Show statistics of a file */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_fstat(char *cmd_line)
{
struct stat buf;
char fname[MAX_ARG];
int fid;
/*-------------------------------------------------------------------*/
/* Get name of file. */
/*-------------------------------------------------------------------*/
if (!get_arg(cmd_line, fname))
return -1;
/*-------------------------------------------------------------------*/
/* Open the file. */
/*-------------------------------------------------------------------*/
fid = open(fname, O_RDONLY);
if (fid < 0)
return -1;
/*-------------------------------------------------------------------*/
/* Get file's statistics. */
/*-------------------------------------------------------------------*/
if (fstat(fid, &buf))
{
close(fid);
return -1;
}
/*-------------------------------------------------------------------*/
/* Close the file. */
/*-------------------------------------------------------------------*/
if (close(fid))
return -1;
/*-------------------------------------------------------------------*/
/* Output some of the stats for file. */
/*-------------------------------------------------------------------*/
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;
}
/***********************************************************************/
/* sh_help: Display help about shell commands */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_help(char *cmd_line)
{
char command[MAX_ARG];
int detailed = 0, i;
/*-------------------------------------------------------------------*/
/* If extra parameter entered, it's a detailed help. */
/*-------------------------------------------------------------------*/
if (get_arg(cmd_line, command))
detailed = 1;
/*-------------------------------------------------------------------*/
/* If it's a detailed help look for command for which to display */
/* the detailed message, else display all available commands. */
/*-------------------------------------------------------------------*/
if (detailed)
{
/*-----------------------------------------------------------------*/
/* Step through all the available commands looking for right one. */
/*-----------------------------------------------------------------*/
for (i = 0; ShellFuncs[i].func; ++i)
{
/*---------------------------------------------------------------*/
/* If command is found, output its help message. */
/*---------------------------------------------------------------*/
if (!strcmp(ShellFuncs[i].cmd, command))
{
printf("%s", ShellFuncs[i].help);
break;
}
}
/*-----------------------------------------------------------------*/
/* If command is not found, return error. */
/*-----------------------------------------------------------------*/
if (ShellFuncs[i].func == NULL)
return -1;
}
else
{
/*-----------------------------------------------------------------*/
/* Display four commands per line. */
/*-----------------------------------------------------------------*/
printf("LIST of available commands.\n");
printf("Type 'help command' to view specific command help\n");
for (i = 0; ShellFuncs[i].func; ++i)
{
/*---------------------------------------------------------------*/
/* Every four commands display and end of line. */
/*---------------------------------------------------------------*/
if (!(i % 4))
putchar('\n');
/*---------------------------------------------------------------*/
/* Display command name. */
/*---------------------------------------------------------------*/
printf("%15s ", ShellFuncs[i].cmd);
}
/*-----------------------------------------------------------------*/
/* Display and end of line at the end. */
/*-----------------------------------------------------------------*/
putchar('\n');
}
return 0;
}
/***********************************************************************/
/* sh_link: Link a file to another file */
/* */
/* Input: cmd_line = rest of line from user */
/* */
/* Returns: 0 on success, -1 on error */
/* */
/***********************************************************************/
static int sh_link(char *cmd_line)
{
int curr;
char filename[MAX_ARG], linkname[MAX_ARG];
/*-------------------------------------------------------------------*/
/* Get name of original file. */
/*-------------------------------------------------------------------*/
curr = get_arg(cmd_line, filename);
if (!curr)
return -1;
/*-------------------------------------------------------------------*/
/* If no name entered for the link, return error. */
/*-------------------------------------------------------------------*/
if (!get_arg(&cmd_line[curr], linkname))
return -1;
/*-------------------------------------------------------------------*/
/* Link and return the result. */
/*-------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -