Subversion Repositories SvarDOS

Rev

Rev 2051 | Rev 2053 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2019 mateusz.vi 1
/****************************************************************************
2
 
3
  TREE - Graphically displays the directory structure of a drive or path
4
 
5
****************************************************************************/
6
 
7
#define VERSION "1.04"
8
 
9
/****************************************************************************
10
 
11
  Written by: Kenneth J. Davis
12
  Date:       August, 2000
13
  Updated:    September, 2000; October, 2000; November, 2000; January, 2001;
14
              May, 2004; Sept, 2005
15
  Contact:    jeremyd@computer.org
16
 
17
 
18
Copyright (c): Public Domain [United States Definition]
19
 
20
Permission is hereby granted, free of charge, to any person obtaining a copy
21
of this software and associated documentation files (the "Software"), to deal
22
in the Software without restriction, including without limitation the rights
23
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24
copies of the Software, and to permit persons to whom the Software is
25
furnished to do so, subject to the following conditions:
26
 
27
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30
NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR AUTHORS BE
31
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
32
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
33
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
34
DEALINGS IN THE SOFTWARE.
35
 
36
****************************************************************************/
37
 
38
 
39
/* Include files */
2034 mateusz.vi 40
#include <dos.h>
2019 mateusz.vi 41
#include <stdlib.h>
42
#include <stdio.h>
43
#include <string.h>
44
 
45
#include "stack.h"
46
 
2035 mateusz.vi 47
/* DOS disk accesses */
48
#include "dosdisk.h"
2019 mateusz.vi 49
 
50
 
51
/* The default extended forms of the lines used. */
52
#define VERTBAR_STR  "\xB3   "                 /* |    */
53
#define TBAR_HORZBAR_STR "\xC3\xC4\xC4\xC4"    /* +--- */
54
#define CBAR_HORZBAR_STR "\xC0\xC4\xC4\xC4"    /* \--- */
55
 
56
/* Global flags */
57
#define SHOWFILESON    1  /* Display names of files in directories       */
58
#define SHOWFILESOFF   0  /* Don't display names of files in directories */
59
 
60
#define ASCIICHARS     1  /* Use ASCII [7bit] characters                 */
61
#define EXTENDEDCHARS  0  /* Use extended ASCII [8bit] characters        */
62
 
63
#define NOPAUSE        0  /* Default, don't pause after screenfull       */
64
#define PAUSE          1  /* Wait for keypress after each page           */
65
 
66
 
67
/* Global variables */
68
short showFiles = SHOWFILESOFF;
69
short charSet = EXTENDEDCHARS;
70
short pause = NOPAUSE;
71
 
72
short dspAll = 0;  /* if nonzero includes HIDDEN & SYSTEM files in output */
73
short dspSize = 0; /* if nonzero displays filesizes                       */
74
short dspAttr = 0; /* if nonzero displays file attributes [DACESHRBP]     */
75
short dspSumDirs = 0; /* show count of subdirectories  (per dir and total)*/
76
 
77
 
78
/* maintains total count, for > 4billion dirs, use a __int64 */
79
unsigned long totalSubDirCnt = 0;
80
 
81
 
82
/* text window size, used to determine when to pause,
83
   Note: rows is total rows available - 2
2022 mateusz.vi 84
   1 is for pause message and then one to prevent auto scroll up
2019 mateusz.vi 85
*/
86
short cols=80, rows=23;   /* determined these on startup (when possible)  */
87
 
88
 
89
 
90
/* Global constants */
91
#define SERIALLEN 16      /* Defines max size of volume & serial number   */
92
#define VOLLEN 128
93
 
94
#define MAXBUF 1024       /* Must be larger than max file path length     */
95
char path[MAXBUF];        /* Path to begin search from, default=current   */
96
 
97
#define MAXPADLEN (MAXBUF*2) /* Must be large enough to hold the maximum padding */
98
/* (MAXBUF/2)*4 == (max path len / min 2chars dirs "?\") * 4chars per padding    */
99
 
100
/* The maximum size any line of text output can be, including room for '\0'*/
101
#define MAXLINE 160        /* Increased to fit two lines for translations  */
102
 
103
 
104
/* The hard coded strings used by the following show functions.            */
105
 
106
/* common to many functions [Set 1] */
107
char newLine[MAXLINE] = "\n";
108
 
109
/* showUsage [Set 2] - Each %c will be replaced with proper switch/option */
110
char treeDescription[MAXLINE] = "Graphically displays the directory structure of a drive or path.\n";
111
char treeUsage[MAXLINE] =       "TREE [drive:][path] [%c%c] [%c%c]\n";
112
char treeFOption[MAXLINE] =     "   %c%c   Display the names of the files in each directory.\n";
113
char treeAOption[MAXLINE] =     "   %c%c   Use ASCII instead of extended characters.\n";
114
 
115
/* showInvalidUsage [Set 3] */
116
char invalidOption[MAXLINE] = "Invalid switch - %s\n";  /* Must include the %s for option given. */
117
char useTreeHelp[MAXLINE] =   "Use TREE %c? for usage information.\n"; /* %c replaced with switch */
118
 
119
/* showVersionInfo [Set 4] */
120
/* also uses treeDescription */
121
char treeGoal[MAXLINE] =      "Written to work with FreeDOS\n";
122
char treePlatforms[MAXLINE] = "Win32(c) console and DOS with LFN support.\n";
123
char version[MAXLINE] =       "Version %s\n"; /* Must include the %s for version string. */
124
char writtenBy[MAXLINE] =     "Written by: Kenneth J. Davis\n";
125
char writtenDate[MAXLINE] =   "Date:       2000, 2001, 2004\n";
126
char contact[MAXLINE] =       "Contact:    jeremyd@computer.org\n";
127
char copyright[MAXLINE] =     "Copyright (c): Public Domain [United States Definition]\n";
128
 
129
/* showInvalidDrive [Set 5] */
130
char invalidDrive[MAXLINE] = "Invalid drive specification\n";
131
 
132
/* showInvalidPath [Set 6] */
133
char invalidPath[MAXLINE] = "Invalid path - %s\n"; /* Must include %s for the invalid path given. */
134
 
135
/* Misc Error messages [Set 7] */
2052 mateusz.vi 136
 
2019 mateusz.vi 137
/* showOutOfMemory */
138
/* %s required to display what directory we were processing when ran out of memory. */
139
char outOfMemory[MAXLINE] = "Out of memory on subdirectory: %s\n";
140
 
