コード例 #1
0
ファイル: ls2.c プロジェクト: orange8637/c
show_file_info( char *filename, struct stat *info_p )
/*
 * display the info about 'filename'.  The info is stored in struct at *info_p
 */
{
	//char	*uid_to_name(), *ctime(), *gid_to_name(), *filemode();
#ifdef TAGGED_STYLE

	printf("%s:\n", filename );			/* print name	 */

	printf("\t  mode: %s\n", filemode(info_p->st_mode) );
	printf("\t links: %d\n", (int) info_p->st_nlink);	/* links */
	printf("\t owner: %s\n", uid_to_name(info_p->st_uid) );
	printf("\t group: %s\n", gid_to_name(info_p->st_gid) );
	printf("\t  size: %ld\n",(long)info_p->st_size);	/* size  */
	printf("\t   mod: %s",   ctime(&info_p->st_mtime));
	printf("\taccess: %s",   ctime(&info_p->st_atime));
#endif
#ifdef	LS_STYLE
	printf( "%s"    , filemode(info_p->st_mode) );
	printf( "%4d "  , (int) info_p->st_nlink);	
	printf( "%-8s " , uid_to_name(info_p->st_uid) );
	printf( "%-8s " , gid_to_name(info_p->st_gid) );
	printf( "%8ld " , (long)info_p->st_size);
	printf( "%.12s ", 4+ctime(&info_p->st_mtime));
	printf( "%s\n"  , filename );
#endif

}
コード例 #2
0
ファイル: ls2.c プロジェクト: 19thhell/LinuxCommand
void show_file_info(char *filename,struct stat *info_p,int maxlink,int maxsize)
/*
 * display the info about 'filename'.  The info is stored in struct at *info_p
 */
{
	char	*uid_to_name(short uid),*gid_to_name(short gid),*filemode(int mode);

    struct tm *mtime = gmtime(&info_p->st_mtime);
	printf("%s ",filemode(info_p->st_mode));
	printf("%*d ",maxlink,(int)info_p->st_nlink);	/* links */
	printf("%s ",uid_to_name(info_p->st_uid));
	printf("%s ",gid_to_name(info_p->st_gid));
	printf("%*ld ",maxsize,(long)info_p->st_size);	/* size  */
	printf("%d-%02d-%02d ",(mtime->tm_year) + 1900,(mtime->tm_mon) + 1,mtime->tm_mday);
	printf("%02d:%02d ",mtime->tm_hour + 8,mtime->tm_min);
	printf("%s \n",filename);			/* print name	 */
}
コード例 #3
0
int
main(int argc, char **argv)
{
	int ch, ret;
	int fileflag, ttyflag, vnodeflag;
	char buf[_POSIX2_LINE_MAX];
	const char *opts;

	fileflag = swapflag = ttyflag = vnodeflag = 0;

	/* We will behave like good old swapinfo if thus invoked */
	opts = strrchr(argv[0],'/');
	if (opts)
		opts++;
	else
		opts = argv[0];
	if (!strcmp(opts,"swapinfo")) {
		swapflag = 1;
		opts = "ghkmM:N:";
		usagestr = "swapinfo [-ghkm] [-M core] [-N system]";
	} else {
		opts = "TM:N:fhiknstv";
		usagestr = "pstat [-Tfhknst] [-M core] [-N system]";
	}

	while ((ch = getopt(argc, argv, opts)) != -1) {
		switch (ch) {
		case 'f':
			fileflag = 1;
			break;
		case 'g':
			if (setenv("BLOCKSIZE", "1G", 1) == -1)
				warn("setenv: cannot set BLOCKSIZE=1K");
			break;
		case 'k':
			if (setenv("BLOCKSIZE", "1K", 1) == -1)
				warn("setenv: cannot set BLOCKSIZE=1K");
			break;
		case 'm':
			if (setenv("BLOCKSIZE", "1M", 1) == -1)
				warn("setenv: cannot set BLOCKSIZE=1K");
			break;
		case 'h':
			humanflag = 1;
			break;
		case 'M':
			memf = optarg;
			break;
		case 'N':
			nlistf = optarg;
			break;
		case 'n':
			usenumflag = 1;
			break;
		case 's':
			++swapflag;
			break;
		case 'T':
			totalflag = 1;
			break;
		case 't':
			ttyflag = 1;
			break;
		case 'v':
		case 'i':		/* Backward compatibility. */
			errx(1, "vnode mode not supported");
#if 0
			vnodeflag = 1;
			break;
#endif
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	/*
	 * Discard setgid privileges if not the running kernel so that bad
	 * guys can't print interesting stuff from kernel memory.
	 */
	if (nlistf != NULL || memf != NULL)
		setgid(getgid());

	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
		errx(1, "kvm_openfiles: %s", buf);
#ifdef USE_KCORE
	if (kcore_wrapper_open(nlistf, memf, buf))
		errx(1, "kcore_open: %s", buf);
#endif
	if ((ret = kvm_nlist(kd, nl)) != 0) {
		size_t i;
		int quit = 0;

		if (ret == -1)
			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
		for (i = 0; i < NL_LAST_MANDATORY; i++) {
			if (!nl[i].n_value) {
				quit = 1;
				warnx("undefined symbol: %s", nl[i].n_name);
			}
		}
		if (quit)
			exit(1);
	}
	if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
		usage();
	if (fileflag || totalflag)
		filemode();
	if (vnodeflag)
		vnodemode();
	if (ttyflag)
		ttymode();
	if (swapflag || totalflag)
		swapmode();
	exit (0);
}
コード例 #4
0
ファイル: pstat.c プロジェクト: mulichao/freebsd
int
main(int argc, char *argv[])
{
	int ch, quit, ret;
	int fileflag, ttyflag;
	unsigned int i;
	char buf[_POSIX2_LINE_MAX];
	const char *opts;

	fileflag = swapflag = ttyflag = 0;

	/* We will behave like good old swapinfo if thus invoked */
	opts = strrchr(argv[0], '/');
	if (opts)
		opts++;
	else
		opts = argv[0];
	if (!strcmp(opts, "swapinfo")) {
		swapflag = 1;
		opts = "ghkmM:N:";
		usagestr = "swapinfo [-ghkm] [-M core [-N system]]";
	} else {
		opts = "TM:N:fghkmnst";
		usagestr = "pstat [-Tfghkmnst] [-M core [-N system]]";
	}

	while ((ch = getopt(argc, argv, opts)) != -1)
		switch (ch) {
		case 'f':
			fileflag = 1;
			break;
		case 'g':
			setenv("BLOCKSIZE", "1G", 1);
			break;
		case 'h':
			humanflag = 1;
			break;
		case 'k':
			setenv("BLOCKSIZE", "1K", 1);
			break;
		case 'm':
			setenv("BLOCKSIZE", "1M", 1);
			break;
		case 'M':
			memf = optarg;
			break;
		case 'N':
			nlistf = optarg;
			break;
		case 'n':
			usenumflag = 1;
			break;
		case 's':
			++swapflag;
			break;
		case 'T':
			totalflag = 1;
			break;
		case 't':
			ttyflag = 1;
			break;
		default:
			usage();
		}

	/*
	 * Initialize symbol names list.
	 */
	for (i = 0; i < NNAMES; i++)
		nl[namelist[i].order].n_name = strdup(namelist[i].name);

	if (memf != NULL) {
		kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
		if (kd == NULL)
			errx(1, "kvm_openfiles: %s", buf);
		if ((ret = kvm_nlist(kd, nl)) != 0) {
			if (ret == -1)
				errx(1, "kvm_nlist: %s", kvm_geterr(kd));
			quit = 0;
			for (i = 0; nl[i].n_name[0] != '\0'; ++i)
				if (nl[i].n_value == 0) {
					quit = 1;
					warnx("undefined symbol: %s",
					    nl[i].n_name);
				}
			if (quit)
				exit(1);
		}
	}
	if (!(fileflag | ttyflag | swapflag | totalflag))
		usage();
	if (fileflag || totalflag)
		filemode();
	if (ttyflag)
		ttymode();
	if (swapflag || totalflag)
		swapmode();
	exit (0);
}