optional added

This commit is contained in:
2025-03-03 18:33:08 +08:00
parent a226786836
commit 962a7083da
5 changed files with 173 additions and 154 deletions

View File

@ -189,6 +189,7 @@ UPROGS=\
$U/_wc\
$U/_zombie\
$U/_sleep\
$U/_uptime\
$U/_pingpong\
$U/_primes\
$U/_find\

View File

@ -1 +1 @@
3
5

View File

@ -3,20 +3,48 @@
#include "kernel/types.h"
#include "user/user.h"
void find(char *path, char *filename) {
int fd = open(path, 0);
int match_pattern(const char *name, const char *pattern) {
const char *star = 0;
const char *name_ptr = name;
const char *pattern_ptr = pattern;
while (1) {
if (*pattern_ptr == '*') {
star = pattern_ptr++;
name_ptr = name;
continue;
}
if (!*name)
return (!*pattern_ptr || (star && !*++pattern_ptr));
if (*pattern_ptr == '?' || *pattern_ptr == *name) {
pattern_ptr++;
name++;
continue;
}
if (star) {
pattern_ptr = star + 1;
name = ++name_ptr;
continue;
}
return 0;
}
}
void find(char *path, char *pattern) {
char buf[512], *p;
int fd;
struct dirent de;
struct stat st;
if (fd < 0 || strlen(path) + 1 + DIRSIZ + 1 >= sizeof(buf)) {
fprintf(2, "find: Invalid path: %s\n", path);
// close(fd);
if ((fd = open(path, 0)) < 0) {
fprintf(2, "find: cannot open %s\n", path);
return;
}
if (fstat(fd, &st) < 0) {
fprintf(2, "find: Failed to stat %s\n", path);
fprintf(2, "find: cannot stat %s\n", path);
close(fd);
return;
}
@ -27,36 +55,42 @@ void find(char *path, char *filename) {
return;
}
strncpy(buf, path, strlen(path) + 1);
p = buf + strlen(path);
if (strlen(path) + 1 + DIRSIZ + 1 > sizeof buf) {
fprintf(2, "find: path too long\n");
close(fd);
return;
}
strcpy(buf, path);
p = buf + strlen(buf);
*p++ = '/';
while (read(fd, &de, sizeof(de)) == sizeof(de)) {
if (de.inum == 0)
continue;
if (!strcmp(de.name, ".") || !strcmp(de.name, ".."))
if (de.inum == 0 || !strcmp(de.name, ".") || !strcmp(de.name, ".."))
continue;
memmove(p, de.name, DIRSIZ);
p[DIRSIZ] = 0;
if (stat(buf, &st) < 0) {
fprintf(2, "find: Failed to stat %s\n", buf);
fprintf(2, "find: cannot stat %s\n", buf);
continue;
}
if (st.type == T_DIR) {
find(buf, filename);
find(buf, pattern);
} else if (st.type == T_FILE) {
if (!strcmp(filename, "*") || !strcmp(filename, de.name)) {
printf("%s\n", buf);
if (match_pattern(de.name, pattern)) {
fprintf(1, "%s\n", buf);
}
}
}
close(fd);
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: find path filename\n");
fprintf(2, "Usage: find <path> <pattern>\n");
exit(1);
}
find(argv[1], argv[2]);

View File

@ -1,8 +1,9 @@
// Shell.
#include "kernel/fcntl.h"
#include "kernel/fs.h"
#include "kernel/types.h"
#include "user/user.h"
#include "kernel/fcntl.h"
// Parsed command representation
#define EXEC 1
@ -53,11 +54,11 @@ int fork1(void); // Fork but panics on failure.
void panic(char *);
struct cmd *parsecmd(char *);
void runcmd(struct cmd *) __attribute__((noreturn));
// void handle_tab_completion(char *buf, int *i, int nbuf);
// 尝试添加tab补全功能
// Execute cmd. Never returns.
void
runcmd(struct cmd *cmd)
{
void runcmd(struct cmd *cmd) {
int p[2];
struct backcmd *bcmd;
struct execcmd *ecmd;
@ -131,9 +132,7 @@ runcmd(struct cmd *cmd)
exit(0);
}
int
getcmd(char *buf, int nbuf)
{
int getcmd(char *buf, int nbuf) {
write(2, "$ ", 2);
memset(buf, 0, nbuf);
gets(buf, nbuf);
@ -142,9 +141,7 @@ getcmd(char *buf, int nbuf)
return 0;
}
int
main(void)
{
int main(void) {
static char buf[100];
int fd;
@ -158,6 +155,9 @@ main(void)
// Read and run input commands.
while (getcmd(buf, sizeof(buf)) >= 0) {
if (strcmp(buf, "exit\n") == 0) {
exit(0);
}
if (buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' ') {
// Chdir must be called by the parent, not the child.
buf[strlen(buf) - 1] = 0; // chop \n
@ -172,16 +172,12 @@ main(void)
exit(0);
}
void
panic(char *s)
{
void panic(char *s) {
fprintf(2, "%s\n", s);
exit(1);
}
int
fork1(void)
{
int fork1(void) {
int pid;
pid = fork();
@ -193,9 +189,7 @@ fork1(void)
// PAGEBREAK!
// Constructors
struct cmd*
execcmd(void)
{
struct cmd *execcmd(void) {
struct execcmd *cmd;
cmd = malloc(sizeof(*cmd));
@ -204,9 +198,8 @@ execcmd(void)
return (struct cmd *)cmd;
}
struct cmd*
redircmd(struct cmd *subcmd, char *file, char *efile, int mode, int fd)
{
struct cmd *redircmd(struct cmd *subcmd, char *file, char *efile, int mode,
int fd) {
struct redircmd *cmd;
cmd = malloc(sizeof(*cmd));
@ -220,9 +213,7 @@ redircmd(struct cmd *subcmd, char *file, char *efile, int mode, int fd)
return (struct cmd *)cmd;
}
struct cmd*
pipecmd(struct cmd *left, struct cmd *right)
{
struct cmd *pipecmd(struct cmd *left, struct cmd *right) {
struct pipecmd *cmd;
cmd = malloc(sizeof(*cmd));
@ -233,9 +224,7 @@ pipecmd(struct cmd *left, struct cmd *right)
return (struct cmd *)cmd;
}
struct cmd*
listcmd(struct cmd *left, struct cmd *right)
{
struct cmd *listcmd(struct cmd *left, struct cmd *right) {
struct listcmd *cmd;
cmd = malloc(sizeof(*cmd));
@ -246,9 +235,7 @@ listcmd(struct cmd *left, struct cmd *right)
return (struct cmd *)cmd;
}
struct cmd*
backcmd(struct cmd *subcmd)
{
struct cmd *backcmd(struct cmd *subcmd) {
struct backcmd *cmd;
cmd = malloc(sizeof(*cmd));
@ -263,9 +250,7 @@ backcmd(struct cmd *subcmd)
char whitespace[] = " \t\r\n\v";
char symbols[] = "<|>&;()";
int
gettoken(char **ps, char *es, char **q, char **eq)
{
int gettoken(char **ps, char *es, char **q, char **eq) {
char *s;
int ret;
@ -308,9 +293,7 @@ gettoken(char **ps, char *es, char **q, char **eq)
return ret;
}
int
peek(char **ps, char *es, char *toks)
{
int peek(char **ps, char *es, char *toks) {
char *s;
s = *ps;
@ -325,9 +308,7 @@ struct cmd *parsepipe(char**, char*);
struct cmd *parseexec(char **, char *);
struct cmd *nulterminate(struct cmd *);
struct cmd*
parsecmd(char *s)
{
struct cmd *parsecmd(char *s) {
char *es;
struct cmd *cmd;
@ -342,9 +323,7 @@ parsecmd(char *s)
return cmd;
}
struct cmd*
parseline(char **ps, char *es)
{
struct cmd *parseline(char **ps, char *es) {
struct cmd *cmd;
cmd = parsepipe(ps, es);
@ -359,9 +338,7 @@ parseline(char **ps, char *es)
return cmd;
}
struct cmd*
parsepipe(char **ps, char *es)
{
struct cmd *parsepipe(char **ps, char *es) {
struct cmd *cmd;
cmd = parseexec(ps, es);
@ -372,9 +349,7 @@ parsepipe(char **ps, char *es)
return cmd;
}
struct cmd*
parseredirs(struct cmd *cmd, char **ps, char *es)
{
struct cmd *parseredirs(struct cmd *cmd, char **ps, char *es) {
int tok;
char *q, *eq;
@ -397,9 +372,7 @@ parseredirs(struct cmd *cmd, char **ps, char *es)
return cmd;
}
struct cmd*
parseblock(char **ps, char *es)
{
struct cmd *parseblock(char **ps, char *es) {
struct cmd *cmd;
if (!peek(ps, es, "("))
@ -413,9 +386,7 @@ parseblock(char **ps, char *es)
return cmd;
}
struct cmd*
parseexec(char **ps, char *es)
{
struct cmd *parseexec(char **ps, char *es) {
char *q, *eq;
int tok, argc;
struct execcmd *cmd;
@ -447,9 +418,7 @@ parseexec(char **ps, char *es)
}
// NUL-terminate all the counted strings.
struct cmd*
nulterminate(struct cmd *cmd)
{
struct cmd *nulterminate(struct cmd *cmd) {
int i;
struct backcmd *bcmd;
struct execcmd *ecmd;

15
user/uptime.c Normal file
View File

@ -0,0 +1,15 @@
#include "kernel/types.h"
#include "user/user.h"
int main(int argc, char *argv[]) {
if (argc != 1) {
// 错误输出到stderr文件描述符2
fprintf(2, "Usage: uptime\n");
exit(1);
}
fprintf(1,"up: %d\n",uptime());
exit(0);
}