#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <fcntl.h>
#include <sys/openprom.h>
#include <sys/openpromio.h>
#include <sys/stat.h>
#include <sys/types.h>


#define DECIMAL_OUTPUT

int main(int argc, char *argv[]) {
  int openprom, i;
  struct openpromio *property;

/* Ought to check args here */

  if ((property=(struct openpromio*)malloc(
				      sizeof(struct openpromio*) +
				      OPROMMAXPARAM))
      ==NULL) {
    fprintf(stderr, "%s cannot allocate mem.\n", argv[0]);
    exit(1);
  }
  strcpy(property->oprom_array, argv[1]);
  property->oprom_size=OPROMMAXPARAM;

  fprintf(stderr,"%s: getting OpenPROM property [%s].\n",argv[0], argv[1]);

  if ((openprom=open("/dev/openprom", O_RDWR)) == -1) {
    fprintf(stderr, "%s open: %s\n", argv[0], strerror(errno));
    exit(1);
  }

  if (ioctl(openprom, OPROMGETOPT, property)==-1) {
    fprintf(stderr, "%s ioctl(OPROMGETOPT): %s\n", argv[0], strerror(errno));
      exit(1);
    }

  close(openprom);

  if (property->oprom_size != 0)
    for (i=0;
	 i < property->oprom_size ||
	 (puts(property->oprom_array), 0);
	 i++)
      if (!isascii(property->oprom_array[i])) {
#if defined(HEX_OUTPUT)
	fprintf(stderr, "%s: non-ascii results; Listing in hex:\n", argv[0]);
	for (i=0; i < property->oprom_size; i++) {
	  printf("%02x%02x ", (unsigned char)property->oprom_array[2*i],
		 (unsigned char)property->oprom_array[2*i+1]);
	    /* unsigned char because Sun are twinks. */
	  if ((i % 8) == 7) puts("");
	}
#elif defined(DECIMAL_OUTPUT)
	fprintf(stderr, "%s: non-ascii results; Listing in decimal:\n",
		argv[0]);
	for (i=0; i < property->oprom_size; i++) {
	  printf("%03d ", (unsigned char)property->oprom_array[i]);
	    /* unsigned char because Sun are twinks. */
	  if ((i % 17) == 16) puts("");
	}
#endif
	continue; /* break outer for */
      }

  fprintf(stderr, "%s: %d bytes returned.\n", argv[0], property->oprom_size);

  return 0;
}
