summaryrefslogtreecommitdiff
path: root/source3/smbd/dir.c
blob: d751653263286a25caab9efd47488a408fc3e7d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   Directory handling routines
   Copyright (C) Andrew Tridgell 1992-1998
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

extern int DEBUGLEVEL;
extern connection_struct Connections[];

/*
   This module implements directory related functions for Samba.
*/



uint32 dircounter = 0;


#define NUMDIRPTRS 256


static struct dptr_struct
{
  int pid;
  int cnum;
  uint32 lastused;
  void *ptr;
  BOOL valid;
  BOOL finished;
  BOOL expect_close;
  char *wcard; /* Field only used for lanman2 trans2_findfirst/next searches */
  uint16 attr; /* Field only used for lanman2 trans2_findfirst/next searches */
  char *path;
}
dirptrs[NUMDIRPTRS];


static int dptrs_open = 0;

/****************************************************************************
initialise the dir array
****************************************************************************/
void init_dptrs(void)
{
  static BOOL dptrs_init=False;
  int i;

  if (dptrs_init) return;
  for (i=0;i<NUMDIRPTRS;i++)    
    {
      dirptrs[i].valid = False;
      dirptrs[i].wcard = NULL;
      dirptrs[i].ptr = NULL;
      string_init(&dirptrs[i].path,"");
    }
  dptrs_init = True;
}

/****************************************************************************
idle a dptr - the directory is closed but the control info is kept
****************************************************************************/
static void dptr_idle(int key)
{
  if (dirptrs[key].valid && dirptrs[key].ptr) {
    DEBUG(4,("Idling dptr key %d\n",key));
    dptrs_open--;
    CloseDir(dirptrs[key].ptr);
    dirptrs[key].ptr = NULL;
  }    
}

/****************************************************************************
idle the oldest dptr
****************************************************************************/
static void dptr_idleoldest(void)
{
  int i;
  uint32 old=dircounter+1;
  int oldi= -1;
  for (i=0;i<NUMDIRPTRS;i++)
    if (dirptrs[i].valid && dirptrs[i].ptr && dirptrs[i].lastused < old) {
      old = dirptrs[i].lastused;
      oldi = i;
    }
  if (oldi != -1)
    dptr_idle(oldi);
  else
    DEBUG(0,("No dptrs available to idle??\n"));
}

/****************************************************************************
get the dir ptr for a dir index
****************************************************************************/
static void *dptr_get(int key,uint32 lastused)
{
  struct dptr_struct *dp = &dirptrs[key];

  if (dp->valid) {
    if (lastused) dp->lastused = lastused;
    if (!dp->ptr) {
      if (dptrs_open >= MAXDIR)
	dptr_idleoldest();
      DEBUG(4,("Reopening dptr key %d\n",key));
      if ((dp->ptr = OpenDir(dp->cnum, dp->path, True)))
	dptrs_open++;
    }
    return(dp->ptr);
  }
  return(NULL);
}

/****************************************************************************
get the dir path for a dir index
****************************************************************************/
char *dptr_path(int key)
{
  if (dirptrs[key].valid)
    return(dirptrs[key].path);
  return(NULL);
}

/****************************************************************************
get the dir wcard for a dir index (lanman2 specific)
****************************************************************************/
char *dptr_wcard(int key)
{
  if (dirptrs[key].valid)
    return(dirptrs[key].wcard);
  return(NULL);
}

/****************************************************************************
set the dir wcard for a dir index (lanman2 specific)
Returns 0 on ok, 1 on fail.
****************************************************************************/
BOOL dptr_set_wcard(int key, char *wcard)
{
  if (dirptrs[key].valid) {
    dirptrs[key].wcard = wcard;
    return True;
  }
  return False;
}

/****************************************************************************
set the dir attrib for a dir index (lanman2 specific)
Returns 0 on ok, 1 on fail.
****************************************************************************/
BOOL dptr_set_attr(int key, uint16 attr)
{
  if (dirptrs[key].valid) {
    dirptrs[key].attr = attr;
    return True;
  }
  return False;
}

