⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tfscli.c

📁 可移到ucos上的文件系统
💻 C
📖 第 1 页 / 共 2 页
字号:
	lcnt = 0;
	cp = (char *) (TFS_BASE(fp));
	for(i=0;i<fp->filsize;i++) {
		putchar(*cp);
		if ((*cp == '\r') || (*cp == '\n')) {
			lcnt++;
			if (lcnt == more) {
				if (More() == 0)
					break;
				lcnt = 0;
			}
		}
		cp++;
	}
}

char *TfsHelp[] = {
	"Tiny File System Interface",
	"-[df:i:mv] operation [args]...",
	"",
	"Options:",
	" -d    tfs device",
	" -f    flags (see below)",
	" -i    info",
	" -m    enable more throttle",
	" -v    enable verbosity",
	" -x    set exit flag if error",
	"",
	"Operations:",
	" init, stat, clean[r], check [var], ls [filter], ld[v] {name}",
	" rm {filter}, cat {name}, run {name}, freemem [var], trace [lvl]",
	" info {file} {var}, log {on|off} {msg}, size {file} {var}",
	" cp {from_name} {to_name | addr}, add {name} {src_addr} {size}",
	"",
	"Flags:",
	" E=exec_binary, e=exec_script, c=compressed",
	" b=run_at_boot, B=qry_run_at_boot, i=inplace_modifiable",
	" 0-3=usrlvl_0-3, u=ulvl_unreadable",
	0,
};

/* Tfs():
 *	Entry point for the tfs command through the monitor's command line
 *	interface.  This function provides access to most TFS functionality
 *	through the CLI.
 */