141
/* main [Set 1] */
142
char pathListingNoLabel[MAXLINE] = "Directory PATH listing\n";
143
char pathListingWithLabel[MAXLINE] = "Directory PATH listing for Volume %s\n"; /* %s for label */
144
char serialNumber[MAXLINE] = "Volume serial number is %s\n"; /* Must include %s for serial #   */
145
char noSubDirs[MAXLINE] = "No subdirectories exist\n\n";
146
char pauseMsg[MAXLINE]  = " --- Press any key to continue ---\n";
147
 
148
/* Option Processing - parseArguments [Set 8]      */
149
char optionchar1 = '/';  /* Primary character used to determine option follows  */
150
char optionchar2 = '-';  /* Secondary character used to determine option follows  */
151
const char OptShowFiles[2] = { 'F', 'f' };  /* Show files */
152
const char OptUseASCII[2]  = { 'A', 'a' };  /* Use ASCII only */
153
const char OptVersion[2]   = { 'V', 'v' };  /* Version information */
154
const char OptPause[2]     = { 'P', 'p' };  /* Pause after each page (screenfull) */
155
const char OptDisplay[2]   = { 'D', 'd' };  /* modify Display settings */
156
 
157
 
158
/* Procedures */
159
 
160
 
2044 mateusz.vi 161
/* returns the current drive (A=0, B=1, etc) */
162
static unsigned char getdrive(void);
163
#pragma aux getdrive = \
164
"mov ah, 0x19" \
165
"int 0x21" \
166
modify [ah] \
167
value [al]
168
 
169
 
2049 mateusz.vi 170
/* waits for a keypress, flushes keyb buffer, returns nothing */
171
static void waitkey(void);
172
#pragma aux waitkey = \
173
"mov ah, 0x08" \
174
"int 0x21" \
175
/* flush keyb buffer in case it was an extended key */ \
176
"mov ax, 0x0C0B" \
177
"int 0x21" \
178
modify [ax]
179
 
180
 
2050 mateusz.vi 181
/* checks if stdout appears to be redirected. returns 0 if not, non-zero otherwise. */
2051 mateusz.vi 182
static unsigned char is_stdout_redirected(void);
183
#pragma aux is_stdout_redirected = \
184
"mov ax, 0x4400"    /* DOS 2+, IOCTL Get Device Info */            \
185
"mov bx, 0x0001"    /* file handle (stdout) */                     \
186
"int 0x21" \
187
"jc DONE"           /* on error AL contains a non-zero err code */ \
188
"and dl, 0x80"      /* bit 7 of DL is the "CHAR" flag */           \
189
"xor dl, 0x80"      /* return 0 if CHAR bit is set */              \
190
"mov al, dl" \
191
"DONE:" \
192
modify [ax bx dx] \
193
value [al]
2034 mateusz.vi 194
 
195
 
2052 mateusz.vi 196
static int truename(char *path, const char *origpath) {
197
  unsigned short origpath_seg = FP_SEG(origpath);
198
  unsigned short origpath_off = FP_OFF(origpath);
199
  unsigned short dstpath_seg = FP_SEG(path);
200
  unsigned short dstpath_off = FP_OFF(path);
201
  unsigned char cflag = 0;
202
 
203
  /* resolve path with truename */
204
  _asm {
205
    push ax
206
    push si
207
    push di
208
    push es
209
    push ds
210
 
211
    mov ah, 0x60          /* AH = 0x60 -> TRUENAME */
212
    mov di, dstpath_off   /* ES:DI -> dst buffer */
213
    mov es, dstpath_seg
214
    mov si, origpath_off  /* DS:SI -> src path */
215
    mov ds, origpath_seg
216
    int 0x21
217
    jnc DONE
218
    mov cflag, 1
219
    DONE:
220
 
221
    pop ds
222
    pop es
223
    pop di
224
    pop si
225
    pop ax
226
  }
227
 
228
  return(cflag);
229
}
230
 
231
 
2019 mateusz.vi 232
/* sets rows & cols to size of actual console window
233
 * force NOPAUSE if appears output redirected to a file or
234
 * piped to another program
235
 * Uses hard coded defaults and leaves pause flag unchanged
236
 * if unable to obtain information.
237
 */
2034 mateusz.vi 238
static void getConsoleSize(void) {
239
  unsigned short far *bios_cols = (unsigned short far *)MK_FP(0x40,0x4A);
240
  unsigned short far *bios_size = (unsigned short far *)MK_FP(0x40,0x4C);
2019 mateusz.vi 241
 
2050 mateusz.vi 242
  if (is_stdout_redirected() != 0) {
243
    /* e.g. redirected to a file, tree > filelist.txt */
244
    /* Output to a file or program, so no screen to fill (no max cols or rows) */
2034 mateusz.vi 245
      pause = NOPAUSE;   /* so ignore request to pause */
2050 mateusz.vi 246
  } else { /* e.g. the console */
247
    if ((*bios_cols == 0) || (*bios_size == 0)) { /* MDA does not report size */
248
      cols = 80;
249
      rows = 23;
250
    } else {
251
      cols = *bios_cols;
252
      rows = *bios_size / cols / 2;
253
      if (rows > 2) rows -= 2; /* necessary to keep screen from scrolling */
254
    }
2019 mateusz.vi 255
  }
256
}
257
 
2034 mateusz.vi 258
 
2019 mateusz.vi 259
/* when pause == NOPAUSE then identical to printf,
260
   otherwise counts lines printed and pauses as needed.
261
   Should be used for all messages printed that do not
262
   immediately exit afterwards (else printf may be used).
263
   May display N too many lines before pause if line is
264
   printed that exceeds cols [N=linelen%cols] and lacks
265
   any newlines (but this should not occur in tree).
266
*/
267
#include <stdarg.h>  /* va_list, va_start, va_end */
2037 mateusz.vi 268
static int pprintf(const char *msg, ...) {
2030 mateusz.vi 269
  static int lineCnt = -1;
2019 mateusz.vi 270
  static int lineCol = 0;
271
  va_list argptr;
272
  int cnt;
273
  char buffer[MAXBUF];
274
 
2030 mateusz.vi 275
  if (lineCnt == -1) lineCnt = rows;
276
 
2019 mateusz.vi 277
  va_start(argptr, msg);
278
  cnt = vsprintf(buffer, msg, argptr);
279
  va_end(argptr);
280
 
281
  if (pause == PAUSE)
282
  {
2030 mateusz.vi 283
    char *l = buffer;
284
    char *t;
2019 mateusz.vi 285
    /* cycle through counting newlines and lines > cols */
2030 mateusz.vi 286
    for (t = strchr(l, '\n'); t != NULL; t = strchr(l, '\n'))
2019 mateusz.vi 287
    {
2030 mateusz.vi 288
      char c;
2019 mateusz.vi 289
      t++;             /* point to character after newline */
2030 mateusz.vi 290
      c = *t;          /* store current value */
2019 mateusz.vi 291
      *t = '\0';       /* mark as end of string */
292
 
293
      /* print all but last line of a string that wraps across rows */
294
      /* adjusting for partial lines printed without the newlines   */
295
      while (strlen(l)+lineCol > cols)
296
      {
297
        char c = l[cols-lineCol];
298
        l[cols-lineCol] = '\0';
299
        printf("%s", l);
300
        l[cols-lineCol] = c;
301
        l += cols-lineCol;
302
 
303
        lineCnt--;  lineCol = 0;
2049 mateusz.vi 304
        if (!lineCnt) { lineCnt= rows;  fflush(NULL);  fprintf(stderr, "%s", pauseMsg);  waitkey(); }
2019 mateusz.vi 305
      }
306
 
307
      printf("%s", l); /* print out this line */
308
      *t = c;          /* restore value */
309
      l = t;           /* mark beginning of next line */
310
 
311
      lineCnt--;  lineCol = 0;
2049 mateusz.vi 312
      if (!lineCnt) { lineCnt= rows;  fflush(NULL);  fprintf(stderr, "%s", pauseMsg);  waitkey(); }
2019 mateusz.vi 313
    }
314
    printf("%s", l);   /* print rest of string that lacks newline */
315
    lineCol = strlen(l);
316
  }
317
  else  /* NOPAUSE */
318
    printf("%s", buffer);
319
 
320
  return cnt;
321
}
322
 
