optional added
This commit is contained in:
1
Makefile
1
Makefile
@ -189,6 +189,7 @@ UPROGS=\
|
|||||||
$U/_wc\
|
$U/_wc\
|
||||||
$U/_zombie\
|
$U/_zombie\
|
||||||
$U/_sleep\
|
$U/_sleep\
|
||||||
|
$U/_uptime\
|
||||||
$U/_pingpong\
|
$U/_pingpong\
|
||||||
$U/_primes\
|
$U/_primes\
|
||||||
$U/_find\
|
$U/_find\
|
||||||
|
|||||||
66
user/find.c
66
user/find.c
@ -3,20 +3,48 @@
|
|||||||
#include "kernel/types.h"
|
#include "kernel/types.h"
|
||||||
#include "user/user.h"
|
#include "user/user.h"
|
||||||
|
|
||||||
void find(char *path, char *filename) {
|
int match_pattern(const char *name, const char *pattern) {
|
||||||
int fd = open(path, 0);
|
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;
|
char buf[512], *p;
|
||||||
|
int fd;
|
||||||
struct dirent de;
|
struct dirent de;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
if (fd < 0 || strlen(path) + 1 + DIRSIZ + 1 >= sizeof(buf)) {
|
if ((fd = open(path, 0)) < 0) {
|
||||||
fprintf(2, "find: Invalid path: %s\n", path);
|
fprintf(2, "find: cannot open %s\n", path);
|
||||||
// close(fd);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fstat(fd, &st) < 0) {
|
if (fstat(fd, &st) < 0) {
|
||||||
fprintf(2, "find: Failed to stat %s\n", path);
|
fprintf(2, "find: cannot stat %s\n", path);
|
||||||
close(fd);
|
close(fd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -27,36 +55,42 @@ void find(char *path, char *filename) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
strncpy(buf, path, strlen(path) + 1);
|
if (strlen(path) + 1 + DIRSIZ + 1 > sizeof buf) {
|
||||||
p = buf + strlen(path);
|
fprintf(2, "find: path too long\n");
|
||||||
|
close(fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(buf, path);
|
||||||
|
p = buf + strlen(buf);
|
||||||
*p++ = '/';
|
*p++ = '/';
|
||||||
|
|
||||||
while (read(fd, &de, sizeof(de)) == sizeof(de)) {
|
while (read(fd, &de, sizeof(de)) == sizeof(de)) {
|
||||||
if (de.inum == 0)
|
if (de.inum == 0 || !strcmp(de.name, ".") || !strcmp(de.name, ".."))
|
||||||
continue;
|
|
||||||
if (!strcmp(de.name, ".") || !strcmp(de.name, ".."))
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
memmove(p, de.name, DIRSIZ);
|
memmove(p, de.name, DIRSIZ);
|
||||||
p[DIRSIZ] = 0;
|
p[DIRSIZ] = 0;
|
||||||
|
|
||||||
if (stat(buf, &st) < 0) {
|
if (stat(buf, &st) < 0) {
|
||||||
fprintf(2, "find: Failed to stat %s\n", buf);
|
fprintf(2, "find: cannot stat %s\n", buf);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (st.type == T_DIR) {
|
if (st.type == T_DIR) {
|
||||||
find(buf, filename);
|
find(buf, pattern);
|
||||||
} else if (st.type == T_FILE) {
|
} else if (st.type == T_FILE) {
|
||||||
if (!strcmp(filename, "*") || !strcmp(filename, de.name)) {
|
if (match_pattern(de.name, pattern)) {
|
||||||
printf("%s\n", buf);
|
fprintf(1, "%s\n", buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc != 3) {
|
if (argc != 3) {
|
||||||
printf("Usage: find path filename\n");
|
fprintf(2, "Usage: find <path> <pattern>\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
find(argv[1], argv[2]);
|
find(argv[1], argv[2]);
|
||||||
|
|||||||
85
user/sh.c
85
user/sh.c
@ -1,8 +1,9 @@
|
|||||||
// Shell.
|
// Shell.
|
||||||
|
|
||||||
|
#include "kernel/fcntl.h"
|
||||||
|
#include "kernel/fs.h"
|
||||||
#include "kernel/types.h"
|
#include "kernel/types.h"
|
||||||
#include "user/user.h"
|
#include "user/user.h"
|
||||||
#include "kernel/fcntl.h"
|
|
||||||
|
|
||||||
// Parsed command representation
|
// Parsed command representation
|
||||||
#define EXEC 1
|
#define EXEC 1
|
||||||
@ -53,11 +54,11 @@ int fork1(void); // Fork but panics on failure.
|
|||||||
void panic(char *);
|
void panic(char *);
|
||||||
struct cmd *parsecmd(char *);
|
struct cmd *parsecmd(char *);
|
||||||
void runcmd(struct cmd *) __attribute__((noreturn));
|
void runcmd(struct cmd *) __attribute__((noreturn));
|
||||||
|
// void handle_tab_completion(char *buf, int *i, int nbuf);
|
||||||
|
// 尝试添加tab补全功能
|
||||||
|
|
||||||
// Execute cmd. Never returns.
|
// Execute cmd. Never returns.
|
||||||
void
|
void runcmd(struct cmd *cmd) {
|
||||||
runcmd(struct cmd *cmd)
|
|
||||||
{
|
|
||||||
int p[2];
|
int p[2];
|
||||||
struct backcmd *bcmd;
|
struct backcmd *bcmd;
|
||||||
struct execcmd *ecmd;
|
struct execcmd *ecmd;
|
||||||
@ -131,9 +132,7 @@ runcmd(struct cmd *cmd)
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int getcmd(char *buf, int nbuf) {
|
||||||
getcmd(char *buf, int nbuf)
|
|
||||||
{
|
|
||||||
write(2, "$ ", 2);
|
write(2, "$ ", 2);
|
||||||
memset(buf, 0, nbuf);
|
memset(buf, 0, nbuf);
|
||||||
gets(buf, nbuf);
|
gets(buf, nbuf);
|
||||||
@ -142,9 +141,7 @@ getcmd(char *buf, int nbuf)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int main(void) {
|
||||||
main(void)
|
|
||||||
{
|
|
||||||
static char buf[100];
|
static char buf[100];
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
@ -158,6 +155,9 @@ main(void)
|
|||||||
|
|
||||||
// Read and run input commands.
|
// Read and run input commands.
|
||||||
while (getcmd(buf, sizeof(buf)) >= 0) {
|
while (getcmd(buf, sizeof(buf)) >= 0) {
|
||||||
|
if (strcmp(buf, "exit\n") == 0) {
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
if (buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' ') {
|
if (buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' ') {
|
||||||
// Chdir must be called by the parent, not the child.
|
// Chdir must be called by the parent, not the child.
|
||||||
buf[strlen(buf) - 1] = 0; // chop \n
|
buf[strlen(buf) - 1] = 0; // chop \n
|
||||||
@ -172,16 +172,12 @@ main(void)
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void panic(char *s) {
|
||||||
panic(char *s)
|
|
||||||
{
|
|
||||||
fprintf(2, "%s\n", s);
|
fprintf(2, "%s\n", s);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int fork1(void) {
|
||||||
fork1(void)
|
|
||||||
{
|
|
||||||
int pid;
|
int pid;
|
||||||
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
@ -193,9 +189,7 @@ fork1(void)
|
|||||||
// PAGEBREAK!
|
// PAGEBREAK!
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
struct cmd*
|
struct cmd *execcmd(void) {
|
||||||
execcmd(void)
|
|
||||||
{
|
|
||||||
struct execcmd *cmd;
|
struct execcmd *cmd;
|
||||||
|
|
||||||
cmd = malloc(sizeof(*cmd));
|
cmd = malloc(sizeof(*cmd));
|
||||||
@ -204,9 +198,8 @@ execcmd(void)
|
|||||||
return (struct cmd *)cmd;
|
return (struct cmd *)cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct cmd*
|
struct cmd *redircmd(struct cmd *subcmd, char *file, char *efile, int mode,
|
||||||
redircmd(struct cmd *subcmd, char *file, char *efile, int mode, int fd)
|
int fd) {
|
||||||
{
|
|
||||||
struct redircmd *cmd;
|
struct redircmd *cmd;
|
||||||
|
|
||||||
cmd = malloc(sizeof(*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;
|
return (struct cmd *)cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct cmd*
|
struct cmd *pipecmd(struct cmd *left, struct cmd *right) {
|
||||||
pipecmd(struct cmd *left, struct cmd *right)
|
|
||||||
{
|
|
||||||
struct pipecmd *cmd;
|
struct pipecmd *cmd;
|
||||||
|
|
||||||
cmd = malloc(sizeof(*cmd));
|
cmd = malloc(sizeof(*cmd));
|
||||||
@ -233,9 +224,7 @@ pipecmd(struct cmd *left, struct cmd *right)
|
|||||||
return (struct cmd *)cmd;
|
return (struct cmd *)cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct cmd*
|
struct cmd *listcmd(struct cmd *left, struct cmd *right) {
|
||||||
listcmd(struct cmd *left, struct cmd *right)
|
|
||||||
{
|
|
||||||
struct listcmd *cmd;
|
struct listcmd *cmd;
|
||||||
|
|
||||||
cmd = malloc(sizeof(*cmd));
|
cmd = malloc(sizeof(*cmd));
|
||||||
@ -246,9 +235,7 @@ listcmd(struct cmd *left, struct cmd *right)
|
|||||||
return (struct cmd *)cmd;
|
return (struct cmd *)cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct cmd*
|
struct cmd *backcmd(struct cmd *subcmd) {
|
||||||
backcmd(struct cmd *subcmd)
|
|
||||||
{
|
|
||||||
struct backcmd *cmd;
|
struct backcmd *cmd;
|
||||||
|
|
||||||
cmd = malloc(sizeof(*cmd));
|
cmd = malloc(sizeof(*cmd));
|
||||||
@ -263,9 +250,7 @@ backcmd(struct cmd *subcmd)
|
|||||||
char whitespace[] = " \t\r\n\v";
|
char whitespace[] = " \t\r\n\v";
|
||||||
char symbols[] = "<|>&;()";
|
char symbols[] = "<|>&;()";
|
||||||
|
|
||||||
int
|
int gettoken(char **ps, char *es, char **q, char **eq) {
|
||||||
gettoken(char **ps, char *es, char **q, char **eq)
|
|
||||||
{
|
|
||||||
char *s;
|
char *s;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
@ -308,9 +293,7 @@ gettoken(char **ps, char *es, char **q, char **eq)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int peek(char **ps, char *es, char *toks) {
|
||||||
peek(char **ps, char *es, char *toks)
|
|
||||||
{
|
|
||||||
char *s;
|
char *s;
|
||||||
|
|
||||||
s = *ps;
|
s = *ps;
|
||||||
@ -325,9 +308,7 @@ struct cmd *parsepipe(char**, char*);
|
|||||||
struct cmd *parseexec(char **, char *);
|
struct cmd *parseexec(char **, char *);
|
||||||
struct cmd *nulterminate(struct cmd *);
|
struct cmd *nulterminate(struct cmd *);
|
||||||
|
|
||||||
struct cmd*
|
struct cmd *parsecmd(char *s) {
|
||||||
parsecmd(char *s)
|
|
||||||
{
|
|
||||||
char *es;
|
char *es;
|
||||||
struct cmd *cmd;
|
struct cmd *cmd;
|
||||||
|
|
||||||
@ -342,9 +323,7 @@ parsecmd(char *s)
|
|||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct cmd*
|
struct cmd *parseline(char **ps, char *es) {
|
||||||
parseline(char **ps, char *es)
|
|
||||||
{
|
|
||||||
struct cmd *cmd;
|
struct cmd *cmd;
|
||||||
|
|
||||||
cmd = parsepipe(ps, es);
|
cmd = parsepipe(ps, es);
|
||||||
@ -359,9 +338,7 @@ parseline(char **ps, char *es)
|
|||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct cmd*
|
struct cmd *parsepipe(char **ps, char *es) {
|
||||||
parsepipe(char **ps, char *es)
|
|
||||||
{
|
|
||||||
struct cmd *cmd;
|
struct cmd *cmd;
|
||||||
|
|
||||||
cmd = parseexec(ps, es);
|
cmd = parseexec(ps, es);
|
||||||
@ -372,9 +349,7 @@ parsepipe(char **ps, char *es)
|
|||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct cmd*
|
struct cmd *parseredirs(struct cmd *cmd, char **ps, char *es) {
|
||||||
parseredirs(struct cmd *cmd, char **ps, char *es)
|
|
||||||
{
|
|
||||||
int tok;
|
int tok;
|
||||||
char *q, *eq;
|
char *q, *eq;
|
||||||
|
|
||||||
@ -397,9 +372,7 @@ parseredirs(struct cmd *cmd, char **ps, char *es)
|
|||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct cmd*
|
struct cmd *parseblock(char **ps, char *es) {
|
||||||
parseblock(char **ps, char *es)
|
|
||||||
{
|
|
||||||
struct cmd *cmd;
|
struct cmd *cmd;
|
||||||
|
|
||||||
if (!peek(ps, es, "("))
|
if (!peek(ps, es, "("))
|
||||||
@ -413,9 +386,7 @@ parseblock(char **ps, char *es)
|
|||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct cmd*
|
struct cmd *parseexec(char **ps, char *es) {
|
||||||
parseexec(char **ps, char *es)
|
|
||||||
{
|
|
||||||
char *q, *eq;
|
char *q, *eq;
|
||||||
int tok, argc;
|
int tok, argc;
|
||||||
struct execcmd *cmd;
|
struct execcmd *cmd;
|
||||||
@ -447,9 +418,7 @@ parseexec(char **ps, char *es)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NUL-terminate all the counted strings.
|
// NUL-terminate all the counted strings.
|
||||||
struct cmd*
|
struct cmd *nulterminate(struct cmd *cmd) {
|
||||||
nulterminate(struct cmd *cmd)
|
|
||||||
{
|
|
||||||
int i;
|
int i;
|
||||||
struct backcmd *bcmd;
|
struct backcmd *bcmd;
|
||||||
struct execcmd *ecmd;
|
struct execcmd *ecmd;
|
||||||
|
|||||||
15
user/uptime.c
Normal file
15
user/uptime.c
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user