Category : HD Utilities
Archive   : PLUG12.ZIP
Filename : DIRUTIL.C

 
Output of file : DIRUTIL.C contained in archive : PLUG12.ZIP

/************************************************************************/
/* */
/* Functions Dealing with Directory Structures - DIRUTIL.C */
/* */
/* Version 1.0, July 1987 */
/* Version 1.2, August 1987 */
/* */
/* Compiled under Lattice C version 3.20 (Large model -mls) */
/* */
/* Author: Lawrence E. Himes */
/* */
/* Rev 1.2 - getmem() calls changed to malloc() for portability. */
/* Allow read-only files to be moved. */
/* Add dir_pl structure to remove reliance on getmem() */
/* "trick" during file move. */
/* */
/************************************************************************/

#include
#include
#include "dglobal.h"
#include "dir.h"

struct dir_ch *dhead, *dtail;
int totalde = 0; /* Total directory ents */

/****************************************************************/
/* */
/* loadroot () - Load up the root directory */
/* */
/****************************************************************/

loadroot ()
{
int r, x;
int c;
unsigned s, e;
struct dir_ch *d;
struct dir *de;

s = Frootsec; /* First Root Sector */
e = s + Rootsecs;
for (dtail = NULL ; s < e ; s++) {
d = (struct dir_ch *) (malloc (sizeof (struct dir_ch)));
if (!d) { r = Nomem; break; }
if (!dtail) dhead = d;
else dtail->dirnext = d;
dtail = d;
d->secno = s;
de = d->dirent;
r = absdio (0, drive, de, s, 1);
if (r) break;
for (x = 0; x < 16; de++, x++ ) {
if (!(c = de->fname [0])) goto _end;
if ((c != 0xE5) && (c != '.')) ++totalde;
}
}
_end: return r;
}

/****************************************************************/
/* */
/* loadsubs () - Load all subdirectories */
/* */
/****************************************************************/

loadsubs ()
{
int r, e, x, c;
struct dir_ch *d;
struct dir *de;

for (r = x = 0, d = dhead; x < totalde; ) {
de = d->dirent;
for (e = 0; e < 16 ; e++, de++ ) {
if (!(c = de->fname [0])) break;
if ((c == 0xE5) || (c == '.')) continue;
x++;
if (de->attribute & subdir) {
r = loadsubd (de->fcluster);
if (r) goto _end;
}
}
d = d->dirnext;
}
dtail->dirnext = NULL;
_end: return r;
}

/****************************************************************/
/* */
/* loadsubd () - Load one subdirectory */
/* */
/****************************************************************/

loadsubd (clu)
unsigned clu; /* Starting cluster */
{
int r, e, x;
int c;
unsigned s;
struct dir_ch *d;
struct dir *de;

for ( r = 0 ; !r ; ) {
s = cvtctos (clu); /* Get base sector # */
e = 0;
while (e++ < clsecs) {
d = (struct dir_ch *) (malloc (sizeof (struct dir_ch)));
if (!d) { r = Nomem; goto _end; }
dtail->dirnext = d;
dtail = d;
d->secno = s;
de = d->dirent;
r = absdio (0, drive, de, s++, 1);
if (r) break;
for (x = 0; x < 16; de++, x++ ) {
if (!(c = de->fname [0])) goto _end;
if ((c != 0xE5) && (c != '.')) ++totalde;
}
}
clu = getfate (clu); /* Get next cluster */
if (clu >= Cutoff) break; /* End of chain */
}
_end: return r;
}

struct dir_pl *dpl = NULL; /* Directory pointer list */

/****************************************************************/
/* */
/* blddpl () - Build directory pointer list */
/* */
/****************************************************************/

blddpl ()
{
int r, e;
int c;
struct dir_ch *d;
struct dir *de;
struct dir_pl *pl;

dpl = (struct dir_pl *) (malloc (totalde * sizeof (struct dir_pl)));
if (!dpl) return Nomem;
pl = dpl;
for (r = 0, d = dhead; d ; ) {
de = d->dirent;
for (e = 0; e < 16 ; e++, de++ ) {
if (!(c = de->fname [0])) break;
if ((c != 0xE5) && (c != '.')) {
pl->dp = de;
pl->inx = e;
pl++;
}
}
d = d->dirnext;
}
return 0;
}

/****************************************************************/
/* */
/* compclu () - Compare cluster numbers for sort */
/* */
/****************************************************************/

compclu (a, b) /* Descending sort */
struct dir **a, **b;
{
return ((*a)->fcluster < (*b)->fcluster) ? 1 : -1;
}

/****************************************************************/
/* */
/* sortdpl () - Sort directory pointer list */
/* */
/****************************************************************/

sortdpl ()
{
qsort (dpl, totalde, sizeof (struct dir_pl), &compclu);
return 0;
}

/****************************************************************/
/* */
/* bestfit () - Find File which best fits a hole */
/* */
/* Rev 1.2 - Allow Read-only Files to be moved also. */
/* */
/****************************************************************/

bestfit (hole, loc)
unsigned hole; /* Size of hole */
unsigned loc; /* Location (base cluster) of hole */
{
int x, bf;
unsigned csm1, e, bfs;
struct dir *de;

csm1 = clsize - 1;
bfs = 0;
for (bf = -1, x = 0; x < totalde ; x++ ) {
if (!(de = dpl[x].dp)) continue;
if (de->fcluster < loc) break;
if (!(de->attribute & 0x1E)) { /* Includes Read-only */
e = (de->fsize + csm1) / clsize;
if ((e > hole) || (e <= bfs)) continue;
bf = x;
if (hole == e) break;
bfs = e;
}
}
return bf;
}

/****************************************************************/
/* */
/* gnde () - Get standard format name from Dir ent */
/* */
/****************************************************************/

void gnde (de, s)
struct dir *de;
char *s;
{
int c;

for ( c = 0; c < 8 ; s++ ) {
*s = de->fname [c++];
if ((*s == ' ') || (*s == 0)) break;
}
*s++ = '.';
for ( c = 0; c < 3 ; s++ ) {
*s = de->fextn [c++];
if ((*s == ' ') || (*s == 0)) break;
}
if (*(s-1) == '.') --s;
*s = 0;
}



  3 Responses to “Category : HD Utilities
Archive   : PLUG12.ZIP
Filename : DIRUTIL.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/