323
 
324
/* Displays to user valid options then exits program indicating no error */
2037 mateusz.vi 325
static void showUsage(void) {
2019 mateusz.vi 326
  printf("%s%s%s%s", treeDescription, newLine, treeUsage, newLine);
327
  printf("%s%s%s", treeFOption, treeAOption, newLine);
328
  exit(1);
329
}
330
 
331
 
332
/* Displays error message then exits indicating error */
2037 mateusz.vi 333
static void showInvalidUsage(char * badOption) {
2019 mateusz.vi 334
  printf(invalidOption, badOption);
335
  printf("%s%s", useTreeHelp, newLine);
336
  exit(1);
337
}
338
 
339
 
340
/* Displays author, copyright, etc info, then exits indicating no error. */
2037 mateusz.vi 341
static void showVersionInfo(void) {
2019 mateusz.vi 342
  printf("%s%s%s%s%s", treeDescription, newLine, treeGoal, treePlatforms, newLine);
343
  printf(version, VERSION);
344
  printf("%s%s%s%s%s", writtenBy, writtenDate, contact, newLine, newLine);
345
  printf("%s%s", copyright, newLine);
346
  exit(1);
347
}
348
 
349
 
350
/* Displays error messge for invalid drives and exits */
2037 mateusz.vi 351
static void showInvalidDrive(void) {
2019 mateusz.vi 352
  printf(invalidDrive);
353
  exit(1);
354
}
355
 
356
 
357
/* Takes a fullpath, splits into drive (C:, or \\server\share) and path */
2037 mateusz.vi 358
static void splitpath(char *fullpath, char *drive, char *path);
2019 mateusz.vi 359
 
360
/**
361
 * Takes a given path, strips any \ or / that may appear on the end.
362
 * Returns a pointer to its static buffer containing path
363
 * without trailing slash and any necessary display conversions.
364
 */
2037 mateusz.vi 365
static char *fixPathForDisplay(char *path);
2019 mateusz.vi 366
 
367
/* Displays error message for invalid path; Does NOT exit */
2037 mateusz.vi 368
static void showInvalidPath(char *path) {
2019 mateusz.vi 369
  char partialPath[MAXBUF], dummy[MAXBUF];
370
 
371
  pprintf("%s\n", path);
372
  splitpath(path, dummy, partialPath);
373
  pprintf(invalidPath, fixPathForDisplay(partialPath));
374
}
375
 
376
/* Displays error message for out of memory; Does NOT exit */
2037 mateusz.vi 377
static void showOutOfMemory(char *path) {
2019 mateusz.vi 378
  pprintf(outOfMemory, path);
379
}
380
 
381
 
382
/**
383
 * Takes a fullpath, splits into drive (C:, or \\server\share) and path
384
 * It assumes a colon as the 2nd character means drive specified,
385
 * a double slash \\ (\\, //, \/, or /\) specifies network share.
386
 * If neither drive nor network share, then assumes whole fullpath
387
 * is path, and sets drive to "".
388
 * If drive specified, then set drive to it and colon, eg "C:", with
389
 * the rest of fullpath being set in path.
390
 * If network share, the slash slash followed by the server name,
391
 * another slash and either the rest of fullpath or up to, but not
392
 * including, the next slash are placed in drive, eg "\\KJD\myshare";
393
 * the rest of the fullpath including the slash are placed in
394
 * path, eg "\mysubdir"; where fullpath is "\\KJD\myshare\mysubdir".
395
 * None of these may be NULL, and drive and path must be large
396
 * enough to hold fullpath.
397
 */
2037 mateusz.vi 398
static void splitpath(char *fullpath, char *drive, char *path) {
2027 mateusz.vi 399
  char *src = fullpath;
400
  char oldchar;
2019 mateusz.vi 401
 
402
  /* If either network share or path only starting at root directory */
403
  if ( (*src == '\\') || (*src == '/') )
404
  {
405
    src++;
406
 
407
    if ( (*src == '\\') || (*src == '/') ) /* network share */
408
    {
409
      src++;
410
 
411
      /* skip past server name */
412
      while ( (*src != '\\') && (*src != '/') && (*src != '\0') )
413
        src++;
414
 
415
      /* skip past slash (\ or /) separating  server from share */
416
      if (*src != '\0') src++;
417
 
418
      /* skip past share name */
419
      while ( (*src != '\\') && (*src != '/') && (*src != '\0') )
420
        src++;
421
 
422
      /* src points to start of path, either a slash or '\0' */
423
      oldchar = *src;
424
      *src = '\0';
425
 
426
      /* copy server name to drive */
427
      strcpy(drive, fullpath);
428
 
429
      /* restore character used to mark end of server name */
430
      *src = oldchar;
431
 
432
      /* copy path */
433
      strcpy(path, src);
434
    }
435
    else /* path only starting at root directory */
436
    {
437
      /* no drive, so set path to same as fullpath */
438
      strcpy(drive, "");
439
      strcpy(path, fullpath);
440
    }
441
  }
442
  else
443
  {
444
    if (*src != '\0') src++;
445
 
446
    /* Either drive and path or path only */
447
    if (*src == ':')
448
    {
449
      /* copy drive specified */
450
      *drive = *fullpath;  drive++;
451
      *drive = ':';        drive++;
452
      *drive = '\0';
453
 
454
      /* copy path */
455
      src++;
456
      strcpy(path, src);
457
    }
458
    else
459
    {
460
      /* no drive, so set path to same as fullpath */
461
      strcpy(drive, "");
462
      strcpy(path, fullpath);
463
    }
464
  }
465
}
466
 
