packvfs
diff pvfsh/src/main.c @ 2:dc23ab0545a6
- fleshed out some more fucntions with the code for the case where file/dir are real
- added install/uninstall targets to the makefile
- added pvfsh (packvfs shell) test program
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 04 Nov 2013 03:50:55 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/pvfsh/src/main.c Mon Nov 04 03:50:55 2013 +0200 1.3 @@ -0,0 +1,183 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <ctype.h> 1.8 +#include "pvfs.h" 1.9 + 1.10 +char *strip_wspace(char *str); 1.11 +char **tokenize(char *str); 1.12 +void cmdproc(int argc, char **argv); 1.13 + 1.14 +int main(void) 1.15 +{ 1.16 + int i; 1.17 + char buf[512]; 1.18 + 1.19 + printf("welcome to packvfs shell\n"); 1.20 + 1.21 + fputs("> ", stdout); 1.22 + fflush(stdout); 1.23 + 1.24 + while(fgets(buf, sizeof buf, stdin)) { 1.25 + char *inp = strip_wspace(buf); 1.26 + 1.27 + if(inp && *inp && *inp != '#') { 1.28 + char **tokens = tokenize(inp); 1.29 + for(i=0; tokens[i]; i++); 1.30 + 1.31 + cmdproc(i, tokens); 1.32 + } 1.33 + 1.34 + fputs("> ", stdout); 1.35 + fflush(stdout); 1.36 + } 1.37 + putchar('\n'); 1.38 + 1.39 + return 0; 1.40 +} 1.41 + 1.42 +char *strip_wspace(char *str) 1.43 +{ 1.44 + char *end = str + strlen(str) - 1; 1.45 + 1.46 + while(*str && isspace(*str)) str++; 1.47 + 1.48 + while(isspace(*end)) end--; 1.49 + end[1] = 0; 1.50 + 1.51 + return str; 1.52 +} 1.53 + 1.54 +#define SEP " \t\v\n\r" 1.55 + 1.56 +char **tokenize(char *str) 1.57 +{ 1.58 + int idx = 1; 1.59 + static char *argv[256]; 1.60 + 1.61 + argv[0] = strtok(str, SEP); 1.62 + while((argv[idx] = strtok(0, SEP))) idx++; 1.63 + 1.64 + argv[idx] = 0; 1.65 + return argv; 1.66 +} 1.67 + 1.68 +void cmd_chdir(int argc, char **argv); 1.69 +void cmd_list(int argc, char **argv); 1.70 +void cmd_train(int argc, char **argv); 1.71 +void cmd_cat(int argc, char **argv); 1.72 +void cmd_pwd(int argc, char **argv); 1.73 + 1.74 +static struct { const char *cmd; void (*proc)(int, char**); } cmdlist[] = { 1.75 + {"cd", cmd_chdir}, 1.76 + {"chdir", cmd_chdir}, 1.77 + {"ls", cmd_list}, 1.78 + {"sl", cmd_train}, 1.79 + {"cat", cmd_cat}, 1.80 + {"pwd", cmd_pwd}, 1.81 + {0, 0} 1.82 +}; 1.83 + 1.84 +void cmdproc(int argc, char **argv) 1.85 +{ 1.86 + int i; 1.87 + 1.88 + for(i=0; cmdlist[i].cmd; i++) { 1.89 + if(strcmp(argv[0], cmdlist[i].cmd) == 0) { 1.90 + cmdlist[i].proc(argc, argv); 1.91 + return; 1.92 + } 1.93 + } 1.94 + 1.95 + fprintf(stderr, "unknown command: %s\n", argv[0]); 1.96 +} 1.97 + 1.98 +void cmd_chdir(int argc, char **argv) 1.99 +{ 1.100 + if(argv[1]) { 1.101 + if(pvfs_chdir(argv[1]) == -1) { 1.102 + fprintf(stderr, "failed to change into directory: %s\n", argv[1]); 1.103 + } 1.104 + } else { 1.105 + fprintf(stderr, "syntax: cd <path>\n"); 1.106 + } 1.107 +} 1.108 + 1.109 +static int lscmp(const void *a, const void *b) 1.110 +{ 1.111 + return strcmp(*(char**)a, *(char**)b); 1.112 +} 1.113 + 1.114 +void cmd_list(int argc, char **argv) 1.115 +{ 1.116 + char *dirpath = "."; 1.117 + PVFS_DIR *dir; 1.118 + struct pvfs_dirent *dent; 1.119 + char **entries; 1.120 + int i, num_entries = 0; 1.121 + 1.122 + if(argv[1]) { 1.123 + dirpath = argv[1]; 1.124 + } 1.125 + 1.126 + if(!(dir = pvfs_opendir(dirpath))) { 1.127 + fprintf(stderr, "failed to open directory\n"); 1.128 + return; 1.129 + } 1.130 + 1.131 + while((dent = pvfs_readdir(dir))) { 1.132 + num_entries++; 1.133 + } 1.134 + 1.135 + if(!(entries = malloc(num_entries * sizeof *entries))) { 1.136 + fprintf(stderr, "out of memory\n"); 1.137 + pvfs_closedir(dir); 1.138 + return; 1.139 + } 1.140 + 1.141 + i = 0; 1.142 + pvfs_rewinddir(dir); 1.143 + while((dent = pvfs_readdir(dir))) { 1.144 + entries[i++] = strdup(dent->d_name); 1.145 + } 1.146 + pvfs_closedir(dir); 1.147 + 1.148 + qsort(entries, num_entries, sizeof *entries, lscmp); 1.149 + /*(int (*)(const void*, const void*))strcmp);*/ 1.150 + 1.151 + for(i=0; i<num_entries; i++) { 1.152 + puts(entries[i]); 1.153 + free(entries[i]); 1.154 + } 1.155 + free(entries); 1.156 +} 1.157 + 1.158 +void cmd_train(int argc, char **argv) 1.159 +{ 1.160 + printf("sorry, we're all out of trains at the moment\n"); 1.161 +} 1.162 + 1.163 +void cmd_cat(int argc, char **argv) 1.164 +{ 1.165 + int i; 1.166 + char buf[512]; 1.167 + 1.168 + for(i=1; i<argc; i++) { 1.169 + size_t sz; 1.170 + PVFS_FILE *fp = pvfs_fopen(argv[i], "rb"); 1.171 + if(!fp) { 1.172 + fprintf(stderr, "failed to open file: %s\n", argv[i]); 1.173 + continue; 1.174 + } 1.175 + 1.176 + while((sz = pvfs_fread(buf, 1, sizeof buf, fp)) > 0) { 1.177 + fwrite(buf, 1, sz, stdout); 1.178 + } 1.179 + fflush(stdout); 1.180 + } 1.181 +} 1.182 + 1.183 +void cmd_pwd(int argc, char **argv) 1.184 +{ 1.185 + printf("not implemented yet!\n"); 1.186 +}