/****************************************************************************
get the dir attrib for a dir index (lanman2 specific)
****************************************************************************/
uint16 dptr_attr(int key)
{
  if (dirptrs[key].valid)
    return(dirptrs[key].attr);
  return(0);
}

/****************************************************************************
close a dptr
****************************************************************************/
void dptr_close(int key)
{
  /* OS/2 seems to use -1 to indicate "close all directories" */
  if (key == -1) {
    int i;
    for (i=0;i<NUMDIRPTRS;i++) 
      dptr_close(i);
    return;
  }

  if (key < 0 || key >= NUMDIRPTRS) {
    DEBUG(3,("Invalid key %d given to dptr_close\n",key));
    return;
  }

  if (dirptrs[key].valid) {
    DEBUG(4,("closing dptr key %d\n",key));
    if (dirptrs[key].ptr) {
      CloseDir(dirptrs[key].ptr);
      dptrs_open--;
    }
    /* Lanman 2 specific code */
    if (dirptrs[key].wcard)
      free(dirptrs[key].wcard);
    dirptrs[key].valid = False;
    string_set(&dirptrs[key].path,"");
  }
}

/****************************************************************************
close all dptrs for a cnum
****************************************************************************/
void dptr_closecnum(int cnum)
{
  int i;
  for (i=0;i<NUMDIRPTRS;i++)
    if (dirptrs[i].valid && dirptrs[i].cnum == cnum)
      dptr_close(i);
}

/****************************************************************************
idle all dptrs for a cnum
****************************************************************************/
void dptr_idlecnum(int cnum)
{
  int i;
  for (i=0;i<NUMDIRPTRS;i++)
    if (dirptrs[i].valid && dirptrs[i].cnum == cnum && dirptrs[i].ptr)
      dptr_idle(i);
}

/****************************************************************************
close a dptr that matches a given path, only if it matches the pid also
****************************************************************************/
void dptr_closepath(char *path,int pid)
{
  int i;
  for (i=0;i<NUMDIRPTRS;i++)
    if (dirptrs[i].valid && pid == dirptrs[i].pid &&
	strequal(dirptrs[i].path,path))
      dptr_close(i);
}

/****************************************************************************
  start a directory listing
****************************************************************************/
static BOOL start_dir(int cnum,char *directory)
{
  DEBUG(5,("start_dir cnum=%d dir=%s\n",cnum,directory));

  if (!check_name(directory,cnum))
    return(False);
  
  if (! *directory)
    directory = ".";

  Connections[cnum].dirptr = OpenDir(cnum, directory, True);
  if (Connections[cnum].dirptr) {    
    dptrs_open++;
    string_set(&Connections[cnum].dirpath,directory);
    return(True);
  }
  
  return(False);
}


/****************************************************************************
create a new dir ptr
****************************************************************************/
int dptr_create(int cnum,char *path, BOOL expect_close,int pid)
{
  int i;
  uint32 old;
  int oldi;

  if (!start_dir(cnum,path))
    return(-2); /* Code to say use a unix error return code. */

  if (dptrs_open >= MAXDIR)
    dptr_idleoldest();

  for (i=0;i<NUMDIRPTRS;i++)
    if (!dirptrs[i].valid)
      break;
  if (i == NUMDIRPTRS) i = -1;


  /* as a 2nd option, grab the oldest not marked for expect_close */
  if (i == -1) {
    old=dircounter+1;
    oldi= -1;
    for (i=0;i<NUMDIRPTRS;i++)
      if (!dirptrs[i].expect_close && dirptrs[i].lastused < old) {
	old = dirptrs[i].lastused;
	oldi = i;
      }
    i = oldi;
  }

  /* a 3rd option - grab the oldest one */
  if (i == -1) {
    old=dircounter+1;
    oldi= -1;
    for (i=0;i<NUMDIRPTRS;i++)
      if (dirptrs[i].lastused < old) {
	old = dirptrs[i].lastused;
	oldi = i;
      }
    i = oldi;
  }

  if (i == -1) {
    DEBUG(0,("Error - all dirptrs in use??\n"));
    return(-1);
  }

  if (dirptrs[i].valid)
    dptr_close(i);

  dirptrs[i].ptr = Connections[cnum].dirptr;
  string_set(&dirptrs[i].path,path);
  dirptrs[i].lastused = dircounter++;
  dirptrs[i].finished = False;
  dirptrs[i].cnum = cnum;
  dirptrs[i].pid = pid;
  dirptrs[i].expect_close = expect_close;
  dirptrs[i].wcard = NULL; /* Only used in lanman2 searches */
  dirptrs[i].attr = 0; /* Only used in lanman2 searches */
  dirptrs[i].valid = True;

  DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
	   i,path,expect_close));  

  return(i);
}