467
 
468
/* Parses the command line and sets global variables. */
2037 mateusz.vi 469
static void parseArguments(int argc, char *argv[]) {
2027 mateusz.vi 470
  int i;     /* temp loop variable */
2019 mateusz.vi 471
 
472
  /* if no drive specified on command line, use current */
2052 mateusz.vi 473
  if (truename(path, ".") != 0) showInvalidDrive();
2019 mateusz.vi 474
 
475
  for (i = 1; i < argc; i++)
476
  {
477
    /* Check if user is giving an option or drive/path */
478
    if ((argv[i][0] == optionchar1) || (argv[i][0] == optionchar2) )
479
    {
480
      /* check multi character options 1st */
481
      if ((argv[i][1] == OptDisplay[0]) || (argv[i][1] == OptDisplay[1]))
482
      {
483
        switch (argv[i][2] & 0xDF)
484
        {
485
          case 'A' :       /*  /DA  display attributes */
486
            dspAttr = 1;
487
            break;
488
          case 'F' :       /*  /DF  display filesizes  */
489
            dspSize = 1;
490
            break;
491
          case 'H' :       /*  /DH  display hidden & system files (normally not shown) */
492
            dspAll = 1;
493
            break;
494
          case 'R' :       /*  /DR  display results at end */
495
            dspSumDirs = 1;
496
            break;
497
          default:
498
            showInvalidUsage(argv[i]);
499
        }
500
      }
501
      else /* a 1 character option (or invalid) */
502
      {
503
        if (argv[i][2] != '\0')
504
          showInvalidUsage(argv[i]);
505
 
506
        /* Must check both uppercase and lowercase                        */
507
        if ((argv[i][1] == OptShowFiles[0]) || (argv[i][1] == OptShowFiles[1]))
508
          showFiles = SHOWFILESON; /* set file display flag appropriately */
509
        else if ((argv[i][1] == OptUseASCII[0]) || (argv[i][1] == OptUseASCII[1]))
510
          charSet = ASCIICHARS;    /* set charset flag appropriately      */
511
        else if (argv[i][1] == '?')
512
          showUsage();             /* show usage info and exit            */
513
        else if ((argv[i][1] == OptVersion[0]) || (argv[i][1] == OptVersion[1]))
514
          showVersionInfo();       /* show version info and exit          */
515
        else if ((argv[i][1] == OptPause[0]) || (argv[i][1] == OptPause[1]))
516
          pause = PAUSE;     /* wait for keypress after each page (pause) */
517
        else /* Invalid or unknown option */
518
          showInvalidUsage(argv[i]);
519
      }
2052 mateusz.vi 520
    } else { /* should be a drive/path */
521
      if (truename(path, argv[i]) != 0) showInvalidDrive();
2019 mateusz.vi 522
    }
523
  }
524
}
525
 
526
 
527
/**
528
 * Fills in the serial and volume variables with the serial #
529
 * and volume found using path.
2022 mateusz.vi 530
 * If there is an error getting the volume & serial#, then an
2019 mateusz.vi 531
 * error message is displayed and the program exits.
532
 * Volume and/or serial # returned may be blank if the path specified
2022 mateusz.vi 533
 * does not contain them, or an error retrieving
2019 mateusz.vi 534
 * (ie UNC paths under DOS), but path is valid.
535
 */
2037 mateusz.vi 536
static void GetVolumeAndSerial(char *volume, char *serial, char *path) {
2019 mateusz.vi 537
  char rootPath[MAXBUF];
538
  char dummy[MAXBUF];
539
  union serialNumber {
540
    DWORD serialFull;
541
    struct {
542
      WORD a;
543
      WORD b;
544
    } serialParts;
545
  } serialNum;
546
 
547
  /* get drive letter or share server\name */
548
  splitpath(path, rootPath, dummy);
549
  strcat(rootPath, "\\");
550
 
2042 mateusz.vi 551
  if (GetVolumeInformation(rootPath, volume, VOLLEN, &serialNum.serialFull) == 0) {
552
    showInvalidDrive();
553
  }
2019 mateusz.vi 554
 
555
  if (serialNum.serialFull == 0)
556
    serial[0] = '\0';
557
  else
558
    sprintf(serial, "%04X:%04X",
559
      serialNum.serialParts.b, serialNum.serialParts.a);
560
}
561
 
562
 
563
/**
564
 * Stores directory information obtained from FindFirst/Next that
565
 * we may wish to make use of when displaying directory entry.
566
 * e.g. attribute, dates, etc.
567
 */
568
typedef struct DIRDATA
569
{
570
  DWORD subdirCnt;          /* how many subdirectories we have */
571
  DWORD fileCnt;            /* how many [normal] files we have */
2047 mateusz.vi 572
  unsigned short attrib;    /* Directory attributes            */
2019 mateusz.vi 573
} DIRDATA;
574
 
575
/**
576
 * Contains the information stored in a Stack necessary to allow
577
 * non-recursive function to display directory tree.
578
 */
579
typedef struct SUBDIRINFO
580
{
581
  struct SUBDIRINFO * parent; /* points to parent subdirectory                */
582
  char *currentpath;    /* Stores the full path this structure represents     */
583
  char *subdir;         /* points to last subdir within currentpath           */
584
  char *dsubdir;        /* Stores a display ready directory name              */
585
  long subdircnt;       /* Initially a count of how many subdirs in this dir  */
2046 mateusz.vi 586
  struct FFDTA *findnexthnd; /* The handle returned by findfirst, used in findnext */
2019 mateusz.vi 587
  struct DIRDATA ddata; /* Maintain directory information, eg attributes      */
588
} SUBDIRINFO;
589
 
590
 
591
/**
592
 * Returns 0 if no subdirectories, count if has subdirs.
593
 * Path must end in slash \ or /
594
 * On error (invalid path) displays message and returns -1L.
595
 * Stores additional directory data in ddata if non-NULL
596
 * and path is valid.
597
 */
