view ffstools/tiffs-rd/ls.c @ 235:e17bb8818318

tiffs ls: show read-only file flag
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 26 Jan 2014 18:17:03 +0000
parents 024042383a26
children 254de9560ef3
line wrap: on
line source

/*
 * This C module implements the ls command.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "types.h"
#include "struct.h"
#include "globals.h"
#include "pathname.h"

static void
segment_size_callback(inf, opaque)
	struct inode_info *inf;
	u_long opaque;
{
	size_t *accump = (size_t *) opaque;
	struct chunkinfo chi;

	size_extra_chunk(inf, &chi);
	*accump += chi.len;
}

size_t
get_file_size(seghead_ino, deleted)
{
	struct chunkinfo chi;
	size_t accum;

	size_head_chunk(inode_info[seghead_ino], &chi);
	accum = chi.len;
	iterate_seg_file(seghead_ino, segment_size_callback, (u_long) &accum,
			 deleted);
	return(accum);
}

void
ls_callback(pathname, ino, depth)
	char *pathname;
{
	struct inode_info *inf = inode_info[ino];
	u_long size;
	char readonly;

	if (inf->type & 0x10)
		readonly = ' ';
	else
		readonly = 'r';
	switch (inf->type) {
	case 0xE1:
	case 0xF1:
		size = get_file_size(ino, 0);
		printf("f%c %7lu %s\n", readonly, size, pathname);
		return;
	case 0xE2:
	case 0xF2:
		printf("d%c         %s\n", readonly, pathname);
		return;
	case 0xE3:
	case 0xF3:
		printf("l%c         %s\n", readonly, pathname);
		return;
	default:
		fprintf(stderr,
			"BUG: bad inode byte %02X reached ls_callback()\n",
			inf->type);
		exit(1);
	}
}

cmd_ls()
{
	read_ffs_image();
	find_inode_block();
	alloc_inode_table();
	find_root_inode();
	traverse_visible_tree(ls_callback);
	exit(0);
}