#include "internal.h"
#include <stdio.h>
#include <fdisk.h>

const char	scan_partitions_usage[] = "scan_partitions partition-type ...";

static void 
print_partitions(unsigned char type) {
  struct fdisk_partition *p;
  
  p = fdisk_partitions[type];

  while (NULL != p) {
    printf("%s ",p->name);
    p = p->next_by_type;
  }
}

static void
print_partitions_by_type_name(char *type_name) {
  if (0 == strcmp(type_name, "msdos")) {
    print_partitions(0x4);
    print_partitions(0x6);
  }
  if (0 == strcmp(type_name, "extended")) {
    print_partitions(0x5);
  }
  if (0 == strcmp(type_name, "minix")) {
    print_partitions(0x81);
  }
  if (0 == strcmp(type_name, "swap")) {
    print_partitions(0x82);
  }
  if (0 == strcmp(type_name, "ext2")) {
    print_partitions(0x83);
  }
}

extern int
scan_partitions_main(struct FileInfo * i, int argc, char * * argv)
{
  fdisk_reread();

  if (argc == 1) {
    int j;

    for (j=0; j<=255; j++) {
      print_partitions(j);
    }
  } else {
    int j;

    for (j=1; j<argc; j++) {
      print_partitions_by_type_name(argv[j]);
    }
  }
  return(0);
}
