diff options
Diffstat (limited to 'toys')
-rw-r--r-- | toys/df.c | 125 | ||||
-rw-r--r-- | toys/toysh.c | 214 |
2 files changed, 339 insertions, 0 deletions
diff --git a/toys/df.c b/toys/df.c new file mode 100644 index 00000000..cbebd157 --- /dev/null +++ b/toys/df.c @@ -0,0 +1,125 @@ +/* vi: set sw=4 ts=4: */ +/* + * df.c - report free disk space. + * + * Implemented roughly according to SUSv3: + * http://www.opengroup.org/onlinepubs/009695399/utilities/df.html + * + * usage: df [-k] [-P|-t] [file...] + */ + +#include "toys.h" + +static void show_mt(struct mtab_list *mt) +{ + int len; + long size, used, avail, percent; + uint64_t block; + + // Return if it wasn't found (should never happen, but with /etc/mtab...) + if (!mt) return; + + // If we have -t, skip other filesystem types + if (toy.df.fstype) { + struct string_list *sl; + + for (sl = toy.df.fstype; sl; sl = sl->next) + if (!strcmp(mt->type, sl->str)) break; + if (!sl) return; + } + + // If we don't have -a, skip synthetic filesystems + if (!(toys.optflags & 1) && !mt->statvfs.f_blocks) return; + + // Figure out how much total/used/free space this filesystem has, + // forcing 64-bit math because filesystems are big now. + block = mt->statvfs.f_bsize ? : 1; + + size = (long)((block * mt->statvfs.f_blocks) / toy.df.units); + used = (long)((block * (mt->statvfs.f_blocks-mt->statvfs.f_bfree)) + / toy.df.units); + avail = (long)((block + * (getuid() ? mt->statvfs.f_bavail : mt->statvfs.f_bfree)) + / toy.df.units); + percent = 100-(long)((100*(uint64_t)avail)/size); + + // Figure out appropriate spacing + len = 25 - strlen(mt->device); + if (len < 1) len = 1; + if (CFG_DF_PEDANTIC && (toys.optflags & 8)) { + printf("%s %ld %ld %ld %ld%% %s\n", mt->device, size, used, avail, + percent, mt->dir); + } else { + printf("%s% *ld % 10ld % 9ld % 3ld%% %s\n",mt->device, len, + size, used, avail, percent, mt->dir); + } +} + +int df_main(void) +{ + struct mtab_list *mt, *mt2, *mtlist; + char **argv; + + // get_optflags("Pkt:a",&(toy.df.fstype)); + argv = NULL; + + // Handle -P and -k + toy.df.units = 1024; + if (CFG_DF_PEDANTIC && (toys.optflags & 8)) { + // Units are 512 bytes if you select "pedantic" without "kilobytes". + if ((toys.optflags&3) == 1) toy.df.units = 512; + printf("Filesystem %ld-blocks Used Available Capacity Mounted on\n", + toy.df.units); + } else puts("Filesystem\t1K-blocks\tUsed Available Use% Mounted on"); + + mtlist = getmountlist(1); + + // If we have a list of filesystems on the command line, loop through them. + if (argv) { + char *next; + + for(next = *argv; *next; next++) { + struct stat st; + + // Stat it (complain if we can't). + if(!stat(next, &st)) { + perror_msg("`%s'", next); + toys.exitval = 1; + continue; + } + + // Find and display this filesystem. Use _last_ hit in case of + // -- bind mounts. + mt2 = NULL; + for (mt = mtlist; mt; mt = mt->next) + if (st.st_dev == mt->stat.st_dev) mt2 = mt; + show_mt(mt2); + } + } else { + // Get and loop through mount list. + + for (mt = mtlist; mt; mt = mt->next) { + struct mtab_list *mt2, *mt3; + + if (!mt->stat.st_dev) continue; + + // Filter out overmounts. + mt3 = mt; + for (mt2 = mt->next; mt2; mt2 = mt2->next) { + if (mt->stat.st_dev == mt2->stat.st_dev) { + // For --bind mounts, take last match + if (!strcmp(mt->device, mt2->device)) mt3 = mt2; + // Filter out overmounts + mt2->stat.st_dev = 0; + } + } + show_mt(mt3); + } + } + + if (CFG_TOYS_FREE) { + llist_free(mtlist, NULL); + free(argv); + } + return 0; +} diff --git a/toys/toysh.c b/toys/toysh.c new file mode 100644 index 00000000..7ac430c9 --- /dev/null +++ b/toys/toysh.c @@ -0,0 +1,214 @@ +/* vi: set sw=4 ts=4: + * + * toysh - toybox shell + * + * Copyright 2006 Rob Landley <rob@landley.net> + * + * The spec for this is at: + * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html + * + * Although things like the bash man page are good to read too. + */ + +// Handle embedded NUL bytes in the command line. + +#include "toys.h" + +// A single executable, its arguments, and other information we know about it. +#define TOYSH_FLAG_EXIT 1 +#define TOYSH_FLAG_SUSPEND 2 +#define TOYSH_FLAG_PIPE 4 +#define TOYSH_FLAG_AND 8 +#define TOYSH_FLAG_OR 16 +#define TOYSH_FLAG_AMP 32 +#define TOYSH_FLAG_SEMI 64 +#define TOYSH_FLAG_PAREN 128 + +// What we know about a single process. +struct command { + struct command *next; + int flags; // exit, suspend, && || + int pid; // pid (or exit code) + int argc; + char *argv[0]; +}; + +// A collection of processes piped into/waiting on each other. +struct pipeline { + struct pipeline *next; + int job_id; + struct command *cmd; + char *cmdline; // Unparsed line for display purposes + int cmdlinelen; // How long is cmdline? +}; + +// Parse one word from the command line, appending one or more argv[] entries +// to struct command. Handles environment variable substitution and +// substrings. Returns pointer to next used byte, or NULL if it +// hit an ending token. +static char *parse_word(char *start, struct command **cmd) +{ + char *end; + + // Detect end of line (and truncate line at comment) + if (CFG_TOYSH_PIPES && strchr("><&|(;", *start)) return 0; + + // Grab next word. (Add dequote and envvar logic here) + end = start; + while (*end && !isspace(*end)) end++; + (*cmd)->argv[(*cmd)->argc++] = xstrndup(start, end-start); + + // Allocate more space if there's no room for NULL terminator. + + if (!((*cmd)->argc & 7)) + xrealloc((void **)cmd, + sizeof(struct command) + ((*cmd)->argc+8)*sizeof(char *)); + (*cmd)->argv[(*cmd)->argc] = 0; + return end; +} + +// Parse a line of text into a pipeline. +// Returns a pointer to the next line. + +static char *parse_pipeline(char *cmdline, struct pipeline *line) +{ + struct command **cmd = &(line->cmd); + char *start = line->cmdline = cmdline; + + if (!cmdline) return 0; + + if (CFG_TOYSH_JOBCTL) line->cmdline = cmdline; + + // Parse command into argv[] + for (;;) { + char *end; + + // Skip leading whitespace and detect end of line. + while (isspace(*start)) start++; + if (!*start || *start=='#') { + if (CFG_TOYSH_JOBCTL) line->cmdlinelen = start-cmdline; + return 0; + } + + // Allocate next command structure if necessary + if (!*cmd) *cmd = xzalloc(sizeof(struct command)+8*sizeof(char *)); + + // Parse next argument and add the results to argv[] + end = parse_word(start, cmd); + + // If we hit the end of this command, how did it end? + if (!end) { + if (CFG_TOYSH_PIPES && *start) { + if (*start==';') { + start++; + break; + } + // handle | & < > >> << || && + } + break; + } + start = end; + } + + if (CFG_TOYSH_JOBCTL) line->cmdlinelen = start-cmdline; + + return start; +} + +// Execute the commands in a pipeline +static void run_pipeline(struct pipeline *line) +{ + struct toy_list *tl; + struct command *cmd = line->cmd; + if (!cmd || !cmd->argc) return; + + tl = toy_find(cmd->argv[0]); + // Is this command a builtin that should run in this process? + if (tl && (tl->flags & TOYFLAG_NOFORK)) { + struct toy_list *which = toys.which; + char **argv = toys.argv; + + toy_init(tl, cmd->argv); + cmd->pid = tl->toy_main(); + toy_init(which, argv); + } else { + int status; + + cmd->pid = vfork(); + if (!cmd->pid) xexec(cmd->argv); + else waitpid(cmd->pid, &status, 0); + + if (CFG_TOYSH_FLOWCTL || CFG_TOYSH_PIPES) { + if (WIFEXITED(status)) cmd->pid = WEXITSTATUS(status); + if (WIFSIGNALED(status)) cmd->pid = WTERMSIG(status); + } + } + + return; +} + +// Free the contents of a command structure +static void free_cmd(void *data) +{ + struct command *cmd=(struct command *)data; + + while(cmd->argc) free(cmd->argv[--cmd->argc]); +} + + +// Parse a command line and do what it says to do. +static void handle(char *command) +{ + struct pipeline line; + char *start = command; + + // Loop through commands in this line + + for (;;) { + + // Parse a group of connected commands + + memset(&line,0,sizeof(struct pipeline)); + start = parse_pipeline(start, &line); + if (!line.cmd) break; + + // Run those commands + + run_pipeline(&line); + llist_free(line.cmd, free_cmd); + } +} + +int cd_main(void) +{ + char *dest = toys.argv[1] ? toys.argv[1]: getenv("HOME"); + if (chdir(dest)) error_exit("chdir %s",dest); + return 0; +} + +int exit_main(void) +{ + exit(toys.argv[1] ? atoi(toys.argv[1]) : 0); +} + +int toysh_main(void) +{ + char *command=NULL; + FILE *f; + + // TODO get_optflags(argv, "c:", &command); + + f = toys.argv[1] ? xfopen(toys.argv[1], "r") : NULL; + if (command) handle(command); + else { + unsigned cmdlen=0; + for (;;) { + if (!f) putchar('$'); + if (1 > getline(&command, &cmdlen, f ? : stdin)) break; + handle(command); + } + if (CFG_TOYS_FREE) free(command); + } + + return 1; +} |