[comp.lang.c] Turbo C & qsort

pmswl@dcatla.UUCP (Scott W. Leonard) (02/04/89)

I hope one of you C wizards can help me.  I've written a directory
program using Turbo C 2.0.  I'm to the point of cleaning it up a
bit.  I currently have a file sort function, but it is a bit bulky
and time consuming.  I would really like to use Turbo's 'qsort'
function.  I've tried it about every way possible, but nothing
seems to work for me.

I would also like to know if it is possible to directly load my
'files' array from findfirst/next.  Right now I'm copying everything
from 'struct ffblk' to my own array which takes a little time.

I've included a sample program that is a simplified version of
the program/problem.

Any help at all will be appreciated.


/*   This is the sample.  Compiled with Turbo C 2.0 */

#define FA_RDWR     0
#define FA_ALL      FA_RDONLY+FA_HIDDEN+FA_SYSTEM+FA_ARCH+FA_RDWR+FA_DIREC
#define FILESMAX    512                 /* max. number of files */

#include    <alloc.h>
#include    <dir.h>
#include    <dos.h>
#include    <stdio.h>

struct{                                 /* this is the main structure */
    char  far   *names[FILESMAX];       /* stores file names in far */
    int         attrs[FILESMAX];        /* file attributes */
    unsigned    fil_time[FILESMAX];     /* file times */
    unsigned    fil_date[FILESMAX];     /* file dates */
    long        f_size[FILESMAX];       /* file sizes */
}files;

main()
{
    char    name2[ 80 ];                /* dummy var for displaying names */
    int ndx;                            /* pointer to file */
    int x;                              /* return from findfirst/next */
    int nfiles;                         /* total number of files found */
    register i;                         /* dummy for for loop */
    struct  ffblk r;                    /* struct used by findfirst/next */

    ndx = nfiles = 0;                   /* init pointers to beginning */

    x = findfirst( "*.*", &r, FA_ALL );             /* get first file   */
    while( !x ){                                    /* until no more   */
                    /* init. all to zero */
        files.attrs[ ndx ] = files.f_size[ ndx ] = 0;
        files.fil_time[ ndx ] = files.fil_date[ ndx ] = 0;

                                         /* copy theirs to mine */
        files.fil_time[ ndx ] = r.ff_ftime;
        files.fil_date[ ndx ] = r.ff_fdate;
        files.attrs[ ndx ] = r.ff_attrib;
        files.f_size[ ndx ] = r.ff_fsize;
        files.names[ ndx ] = farmalloc(strlen(r.ff_name) + 1);

                    /* only way I can figure to copy to far */
        for( i = 0 ; i <= strlen(r.ff_name) ; i++ )
            files.names[ndx][i] = r.ff_name[i];

        ndx++;                                     /* inc. file pointer     */
        nfiles++;                                  /* inc. total # of files */
        x = findnext( &r );                        /* get next file         */
    }

/*    qsort( ?, ?, ?, ? );   */                    /* the big question mark */

    for( i = 0 ; i < nfiles ; i++ ){               /* just for display */
        sprintf( name2, "%Fs", files.names[ i ] ); /* purpose */
        printf( "%s\n", name2 );
    }
}
-- 
========================================================================== 
 Scott W. Leonard
 Digital Communications Associates
 Alpharetta, Georgia 30201    pmswl@dcatla.com   or   gatech!dcatla!pmswl

ked@garnet.berkeley.edu (Earl H. Kinmonth) (02/04/89)

In article <13722@dcatla.UUCP> pmswl@sunb.UUCP (Scott W. Leonard) writes:
>
>
>
>I hope one of you C wizards can help me.  I've written a directory
>program using Turbo C 2.0.  I'm to the point of cleaning it up a
>bit.  I currently have a file sort function, but it is a bit bulky
>and time consuming.  I would really like to use Turbo's 'qsort'
>function.  I've tried it about every way possible, but nothing
>seems to work for me.

I'm not going to take the time to read through your code.  I have enough
buggy code of my own to look at. -:)  I will give you one major hint.
What qsort passes to the sort function has ONE MORE LEVEL OF INDIRECTION
than you might expect.  For example, if you are trying to sort

char	*barf[MAXBARF];

the qsort call is

qsort( (char *) barf, MAXBARF, sizeof(char **), sortfunc)

and

sortfunc(as,bs)
char	**as;
char	**bs;
{
	return(strcmp(*as,*bs));
}

Happy sorting.