2037 mateusz.vi 598
static long hasSubdirectories(char *path, DIRDATA *ddata) {
2040 mateusz.vi 599
  static struct WIN32_FIND_DATA findData;
2046 mateusz.vi 600
  struct FFDTA *hnd;
2019 mateusz.vi 601
  static char buffer[MAXBUF];
602
  int hasSubdirs = 0;
603
 
604
  /* get the handle to start with (using wildcard spec) */
605
  strcpy(buffer, path);
606
  strcat(buffer, "*");
607
 
608
  /* Use FindFirstFileEx when available (falls back to FindFirstFile).
609
   * Allows us to limit returned results to just directories
610
   * if supported by underlying filesystem.
611
   */
2040 mateusz.vi 612
  hnd = FindFirstFile(buffer, &findData);
2048 mateusz.vi 613
  if (hnd == NULL) {
2019 mateusz.vi 614
    showInvalidPath(path); /* Display error message */
2048 mateusz.vi 615
    return(-1);
2019 mateusz.vi 616
  }
617
 
618
  /*  cycle through entries counting directories found until no more entries */
619
  do {
2047 mateusz.vi 620
    if (((findData.attrib & FILE_A_DIR) != 0) &&
621
        ((findData.attrib &
622
         (FILE_A_HIDDEN | FILE_A_SYSTEM)) == 0 || dspAll) ) {
623
      if (findData.cFileName[0] != '.') { /* ignore '.' and '..' */
2019 mateusz.vi 624
        hasSubdirs++;      /* subdir of initial path found, so increment counter */
2047 mateusz.vi 625
      }
2019 mateusz.vi 626
    }
627
  } while(FindNextFile(hnd, &findData) != 0);
628
 
629
  /* prevent resource leaks, close the handle. */
630
  FindClose(hnd);
631
 
632
  if (ddata != NULL)  // don't bother if user doesn't want them
633
  {
634
    /* The root directory of a volume (including non root paths
635
       corresponding to mount points) may not have a current (.) and
636
       parent (..) entry.  So we can't get attributes for initial
2022 mateusz.vi 637
       path in above loop from the FindFile call as it may not show up
2019 mateusz.vi 638
       (no . entry).  So instead we explicitly get them here.
639
    */
2047 mateusz.vi 640
    if (GetFileAttributes(&(ddata->attrib), path) != 0) {
2019 mateusz.vi 641
      //printf("ERROR: unable to get file attr, %i\n", GetLastError());
2047 mateusz.vi 642
      ddata->attrib = 0;
2019 mateusz.vi 643
    }
644
 
645
    /* a curiosity, for showing sum of directories process */
646
    ddata->subdirCnt = hasSubdirs;
647
  }
648
  totalSubDirCnt += hasSubdirs;
649
 
650
  return hasSubdirs;
651
}
652
 
653
 
654
/**
655
 * Allocates memory and stores the necessary stuff to allow us to
656
 * come back to this subdirectory after handling its subdirectories.
657
 * parentpath must end in \ or / or be NULL, however
658
 * parent should only be NULL for initialpath
659
 * if subdir does not end in slash, one is added to stored subdir
660
 * dsubdir is subdir already modified so ready to display to user
661
 */
2037 mateusz.vi 662
static SUBDIRINFO *newSubdirInfo(SUBDIRINFO *parent, char *subdir, char *dsubdir) {
2027 mateusz.vi 663
  int parentLen, subdirLen;
2032 mateusz.vi 664
  SUBDIRINFO *temp;
2019 mateusz.vi 665
 
666
  /* Get length of parent directory */
667
  if (parent == NULL)
668
    parentLen = 0;
669
  else
670
    parentLen = strlen(parent->currentpath);
671
 
672
  /* Get length of subdir, add 1 if does not end in slash */
673
  subdirLen = strlen(subdir);
674
  if ((subdirLen < 1) || ( (*(subdir+subdirLen-1) != '\\') && (*(subdir+subdirLen-1) != '/') ) )
675
    subdirLen++;
676
 
2032 mateusz.vi 677
  temp = (SUBDIRINFO *)malloc(sizeof(SUBDIRINFO));
2022 mateusz.vi 678
  if (temp == NULL)
2019 mateusz.vi 679
  {
680
    showOutOfMemory(subdir);
681
    return NULL;
682
  }
683
  if ( ((temp->currentpath = (char *)malloc(parentLen+subdirLen+1)) == NULL) ||
684
       ((temp->dsubdir = (char *)malloc(strlen(dsubdir)+1)) == NULL) )
685
  {
686
    showOutOfMemory(subdir);
687
    if (temp->currentpath != NULL) free(temp->currentpath);
688
    free(temp);
689
    return NULL;
690
  }
691
  temp->parent = parent;
692
  if (parent == NULL)
693
    strcpy(temp->currentpath, "");
694
  else
695
    strcpy(temp->currentpath, parent->currentpath);
696
  strcat(temp->currentpath, subdir);
697
  /* if subdir[subdirLen-1] == '\0' then we must append a slash */
698
  if (*(subdir+subdirLen-1) == '\0')
699
    strcat(temp->currentpath, "\\");
700
  temp->subdir = temp->currentpath+parentLen;
701
  strcpy(temp->dsubdir, dsubdir);
702
  if ((temp->subdircnt = hasSubdirectories(temp->currentpath, &(temp->ddata))) == -1L)
703
  {
704
    free (temp->currentpath);
705
    free (temp->dsubdir);
706
    free(temp);
707
    return NULL;
708
  }
2048 mateusz.vi 709
  temp->findnexthnd = NULL;
2019 mateusz.vi 710
 
711
  return temp;
712
}
713
 
2048 mateusz.vi 714
 
2019 mateusz.vi 715
/**
716
 * Extends the padding with the necessary 4 characters.
717
 * Returns the pointer to the padding.
2022 mateusz.vi 718
 * padding should be large enough to hold the additional
2019 mateusz.vi 719
 * characters and '\0', moreSubdirsFollow specifies if
720
 * this is the last subdirectory in a given directory
721
 * or if more follow (hence if a | is needed).
722
 * padding must not be NULL
723
 */
2037 mateusz.vi 724
static char * addPadding(char *padding, int moreSubdirsFollow) {
725
  if (moreSubdirsFollow) {
726
    /* 1st char is | or a vertical bar */
727
    if (charSet == EXTENDEDCHARS) {
728
      strcat(padding, VERTBAR_STR);
729
    } else {
730
      strcat(padding, "|   ");
2019 mateusz.vi 731
    }
2037 mateusz.vi 732
  } else {
733
    strcat(padding, "    ");
734
  }
2019 mateusz.vi 735
 
2037 mateusz.vi 736
  return(padding);
2019 mateusz.vi 737
}
738
 
739
/**
2025 mateusz.vi 740
 * Removes the last padding added (last 4 characters added).
741
 * Does nothing if less than 4 characters in string.
2019 mateusz.vi 742
 * padding must not be NULL
743
 * Returns the pointer to padding.
744
 */