#define DPTR_MASK ((uint32)(((uint32)1)<<31))

/****************************************************************************
fill the 5 byte server reserved dptr field
****************************************************************************/
BOOL dptr_fill(char *buf1,unsigned int key)
{
  unsigned char *buf = (unsigned char *)buf1;
  void *p = dptr_get(key,0);
  uint32 offset;
  if (!p) {
    DEBUG(1,("filling null dirptr %d\n",key));
    return(False);
  }
  offset = TellDir(p);
  DEBUG(6,("fill on key %d dirptr 0x%x now at %d\n",key,p,offset));
  buf[0] = key;
  SIVAL(buf,1,offset | DPTR_MASK);
  return(True);
}


/****************************************************************************
return True is the offset is at zero
****************************************************************************/
BOOL dptr_zero(char *buf)
{
  return((IVAL(buf,1)&~DPTR_MASK) == 0);
}

/****************************************************************************
fetch the dir ptr and seek it given the 5 byte server field
****************************************************************************/
void *dptr_fetch(char *buf,int *num)
{
  unsigned int key = *(unsigned char *)buf;
  void *p = dptr_get(key,dircounter++);
  uint32 offset;
  if (!p) {
    DEBUG(3,("fetched null dirptr %d\n",key));
    return(NULL);
  }
  *num = key;
  offset = IVAL(buf,1)&~DPTR_MASK;
  SeekDir(p,offset);
  DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
	   key,dptr_path(key),offset));
  return(p);
}

/****************************************************************************
fetch the dir ptr and seek it given the lanman2 parameter block
****************************************************************************/
void *dptr_fetch_lanman2(char *params,int dptr_num)
{
  void *p = dptr_get(dptr_num,dircounter++);
  uint32 resume_key = SVAL(params,6);
  BOOL uses_resume_key = BITSETW(params+10,2);
  BOOL continue_bit = BITSETW(params+10,3);

  if (!p) {
    DEBUG(3,("fetched null dirptr %d\n",dptr_num));
    return(NULL);
  }
  if(uses_resume_key && !continue_bit)
    SeekDir(p,resume_key);
  DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr_path(dptr_num)));
  return(p);
}

/****************************************************************************
check a filetype for being valid
****************************************************************************/
BOOL dir_check_ftype(int cnum,int mode,struct stat *st,int dirtype)
{
  if (((mode & ~dirtype) & (aHIDDEN | aSYSTEM | aDIR)) != 0)
    return False;
  return True;
}