int
Tfs(int argc, char *argv[])
{
	extern	int optind;
	extern	char *optarg;
	TDEV	*tdp, *tdptmp;
	TFILE	*fp;
	TINFO	tinfo;
	long	size;
	int		opt, verbose, i, more, retval, setexitflag, status;
	char	*src, *name, *info, *flags, *to, *from, *devprefix;

	tdp = 0;
	more = 0;
	retval = 0;
	verbose = 0;
	status = TFS_OKAY;
	setexitflag = 0;
	info = (char *)0;
	flags = (char *)0;
	devprefix = (char *)0;
	while ((opt = getopt(argc, argv, "d:vmf:i:x")) != -1) {
		switch (opt) {
		case 'd':
			devprefix = optarg;
			tdp = gettfsdev_fromprefix(devprefix,1);
			if (!tdp)
				return(0);
			break;
		case 'f':
			flags = optarg;
			break;
		case 'i':
			info = optarg;
			break;
		case 'm':
			more++;
			break;
		case 'v':
			verbose++;
			break;
		case 'x':
			setexitflag = 1;
			break;
		default:
			goto done;
		}
	}

	if (argc == optind) {
		retval = -1;
	}
	else if (strcmp(argv[optind], "trace") == 0) {
		if (argc == optind + 2)
			tfsTrace = strtoul(argv[optind+1],0,0);
		else if (argc == optind + 1)
			printf("Current trace mode: 0x%lx\n",tfsTrace);
		else
			retval = -1;
	}
	else if (strcmp(argv[optind], "init") == 0) {
		if (getUsrLvl() < MAXUSRLEVEL) {
			status = showTfsError(TFSERR_USERDENIED,0);
		}
		else {
			_tfsinit(tdp);
			tfsclear(tdp);
		}
	}
	else if (strcmp(argv[optind], "log") == 0) {
		retval = tfsLogCmd(argc,argv,optind);
	}
	else if (strcmp(argv[optind], "stat") == 0) {
		char buf[32];
		int	 opencnt;
		struct	tfsdat *slot;

		/* Display current TFS memory usage: */
		tfsmemuse(tdp,&tinfo,1);
		printf("TFS Hdr size: %d\n",TFSHDRSIZ);

		/* Display currently opened files: */
		opencnt = 0;
		slot = tfsSlots;
		for (i=0;i<TFS_MAXOPEN;i++,slot++) {
			if (slot->offset >= 0) {
				printf("%3d: 0x%08lx %s %s\n",i,(ulong)(slot->base),
					tfsmodebtoa(slot->flagmode,buf),slot->hdr.name);
				opencnt++;
			}
		}
		printf("Total files currently opened: %d\n",opencnt);
	}
	else if (strcmp(argv[optind], "freemem") == 0) {
		char *prefix;

		tfsmemuse(tdp,&tinfo,0);
		if (tdp)
			prefix = tdp->prefix;
		else
			prefix = "";
		if (argc == optind + 1) {
			printf("0x%x (%d) bytes available to TFS %s\n",
				tinfo.memfordata,tinfo.memfordata,prefix);
		}
		else if (argc == optind + 2) {
			shell_sprintf(argv[optind+1],"0x%x",tinfo.memfordata);
		}
		else 
			retval = -1;
		goto done;
	}
	else if (strcmp(argv[optind], "info") == 0) {
		if (argc == optind + 3) {
			fp = tfsstat(argv[optind+1]);
			if ((!fp) ||
				((fp->flags & TFS_UNREAD) && (TFS_USRLVL(fp) > getUsrLvl())))
				setenv(argv[optind+2],0);
			else
				setenv(argv[optind+2],fp->info);
		}
		else
			retval = -1;
	}
	else if (strcmp(argv[optind], "size") == 0) {
		if (argc == optind + 3) {
			fp = tfsstat(argv[optind+1]);
			if ((!fp) ||
				((fp->flags & TFS_UNREAD) && (TFS_USRLVL(fp) > getUsrLvl())))
				setenv(argv[optind+2],0);
			else {
				shell_sprintf(argv[optind+2],"%ld",fp->filsize);
			}
		}
		else
			retval = -1;
	}
	else if (strcmp(argv[optind], "ls") == 0) {
		if (verbose > 1)
			status = tfsvlist(&argv[optind+1],verbose,more << 1);
		else
			status = tfsqlist(&argv[optind+1],verbose,more << 3);

		if (status != TFS_OKAY)
			status = showTfsError(status,"ls");
	}
	else if (strcmp(argv[optind], "cp") == 0) {
		char	buf[16];

		if (argc == optind + 3) {
			from = argv[optind + 1];
			to = argv[optind + 2];
			if (tfscheckfile(from) == TFS_OKAY) {
				fp = tfsstat(from);
				if (!flags)
					flags = tfsflagsbtoa(fp->flags,buf);
				if (!info)
					info = fp->info;
				if (fp) {
					if ((fp->flags & TFS_UNREAD) &&
						(TFS_USRLVL(fp) > getUsrLvl())) {
						status = showTfsError(TFSERR_USERDENIED,from);
					}
					else if (to[0] == '0' && to[1] == 'x') {
						memcpy((char *)strtol(to,0,16),
							TFS_BASE(fp),TFS_SIZE(fp));
					}
					else {
						status = tfsadd(to,info,flags,
							(char *)(TFS_BASE(fp)),fp->filsize);
						if (status != TFS_OKAY)
							status = showTfsError(status,to);
					}
				}
				else {
					status = showTfsError(TFSERR_NOFILE,from);
				}
			}
		}
		else
			retval = -1;
	}
	else if (strcmp(argv[optind], "cat") == 0) {
		if (argc >= optind + 2) {
			for(i=optind+1;i<argc;i++) {
				name = argv[i];
				if (tfscheckfile(name) == TFS_OKAY) {
					fp = tfsstat(name);
					if (fp) {
						if ((fp->flags & TFS_UNREAD) &&
							(TFS_USRLVL(fp) > getUsrLvl())) {
							status = showTfsError(TFSERR_USERDENIED,name);
						}
						else {
							tfscat(fp,more << 3);	/* more times 8 */
						}
					}
					else {
						status = showTfsError(TFSERR_NOFILE,name);
					}
				}
			}
		}
		else
			retval = -1;
	}
	/* rms: "remove space".  Used for testing only... Keep removing files
	 * until the number of bytes freed up is greater than the argument
	 * to rms. Skip over all files listed after the first size argument.
	 * Arglist to tfs rms is...
	 *
	 *	tfs rms {SIZE} [EXCLUDEFILE1] [EXCLUDEFILE2] ...
	 *	where SIZE is the amount of space to freeup and EXCLUDEFILEN is
	 *	a filename that is not to be removed.
	 */
	else if (strcmp(argv[optind], "rms") == 0) {
		if (argc >= optind + 2) {
			int	insize, totsize;

			totsize = 0;
			insize = strtol(argv[optind+1],0,0);
			tfsreorder();
			for(i=0;tfsAlist[i];i++) {
				int j;

				for(j=optind+1;j<argc;j++) {
					if (!strcmp(TFS_NAME(tfsAlist[i]),argv[j]))
						break;
				}
				if (j != argc) {
					continue;
				}
				if (devprefix &&
					strncmp(TFS_NAME(tfsAlist[i]),devprefix,strlen(devprefix)))
					continue;
				totsize += (TFS_SIZE(tfsAlist[i]) + TFSHDRSIZ);
				if (verbose)
					printf("rms: removing %s (%ld)\n",TFS_NAME(tfsAlist[i]),
						TFS_SIZE(tfsAlist[i]) + TFSHDRSIZ);
				_tfsunlink(TFS_NAME(tfsAlist[i]));
				if (totsize > insize)
					break;
				totsize += TFSHDRSIZ;
			}
			if (totsize < insize)
				printf("rms: deletion of %d bytes failed\n",insize);
		}
		else
			retval = -1;
	}
	else if (strcmp(argv[optind], "rm") == 0) {
		if (argc >= optind + 2) {
			if ((status = tfsrm(&argv[optind+1])) != TFS_OKAY)
				status = showTfsError(status,0);
		}
		else
			retval = -1;
	}
	else if (strcmp(argv[optind], "fix") == 0) {
		tfsfixup(verbose,1);
	}
	else if (strncmp(argv[optind], "clean",5) == 0) {
		int reset = 0;

#if DEFRAG_TEST_ENABLED
		if (argc == optind + 3) {
			ExitPoint = atoi(argv[optind+1]);
			ExitSector = atoi(argv[optind+2]);
			printf("Exitpoint=%d, Exitsector=%d\n",ExitPoint,ExitSector);
		}
		else
			ExitPoint = ExitSector = 0;
#else
		if (argc != optind+1)
			return(-1);
#endif
		/* If command is 'cleanr', then reset at end of tfsclean()... */
		if (argv[optind][5] == 'r')
			reset = 1;
		else if (argv[optind][5] != 0)
			return(-1);

		/* If tdp has been set by the -d option, only defrag the affected */
		/* device; else, defrag all devices... */
		for (tdptmp=tfsDeviceTbl;tdptmp->start != TFSEOT;tdptmp++) {
			if (!tdp || (tdp == tdptmp))
				tfsclean(0,0,0,0,tdptmp,reset,verbose+1);
		}
	}
	else if (strcmp(argv[optind], "check") == 0) {
		if ((argc == optind+1) && (verbose == 0))
			verbose = 1;
	
		/* If tdp has been set by the -d option, only check the affected */
		/* device; else, check all devices... */
		for (tdptmp=tfsDeviceTbl;tdptmp->start != TFSEOT;tdptmp++) {
			if (!tdp || (tdp == tdptmp)) {
				if (tfscheck(tdptmp,verbose) != TFS_OKAY)
					status = TFSERR_CORRUPT;
			}
		}
		/* If an additional argument is provided after the "check" command
		 * then assume it is a shell variable name that is to be populated
		 * with the pass/fail status of the check...
		 */
		if (argc == optind+2)
			setenv(argv[optind+1],status == TFS_OKAY ? "PASS" : "FAIL");
	}
	else if (!strncmp(argv[optind], "ld",2)) {
		if (argc == optind + 2) {
			int vfy = 0;

			if (argv[optind][2] == 'v')
				vfy = 1;
			else if (argv[optind][2] != 0)
				return(-1);
			name = argv[optind + 1];
			status = tfsld(name,verbose,vfy);
			if (status != TFS_OKAY)
				status = showTfsError(status,name);
		}
		else
			retval = -1;
	}
	else if (strcmp(argv[optind], "run") == 0) {
		if (argc >= optind + 2) {
			name = argv[optind + 1];
			status = tfsrun(&argv[optind+1],verbose);
			if (status != TFS_OKAY)
				status = showTfsError(status,name);
		}
		else
			retval = -1;
	}
	else if (!(strcmp(argv[optind], "add"))) {
		if (argc == optind + 4) {
			name = argv[optind + 1];
			src = (char *) strtol(argv[optind + 2], (char **) 0, 0);
			size = strtol(argv[optind + 3], (char **) 0, 0);
			status = tfsadd(name, info, flags, src, size);
			if (status != TFS_OKAY)
				status = showTfsError(status,name);
		}
		else
			retval = -1;
	}
	else
		retval = -1;
done:
	if ((setexitflag) && ((retval == -1) || (status != TFS_OKAY)))
		ScriptExitFlag = EXIT_SCRIPT;
	return(retval);
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -