/*
 *   Copyright (c) 1999 David Parsons. All rights reserved.
 *   
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *  3. All advertising materials mentioning features or use of this
 *     software must display the following acknowledgement:
 *     
 *   This product includes software developed by David Parsons
 *   (orc@pell.chi.il.us)
 *
 *  4. My name may not be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *     
 *  THIS SOFTWARE IS PROVIDED BY DAVID PARSONS ``AS IS'' AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 *  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID
 *  PARSONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 *  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 *  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 *  THE POSSIBILITY OF SUCH DAMAGE.
 */


/*
 * par: partition table handling.
 */


#include <stdio.h>
#include <malloc.h>
#include <basis/rpopen.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <ndialog.h>

#include "util.h"
#include "pathnames.h"
#include "part.h"
#include "media.h"


/* IS_OK() picks disks that exist and are not either CDROMs or removable
 */
#define IS_OK(x)	( (x & ID_EXIST) && !(x & (ID_CDROM|ID_REMOVABLE)) )


/*
 * get_disk_par() gets the partition table for a given disk (not needed
 *                on a system with a working devfs)
 */
void
get_disk_par(char *device, int *nr_arry, struct raw_par **arry)
{
    struct raw_par curpar;
    FILE *pipe;
    char bfr[200];
    char pardev[40];
    int rc;
    static char *cfdiskargs[] = { "cfdisk", "-Pt", 0/* device slot */, 0 };
    int parno, flags, id, endcyl, nrsects;

    cfdiskargs[2] = device;

    if (*arry == 0)
	*arry = malloc(1);

    if ( (pipe = rpopen(PATH_CFDISK, cfdiskargs, "r")) != 0) {
	while (fgets(bfr, sizeof bfr, pipe)) {
	    rc = sscanf(bfr, "%d 0x%x %*d %*d %*d 0x%x %*d %*d %d %*d %d",
			    &parno, &flags, &id, &endcyl, &nrsects);
	    if (rc != 5 || parno <=  0)
		continue;

	    sprintf(pardev, "%s%d", device, parno);
	    curpar.par_dev = strdup(pardev);
	    curpar.par_blocks = nrsects/2;
	    curpar.par_bootflag = flags & 0x80;
	    curpar.par_fstype = id;
	    curpar.par_lastcyl = endcyl;

	    fprintf(stderr, "%s: %ld blocks, lastcyl=%d, type=%x\n",
			curpar.par_dev, curpar.par_blocks,
			curpar.par_lastcyl, curpar.par_fstype);
	    (*arry) = realloc(*arry, (1+*nr_arry)*sizeof (*arry)[0]);
	    (*arry)[*nr_arry] = curpar;
	    (*nr_arry)++;
	}
	rpclose(pipe);
    }
} /* get_disk_par */


/*
 * get_par_list(): gets a list of all the partitions on disks in a disk list.
 */
void
get_par_list(LIA disk_list, int *nr_par, struct raw_par **par)
{
    int x;

    *par = 0;
    *nr_par = 0;
    for (x=0; x < LIAcount(disk_list); x++)
	get_disk_par(LIAlist(disk_list)[x].id, nr_par, par);
} /* get_par_list */


/*
 * delete_par_list() wipes out the contents of a raw_par array
 */
void
delete_par_list(int nr_par, struct raw_par *par)
{
    int x;

    for (x=0; x < nr_par; x++)
	free(par[x].par_dev);
    free(par);
} /* delete_par_list */


/*
 * get_disk_list() -- build a disk list containing all the disks we have NOT
 *                    autoconfigured.
 * This disklist is returned as a ListItem array, which the user may dispose
 * of with free() when they're done with it.
 */
LIA
get_disk_list(int rootdisk)
{
    int x;
    LIA list;

    list = newLIA(0,0);

    for (x=0; x < NrDisk; x++) {
	/* if this is the root device and we've autoconfigured it,
	 * don't add to the list.
	 */
	if (x == rootdisk)
	    continue;

	/* If it's doesn't exist or isn't a disk, don't add it */
	if (IS_OK(Disk[x].flags))
	    addToLIA(list, Disk[x].device, strdup(describe_media(&Disk[x])), 0);
    }
    return list;
} /* get_disk_list */


/*
 * delete_disk_list() wipes out a previously defined disk list
 */
void
delete_disk_list(LIA disk_list)
{
    int x;

    for (x=0; x < LIAcount(disk_list); x++)
	free(LIAlist(disk_list)[x].item);
    free(LIAlist(disk_list));
} /* delete_disk_list */