/****************************************************************************
  get a directory entry
****************************************************************************/
BOOL get_dir_entry(int cnum,char *mask,int dirtype,char *fname,int *size,int *mode,time_t *date,BOOL check_descend)
{
  char *dname;
  BOOL found = False;
  struct stat sbuf;
  pstring path;
  pstring pathreal;
  BOOL isrootdir;
  pstring filename;
  BOOL matched;
  BOOL needslash;

  *path = *pathreal = *filename = 0;

  isrootdir = (strequal(Connections[cnum].dirpath,"./") ||
	       strequal(Connections[cnum].dirpath,".") ||
	       strequal(Connections[cnum].dirpath,"/"));
  
  needslash = 
        ( Connections[cnum].dirpath[strlen(Connections[cnum].dirpath) -1] != '/');

  if (!Connections[cnum].dirptr)
    return(False);
  
  while (!found)
    {
      dname = ReadDirName(Connections[cnum].dirptr);

      DEBUG(6,("readdir on dirptr 0x%x now at offset %d\n",
	    Connections[cnum].dirptr,TellDir(Connections[cnum].dirptr)));
      
      if (dname == NULL) 
	return(False);
      
      matched = False;

      pstrcpy(filename,dname);      

      if ((strcmp(filename,mask) == 0) ||
	  (name_map_mangle(filename,True,SNUM(cnum)) &&
	   mask_match(filename,mask,False,False)))
	{
	  if (isrootdir && (strequal(filename,"..") || strequal(filename,".")))
	    continue;

	  pstrcpy(fname,filename);
	  *path = 0;
	  pstrcpy(path,Connections[cnum].dirpath);
          if(needslash)
  	    strcat(path,"/");
	  pstrcpy(pathreal,path);
	  strcat(path,fname);
	  strcat(pathreal,dname);
	  if (sys_stat(pathreal,&sbuf) != 0) 
	    {
	      DEBUG(5,("Couldn't stat 1 [%s]\n",path));
	      continue;
	    }

	  if (check_descend &&
	      !strequal(fname,".") && !strequal(fname,".."))
	    continue;
	  
	  *mode = dos_mode(cnum,pathreal,&sbuf);

	  if (!dir_check_ftype(cnum,*mode,&sbuf,dirtype)) {
	    DEBUG(5,("[%s] attribs didn't match %x\n",filename,dirtype));
	    continue;
	  }

	  *size = sbuf.st_size;
	  *date = sbuf.st_mtime;

	  DEBUG(5,("get_dir_entry found %s fname=%s\n",pathreal,fname));
	  
	  found = True;
	}
    }

  return(found);
}



typedef struct
{
  int pos;
  int numentries;
  int mallocsize;
  char *data;
  char *current;
} Dir;


/*******************************************************************
open a directory
********************************************************************/
void *OpenDir(int cnum, char *name, BOOL use_veto)
{
  Dir *dirp;
  char *n;
  void *p = sys_opendir(name);
  int used=0;

  if (!p) return(NULL);
  dirp = (Dir *)malloc(sizeof(Dir));
  if (!dirp) {
    closedir(p);
    return(NULL);
  }
  dirp->pos = dirp->numentries = dirp->mallocsize = 0;
  dirp->data = dirp->current = NULL;

  while ((n = readdirname(p)))
  {
    int l = strlen(n)+1;

    /* If it's a vetoed file, pretend it doesn't even exist */
    if (use_veto && IS_VETO_PATH(cnum, n)) continue;

    if (used + l > dirp->mallocsize) {
      int s = MAX(used+l,used+2000);
      char *r;
      r = (char *)Realloc(dirp->data,s);
      if (!r) {
	DEBUG(0,("Out of memory in OpenDir\n"));
	break;
      }
      dirp->data = r;
      dirp->mallocsize = s;
      dirp->current = dirp->data;
    }
    strcpy(dirp->data+used,n);
    used += l;
    dirp->numentries++;
  }

  closedir(p);
  return((void *)dirp);
}


/*******************************************************************
close a directory
********************************************************************/
void CloseDir(void *p)
{
  Dir *dirp = (Dir *)p;
  if (!dirp) return;    
  if (dirp->data) free(dirp->data);
  free(dirp);
}

/*******************************************************************
read from a directory
********************************************************************/
char *ReadDirName(void *p)
{
  char *ret;
  Dir *dirp = (Dir *)p;

  if (!dirp || !dirp->current || dirp->pos >= dirp->numentries) return(NULL);

  ret = dirp->current;
  dirp->current = skip_string(dirp->current,1);
  dirp->pos++;

  return(ret);
}


