/* Program to seek to a certain position on a disk partition  */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>


#define REISERFS_DISK_OFFSET_IN_BYTES (64 * 1024)
#define REISERFS_OLD_DISK_OFFSET_IN_BYTES (8 * 1024)

struct reiserfs_super_block
{
	u_char		s_block_count[4];
	u_char		s_free_blocks[4];
	u_char		s_root_block[4];
	u_char		s_journal_block[4];
	u_char		s_journal_dev[4];
	u_char		s_orig_journal_size[4];
	u_char		s_journal_trans_max[4];
	u_char		s_journal_block_count[4];
	u_char		s_journal_max_batch[4];
	u_char		s_journal_max_commit_age[4];
	u_char		s_journal_max_trans_age[4];
	u_char		s_blocksize[2];
	u_char		s_oid_maxsize[2];
	u_char		s_oid_cursize[2];
	u_char		s_state[2];
	u_char		s_magic[12];
};

int
main(int argc, char **argv)
{
  off_t seekpos;
  ssize_t rbytes;
  char *device;
  int fd;
  char *progname = argv[0];
  struct reiserfs_super_block rfs_sblock;
  char buf[1024];

  if (argc != 2) {
    fprintf(stderr, "Usage: %s partition\n", progname);
    exit(EXIT_FAILURE);
  }
  device = argv[1];
  fd = open(device, O_RDONLY);
  if (fd < 0) {
    perror(progname);
    exit(EXIT_FAILURE);
  }
/*   seekpos = lseek(fd, 1024, SEEK_SET); */
/*   printf("The seek position is %d\n", seekpos); */

  /* seek to reiserfs offset */
  seekpos = lseek(fd, REISERFS_DISK_OFFSET_IN_BYTES, SEEK_SET);
  printf("New seek position: %d\n", (int)seekpos);
  /*   rbytes = read(fd, (char *)&rfs_sblock, sizeof(rfs_sblock)); */
  /* Trying for a regular ufs partition */
/*   rbytes = read(fd, (struct reiserfs_super_block *)&rfs_sblock, 512); */
/*   rbytes = read(fd, (char *)&buf, 1024); */
  rbytes = pread(fd, (struct reiserfs_super_block *)&rfs_sblock, 512, REISERFS_DISK_OFFSET_IN_BYTES);
  if (rbytes < 0)
    perror(argv[0]);
  printf("No. of bytes read %d.\nActual size:%d\n", rbytes, sizeof(rfs_sblock));
  return 0;
}