2037 mateusz.vi 745
static char *removePadding(char *padding) {
2027 mateusz.vi 746
  size_t len = strlen(padding);
2019 mateusz.vi 747
 
2025 mateusz.vi 748
  if (len < 4) return padding;
749
  *(padding + len - 4) = '\0';
2019 mateusz.vi 750
 
751
  return padding;
752
}
753
 
754
/**
755
 * Takes a given path, strips any \ or / that may appear on the end.
756
 * Returns a pointer to its static buffer containing path
757
 * without trailing slash and any necessary display conversions.
758
 */
2037 mateusz.vi 759
static char *fixPathForDisplay(char *path) {
2019 mateusz.vi 760
  static char buffer[MAXBUF];
2027 mateusz.vi 761
  int pathlen;
2019 mateusz.vi 762
 
763
  strcpy(buffer, path);
764
  pathlen = strlen(buffer);
2037 mateusz.vi 765
  if (pathlen > 1) {
2019 mateusz.vi 766
    pathlen--;
2037 mateusz.vi 767
    if ((buffer[pathlen] == '\\') || (buffer[pathlen] == '/')) {
2019 mateusz.vi 768
      buffer[pathlen] = '\0'; // strip off trailing slash on end
2037 mateusz.vi 769
    }
2019 mateusz.vi 770
  }
771
 
772
  return buffer;
773
}
774
 
775
/**
776
 * Displays the current path, with necessary padding before it.
777
 * A \ or / on end of currentpath is not shown.
778
 * moreSubdirsFollow should be nonzero if this is not the last
779
 * subdirectory to be displayed in current directory, else 0.
780
 * Also displays additional information, such as attributes or
781
 * sum of size of included files.
782
 * currentpath is an ASCIIZ string of path to display
783
 *             assumed to be a displayable path (ie. OEM or UTF-8)
784
 * padding is an ASCIIZ string to display prior to entry.
785
 * moreSubdirsFollow is -1 for initial path else >= 0.
786
 */
2037 mateusz.vi 787
static void showCurrentPath(char *currentpath, char *padding, int moreSubdirsFollow, DIRDATA *ddata) {
2019 mateusz.vi 788
  if (padding != NULL)
789
    pprintf("%s", padding);
790
 
791
  /* print lead padding except for initial directory */
792
  if (moreSubdirsFollow >= 0)
793
  {
794
    if (charSet == EXTENDEDCHARS)
795
    {
796
      if (moreSubdirsFollow)
797
        pprintf("%s", TBAR_HORZBAR_STR);
798
      else
799
        pprintf("%s", CBAR_HORZBAR_STR);
2037 mateusz.vi 800
    } else {
2019 mateusz.vi 801
      if (moreSubdirsFollow)
802
        pprintf("+---");
803
      else
804
        pprintf("\\---");
805
    }
806
  }
807
 
808
  /* optional display data */
809
  if (dspAttr)  /* attributes */
2031 mateusz.vi 810
    pprintf("[%c%c%c%c%c] ",
2047 mateusz.vi 811
      (ddata->attrib & FILE_A_DIR)?'D':' ',  /* keep this one? its always true */
812
      (ddata->attrib & FILE_A_ARCH)?'A':' ',
813
      (ddata->attrib & FILE_A_SYSTEM)?'S':' ',
814
      (ddata->attrib & FILE_A_HIDDEN)?'H':' ',
815
      (ddata->attrib & FILE_A_READONLY)?'R':' '
2019 mateusz.vi 816
    );
817
 
818
  /* display directory name */
819
  pprintf("%s\n", currentpath);
820
}
821
 
822
 
2022 mateusz.vi 823
/**
2019 mateusz.vi 824
 * Displays summary information about directory.
825
 * Expects to be called after displayFiles (optionally called)
826
 */
2037 mateusz.vi 827
static void displaySummary(char *padding, int hasMoreSubdirs, DIRDATA *ddata) {
2019 mateusz.vi 828
  addPadding(padding, hasMoreSubdirs);
829
 
2037 mateusz.vi 830
  if (dspSumDirs) {
831
    if (showFiles == SHOWFILESON) {
2019 mateusz.vi 832
      /* print File summary with lead padding, add filesize to it */
833
      pprintf("%s%lu files\n", padding, ddata->fileCnt);
834
    }
835
 
836
    /* print Directory summary with lead padding */
837
    pprintf("%s%lu subdirectories\n", padding, ddata->subdirCnt);
838
 
839
    /* show [nearly] blank line after summary */
840
    pprintf("%s\n", padding);
841
  }
842
 
843
  removePadding(padding);
844
}
845
 
2022 mateusz.vi 846
/**
2019 mateusz.vi 847
 * Displays files in directory specified by path.
848
 * Path must end in slash \ or /
849
 * Returns -1 on error,
850
 *          0 if no files, but no errors either,
851
 *      or  1 if files displayed, no errors.
852
 */
2047 mateusz.vi 853
static int displayFiles(const char *path, char *padding, int hasMoreSubdirs, DIRDATA *ddata) {
2019 mateusz.vi 854
  static char buffer[MAXBUF];
2040 mateusz.vi 855
  struct WIN32_FIND_DATA entry; /* current directory entry info    */
2046 mateusz.vi 856
  struct FFDTA *dir;         /* Current directory entry working with      */
2019 mateusz.vi 857
  unsigned long filesShown = 0;
858
 
859
  /* get handle for files in current directory (using wildcard spec) */
860
  strcpy(buffer, path);
861
  strcat(buffer, "*");
862
  dir = FindFirstFile(buffer, &entry);
2048 mateusz.vi 863
  if (dir == NULL) return(-1);
2019 mateusz.vi 864
 
865
  addPadding(padding, hasMoreSubdirs);
866
 
867
  /* cycle through directory printing out files. */
2022 mateusz.vi 868
  do
2019 mateusz.vi 869
  {
870
    /* print padding followed by filename */
2047 mateusz.vi 871
    if ( ((entry.attrib & FILE_A_DIR) == 0) &&
872
         ( ((entry.attrib & (FILE_A_HIDDEN | FILE_A_SYSTEM)) == 0)  || dspAll) )
2019 mateusz.vi 873
    {
874
      /* print lead padding */
875
      pprintf("%s", padding);
876
 
877
      /* optional display data */
878
      if (dspAttr)  /* file attributes */
2031 mateusz.vi 879
        pprintf("[%c%c%c%c] ",
2047 mateusz.vi 880
          (entry.attrib & FILE_A_ARCH)?'A':' ',
881
          (entry.attrib & FILE_A_SYSTEM)?'S':' ',
882
          (entry.attrib & FILE_A_HIDDEN)?'H':' ',
883
          (entry.attrib & FILE_A_READONLY)?'R':' '
2019 mateusz.vi 884
        );
885
 
886
      if (dspSize)  /* file size */
887
      {
888
        if (entry.nFileSizeHigh)
889
        {
890
          pprintf("******** ");  /* error exceed max value we can display, > 4GB */
891
        }
892
        else
893
        {
2023 mateusz.vi 894
          if (entry.nFileSizeLow < 1048576l)  /* if less than a MB, display in bytes */
2019 mateusz.vi 895
            pprintf("%10lu ", entry.nFileSizeLow);
896
          else                               /* otherwise display in KB */
897
            pprintf("%8luKB ", entry.nFileSizeLow/1024UL);
898
        }
899
      }
900
 
901
      /* print filename */
902
      pprintf("%s\n", entry.cFileName);
903
 
904
      filesShown++;
905
    }
906
  } while(FindNextFile(dir, &entry) != 0);
907
 
908
  if (filesShown)
909
  {
910
    pprintf("%s\n", padding);
911
  }
912
 
913
  /* cleanup directory search */
914
  FindClose(dir);
915
  /* dir = NULL; */
916
 
917
  removePadding(padding);
918
 
919
  /* store for summary display */
920
  if (ddata != NULL) ddata->fileCnt = filesShown;
921
 
922
  return (filesShown)? 1 : 0;
923
}
924
 