/*******************************************************************
seek a dir
********************************************************************/
BOOL SeekDir(void *p,int pos)
{
  Dir *dirp = (Dir *)p;

  if (!dirp) return(False);

  if (pos < dirp->pos) {
    dirp->current = dirp->data;
    dirp->pos = 0;
  }

  while (dirp->pos < pos && ReadDirName(p)) ;

  return(dirp->pos == pos);
}

/*******************************************************************
tell a dir position
********************************************************************/
int TellDir(void *p)
{
  Dir *dirp = (Dir *)p;

  if (!dirp) return(-1);
  
  return(dirp->pos);
}


/* -------------------------------------------------------------------------- **
 * This section manages a global directory cache.
 * (It should probably be split into a separate module.  crh)
 * -------------------------------------------------------------------------- **
 */

typedef struct
  {
  ubi_dlNode  node;
  char       *path;
  char       *name;
  char       *dname;
  int         snum;
  } dir_cache_entry;

static ubi_dlList dir_cache[1] = { { NULL, NULL, 0 } };

void DirCacheAdd( char *path, char *name, char *dname, int snum )
  /* ------------------------------------------------------------------------ **
   * Add an entry to the directory cache.
   *
   *  Input:  path  -
   *          name  -
   *          dname -
   *          snum  -
   *
   *  Output: None.
   *
   * ------------------------------------------------------------------------ **
   */
  {
  int               pathlen;
  int               namelen;
  dir_cache_entry  *entry;

  /* Allocate the structure & string space in one go so that it can be freed
   * in one call to free().
   */
  pathlen = strlen( path ) +1;  /* Bytes required to store path (with nul). */
  namelen = strlen( name ) +1;  /* Bytes required to store name (with nul). */
  entry = (dir_cache_entry *)malloc( sizeof( dir_cache_entry )
                                   + pathlen
                                   + namelen
                                   + strlen( dname ) +1 );
  if( NULL == entry )   /* Not adding to the cache is not fatal,  */
    return;             /* so just return as if nothing happened. */

  /* Set pointers correctly and load values. */
  entry->path  = strcpy( (char *)&entry[1],       path);
  entry->name  = strcpy( &(entry->path[pathlen]), name);
  entry->dname = strcpy( &(entry->name[namelen]), dname);
  entry->snum  = snum;

  /* Add the new entry to the linked list. */
  (void)ubi_dlAddHead( dir_cache, entry );
  DEBUG( 4, ("Added dir cache entry %s %s -> %s\n", path, name, dname ) );

  /* Free excess cache entries. */
  while( DIRCACHESIZE < dir_cache->count )
    free( ubi_dlRemTail( dir_cache ) );

  } /* DirCacheAdd */


char *DirCacheCheck( char *path, char *name, int snum )
  /* ------------------------------------------------------------------------ **
   * Search for an entry to the directory cache.
   *
   *  Input:  path  -
   *          name  -
   *          snum  -
   *
   *  Output: The dname string of the located entry, or NULL if the entry was
   *          not found.
   *
   *  Notes:  This uses a linear search, which is is okay because of
   *          the small size of the cache.  Use a splay tree or hash
   *          for large caches.
   *
   * ------------------------------------------------------------------------ **
   */
  {
  dir_cache_entry *entry;

  for( entry = (dir_cache_entry *)ubi_dlFirst( dir_cache );
       NULL != entry;
       entry = (dir_cache_entry *)ubi_dlNext( entry ) )
    {
    if( entry->snum == snum
        && 0 == strcmp( name, entry->name )
        && 0 == strcmp( path, entry->path ) )
      {
      DEBUG(4, ("Got dir cache hit on %s %s -> %s\n",path,name,entry->dname));
      return( entry->dname );
      }
    }

  return(NULL);
  } /* DirCacheCheck */

void DirCacheFlush( int snum )
  /* ------------------------------------------------------------------------ **
   * Remove all cache entries which have an snum that matches the input.
   *
   *  Input:  snum  -
   *
   *  Output: None.
   *
   * ------------------------------------------------------------------------ **
   */
  {
  dir_cache_entry *entry;
  ubi_dlNodePtr    next;

  for( entry = (dir_cache_entry *)ubi_dlFirst( dir_cache ); NULL != entry; )
    {
    next = ubi_dlNext( entry );
    if( entry->snum == snum )
      free( ubi_dlRemThis( dir_cache, entry ) );
    entry = (dir_cache_entry *)next;
    }
  } /* DirCacheFlush */

