home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume26
/
maint
/
part01
/
sort.c
< prev
Wrap
C/C++ Source or Header
|
1992-05-13
|
10KB
|
410 lines
/******************************************************************************
*******************************************************************************
Site: Western Michigan University Academic Computer Center
System: Directory/File System Maintenance
Program: maint
Version=01 Level=00 01/24/92 Leonard J. Peirce
Purpose: Sort routines for the different sort options.
Arguments: See individual routines.
External variables: None
External functions:
Defined: date_qsort, name_qsort, size_qsort, sort_files
Called: None
Files accessed: See individual routines.
Return codes: See individual routines.
Compiling instructions: See Makefile
Linking instructions: See Makefile
Other information: (C) Copyright 1992, Leonard J. Peirce
********************************************************************************
*******************************************************************************/
/******************************************************************************/
/* */
/* # I N C L U D E F I L E S */
/* */
/******************************************************************************/
#include <string.h>
#include "maint.h"
/******************************************************************************/
/* */
/* # D E F I N E S */
/* */
/******************************************************************************/
/******************************************************************************/
/* */
/* S T R U C T U R E S , U N I O N S , T Y P E D E F S */
/* */
/******************************************************************************/
/******************************************************************************/
/* */
/* E X T E R N A L D E F I N I T I O N S & D E C L A R A T I O N S */
/* */
/******************************************************************************/
extern ENT_DEF *baseptr;
void date_qsort(),
size_qsort(),
name_qsort(),
sort_files();
/******************************************************************************/
/* */
/* S T A T I C D E F I N I T I O N S & D E C L A R A T I O N S */
/* */
/******************************************************************************/
static void swap_entries();
/*******************************************************************************
********************************************************************************
Function: size_qsort
Purpose: Sort the directory entries by size.
Algorithm for quicksort from "Fundamentals of Data Structures",
Ellis Horowitz and Sartaj Sahni, page 137.
Global variables:
Name Examine/Modify/Use/Read/Write
---- -----------------------------
baseptr X
Return Codes:
Code Reason
---- ------
none
********************************************************************************
*******************************************************************************/
void size_qsort(m,n)
/******* FORMAL PARAMETERS *******/
register short m, /* lower-bound of sub-array to be */
/* sorted */
n; /* upper-bound of sub-array to be */
/* sorted */
{ /*** size_qsort ***/
/******** LOCAL VARIABLES ********/
register short i, /* loop and array index */
j; /* " " " " */
static u_long key; /* key size entry for sorting */
if(m < n)
{
i = m;
j = n + 1;
key = baseptr[m].size;
for(;;)
{
do
{
++i;
}
while(baseptr[i].size < key);
do
{
--j;
}
while(baseptr[j].size > key);
if(i < j)
{
/* swap the file entries */
swap_entries(&baseptr[i],&baseptr[j]);
}
else
break;
} /* for(;;) */
swap_entries(&baseptr[m],&baseptr[j]);
size_qsort(m,j-1);
size_qsort(j+1,n);
} /* if(m < n) */
return;
} /*** size_qsort ***/
/*******************************************************************************
********************************************************************************
Function: date_qsort
Purpose: Sort the directory entries by last access date.
Algorithm for quicksort from "Fundamentals of Data Structures",
Ellis Horowitz and Sartaj Sahni, page 137.
Global variables:
Name Examine/Modify/Use/Read/Write
---- -----------------------------
baseptr X
Return Codes:
Code Reason
---- ------
none
********************************************************************************
*******************************************************************************/
void date_qsort(m,n)
/******* FORMAL PARAMETERS *******/
register short m, /* lower-bound of sub-array to be */
/* sorted */
n; /* upper-bound of sub-array to be */
/* sorted */
{ /*** date_qsort ***/
/******** LOCAL VARIABLES ********/
register short i, /* loop and array index */
j; /* " " " " */
static time_t key; /* key size entry for sorting */
if(m < n)
{
i = m;
j = n + 1;
key = baseptr[m].time;
for(;;)
{
do
{
++i;
}
while(baseptr[i].time < key);
do
{
--j;
}
while(baseptr[j].time > key);
if(i < j)
{
/* swap the file entries */
swap_entries(&baseptr[i],&baseptr[j]);
}
else
break;
} /* for(;;) */
swap_entries(&baseptr[m],&baseptr[j]);
date_qsort(m,j-1);
date_qsort(j+1,n);
} /* if(m < n) */
return;
} /*** date_qsort ***/
/*******************************************************************************
********************************************************************************
Function: swap_entries
Purpose: Swap two file entries.
Global variables:
Name Examine/Modify/Use/Read/Write
---- -----------------------------
none
Return Codes:
Code Reason
---- ------
none
********************************************************************************
*******************************************************************************/
static void swap_entries(a,b)
/******* FORMAL PARAMETERS *******/
ENT_DEF *a, /* first to be swapped */
*b; /* other entry to be swapped */
{ /*** swap_entries ***/
/******** LOCAL VARIABLES ********/
static ENT_DEF temp; /* temporary entry for swapping */
memcpy((char *) &temp,(char *) a, sizeof(temp));
memcpy((char *) a,(char *) b, sizeof(temp));
memcpy((char *) b,(char *) &temp, sizeof(temp));
} /*** swap_entries ***/
/*******************************************************************************
********************************************************************************
Function: name_qsort
Purpose: Sort the directory entries by filename.
Algorithm for quicksort from "Fundamentals of Data Structures",
Ellis Horowitz and Sartaj Sahni, page 137.
Global variables:
Name Examine/Modify/Use/Read/Write
---- -----------------------------
baseptr X
Return Codes:
Code Reason
---- ------
none
********************************************************************************
*******************************************************************************/
void name_qsort(m,n)
/******* FORMAL PARAMETERS *******/
register short m, /* lower-bound of sub-array to be */
/* sorted */
n; /* upper-bound of sub-array to be */
/* sorted */
{ /*** name_qsort ***/
/******** LOCAL VARIABLES ********/
register short i, /* loop and array index */
j; /* " " " " */
static char *key; /* pointer to key value */
if(m < n)
{
i = m;
j = n + 1;
key = (baseptr+m)->filename; /* get pointer to filename key */
for(;;)
{
do
{
++i;
}
while(strcmp((baseptr+i)->filename,key) < 0);
do
{
--j;
}
while(strcmp((baseptr+j)->filename,key) > 0);
if(i < j)
{
/* swap the pointers to the filenames and the flags that indicate
* whether or not the filename is too long for all of it to be
* displayed on the screen
*/
swap_entries(&baseptr[i],&baseptr[j]);
}
else
break;
} /* for(;;) */
swap_entries(&baseptr[m],&baseptr[j]);
name_qsort(m,j-1);
name_qsort(j+1,n);
} /* if(m < n) */
return;
} /*** name_qsort ***/
/*******************************************************************************
********************************************************************************
Function: sort_files
Purpose: Sort the directory based on the sort type.
Global variables:
Name Examine/Modify/Use/Read/Write
---- -----------------------------
baseptr X
Return Codes:
Code Reason
---- ------
none
********************************************************************************
*******************************************************************************/
void sort_files(num_file,sort_type)
/******* FORMAL PARAMETERS *******/
short num_file, /* number of files in directory */
sort_type; /* field to sort on */
{ /*** sort_files ***/
switch(sort_type)
{
case(FILENAME):
name_qsort(0,num_file);
break;
case(DATE):
date_qsort(0,num_file);
break;
case(SIZE):
size_qsort(0,num_file);
break;
default:
break;
}
return;
} /*** sort_files ***/