925
 
926
/**
927
 * Common portion of findFirstSubdir and findNextSubdir
928
 * Checks current FindFile results to determine if a valid directory
929
 * was found, and if so copies appropriate data into subdir and dsubdir.
930
 * It will repeat until a valid subdirectory is found or no more
931
 * are found, at which point it closes the FindFile search handle and
2048 mateusz.vi 932
 * return NULL.  If successful, returns FindFile handle.
2019 mateusz.vi 933
 */
2046 mateusz.vi 934
static struct FFDTA *cycleFindResults(struct FFDTA *findnexthnd, struct WIN32_FIND_DATA *entry, char *subdir, char *dsubdir) {
2025 mateusz.vi 935
  /* cycle through directory until 1st non . or .. directory is found. */
936
  do
2019 mateusz.vi 937
  {
2025 mateusz.vi 938
    /* skip files & hidden or system directories */
2047 mateusz.vi 939
    if ((((entry->attrib & FILE_A_DIR) == 0) ||
940
         ((entry->attrib &
941
          (FILE_A_HIDDEN | FILE_A_SYSTEM)) != 0  && !dspAll) ) ||
942
        (entry->cFileName[0] == '.')) {
2048 mateusz.vi 943
      if (FindNextFile(findnexthnd, entry) == 0) {
2025 mateusz.vi 944
        FindClose(findnexthnd);      // prevent resource leaks
2048 mateusz.vi 945
        return(NULL); // no subdirs found
2019 mateusz.vi 946
      }
2048 mateusz.vi 947
    } else {
2025 mateusz.vi 948
      /* set display name */
2038 mateusz.vi 949
      strcpy(dsubdir, entry->cFileName);
2019 mateusz.vi 950
 
2038 mateusz.vi 951
      strcpy(subdir, entry->cFileName);
2025 mateusz.vi 952
      strcat(subdir, "\\");
953
    }
954
  } while (!*subdir); // while (subdir is still blank)
955
 
2019 mateusz.vi 956
  return findnexthnd;
957
}
958
 
959
 
960
/* FindFile buffer used by findFirstSubdir and findNextSubdir only */
2040 mateusz.vi 961
static struct WIN32_FIND_DATA findSubdir_entry; /* current directory entry info    */
2019 mateusz.vi 962
 
963
/**
964
 * Given the current path, find the 1st subdirectory.
965
 * The subdirectory found is stored in subdir.
966
 * subdir is cleared on error or no subdirectories.
967
 * Returns the findfirst search HANDLE, which should be passed to
968
 * findclose when directory has finished processing, and can be
969
 * passed to findnextsubdir to find subsequent subdirectories.
2048 mateusz.vi 970
 * Returns NULL on error.
2019 mateusz.vi 971
 * currentpath must end in \
972
 */
2046 mateusz.vi 973
static struct FFDTA *findFirstSubdir(char *currentpath, char *subdir, char *dsubdir) {
2019 mateusz.vi 974
  static char buffer[MAXBUF];
2046 mateusz.vi 975
  struct FFDTA *dir;         /* Current directory entry working with      */
2019 mateusz.vi 976
 
977
  /* get handle for files in current directory (using wildcard spec) */
978
  strcpy(buffer, currentpath);
979
  strcat(buffer, "*");
980
 
2040 mateusz.vi 981
  dir = FindFirstFile(buffer, &findSubdir_entry);
2048 mateusz.vi 982
  if (dir == NULL) {
2019 mateusz.vi 983
    showInvalidPath(currentpath);
2048 mateusz.vi 984
    return(NULL);
2019 mateusz.vi 985
  }
986
 
987
  /* clear result path */
988
  strcpy(subdir, "");
989
 
2032 mateusz.vi 990
  return cycleFindResults(dir, &findSubdir_entry, subdir, dsubdir);
2019 mateusz.vi 991
}
992
 
993
/**
2022 mateusz.vi 994
 * Given a search HANDLE, will find the next subdirectory,
2019 mateusz.vi 995
 * setting subdir to the found directory name.
2045 mateusz.vi 996
 * dsubdir is the name to display
2019 mateusz.vi 997
 * currentpath must end in \
998
 * If a subdirectory is found, returns 0, otherwise returns 1
999
 * (either error or no more files).
1000
 */
2046 mateusz.vi 1001
static int findNextSubdir(struct FFDTA *findnexthnd, char *subdir, char *dsubdir) {
2019 mateusz.vi 1002
  /* clear result path */
1003
  strcpy(subdir, "");
1004
 
2038 mateusz.vi 1005
  if (FindNextFile(findnexthnd, &findSubdir_entry) == 0) return 1; // no subdirs found
2019 mateusz.vi 1006
 
2048 mateusz.vi 1007
  if (cycleFindResults(findnexthnd, &findSubdir_entry, subdir, dsubdir) == NULL) {
2019 mateusz.vi 1008
    return 1;
2048 mateusz.vi 1009
  }
1010
  return 0;
2019 mateusz.vi 1011
}
1012
 
1013
/**
1014
 * Given an initial path, displays the directory tree with
1015
 * a non-recursive function using a Stack.
1016
 * initialpath must be large enough to hold an added slash \ or /
1017
 * if it does not already end in one.
1018
 * Returns the count of subdirs in initialpath.
1019
 */