/* -------------------------------------------------------------------------- **
 * End of the section that manages the global directory cache.
 * -------------------------------------------------------------------------- **
 */


#ifdef REPLACE_GETWD
/* This is getcwd.c from bash.  It is needed in Interactive UNIX.  To
 * add support for another OS you need to determine which of the
 * conditional compilation macros you need to define.  All the options
 * are defined for Interactive UNIX.
 */
#ifdef ISC
#define HAVE_UNISTD_H
#define USGr3
#define USG
#endif

#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#if defined (__STDC__)
#  define CONST const
#  define PTR void *
#else /* !__STDC__ */
#  define CONST
#  define PTR char *
#endif /* !__STDC__ */

#if !defined (PATH_MAX)
#  if defined (MAXPATHLEN)
#    define PATH_MAX MAXPATHLEN
#  else /* !MAXPATHLEN */
#    define PATH_MAX 1024
#  endif /* !MAXPATHLEN */
#endif /* !PATH_MAX */

#if defined (_POSIX_VERSION) || defined (USGr3) || defined (HAVE_DIRENT_H)
#  if !defined (HAVE_DIRENT)
#    define HAVE_DIRENT
#  endif /* !HAVE_DIRENT */
#endif /* _POSIX_VERSION || USGr3 || HAVE_DIRENT_H */

#if defined (HAVE_DIRENT)
#  define D_NAMLEN(d)	(strlen ((d)->d_name))
#else
#  define D_NAMLEN(d)	((d)->d_namlen)
#endif /* ! (_POSIX_VERSION || USGr3) */

#if defined (USG) || defined (USGr3)
#  define d_fileno d_ino
#endif

#if !defined (alloca)
extern char *alloca ();
#endif /* alloca */

/* Get the pathname of the current working directory,
   and put it in SIZE bytes of BUF.  Returns NULL if the
   directory couldn't be determined or SIZE was too small.
   If successful, returns BUF.  In GNU, if BUF is NULL,
   an array is allocated with `malloc'; the array is SIZE
   bytes long, unless SIZE <= 0, in which case it is as
   big as necessary.  */
#if defined (__STDC__)
char *
getcwd (char *buf, size_t size)
#else /* !__STDC__ */
char *
getcwd (buf, size)
     char *buf;
     int size;
