2013-07-24 13:04:56 +00:00
|
|
|
/*
|
2014-06-30 09:10:35 +00:00
|
|
|
** Simple ("tiny") shell to test filename and directory functions.
|
2016-06-13 18:40:01 +00:00
|
|
|
** Copyright (c) 2013,2016 Christian Groessler, chris@groessler.org
|
2014-06-30 09:10:35 +00:00
|
|
|
*/
|
2013-07-24 13:04:56 +00:00
|
|
|
|
2016-06-13 18:40:01 +00:00
|
|
|
#define VERSION_ASC "0.91"
|
2013-07-24 13:04:56 +00:00
|
|
|
|
|
|
|
#ifdef __ATARI__
|
|
|
|
#define UPPERCASE /* define (e.g. for Atari) to convert filenames etc. to upper case */
|
2013-09-12 10:32:59 +00:00
|
|
|
#define HAVE_SUBDIRS
|
|
|
|
#endif
|
2013-09-11 22:04:51 +00:00
|
|
|
|
2013-09-12 11:04:55 +00:00
|
|
|
#ifdef __APPLE2__
|
|
|
|
#define HAVE_SUBDIRS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __CC65__
|
|
|
|
#define CHECK_SP
|
|
|
|
#endif
|
|
|
|
|
2016-06-13 18:40:01 +00:00
|
|
|
#define KEYB_BUFSZ 127
|
2013-09-11 22:04:51 +00:00
|
|
|
#define PROMPT ">>> "
|
|
|
|
|
2013-07-24 13:04:56 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifndef __CC65__
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/param.h>
|
2013-09-12 10:32:59 +00:00
|
|
|
#define HAVE_SUBDIRS
|
2013-07-24 13:04:56 +00:00
|
|
|
#else
|
|
|
|
#define MAXPATHLEN 64
|
|
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2013-09-11 22:04:51 +00:00
|
|
|
#ifdef CHECK_SP
|
|
|
|
extern unsigned int getsp(void); /* comes from getsp.s */
|
|
|
|
#endif
|
|
|
|
|
2013-07-24 13:04:56 +00:00
|
|
|
#define CMD_NOTHING 0
|
|
|
|
#define CMD_INVALID 1
|
|
|
|
#define CMD_HELP 2
|
|
|
|
#define CMD_QUIT 3
|
|
|
|
#define CMD_LS 4
|
|
|
|
#define CMD_MKDIR 5
|
|
|
|
#define CMD_RMDIR 6
|
|
|
|
#define CMD_CHDIR 7
|
|
|
|
#define CMD_RM 8
|
|
|
|
#define CMD_RENAME 9
|
|
|
|
#define CMD_COPY 10
|
|
|
|
#define CMD_PWD 11
|
2013-09-11 22:04:51 +00:00
|
|
|
#define CMD_CLS 12
|
|
|
|
#define CMD_VERBOSE 13
|
2016-06-13 18:40:01 +00:00
|
|
|
#define CMD_EXEC 14
|
2013-07-24 13:04:56 +00:00
|
|
|
|
2013-09-11 22:04:51 +00:00
|
|
|
static unsigned char verbose;
|
2013-07-24 13:04:56 +00:00
|
|
|
static unsigned char terminate;
|
|
|
|
static unsigned char cmd;
|
2021-12-25 08:57:18 +00:00
|
|
|
static char *cmd_asc, *arg1, *arg2, *arg3, *args; /* 'args': everything after command */
|
|
|
|
static char keyb_buf[KEYB_BUFSZ + 1];
|
|
|
|
static char keyb_buf2[KEYB_BUFSZ + 1];
|
2013-07-24 13:04:56 +00:00
|
|
|
static size_t cpbuf_sz = 4096;
|
|
|
|
|
|
|
|
struct cmd_table {
|
2021-12-25 08:57:18 +00:00
|
|
|
char *name;
|
2013-07-24 13:04:56 +00:00
|
|
|
unsigned char code;
|
|
|
|
} cmd_table[] = {
|
|
|
|
{ "help", CMD_HELP },
|
|
|
|
{ "quit", CMD_QUIT },
|
|
|
|
{ "q", CMD_QUIT },
|
|
|
|
{ "exit", CMD_QUIT },
|
|
|
|
{ "ls", CMD_LS },
|
|
|
|
{ "dir", CMD_LS },
|
|
|
|
{ "md", CMD_MKDIR },
|
2013-09-12 10:32:59 +00:00
|
|
|
#ifdef HAVE_SUBDIRS
|
2013-07-24 13:04:56 +00:00
|
|
|
{ "mkdir", CMD_MKDIR },
|
|
|
|
{ "rd", CMD_RMDIR },
|
|
|
|
{ "rmdir", CMD_RMDIR },
|
|
|
|
{ "cd", CMD_CHDIR },
|
|
|
|
{ "chdir", CMD_CHDIR },
|
2013-09-12 10:32:59 +00:00
|
|
|
#endif
|
2013-07-24 13:04:56 +00:00
|
|
|
{ "rm", CMD_RM },
|
|
|
|
{ "del", CMD_RM },
|
|
|
|
{ "cp", CMD_COPY },
|
|
|
|
{ "copy", CMD_COPY },
|
|
|
|
{ "mv", CMD_RENAME },
|
|
|
|
{ "ren", CMD_RENAME },
|
|
|
|
{ "pwd", CMD_PWD },
|
2016-06-13 18:40:01 +00:00
|
|
|
{ "exec", CMD_EXEC },
|
2013-09-12 10:32:59 +00:00
|
|
|
#ifdef __ATARI__
|
2013-09-11 22:04:51 +00:00
|
|
|
{ "cls", CMD_CLS },
|
|
|
|
#endif
|
|
|
|
{ "verbose", CMD_VERBOSE },
|
2013-07-24 13:04:56 +00:00
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void banner(void)
|
|
|
|
{
|
|
|
|
puts("\"tiny\" command line shell, v" VERSION_ASC);
|
|
|
|
puts("written by chris@groessler.org");
|
|
|
|
puts("type 'help' for help\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_command(void)
|
|
|
|
{
|
|
|
|
unsigned char i = 0;
|
|
|
|
|
2013-09-11 22:04:51 +00:00
|
|
|
#ifdef CHECK_SP
|
|
|
|
static char firstcall = 1;
|
|
|
|
static unsigned int good_sp;
|
|
|
|
unsigned int sp;
|
|
|
|
if (firstcall)
|
|
|
|
sp = good_sp = getsp();
|
|
|
|
else
|
|
|
|
sp = getsp();
|
|
|
|
|
|
|
|
if (sp != good_sp) {
|
|
|
|
printf("SP: 0x%04X ***MISMATCH*** 0x%04X\n", sp, good_sp);
|
|
|
|
}
|
|
|
|
else if (verbose)
|
|
|
|
printf("SP: 0x%04X\n", sp);
|
|
|
|
#endif
|
|
|
|
|
2013-07-24 13:04:56 +00:00
|
|
|
arg1 = arg2 = arg3 = NULL;
|
|
|
|
|
|
|
|
/* issue prompt */
|
|
|
|
printf(PROMPT);
|
|
|
|
|
|
|
|
/* get input from the user */
|
|
|
|
if (! fgets(keyb_buf, KEYB_BUFSZ, stdin)) {
|
|
|
|
puts("");
|
|
|
|
cmd = CMD_QUIT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-13 18:40:01 +00:00
|
|
|
/* put everything after first string into 'args' */
|
|
|
|
|
|
|
|
strcpy(keyb_buf2, keyb_buf); /* use a backup copy for 'args' */
|
|
|
|
|
|
|
|
/* skip over the first non-whitespace item */
|
|
|
|
cmd_asc = strtok(keyb_buf2, " \t\n");
|
|
|
|
if (cmd_asc)
|
|
|
|
args = strtok(NULL, ""); /* get everything */
|
|
|
|
else
|
|
|
|
*args = 0; /* no arguments */
|
|
|
|
|
2013-07-24 13:04:56 +00:00
|
|
|
/* split input into cmd, arg1, arg2, arg3 */
|
|
|
|
|
|
|
|
/* get and parse command */
|
|
|
|
cmd_asc = strtok(keyb_buf, " \t\n");
|
|
|
|
if (! cmd_asc) {
|
|
|
|
cmd = CMD_NOTHING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cmd = CMD_INVALID;
|
|
|
|
while (cmd_table[i].name) {
|
|
|
|
if (! strcmp(cmd_table[i].name, cmd_asc)) {
|
|
|
|
cmd = cmd_table[i].code;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get arguments */
|
|
|
|
arg1 = strtok(NULL, " \t\n");
|
|
|
|
if (! arg1)
|
|
|
|
return;
|
|
|
|
arg2 = strtok(NULL, " \t\n");
|
|
|
|
if (! arg2)
|
|
|
|
return;
|
|
|
|
arg3 = strtok(NULL, " \t\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_help(void)
|
|
|
|
{
|
|
|
|
puts("quit, exit - exit shell");
|
|
|
|
puts("ls, dir - display current directory");
|
|
|
|
puts(" and drive contents");
|
|
|
|
puts("rm, del - delete file");
|
|
|
|
puts("cp, copy - copy file");
|
|
|
|
puts("mv, ren - rename file");
|
|
|
|
puts("cd, chdir - change directory or drive");
|
|
|
|
puts("md, mkdir - make directory or drive");
|
|
|
|
puts("rd, rmdir - remove directory or drive");
|
2016-06-13 18:40:01 +00:00
|
|
|
puts("exec - run program");
|
2013-09-12 10:32:59 +00:00
|
|
|
#ifdef __ATARI__
|
2013-09-11 22:04:51 +00:00
|
|
|
puts("cls - clear screen");
|
|
|
|
#endif
|
|
|
|
puts("verbose - set verbosity level");
|
2013-07-24 13:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_ls(void)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
2021-12-25 08:57:18 +00:00
|
|
|
char *arg;
|
2013-07-24 13:04:56 +00:00
|
|
|
struct dirent *dirent;
|
|
|
|
#ifdef __ATARI__
|
|
|
|
char need_free = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (arg2) {
|
|
|
|
puts("usage: ls [dir]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print directory listing */
|
|
|
|
if (arg1) {
|
|
|
|
#ifdef UPPERCASE
|
|
|
|
strupr(arg1);
|
|
|
|
#endif
|
|
|
|
#ifdef __ATARI__
|
|
|
|
/* not sure if this shouldn't be done by the runtime lib */
|
|
|
|
if (*(arg1 + strlen(arg1) - 1) == ':' || *(arg1 + strlen(arg1) - 1) == '>') {
|
|
|
|
arg = malloc(strlen(arg1) + 4);
|
|
|
|
if (! arg) {
|
|
|
|
printf("malloc failed: %s", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
need_free = 1;
|
|
|
|
memcpy(arg, arg1, strlen(arg1) + 1);
|
|
|
|
strcat(arg, "*.*");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
arg = arg1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
arg = ".";
|
|
|
|
|
2013-09-11 22:04:51 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("Buffer addr: %p\n", arg);
|
2013-07-24 13:04:56 +00:00
|
|
|
dir = opendir(arg);
|
|
|
|
#ifdef __ATARI__
|
|
|
|
if (need_free) free(arg);
|
|
|
|
#endif
|
|
|
|
if (! dir) {
|
|
|
|
puts("opendir failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (dirent = readdir(dir))
|
|
|
|
puts(dirent->d_name);
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_rm(void)
|
|
|
|
{
|
|
|
|
if (!arg1 || arg2) {
|
|
|
|
puts("usage: rm <file>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UPPERCASE
|
|
|
|
strupr(arg1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (unlink(arg1))
|
|
|
|
printf("remove failed: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2013-09-12 10:32:59 +00:00
|
|
|
#ifdef HAVE_SUBDIRS
|
|
|
|
|
2013-07-24 13:04:56 +00:00
|
|
|
static void cmd_mkdir(void)
|
|
|
|
{
|
|
|
|
if (!arg1 || arg2) {
|
|
|
|
puts("usage: mkdir <dir>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UPPERCASE
|
|
|
|
strupr(arg1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mkdir(arg1, 0777))
|
|
|
|
printf("mkdir failed: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_rmdir(void)
|
|
|
|
{
|
|
|
|
if (!arg1 || arg2) {
|
|
|
|
puts("usage: rmdir <dir>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UPPERCASE
|
|
|
|
strupr(arg1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (rmdir(arg1))
|
|
|
|
printf("rmdir failed: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_chdir(void)
|
|
|
|
{
|
|
|
|
if (!arg1 || arg2) {
|
|
|
|
puts("usage: cddir <dir>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UPPERCASE
|
|
|
|
strupr(arg1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (chdir(arg1))
|
|
|
|
printf("chdir failed: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_pwd(void)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
if (arg1) {
|
|
|
|
puts("usage: pwd");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = malloc(MAXPATHLEN);
|
|
|
|
if (! buf) {
|
|
|
|
printf("malloc %u bytes failed: %s\n", MAXPATHLEN, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2013-09-11 22:04:51 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("Buffer addr: %p\n", buf);
|
2013-07-24 13:04:56 +00:00
|
|
|
if (!getcwd(buf, MAXPATHLEN)) {
|
|
|
|
printf("getcwd failed: %s\n", strerror(errno));
|
|
|
|
free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
puts(buf);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2013-09-12 10:32:59 +00:00
|
|
|
#endif /* #ifdef HAVE_SUBDIRS */
|
|
|
|
|
2013-07-24 13:04:56 +00:00
|
|
|
static void cmd_rename(void)
|
|
|
|
{
|
|
|
|
if (!arg2 || arg3) {
|
|
|
|
puts("usage: mv <oldname> <newname>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UPPERCASE
|
|
|
|
strupr(arg1);
|
|
|
|
strupr(arg2);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (rename(arg1, arg2))
|
|
|
|
printf("rename failed: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2016-06-13 18:40:01 +00:00
|
|
|
static void cmd_exec(void)
|
|
|
|
{
|
2021-12-25 08:57:18 +00:00
|
|
|
char *progname, *arguments;
|
2016-06-13 18:40:01 +00:00
|
|
|
|
|
|
|
progname = strtok(args, " \t\n");
|
|
|
|
if (! progname) {
|
|
|
|
puts("usage: exec <progname> [arguments]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
arguments = strtok(NULL, "");
|
|
|
|
|
|
|
|
/*printf("exec: %s %s\n", progname, arguments ? arguments : "");*/
|
2016-06-14 18:44:57 +00:00
|
|
|
(void)exec(progname, arguments);
|
2016-06-13 18:40:01 +00:00
|
|
|
printf("exec error: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2013-07-24 13:04:56 +00:00
|
|
|
static void cmd_copy(void)
|
|
|
|
{
|
|
|
|
int srcfd = -1, dstfd = -1;
|
2021-12-25 08:57:18 +00:00
|
|
|
char *buf;
|
2013-07-24 13:04:56 +00:00
|
|
|
int readsz, writesz;
|
|
|
|
|
|
|
|
if (!arg2 || arg3) {
|
|
|
|
puts("usage: cp <src> <dest>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UPPERCASE
|
|
|
|
strupr(arg1);
|
|
|
|
strupr(arg2);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
buf = malloc(cpbuf_sz);
|
|
|
|
if (! buf) {
|
|
|
|
printf("malloc %u bytes failed: %s\n", cpbuf_sz, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2013-09-11 22:04:51 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("Buffer addr: %p\n", buf);
|
2013-07-24 13:04:56 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (srcfd == -1) {
|
|
|
|
srcfd = open(arg1, O_RDONLY);
|
|
|
|
if (srcfd < 0) {
|
|
|
|
printf("open(%s) failed: %s\n", arg1, strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
readsz = read(srcfd, buf, cpbuf_sz);
|
|
|
|
if (readsz < 0) {
|
|
|
|
printf("read error: %s\n", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (! readsz)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (dstfd == -1) {
|
|
|
|
dstfd = open(arg2, O_WRONLY | O_CREAT | O_TRUNC, 0777);
|
|
|
|
if (dstfd < 0) {
|
|
|
|
printf("open(%s) failed: %s\n", arg2, strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writesz = write(dstfd, buf, readsz);
|
|
|
|
if (writesz < 0 || writesz != readsz) {
|
|
|
|
printf("write error: %s\n", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (readsz != cpbuf_sz)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
if (srcfd >= 0) close(srcfd);
|
|
|
|
if (dstfd >= 0) close(dstfd);
|
|
|
|
}
|
|
|
|
|
2013-09-12 10:32:59 +00:00
|
|
|
#ifdef __ATARI__
|
2013-09-11 22:04:51 +00:00
|
|
|
static void cmd_cls(void)
|
|
|
|
{
|
|
|
|
printf("\f");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void cmd_verbose(void)
|
|
|
|
{
|
|
|
|
unsigned long verb;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
if (!arg1 || arg2) {
|
|
|
|
puts("usage: verbose <level>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
verb = strtoul(arg1, &endptr, 10);
|
|
|
|
if (verb > 255 || *endptr) {
|
2013-09-11 22:16:09 +00:00
|
|
|
puts("invalid verbosity level");
|
2013-09-11 22:04:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
verbose = verb;
|
|
|
|
printf("verbosity level set to %d\n", verbose);
|
|
|
|
}
|
|
|
|
|
2013-07-24 13:04:56 +00:00
|
|
|
static void run_command(void)
|
|
|
|
{
|
|
|
|
switch (cmd) {
|
|
|
|
default: puts("internal error"); return;
|
|
|
|
case CMD_NOTHING: return;
|
|
|
|
case CMD_INVALID: puts("invalid command"); return;
|
|
|
|
case CMD_HELP: cmd_help(); return;
|
|
|
|
case CMD_QUIT: terminate = 1; return;
|
|
|
|
case CMD_LS: cmd_ls(); return;
|
|
|
|
case CMD_RM: cmd_rm(); return;
|
2013-09-12 10:32:59 +00:00
|
|
|
#ifdef HAVE_SUBDIRS
|
2013-07-24 13:04:56 +00:00
|
|
|
case CMD_CHDIR: cmd_chdir(); return;
|
|
|
|
case CMD_MKDIR: cmd_mkdir(); return;
|
|
|
|
case CMD_RMDIR: cmd_rmdir(); return;
|
|
|
|
case CMD_PWD: cmd_pwd(); return;
|
2013-09-12 10:32:59 +00:00
|
|
|
#endif
|
2016-06-13 18:40:01 +00:00
|
|
|
case CMD_EXEC: cmd_exec(); return;
|
2013-07-24 13:04:56 +00:00
|
|
|
case CMD_RENAME: cmd_rename(); return;
|
|
|
|
case CMD_COPY: cmd_copy(); return;
|
2013-09-12 10:32:59 +00:00
|
|
|
#ifdef __ATARI__
|
2013-09-11 22:04:51 +00:00
|
|
|
case CMD_CLS: cmd_cls(); return;
|
|
|
|
#endif
|
|
|
|
case CMD_VERBOSE: cmd_verbose(); return;
|
2013-07-24 13:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
banner();
|
|
|
|
|
|
|
|
while (! terminate) {
|
|
|
|
get_command();
|
|
|
|
run_command();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Local Variables: */
|
|
|
|
/* c-file-style: "cpg" */
|
|
|
|
/* c-basic-offset: 4 */
|
|
|
|
/* End: */
|