2037 mateusz.vi 1020
static long traverseTree(char *initialpath) {
2019 mateusz.vi 1021
  long subdirsInInitialpath;
1022
  char padding[MAXPADLEN] = "";
1023
  char subdir[MAXBUF];
1024
  char dsubdir[MAXBUF];
2027 mateusz.vi 1025
  SUBDIRINFO *sdi;
2019 mateusz.vi 1026
 
1027
  STACK s;
1028
  stackDefaults(&s);
1029
  stackInit(&s);
1030
 
2048 mateusz.vi 1031
  if ( (sdi = newSubdirInfo(NULL, initialpath, initialpath)) == NULL) {
1032
    return(0);
1033
  }
2019 mateusz.vi 1034
  stackPushItem(&s, sdi);
1035
 
1036
  /* Store count of subdirs in initial path so can display message if none. */
1037
  subdirsInInitialpath = sdi->subdircnt;
1038
 
1039
  do
1040
  {
1041
    sdi = (SUBDIRINFO *)stackPopItem(&s);
1042
 
2048 mateusz.vi 1043
    if (sdi->findnexthnd == NULL) { // findfirst not called yet
2019 mateusz.vi 1044
      // 1st time this subdirectory processed, so display its name & possibly files
1045
      if (sdi->parent == NULL) // if initial path
1046
      {
1047
        // display initial path
1048
        showCurrentPath(/*sdi->dsubdir*/initialpath, NULL, -1, &(sdi->ddata));
1049
      }
1050
      else // normal processing (display path, add necessary padding)
1051
      {
1052
        showCurrentPath(sdi->dsubdir, padding, (sdi->parent->subdircnt > 0L)?1 : 0, &(sdi->ddata));
1053
        addPadding(padding, (sdi->parent->subdircnt > 0L)?1 : 0);
1054
      }
1055
 
1056
      if (showFiles == SHOWFILESON)  displayFiles(sdi->currentpath, padding, (sdi->subdircnt > 0L)?1 : 0, &(sdi->ddata));
2024 mateusz.vi 1057
      displaySummary(padding, (sdi->subdircnt > 0L)?1 : 0, &(sdi->ddata));
2019 mateusz.vi 1058
    }
1059
 
1060
    if (sdi->subdircnt > 0) /* if (there are more subdirectories to process) */
1061
    {
1062
      int flgErr;
2048 mateusz.vi 1063
      if (sdi->findnexthnd == NULL) {
2019 mateusz.vi 1064
        sdi->findnexthnd = findFirstSubdir(sdi->currentpath, subdir, dsubdir);
2048 mateusz.vi 1065
        flgErr = (sdi->findnexthnd == NULL);
1066
      } else {
2019 mateusz.vi 1067
        flgErr = findNextSubdir(sdi->findnexthnd, subdir, dsubdir);
1068
      }
1069
 
1070
      if (flgErr) // don't add invalid paths to stack
1071
      {
1072
        printf("INTERNAL ERROR: subdir count changed, expecting %li more!\n", sdi->subdircnt+1L);
1073
 
1074
        sdi->subdircnt = 0; /* force subdir counter to 0, none left */
1075
        stackPushItem(&s, sdi);
1076
      }
1077
      else
1078
      {
1079
        sdi->subdircnt = sdi->subdircnt - 1L; /* decrement subdirs left count */
1080
        stackPushItem(&s, sdi);
1081
 
1082
        /* store necessary information, validate subdir, and if no error store it. */
1083
        if ((sdi = newSubdirInfo(sdi, subdir, dsubdir)) != NULL)
1084
          stackPushItem(&s, sdi);
1085
      }
1086
    }
1087
    else /* this directory finished processing, so free resources */
1088
    {
1089
      /* Remove the padding for this directory, all but initial path. */
1090
      if (sdi->parent != NULL)
1091
        removePadding(padding);
1092
 
1093
      /* Prevent resource leaks, by ending findsearch and freeing memory. */
1094
      FindClose(sdi->findnexthnd);
1095
      if (sdi != NULL)
1096
      {
1097
        if (sdi->currentpath != NULL)
1098
          free(sdi->currentpath);
1099
        free(sdi);
1100
      }
1101
    }
1102
  } while (stackTotalItems(&s)); /* while (stack is not empty) */
1103
 
1104
  stackTerm(&s);
1105
 
1106
  return subdirsInInitialpath;
1107
}
1108
 
1109
 
2037 mateusz.vi 1110
static void FixOptionText(void) {
2019 mateusz.vi 1111
  char buffer[MAXLINE];  /* sprintf can have problems with src==dest */
1112
 
1113
  /* Handle %c for options within messages using Set 8 */
1114
  strcpy(buffer, treeUsage);
1115
  sprintf(treeUsage, buffer, optionchar1, OptShowFiles[0], optionchar1, OptUseASCII[0]);
1116
  strcpy(buffer, treeFOption);
1117
  sprintf(treeFOption, buffer, optionchar1, OptShowFiles[0]);
1118
  strcpy(buffer, treeAOption);
1119
  sprintf(treeAOption, buffer, optionchar1, OptUseASCII[0]);
1120
  strcpy(buffer, useTreeHelp);
1121
  sprintf(useTreeHelp, buffer, optionchar1);
1122
}
1123
 
1124
 
2022 mateusz.vi 1125
/* Loads all messages from the message catalog. */
2037 mateusz.vi 1126
static void loadAllMessages(void) {
2019 mateusz.vi 1127
  /* Changes %c in certain lines with proper option characters. */
1128
  FixOptionText();
1129
}
1130
 
1131
 
2037 mateusz.vi 1132
int main(int argc, char **argv) {
2019 mateusz.vi 1133
  char serial[SERIALLEN]; /* volume serial #  0000:0000 */
1134
  char volume[VOLLEN];    /* volume name (label), possibly none */
1135
 
1136
  /* Load all text from message catalog (or uses hard coded text) */
1137
  loadAllMessages();
1138
 
1139
  /* Parse any command line arguments, obtain path */
1140
  parseArguments(argc, argv);
1141
 
1142
  /* Initialize screen size, may reset pause to NOPAUSE if redirected */
1143
  getConsoleSize();
1144
 
1145
  /* Get Volume & Serial Number */
1146
  GetVolumeAndSerial(volume, serial, path);
1147
  if (strlen(volume) == 0)
1148
    pprintf(pathListingNoLabel);
1149
  else
1150
    pprintf(pathListingWithLabel, volume);
1151
  if (serial[0] != '\0')  /* Don't print anything if no serial# found */
1152
    pprintf(serialNumber, serial);
1153
 
1154
  /* now traverse & print tree, returns nonzero if has subdirectories */
1155
  if (traverseTree(path) == 0)
1156
    pprintf(noSubDirs);
1157
  else if (dspSumDirs) /* show count of directories processed */
1158
    pprintf("\n    %lu total directories\n", totalSubDirCnt+1);
1159
 
1160
  return 0;
1161
}