1 /**************************************************
2 UZI (Unix Z80 Implementation) Utilities: ucp.c
4 14 June 1998 - Reformatted, restructured command
5 switch, sense Ctrl-Z in type. HFB
6 21 Sept 1999 - Corrected the 'constant expression'
7 problem, added some missing breaks.
9 ***************************************************/
21 #define UCP_VERSION "1.3ac"
23 static int16_t *syserror = (int16_t*)&udata.u_error;
25 static char line[128];
26 static char *nextline = NULL;
27 static char *month[] =
28 { "Jan", "Feb", "Mar", "Apr",
29 "May", "Jun", "Jul", "Aug",
30 "Sep", "Oct", "Nov", "Dec" };
34 static int match(char *cmd);
35 static void usage(void);
36 static void prmode(int mode);
37 static int cmd_ls(char *path);
38 static int cmd_chmod(char *modes, char *path);
39 static int cmd_mknod( char *path, char *modes, char *devs);
40 static int cmd_mkdir(char *path);
41 static int cmd_link( char *src, char *dst );
42 static int cmd_get( char *src, char *dest, int binflag);
43 static int cmd_put( char *arg, int binflag);
44 static int cmd_type( char *arg);
45 static int cmd_fdump(char *arg);
46 static int cmd_rm( char *path);
47 static int cmd_rmdir(char *path);
49 static int interactive = 0;
51 int main(int argc, char *argval[])
54 char cmd[30], arg1[1024], arg2[30], arg3[30];
64 } else if (argc == 3) {
66 strncpy(&line[0], argval[2], 127);
70 printf("Usage: ucp FILE [COMMAND]\n");
82 printf("Fuzix UCP version " UCP_VERSION ".%s\n", interactive?" Type ? for help.":"");
85 if (multiline && !pending_line) {
88 if (fgets(line, 128, stdin) == NULL) {
95 nextline = strchr(&line[0], ';');
96 if (nextline != NULL) {
108 count = sscanf(nextline, "%s %s %s %s", cmd, arg1, arg2, arg3);
111 if (count == 0 || cmd[0] == '\0')
114 count = sscanf(line, "%s %s %s %s", cmd, arg1, arg2, arg3);
115 if (nextline != NULL) {
118 if (count == 0 || cmd[0] == '\0')
124 if (strcmp(cmd, "\n") == 0)
128 switch (match(cmd)) {
143 if ((retc = fuzix_chdir(arg1)) != 0) {
144 printf("cd: error number %d\n", *syserror);
151 retc = cmd_mkdir(arg1);
155 if (*arg1 && *arg2 && *arg3)
156 retc = cmd_mknod(arg1, arg2, arg3);
161 retc = cmd_chmod(arg1, arg2);
166 retc = cmd_get(arg1, *arg2 ? arg2 : arg1, 0);
171 retc = cmd_get(arg1, *arg2 ? arg2 : arg1, 1);
176 retc = cmd_put(arg1, 0);
181 retc = cmd_put(arg1, 1);
186 retc = cmd_type(arg1);
191 retc = cmd_fdump(arg1);
200 for (j = 0; j < 4; ++j) {
201 retc = fuzix_getfsys(j, (char*)&fsys);
202 if (retc == 0 && fsys.s_mounted) {
203 printf("%d: %u blks used, %u free; ", j,
204 (fsys.s_fsize - fsys.s_isize) - fsys.s_tfree,
206 printf("%u inodes used, %u free\n",
207 (8 * (fsys.s_isize - 2) - fsys.s_tinode),
215 retc = cmd_rmdir(arg1);
220 retc = cmd_link(arg1, arg2);
228 default: /* ..else.. */
229 printf("Unknown command, type ? for help.\n");
233 } while (multiline || pending_line);
241 static int match(char *cmd)
243 if (strcmp(cmd, "exit") == 0)
245 else if (strcmp(cmd, "quit") == 0)
247 else if (strcmp(cmd, "ls") == 0)
249 else if (strcmp(cmd, "dir") == 0)
251 else if (strcmp(cmd, "cd") == 0)
253 else if (strcmp(cmd, "mkdir") == 0)
255 else if (strcmp(cmd, "mknod") == 0)
257 else if (strcmp(cmd, "chmod") == 0)
259 else if (strcmp(cmd, "get") == 0)
261 else if (strcmp(cmd, "bget") == 0)
263 else if (strcmp(cmd, "put") == 0)
265 else if (strcmp(cmd, "bput") == 0)
267 else if (strcmp(cmd, "type") == 0)
269 else if (strcmp(cmd, "cat") == 0)
271 else if (strcmp(cmd, "dump") == 0)
273 else if (strcmp(cmd, "rm") == 0)
275 else if (strcmp(cmd, "df") == 0)
277 else if (strcmp(cmd, "rmdir") == 0)
279 else if (strcmp(cmd, "ln" ) == 0 )
281 else if (strcmp(cmd, "help") == 0)
283 else if (strcmp(cmd, "?") == 0)
290 static void usage(void)
292 printf("UCP commands:\n");
294 printf("exit|quit\n");
295 printf("dir|ls [path]\n");
297 printf("mkdir dirname\n");
298 printf("mknod name mode dev#\n");
299 printf("chmod mode path\n");
300 printf("[b]get sourcefile [destfile]\n");
301 printf("[b]put uzifile\n");
302 printf("type|cat filename\n");
303 printf("dump filename\n");
305 printf("rmdir dirname\n");
307 printf("ln sourcefile destfile\n");
310 static void prmode(int mode)
328 static int cmd_ls(char *path)
331 struct uzi_stat statbuf;
336 if (fuzix_stat(path, &statbuf) != 0 || (statbuf.st_mode & F_MASK) != F_DIR) {
337 printf("ls: can't stat %s\n", path);
342 d = fuzix_open(path, 0);
344 fprintf(stderr, "ls: can't open %s\n", path);
347 while (fuzix_read(d, (char *) &buf, 16) == 16) {
348 if (buf.d_name[0] == '\0')
351 if (path[0] != '.' || path[1]) {
358 strcat(dname, buf.d_name);
360 if (fuzix_stat(dname, &statbuf) != 0) {
361 fprintf(stderr, "ls: can't stat %s\n", dname);
364 st = (statbuf.st_mode & F_MASK);
366 if ((st & F_MASK) == F_DIR) /* & F_MASK is redundant */
368 else if ((st & F_MASK) == F_CDEV)
370 else if ((st & F_MASK) == F_BDEV)
372 else if ((st & F_MASK) == F_PIPE)
374 else if ((st & F_REG) == 0)
379 prmode(statbuf.st_mode >> 6);
380 prmode(statbuf.st_mode >> 3);
381 prmode(statbuf.st_mode);
383 printf("%4d %5d", statbuf.st_nlink, statbuf.st_ino);
385 if ((statbuf.st_mode & F_MASK) == F_DIR)
389 (statbuf.st_mode & F_CDEV) ?
393 if (statbuf.fst_mtime == 0) { /* st_mtime? */
394 /*printf("--- -- ---- --:--");*/
397 time_t t = statbuf.fst_mtime;
398 struct tm *tm = gmtime(&t);
399 printf("%s %02d %4d ",
409 printf(" %-15s\n", dname);
415 static int cmd_chmod(char *modes, char *path)
420 printf("chmod %s to %s\n", path, modes);
421 if (sscanf(modes, "%o", &mode) != 1) {
422 fprintf(stderr, "chmod: bad mode\n");
425 /* Preserve the type if not specified */
428 if (fuzix_stat(path, &st) != 0) {
429 fprintf(stderr, "chmod: can't stat file '%s': %d\n", path, *syserror);
432 mode = (st.st_mode & ~0x7777) | mode;
434 if (fuzix_chmod(path, mode)) {
435 fprintf(stderr, "chmod: error %d\n", *syserror);
442 static int cmd_mknod( char *path, char *modes, char *devs)
447 if (sscanf(modes, "%o", &mode) != 1) {
448 fprintf(stderr, "mknod: bad mode\n");
451 if ((mode & F_MASK) != F_BDEV && (mode & F_MASK) != F_CDEV) {
452 fprintf(stderr, "mknod: mode is not device\n");
456 sscanf(devs, "%d", &dev);
458 fprintf(stderr, "mknod: bad device\n");
461 if (fuzix_mknod(path, mode, dev) != 0) {
462 fprintf(stderr, "fuzix_mknod: error %d\n", *syserror);
470 static int cmd_mkdir(char *path)
472 if (fuzix_mkdir(path, 0777) != 0) {
473 fprintf(stderr, "mkdir: mknod error %d\n", *syserror);
479 static int cmd_link( char *src, char *dst )
481 if( fuzix_link( src, dst ) != 0 ){
482 fprintf(stderr, "link: error %d\n", *syserror );
488 static int cmd_get( char *src, char *dest, int binflag)
495 fp = fopen(src, binflag ? "rb" : "r");
497 fprintf(stderr, "Source file '%s' not found\n", src);
500 d = fuzix_creat(basename(dest), 0666);
502 fprintf(stderr, "Can't open destination file '%s'; error %d\n", dest, *syserror);
507 nread = fread(cbuf, 1, 512, fp);
510 if (fuzix_write(d, cbuf, nread)) {
511 fprintf(stderr, "fuzix_write error %d\n", *syserror);
524 static int cmd_put( char *arg, int binflag)
531 fp = fopen(arg, binflag ? "wb" : "w");
533 fprintf(stderr, "Can't open destination file.\n");
536 d = fuzix_open(arg, 0);
538 fprintf(stderr, "Can't open unix file error %d\n", *syserror);
542 if ((nread = fuzix_read(d, cbuf, 512)) == 0)
544 if (fwrite(cbuf, 1, nread, fp) != nread) {
545 fprintf(stderr, "fwrite error");
557 static int cmd_type( char *arg)
563 d = fuzix_open(arg, 0);
565 fprintf(stderr, "Can't open unix file error %d\n", *syserror);
569 if ((nread = fuzix_read(d, cbuf, 512)) == 0)
572 for (i = 0; i < nread; i++) {
575 fputc(cbuf[i], stdout);
584 static int cmd_fdump(char *arg)
590 printf("Dump starting.\n");
591 d = fuzix_open(arg, 0);
593 fprintf(stderr, "Can't open unix file error %d\n", *syserror);
597 if ((nread = fuzix_read(d, cbuf, 512)) == 0)
601 printf("Dump done.\n");
606 static int cmd_rm( char *path)
608 struct uzi_stat statbuf;
610 if (fuzix_stat(path, &statbuf) != 0) {
611 fprintf(stderr, "unlink: can't stat %s\n", path);
614 if ((statbuf.st_mode & F_MASK) == F_DIR) {
615 fprintf(stderr, "unlink: %s is a directory\n", path);
618 if (fuzix_unlink(path) != 0) {
619 fprintf(stderr, "unlink: fuzix_unlink errn=or %d\n", *syserror);
626 static int cmd_rmdir(char *path)
628 struct uzi_stat statbuf;
633 if (fuzix_stat(path, &statbuf) != 0) {
634 fprintf(stderr, "rmdir: can't stat %s\n", path);
637 if ((statbuf.st_mode & F_DIR) == 0) {
638 /*-- Constant expression !!! HFB --*/
639 fprintf(stderr, "rmdir: %s is not a directory\n", path);
642 if ((fd = fuzix_open(path, 0)) < 0) {
643 fprintf(stderr, "rmdir: %s is unreadable\n", path);
646 while (fuzix_read(fd, (char *) &dir, sizeof(dir)) == sizeof(dir)) {
649 if (!strcmp(dir.d_name, ".") || !strcmp(dir.d_name, ".."))
651 fprintf(stderr, "rmdir: %s is not empty\n", path);
657 strcpy(newpath, path);
658 strcat(newpath, "/.");
659 if (fuzix_unlink(newpath) != 0) {
660 fprintf(stderr, "rmdir: can't unlink \".\" error %d\n", *syserror);
663 strcat(newpath, ".");
664 if (fuzix_unlink(newpath) != 0) {
665 fprintf(stderr, "rmdir: can't unlink \"..\" error %d\n", *syserror);
668 if (fuzix_unlink(path) != 0) {
669 fprintf(stderr, "rmdir: fuzix_unlink error %d\n", *syserror);