#endif /* !__STDC__ */
{
  static CONST char dots[]
    = "../../../../../../../../../../../../../../../../../../../../../../../\
../../../../../../../../../../../../../../../../../../../../../../../../../../\
../../../../../../../../../../../../../../../../../../../../../../../../../..";
  CONST char *dotp, *dotlist;
  size_t dotsize;
  dev_t rootdev, thisdev;
  ino_t rootino, thisino;
  char path[PATH_MAX + 1];
  register char *pathp;
  char *pathbuf;
  size_t pathsize;
  struct stat st;

  if (buf != NULL && size == 0)
    {
      errno = EINVAL;
      return ((char *)NULL);
    }

  pathsize = sizeof (path);
  pathp = &path[pathsize];
  *--pathp = '\0';
  pathbuf = path;

  if (stat (".", &st) < 0)
    return ((char *)NULL);
  thisdev = st.st_dev;
  thisino = st.st_ino;

  if (stat ("/", &st) < 0)
    return ((char *)NULL);
  rootdev = st.st_dev;
  rootino = st.st_ino;

  dotsize = sizeof (dots) - 1;
  dotp = &dots[sizeof (dots)];
  dotlist = dots;
  while (!(thisdev == rootdev && thisino == rootino))
    {
      register DIR *dirstream;
      register struct dirent *d;
      dev_t dotdev;
      ino_t dotino;
      char mount_point;
      int namlen;

      /* Look at the parent directory.  */
      if (dotp == dotlist)
	{
	  /* My, what a deep directory tree you have, Grandma.  */
	  char *new;
	  if (dotlist == dots)
	    {
	      new = malloc (dotsize * 2 + 1);
	      if (new == NULL)
		goto lose;
	      memcpy (new, dots, dotsize);
	    }
	  else
	    {
	      new = realloc ((PTR) dotlist, dotsize * 2 + 1);
	      if (new == NULL)
		goto lose;
	    }
	  memcpy (&new[dotsize], new, dotsize);
	  dotp = &new[dotsize];
	  dotsize *= 2;
	  new[dotsize] = '\0';
	  dotlist = new;
	}

      dotp -= 3;

      /* Figure out if this directory is a mount point.  */
      if (stat (dotp, &st) < 0)
	goto lose;
      dotdev = st.st_dev;
      dotino = st.st_ino;
      mount_point = dotdev != thisdev;

      /* Search for the last directory.  */
      dirstream = opendir(dotp);
      if (dirstream == NULL)
	goto lose;
      while ((d = (struct dirent *)readdir(dirstream)) != NULL)
	{
	  if (d->d_name[0] == '.' &&
	      (d->d_name[1] == '\0' ||
		(d->d_name[1] == '.' && d->d_name[2] == '\0')))
	    continue;
	  if (mount_point || d->d_fileno == thisino)
	    {
	      char *name;

	      namlen = D_NAMLEN(d);
	      name = (char *)
		alloca (dotlist + dotsize - dotp + 1 + namlen + 1);
	      memcpy (name, dotp, dotlist + dotsize - dotp);
	      name[dotlist + dotsize - dotp] = '/';
	      memcpy (&name[dotlist + dotsize - dotp + 1],
		      d->d_name, namlen + 1);
	      if (lstat (name, &st) < 0)
		{
		  int save = errno;
		  closedir(dirstream);
		  errno = save;
		  goto lose;
		}
	      if (st.st_dev == thisdev && st.st_ino == thisino)
		break;
	    }
	}
      if (d == NULL)
	{
	  int save = errno;
	  closedir(dirstream);
	  errno = save;
	  goto lose;
	}
      else
	{
	  size_t space;

	  while ((space = pathp - pathbuf) <= namlen)
	    {
	      char *new;

	      if (pathbuf == path)
		{
		  new = malloc (pathsize * 2);
		  if (!new)
		    goto lose;
		}
	      else
		{
		  new = realloc ((PTR) pathbuf, (pathsize * 2));
		  if (!new)
		    goto lose;
		  pathp = new + space;
		}
	      (void) memcpy (new + pathsize + space, pathp, pathsize - space);
	      pathp = new + pathsize + space;
	      pathbuf = new;
	      pathsize *= 2;
	    }

	  pathp -= namlen;
	  (void) memcpy (pathp, d->d_name, namlen);
	  *--pathp = '/';
	  closedir(dirstream);
	}

      thisdev = dotdev;
      thisino = dotino;
    }

  if (pathp == &path[sizeof(path) - 1])
    *--pathp = '/';

  if (dotlist != dots)
    free ((PTR) dotlist);

  {
    size_t len = pathbuf + pathsize - pathp;
    if (buf == NULL)
      {
	if (len < (size_t) size)
	  len = size;
	buf = (char *) malloc (len);
	if (buf == NULL)
	  goto lose2;
      }
    else if ((size_t) size < len)
      {
	errno = ERANGE;
	goto lose2;
      }
    (void) memcpy((PTR) buf, (PTR) pathp, len);
  }

  if (pathbuf != path)
    free (pathbuf);

  return (buf);

 lose:
  if ((dotlist != dots) && dotlist)
    {
      int e = errno;
      free ((PTR) dotlist);
      errno = e;
    }

 lose2:
  if ((pathbuf != path) && pathbuf)
    {
      int e = errno;
      free ((PTR) pathbuf);
      errno = e;
    }
  return ((char *)NULL);
}
#endif