Simple embedded shell with runtime pluggable commands.
Implements a simple unix-like shell for embedded systems with a pluggable command architecture.
Diff: SimpleShell.cpp
- Revision:
- 32:fc09f0eb1e8a
- Parent:
- 31:27e8130a0d8f
- Child:
- 33:84dc443909a0
diff -r 27e8130a0d8f -r fc09f0eb1e8a SimpleShell.cpp --- a/SimpleShell.cpp Fri Dec 28 17:10:31 2018 +0000 +++ b/SimpleShell.cpp Fri Dec 28 19:54:48 2018 +0000 @@ -187,13 +187,47 @@ return; } +#include "fnmatch.h" + +list<char*> *scandir(char *pattern, char *path) +{ + DIR *d; + list<char*> *listp; + + struct dirent *p; + if ((d = opendir(path)) != NULL) { + listp = new list<char*>; + while ((p = readdir(d)) != NULL) { + if (fnmatch(pattern, p->d_name, FNM_PATHNAME|FNM_CASEFOLD) != FNM_NOMATCH) { + char *s = new char[sizeof(p->d_name)]; + strcpy(s, p->d_name); + listp->push_back(s); + } + } + closedir(d); + } + return listp; +} + void SimpleShell::rm(int argc, char **argv) { if (argc >= 2) { for (int i=1; i < argc; i++) { - if (remove(canon(argv[i]))) { - printf("%s: cannot remove\n", argv[i]); + if (haswildcard(argv[i])) { + list<char*> *fl = scandir(argv[i], _cwd); + char *s; + while (!fl->empty()) { + s = fl->front(); + printf("<%s>\n", s); + fl->pop_front(); + free(s); + } + delete(fl); + } else { + if (remove(canon(argv[i]))) { + printf("%s: cannot remove\n", argv[i]); + } } } } else {