/*
 * Read Sun Disk label  -- hack of program from sunsolve.sun.com (FAQ 1533)
 *X86 is:
 *<DEFAULT cyl 2691 alt 2 hd 9 sec 85>
 *
 *SPARC is:
 * <SUN1.05 cyl 2036 alt 2 hd 14 sec 72>
 *
 *Root or group sys access is required to execute this program.
 *
 */



#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/dkio.h>
#include <sys/dklabel.h>
#include <fcntl.h>

main(argc, argv)
  int argc;
char *argv[];
{
#ifdef SPARC_ARC
  int label_loc = (512 * DK_LABEL_LOC);
#define ZEXAMPLE       "\tExample: rdlabel /dev/rdsk/c0t0d0s0\n"
#else
#define ZEXAMPLE       "\tExample: rdlabel /dev/rdsk/c0t0d0p1\n"
  int label_loc = 0;
#endif
  int disk_fd;
  int i;
  struct dk_label label;
  int partition;
  char buffer[80];

  if (argc < 2)
    {
      fprintf(stderr, "Error: Argument expected!\n");
      usage();
      exit(1);
    }

  sprintf(buffer, "%s", argv[1]);
  disk_fd = open( buffer, O_RDONLY );
  if ( disk_fd < 0 )
    {
      perror("Unable to open device ");
      exit(1);
    }
  else
    {
      lseek(disk_fd,label_loc,SEEK_SET);
      (void) read(disk_fd, (char *)&label, sizeof(label));
      (void) close(disk_fd);
    }

  if (label.dkl_magic != DKL_MAGIC) exit(2);
  (void) printf("\t<%s>\n", label.dkl_asciilabel);

  exit(0);

}

usage()
{
  fprintf(stderr, "Usage: readlabel <device>");
  fprintf(stderr, "Warning: You must be root or group sys to run rdlabel!\n");
  fprintf(stderr,ZEXAMPLE);
}
