/branches/dynload/uspace/app/bdsh/cmds/modules/bdd/bdd.c |
---|
0,0 → 1,154 |
/* |
* Copyright (c) 2009 Jiri Svoboda |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#include <stdio.h> |
#include <stdlib.h> |
#include <string.h> |
#include "config.h" |
#include "util.h" |
#include "errors.h" |
#include "entry.h" |
#include "bdd.h" |
#include "cmds.h" |
#include <libblock.h> |
#include <devmap.h> |
#include <errno.h> |
#define BLOCK_SIZE 512 |
#define BPR 16 |
static const char *cmdname = "bdd"; |
/* Dispays help for bdd in various levels */ |
void help_cmd_bdd(unsigned int level) |
{ |
static char helpfmt[] = |
"Usage: %s <device> [<block_number> [<bytes>]]\n"; |
if (level == HELP_SHORT) { |
printf("'%s' dump block device contents.\n", cmdname); |
} else { |
help_cmd_bdd(HELP_SHORT); |
printf(helpfmt, cmdname); |
} |
return; |
} |
/* Main entry point for bdd, accepts an array of arguments */ |
int cmd_bdd(char **argv) |
{ |
unsigned int argc; |
unsigned int i, j; |
dev_handle_t handle; |
block_t *block; |
uint8_t *blk; |
size_t size, bytes, rows; |
int rc; |
bn_t boff; |
uint8_t b; |
/* Count the arguments */ |
for (argc = 0; argv[argc] != NULL; argc ++); |
if (argc < 2 || argc > 4) { |
printf("%s - incorrect number of arguments.\n", cmdname); |
return CMD_FAILURE; |
} |
if (argc >= 3) |
boff = strtol(argv[2], NULL, 0); |
else |
boff = 0; |
if (argc >= 4) |
size = strtol(argv[3], NULL, 0); |
else |
size = 256; |
rc = devmap_device_get_handle(argv[1], &handle, 0); |
if (rc != EOK) { |
printf("Error: could not resolve device `%s'.\n", argv[1]); |
return CMD_FAILURE; |
} |
rc = block_init(handle, BLOCK_SIZE); |
if (rc != EOK) { |
printf("Error: could not init libblock.\n"); |
return CMD_FAILURE; |
} |
rc = block_cache_init(handle, BLOCK_SIZE, 2, CACHE_MODE_WB); |
if (rc != EOK) { |
printf("Error: could not init block cache.\n"); |
return CMD_FAILURE; |
} |
while (size > 0) { |
block = block_get(handle, boff, 0); |
blk = (uint8_t *) block->data; |
bytes = (size < BLOCK_SIZE) ? size : BLOCK_SIZE; |
rows = (bytes + BPR - 1) / BPR; |
for (j = 0; j < rows; j++) { |
for (i = 0; i < BPR; i++) { |
if (j * BPR + i < bytes) |
printf("%02x ", blk[j * BPR + i]); |
else |
printf(" "); |
} |
putchar('\t'); |
for (i = 0; i < BPR; i++) { |
if (j * BPR + i < bytes) { |
b = blk[j * BPR + i]; |
if (b >= 32 && b < 127) |
putchar(b); |
else |
putchar(' '); |
} else { |
putchar(' '); |
} |
} |
putchar('\n'); |
} |
block_put(block); |
if (size > rows * BPR) |
size -= rows * BPR; |
else |
size = 0; |
boff += rows * BPR; |
} |
block_fini(handle); |
return CMD_SUCCESS; |
} |
/branches/dynload/uspace/app/bdsh/cmds/modules/bdd/bdd.h |
---|
0,0 → 1,8 |
#ifndef BDD_H |
#define BDD_H |
/* Prototypes for the bdd command, excluding entry points */ |
#endif /* BDD_H */ |
/branches/dynload/uspace/app/bdsh/cmds/modules/bdd/entry.h |
---|
0,0 → 1,9 |
#ifndef BDD_ENTRY_H |
#define BDD_ENTRY_H |
/* Entry points for the bdd command */ |
extern int cmd_bdd(char **); |
extern void help_cmd_bdd(unsigned int); |
#endif /* BDD_ENTRY_H */ |
/branches/dynload/uspace/app/bdsh/cmds/modules/bdd/bdd_def.h |
---|
0,0 → 1,7 |
{ |
"bdd", |
"Dump block device contents", |
&cmd_bdd, |
&help_cmd_bdd, |
}, |
/branches/dynload/uspace/app/bdsh/cmds/modules/kcon/kcon.c |
---|
30,7 → 30,8 |
#include <stdio.h> |
#include <stdlib.h> |
#include <console.h> |
#include <io/console.h> |
#include <vfs/vfs.h> |
#include "config.h" |
#include "util.h" |
#include "errors.h" |
65,7 → 66,7 |
return CMD_FAILURE; |
} |
console_kcon_enable(); |
console_kcon_enable(fphone(stdout)); |
return CMD_SUCCESS; |
} |
/branches/dynload/uspace/app/bdsh/cmds/modules/pwd/pwd.c |
---|
30,6 → 30,7 |
#include <stdio.h> |
#include <stdlib.h> |
#include <mem.h> |
#include "config.h" |
#include "errors.h" |
/branches/dynload/uspace/app/bdsh/cmds/modules/modules.h |
---|
20,6 → 20,7 |
#include "help/entry.h" |
#include "mkdir/entry.h" |
#include "rm/entry.h" |
#include "bdd/entry.h" |
#include "cat/entry.h" |
#include "touch/entry.h" |
#include "ls/entry.h" |
38,6 → 39,7 |
#include "help/help_def.h" |
#include "mkdir/mkdir_def.h" |
#include "rm/rm_def.h" |
#include "bdd/bdd_def.h" |
#include "cat/cat_def.h" |
#include "touch/touch_def.h" |
#include "ls/ls_def.h" |
/branches/dynload/uspace/app/bdsh/cmds/modules/cp/cp.c |
---|
172,7 → 172,8 |
int cmd_cp(char **argv) |
{ |
unsigned int argc, buffer = 0, verbose = 0; |
unsigned int argc, verbose = 0; |
int buffer = 0; |
int c, opt_ind; |
int64_t ret; |
/branches/dynload/uspace/app/bdsh/cmds/modules/mkdir/mkdir.c |
---|
37,6 → 37,7 |
#include <sys/stat.h> |
#include <getopt.h> |
#include <stdarg.h> |
#include <string.h> |
#include "config.h" |
#include "errors.h" |
/branches/dynload/uspace/app/bdsh/cmds/modules/ls/ls.c |
---|
50,48 → 50,9 |
static char *cmdname = "ls"; |
static inline off_t flen(const char *f) |
{ |
int fd; |
off_t size; |
fd = open(f, O_RDONLY); |
if (fd == -1) |
return 0; |
size = lseek(fd, 0, SEEK_END); |
close(fd); |
if (size < 0) |
size = 0; |
return size; |
} |
static unsigned int ls_scope(const char *path) |
{ |
int fd; |
DIR *dirp; |
dirp = opendir(path); |
if (dirp) { |
closedir(dirp); |
return LS_DIR; |
} |
fd = open(path, O_RDONLY); |
if (fd > 0) { |
close(fd); |
return LS_FILE; |
} |
return LS_BOGUS; |
} |
static void ls_scan_dir(const char *d, DIR *dirp) |
{ |
struct dirent *dp; |
unsigned int scope; |
char *buff; |
if (! dirp) |
108,20 → 69,7 |
/* Don't worry if inserting a double slash, this will be fixed by |
* absolutize() later with subsequent calls to open() or readdir() */ |
snprintf(buff, PATH_MAX - 1, "%s/%s", d, dp->d_name); |
scope = ls_scope(buff); |
switch (scope) { |
case LS_DIR: |
ls_print_dir(dp->d_name); |
break; |
case LS_FILE: |
ls_print_file(dp->d_name, buff); |
break; |
case LS_BOGUS: |
/* Odd chance it was deleted from the time readdir() found |
* it and the time that it was scoped */ |
printf("ls: skipping bogus node %s\n", dp->d_name); |
break; |
} |
ls_print(dp->d_name, buff); |
} |
free(buff); |
129,7 → 77,7 |
return; |
} |
/* ls_print_* currently does nothing more than print the entry. |
/* ls_print currently does nothing more than print the entry. |
* in the future, we will likely pass the absolute path, and |
* some sort of ls_options structure that controls how each |
* entry is printed and what is printed about it. |
136,17 → 84,24 |
* |
* Now we just print basic DOS style lists */ |
static void ls_print_dir(const char *d) |
static void ls_print(const char *name, const char *pathname) |
{ |
printf("%-40s\t<dir>\n", d); |
struct stat s; |
int rc; |
return; |
} |
rc = stat(pathname, &s); |
if (rc != 0) { |
/* Odd chance it was deleted from the time readdir() found it */ |
printf("ls: skipping bogus node %s\n", pathname); |
printf("rc=%d\n", rc); |
return; |
} |
if (s.is_file) |
printf("%-40s\t%llu\n", name, (long long) s.size); |
else |
printf("%-40s\n", name); |
static void ls_print_file(const char *name, const char *pathname) |
{ |
printf("%-40s\t%llu\n", name, (long long) flen(pathname)); |
return; |
} |
166,7 → 121,7 |
int cmd_ls(char **argv) |
{ |
unsigned int argc; |
unsigned int scope; |
struct stat s; |
char *buff; |
DIR *dirp; |
184,19 → 139,17 |
else |
str_cpy(buff, PATH_MAX, argv[1]); |
scope = ls_scope(buff); |
switch (scope) { |
case LS_BOGUS: |
if (stat(buff, &s)) { |
cli_error(CL_ENOENT, buff); |
free(buff); |
return CMD_FAILURE; |
case LS_FILE: |
ls_print_file(buff, buff); |
break; |
case LS_DIR: |
} |
if (s.is_file) { |
ls_print(buff, buff); |
} else { |
dirp = opendir(buff); |
if (! dirp) { |
if (!dirp) { |
/* May have been deleted between scoping it and opening it */ |
cli_error(CL_EFAIL, "Could not stat %s", buff); |
free(buff); |
204,7 → 157,6 |
} |
ls_scan_dir(buff, dirp); |
closedir(dirp); |
break; |
} |
free(buff); |
/branches/dynload/uspace/app/bdsh/cmds/modules/ls/ls.h |
---|
6,11 → 6,8 |
#define LS_FILE 1 |
#define LS_DIR 2 |
static unsigned int ls_scope(const char *); |
static void ls_scan_dir(const char *, DIR *); |
static void ls_print_dir(const char *); |
static void ls_print_file(const char *, const char *); |
static void ls_print(const char *, const char *); |
#endif /* LS_H */ |
/branches/dynload/uspace/app/bdsh/cmds/modules/rm/rm.c |
---|
34,6 → 34,8 |
#include <fcntl.h> |
#include <dirent.h> |
#include <getopt.h> |
#include <mem.h> |
#include <string.h> |
#include "config.h" |
#include "errors.h" |
207,7 → 209,7 |
} |
} |
if (optind == argc) { |
if ((unsigned) optind == argc) { |
cli_error(CL_EFAIL, |
"%s: insufficient arguments. Try %s --help", cmdname, cmdname); |
rm_end(&rm); |