summaryrefslogtreecommitdiffstats
path: root/ls.c
blob: 8800563e923cb36eef62206259efd9f43c5dc5ce (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
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
/* $Header: /home/ksheff/src/e2tools/RCS/ls.c,v 0.8 2004/04/07 03:30:49 ksheff Exp $ */
/*
 * ls.c --- list directories
 * 
 * Copyright (C) 1997 Theodore Ts'o.  This file may be redistributed
 * under the terms of the GNU Public License.
 *
 * Modified by Keith W. Sheffield <shefff@pobox.com> for inclusion with e2tools
 */
/*
 * $Log: ls.c,v $
 * Revision 0.8  2004/04/07 03:30:49  ksheff
 * Updated usage string.
 *
 * Revision 0.7  2004/04/07 02:41:50  ksheff
 * Modified to bracket files with an inode number of 0 with >< characters.
 *
 * Revision 0.6  2004/04/06 20:27:26  ksheff
 * Modified to print "No files found!" for empty directories, corrected the
 * directory name display, and fixed REGEX_OPT masking.
 *
 * Revision 0.5  2002/06/05 23:14:11  ksheff
 * Fixed short display with respect to displaying the contents of multiple
 * directories.
 *
 * Revision 0.4  2002/06/05 23:07:34  ksheff
 * Allowed for multiple directory and file specifications.  Added the -f
 * option for no sorting.
 *
 * Revision 0.3  2002/06/03 23:00:51  ksheff
 * Removed display code from directory iterator.  A list of displayable files
 * is now generated.  This list can be sorted and displayed in a variety of
 * ways.  The -a, -c, -i, -r, and -t options are now accepted.
 *
 * Revision 0.2  2002/03/05 12:12:52  ksheff
 * Removed setting optind for SCO.
 *
 * Revision 0.1  2002/02/27 04:46:21  ksheff
 * initial revision
 *
 */


#include "e2tools.h"
#include "elist.h"
#include <regex.h>
#include <stdint.h>
#include <inttypes.h>

/*
 * list directory
 */

#define LONG_OPT	0x0001
#define DELETED_OPT	0x0002
#define REGEX_OPT   0x0004
#define REVERSE_OPT 0x0008
#define HIDDEN_OPT  0x0010
#define CREATE_OPT  0x0020
#define INODE_OPT   0x0040

#define DIRECTORY_TYPE -1
#define NORMAL_TYPE 0

struct list_dir_struct
{
  int	options;
  regex_t *reg;
  elist_t *files;  
};

typedef struct list_file_struct
{
  
  char *name;
  struct ext2_inode inode;
  ext2_ino_t dir;
  ext2_ino_t inode_num;
  int entry;
  long type;
} ls_file_t;

static const char *monstr[] =
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

static ext2_filsys fs;
static int max_name_len = 0;

static int
list_dir_proc(ext2_ino_t dir, int entry, struct ext2_dir_entry *dirent,
              int offset, int blocksize, char *buf, void *private);
void
free_ls_file_t(void *f);
long
do_list_dir(int argc, char *argv[]);
void
long_disp(ls_file_t *info, int *col, int options);
void
short_disp(ls_file_t *info, int *col, int options);
int
no_sort(void *n1, void *n2);
int
name_sort(void *n1, void *n2);
int
inode_sort(void *n1, void *n2);
int
mod_time_sort(void *n1, void *n2);
int
creat_time_sort(void *n1, void *n2);
long
add_ls_file(char *name, int namelen, ext2_ino_t dir, ext2_ino_t ino,
            int entry, int type, struct list_dir_struct *ls);
elist_t *
remove_ls_dups(elist_t *list);

/* Name:	list_dir_proc()
 *
 * Description:
 *
 *
 * Algorithm:
 *
 *
 * Global Variables:
 *
 * None
 *
 * Arguments:
 *
 *
 * Return Values:
 *
 *
 * Author: Theodore Ts'o
 * Date:   1997
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 * 02/27/02      K.Sheffield        Modified for use with e2tools
 */
static int
list_dir_proc(ext2_ino_t dir, int entry, struct ext2_dir_entry *dirent,
              int UNUSED_PARM(offset), int UNUSED_PARM(blocksize),
	      char UNUSED_PARM(*buf), void *private)
{
  char			name[EXT2_NAME_LEN];
  struct list_dir_struct *ls = (struct list_dir_struct *) private;
  int thislen;
  
  thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
    (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
  strncpy(name, dirent->name, thislen);
  name[thislen] = '\0';

  /* skip hidden files unless we ask for them */
  if (0 == (ls->options & HIDDEN_OPT) &&
      name[0] == '.')
    return(0);
  
  if ((ls->options & REGEX_OPT) &&
      regexec(ls->reg, name, 0, NULL, 0))
    return(0);

  return(add_ls_file(name, thislen, dir, dirent->inode, entry, NORMAL_TYPE,
                     ls));
  
}

/* Name:	add_ls_file()
 *
 * Description:
 *
 *
 * Algorithm:
 *
 *
 * Global Variables:
 *
 * None
 *
 * Arguments:
 *
 *
 * Return Values:
 *
 *
 * Author: K.Sheffield
 * Date:   02/27/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 */
long
add_ls_file(char *name, int namelen, ext2_ino_t dir, ext2_ino_t ino,
            int entry, int type, struct list_dir_struct *ls)
{
  ls_file_t *file_info;
  elist_t *flist;
    
  if (NULL == (file_info = calloc(sizeof(ls_file_t),1)))
    {
      perror("list_dir");
      return(0);
    }

  file_info->dir = dir;
  
  if (entry == DIRENT_DELETED_FILE)
    file_info->inode_num = 0;
  else
    file_info->inode_num = ino;

  file_info->entry = entry;
  file_info->type = type;
  
  if (file_info->inode_num)
    {
    if (read_inode(fs, file_info->inode_num, &file_info->inode))
        {
        free(file_info);
        return 0;
        }
    }

  if (name)
    file_info->name = strdup(name);
  
  if (NULL == (flist = elist_insert(ls->files, file_info)))
    {
      perror("list_dir");
      free_ls_file_t(file_info);
      return 0;
    }
  ls->files = flist;

  if (max_name_len < namelen)
    max_name_len = namelen;
  
  return 0;
}

/* Name:	free_ls_file_t()
 *
 * Description:
 *
 * This function is used to free an ls_file_t structure.
 *
 * Algorithm:
 *
 * Free the file's name if it is not NULL
 * Free the ls_file_t structure
 * Check to make sure the ls_file_t structure is not NULL
 *
 * Global Variables:
 *
 * None.
 *
 * Arguments:
 *
 * void *f;                The structure to free
 *
 * Return Values:
 *
 * none
 *
 * Author: Keith W. Sheffield
 * Date:   06/03/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 *
 */
void free_ls_file_t(void *f)
{
  ls_file_t *n = (ls_file_t *) f;
  
  if (n != NULL)
    {
      if (n->name != NULL)
        free(n->name);
      free(n);
    }
} /* end of free_ls_file_t */ 

/* Name:	do_list_dir()
 *
 * Description:
 *
 *
 * Algorithm:
 *
 *
 * Global Variables:
 *
 * None
 *
 * Arguments:
 *
 *
 * Return Values:
 *
 *
 * Author: Theodore Ts'o
 * Date:   1997
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 * 02/27/02      K.Sheffield        Modified for use with e2tools
 * 04/06/04      K.Sheffield        Modified to print "No Files Found!" for
 *                                  each empty directory.  Corrected masking
 *                                  out REGEX_OPT.
 */

long
do_list_dir(int argc, char *argv[])
{
  ext2_ino_t	root;
  ext2_ino_t	cwd;
  ext2_ino_t	inode=0;
  int		retval;
  int		c;
  int		flags;
  struct list_dir_struct ls;
  char *fs_name;
  char *last_fs_name;
  char *path = NULL;
  char *dup_path = NULL;
  char *dir_name;
  char *base_name;
  int (*file_sort)(void *n1, void *n2) = name_sort;
  void (*file_disp)(ls_file_t *n, int *col, int options) = short_disp;
  elist_t *files=NULL;
  int col=0;
  ls_file_t *cur_file;
  long last_type = 1;
  
  memset(&ls, 0, sizeof(ls));

  last_fs_name = NULL;
#ifdef HAVE_OPTRESET
  optreset = 1;		/* Makes BSD getopt happy */
#endif
  while ((c = getopt (argc, argv, "acDd:filrt")) != EOF)
    {
      switch (c)
        {
        case 'a':
          ls.options |= HIDDEN_OPT;
          break;
        case 'c':
          ls.options |= CREATE_OPT;
          break;
        case 'l':
          file_disp = long_disp;
          break;
        case 'D':
          ls.options |= DELETED_OPT;
          break;
        case 'd':
          fs_name = optarg;
          if (NULL != (path = strchr(fs_name, ':')))
            *path++ = '\0';
          if ((retval = open_filesystem(fs_name, &fs, &root, 0)))
            {
              fputs(error_message(retval), stderr);
              return(1);
            }
          last_fs_name = fs_name;
          break;
        case 'f':
          file_sort = no_sort;
          break;
        case 't':
          file_sort = mod_time_sort;
          break;
        case 'r':
          ls.options |= REVERSE_OPT;
          break;
        case 'i':
          file_sort = inode_sort;
          ls.options |= INODE_OPT;
          break;
        }
    }

  if (argc <= optind)
    {
      fputs("Usage: e2ls [-acDfilrt][-d dir] file\n", stderr);
      return(1);
    }

  if (ls.options & CREATE_OPT &&
      (file_sort == mod_time_sort || file_disp != long_disp))
    file_sort = creat_time_sort;

  /* sort the remaining command line arguments */
  qsort(argv+optind, argc-optind, sizeof(char *), my_strcmp);
  
  for(c=optind;c<argc;c++)
    {
      fs_name = argv[c];
      
      if (NULL != (path = strchr(fs_name, ':')))
        *path++ = '\0';
      else if (last_fs_name != NULL)
        {
        path = fs_name;
        fs_name = last_fs_name;
        }

      /* keep a copy of the file path for later because get_file_parts() is
       * destructive.
       */
      
      if (dup_path)
        {
          free(dup_path);
          dup_path = NULL;
        }

      if (path)
        dup_path = strdup(path);
      
      if (last_fs_name == NULL || strcmp(last_fs_name, fs_name))
        {
          if (last_fs_name != NULL)
            ext2fs_close(fs);
              
          if ((retval = open_filesystem(fs_name, &fs, &root, 0)))
            {
              fputs(error_message(retval), stderr);
              return(1);
            }
          last_fs_name = fs_name;
        }

      dir_name = NULL;
      cwd = root;
      ls.options &= (~REGEX_OPT);

      if (path != NULL && *path != '\0')
        {
          if (get_file_parts(fs, root, path, &cwd, &dir_name, &base_name))
            {
              ext2fs_close(fs);
              return(-1);
            }

          if (is_file_regexp(base_name))
            {
              if (NULL == (ls.reg = (regex_t *) make_regexp(base_name)))
                {
                  fprintf(stderr,
                          "Error creating regular expression for %s\n",
                          base_name);
                  return(1);
                }
              ls.options |= REGEX_OPT;
              inode = cwd;

            }
          /* check to see if the file name exists in the current directory
           */
          else if ((retval = ext2fs_namei(fs, cwd, cwd, base_name, &inode)))
            {
              fputs(error_message(retval), stderr);
              ext2fs_close(fs);
              return(1);
            }
        }
      else
          inode = root;
      
      if(!dir_name)
        dir_name = ".";
            
      if (!inode)
        continue;
            
      flags = DIRENT_FLAG_INCLUDE_EMPTY;
      if (ls.options & DELETED_OPT)
        flags |= DIRENT_FLAG_INCLUDE_REMOVED;
            
      if ((retval = ext2fs_check_directory(fs, inode)))
        {
        if (retval != EXT2_ET_NO_DIRECTORY)
            {
              fputs(error_message(retval), stderr);
              ext2fs_close(fs);
              return(1);
            }
        
        if(add_ls_file(dir_name, 0, cwd, 0, 0, DIRECTORY_TYPE, &ls))
          {
            fputs(error_message(retval), stderr);
            ext2fs_close(fs);
            return(1);
          }
        
        if(add_ls_file(base_name, strlen(base_name), cwd, inode, 0,
                       NORMAL_TYPE, &ls))
          {
            fputs(error_message(retval), stderr);
            ext2fs_close(fs);
            return(1);
          }
        }
      else
        {
          if (ls.options & REGEX_OPT || path == NULL || *path == '\0')
            path = dir_name;
          else if (dup_path)
            path = dup_path;

          
          //        if(add_ls_file((ls.options & REGEX_OPT) ? dir_name : path, 0, inode, 0,
          if(add_ls_file(path, 0, inode, 0, 0, DIRECTORY_TYPE, &ls))
            {
              fputs(error_message(retval), stderr);
              ext2fs_close(fs);
              return(1);
            }
          
          retval = ext2fs_dir_iterate2(fs, inode, flags, 0, list_dir_proc,
                                       &ls);
          if (retval)
            {
              fputs(error_message(retval), stderr);
              ext2fs_close(fs);
              return(1);
            }
        }
    }
  

  elist_sort(ls.files, file_sort, ls.options & REVERSE_OPT);
  
  ls.files = files = remove_ls_dups(ls.files);
  
  if (files == NULL)
    printf("No files found!");
  else
    {
      while(files != NULL)
        {
          cur_file = (ls_file_t *)files->data;
          if (cur_file->type == DIRECTORY_TYPE)
            {
              if (col > 0)
                {
                  putchar('\n');
                  col = 0;
                }
              if (last_type == DIRECTORY_TYPE)
                printf("No files found!\n");
              
              printf("%s:", cur_file->name);
            }
          else
            {
              if (last_type == DIRECTORY_TYPE)
                putchar('\n');
              (file_disp)(cur_file, &col, ls.options);
            }
          
          last_type = cur_file->type;
          
          files = files->next;
        }
      if (last_type == DIRECTORY_TYPE)
        printf("No files found!");
    }
  
  putchar('\n');

  elist_free(ls.files, free_ls_file_t);
    
  ext2fs_close(fs);
  return(0);
}

/* Name:	long_disp()
 *
 * Description:
 *
 * This function displays a file's information for a long listing
 *
 * Algorithm:
 *
 * Generate the time string for the modification time
 * Display the file's permission bits, owner, group, mod time, and name
 *
 * Global Variables:
 *
 * none
 *
 * Arguments:
 *
 * ls_file_t *info;             The structure containing the file information
 * int *col;                    The current column - unused
 * int options;                 Options to ls
 *
 * Return Values:
 *
 * None
 *
 * Author: Keith W. Sheffield
 * Date:   06/03/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 * 06/06/02      K.Sheffield        Increased file size width
 * 04/06/04      K.Sheffield        Modified to show entries with an inode of 0
 *                                  as deleted.
 */
void long_disp(ls_file_t *info, int UNUSED_PARM(*col), int options)
{
  char lbr, rbr;
  char datestr[80];
  time_t modtime;
  struct tm *tm_p;

  
  if (info->entry == DIRENT_DELETED_FILE ||
      info->inode_num == 0)
    {
      lbr = '>';
      rbr = '<';
	}
  else
    {
		lbr = rbr = ' ';
	}

  
  if (info->inode_num)
    {
      if (options & CREATE_OPT)
        modtime = info->inode.i_ctime;
      else
        modtime = info->inode.i_mtime;
      tm_p = localtime(&modtime);
      sprintf(datestr, "%2d-%s-%4d %02d:%02d",
              tm_p->tm_mday, monstr[tm_p->tm_mon],
              1900 + tm_p->tm_year, tm_p->tm_hour,
              tm_p->tm_min);
    }
  else
    strcpy(datestr, "                 ");
      
  printf("%c%6u%c %6o %5d %5d  ", lbr, info->inode_num, rbr,
         info->inode.i_mode, info->inode.i_uid, info->inode.i_gid);
  if (LINUX_S_ISDIR(info->inode.i_mode))
    printf("%7d", info->inode.i_size);
  else
    printf("%7" PRIu64, (uint64_t)(info->inode.i_size |
				  ((__u64)info->inode.i_size_high << 32)));
  printf(" %s %s\n", datestr, info->name);

} /* end of long_disp */ 


/* Name:	short_disp()
 *
 * Description:
 *
 * This function displays a file's information for a long listing
 *
 * Algorithm:
 *
 * Display the file's name at the appropriate column.
 *
 * Global Variables:
 *
 * none
 *
 * Arguments:
 *
 * ls_file_t *info;             The structure containing the file information
 * int *col;                    The current column
 * int options;                 Options to ls
 *
 * Return Values:
 *
 * None
 *
 * Author: Keith W. Sheffield
 * Date:   06/03/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 * 04/06/04      K.Sheffield        Modified to show entries with an inode of 0
 *                                  as deleted.
 */
void short_disp(ls_file_t *info, int *col, int options)
{
  char lbr, rbr;
  char tmp[300];
  int thislen;
  static int max_col_size = 0;

  if (max_col_size == 0)
    {
      max_col_size = 80/(max_name_len + 2 + ((options & INODE_OPT) ? 8 : 0));
      if (max_col_size == 0)
        max_col_size = -1;
      else
        max_col_size = 80/max_col_size;
    }
  
      
  if (info->entry == DIRENT_DELETED_FILE ||
      info->inode_num == 0)
    {
      lbr = '>';
      rbr = '<';
	}
  else
    {
      lbr = 0;
      rbr = ' ';
	}

  if (lbr == 0)
    {
      if (options & INODE_OPT)    
        sprintf(tmp, "%7d %s%c", info->inode_num, info->name, rbr);
      else
        sprintf(tmp, "%s%c", info->name, rbr);
    }
  else
    {
      if (options & INODE_OPT)    
        sprintf(tmp, "%7d %c%s%c", info->inode_num, lbr, info->name, rbr);
      else
        sprintf(tmp, "%c%s%c", lbr, info->name, rbr);
    }
  
  thislen = strlen(tmp);

  if (*col + thislen > 80)
    {
      putchar('\n');
      *col = 0;
    }
  thislen = max_col_size - thislen;
  if (thislen < 0)
    thislen = 0;
  
  printf("%s%*.*s", tmp, thislen, thislen, "");
  *col += max_col_size;
}

/* Name:	no_sort()
 *
 * Description:
 *
 * This function sorts two ls_file_t structures by the file name
 *
 * Algorithm:
 *
 * Assign two ls_file_t pointers from the input void pointers
 * Return the result of the comparison of the directories & type
 *
 * Global Variables:
 *
 * None
 *
 * Arguments:
 *
 * void *n1;                     The first node being compared
 * void *n2;                     The second node being compared
 *
 * Return Values:
 *
 * >0 - n1 > n2
 * =0 - n1 == n2
 * <0 - n1 < n2
 *
 * Author: Keith W. Sheffield
 * Date:   06/03/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 *
 */
int no_sort(void *n1, void *n2)
{
  ls_file_t *f1 = *((ls_file_t **) n1);
  ls_file_t *f2 = *((ls_file_t **) n2);
  int retval;
  
  return((retval = (f1->dir - f2->dir)) ? retval : (f1->type - f2->type));
  
} /* end of name_sort */

/* Name:	name_sort()
 *
 * Description:
 *
 * This function sorts two ls_file_t structures by the file name
 *
 * Algorithm:
 *
 * Assign two ls_file_t pointers from the input void pointers
 * Return the result of the comparison of the names
 *
 * Global Variables:
 *
 * None
 *
 * Arguments:
 *
 * void *n1;                     The first node being compared
 * void *n2;                     The second node being compared
 *
 * Return Values:
 *
 * >0 - n1 > n2
 * =0 - n1 == n2
 * <0 - n1 < n2
 *
 * Author: Keith W. Sheffield
 * Date:   06/03/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 *
 */
int name_sort(void *n1, void *n2)
{
  ls_file_t *f1 = *((ls_file_t **) n1);
  ls_file_t *f2 = *((ls_file_t **) n2);
  int retval;
  
  return((retval = (f1->dir - f2->dir)) ? retval :
         ((retval = (f1->type - f2->type)) ? retval :
          strcmp(f1->name, f2->name)));
} /* end of name_sort */

/* Name:	inode_sort()
 *
 * Description:
 *
 * This function sorts two ls_file_t structures by the file inode number
 *
 * Algorithm:
 *
 * Assign two ls_file_t pointers from the input void pointers
 * Return the result of the comparison of the inode numbers
 *
 * Global Variables:
 *
 * None
 *
 * Arguments:
 *
 * void *n1;                     The first node being compared
 * void *n2;                     The second node being compared
 *
 * Return Values:
 *
 * >0 - n1 > n2
 * =0 - n1 == n2
 * <0 - n1 < n2
 *
 * Author: Keith W. Sheffield
 * Date:   06/03/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 *
 */
int inode_sort(void *n1, void *n2)
{
  ls_file_t *f1 = *((ls_file_t **) n1);
  ls_file_t *f2 = *((ls_file_t **) n2);
  int retval;
  
  return((retval = (f1->dir - f2->dir)) ? retval :
         ((retval = (f1->type - f2->type)) ? retval :
          (int)(f1->inode_num - f2->inode_num)));
} /* end of inode_sort */ 

/* Name:	mod_time_sort()
 *
 * Description:
 *
 * This function sorts two ls_file_t structures by the file modification time
 *
 * Algorithm:
 *
 * Assign two ls_file_t pointers from the input void pointers
 * Return the result of the comparison of the modification time
 *
 * Global Variables:
 *
 * None
 *
 * Arguments:
 *
 * void *n1;                     The first node being compared
 * void *n2;                     The second node being compared
 *
 * Return Values:
 *
 * >0 - n1 > n2
 * =0 - n1 == n2
 * <0 - n1 < n2
 *
 * Author: Keith W. Sheffield
 * Date:   06/03/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 *
 */
int mod_time_sort(void *n1, void *n2)
{
  ls_file_t *f1 = *((ls_file_t **) n1);
  ls_file_t *f2 = *((ls_file_t **) n2);
  int retval;
  
  return((retval = (f1->dir - f2->dir)) ? retval :
         ((retval = (f1->type - f2->type)) ? retval :
          (int)(f2->inode.i_mtime - f1->inode.i_mtime)));
  
} /* end of mod_time_sort */

/* Name:	creat_time_sort()
 *
 * Description:
 *
 * This function sorts two ls_file_t structures by the file creation time
 *
 * Algorithm:
 *
 * Assign two ls_file_t pointers from the input void pointers
 * Return the result of the comparison of the creation time
 *
 * Global Variables:
 *
 * None
 *
 * Arguments:
 *
 * void *n1;                     The first node being compared
 * void *n2;                     The second node being compared
 *
 * Return Values:
 *
 * >0 - n1 > n2
 * =0 - n1 == n2
 * <0 - n1 < n2
 *
 * Author: Keith W. Sheffield
 * Date:   06/03/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 *
 */
int creat_time_sort(void *n1, void *n2)
{
  ls_file_t *f1 = *((ls_file_t **) n1);
  ls_file_t *f2 = *((ls_file_t **) n2);
  int retval;
  
  return((retval = (f1->dir - f2->dir)) ? retval :
         ((retval = (f1->type - f2->type)) ? retval :
          (int)(f2->inode.i_ctime - f1->inode.i_ctime)));
} /* end of creat_time_sort */ 

/* Name:	remove_ls_dups()
 *
 * Description:
 *
 * This function will remove the first directory node if it is the only one
 * found.  It will also remove bogus entries.
 *
 * Algorithm:
 *
 * For each node in the linked list
 *     If the current node has no data
 *        Remove it from the linked list
 *     If the current entry is a DIRECTORY_TYPE
 *        Increment the number of directories.
 * Return the starting node in the linked list
 *
 * Global Variables:
 *
 * None.
 *
 * Arguments:
 *
 * elist *list;                 The linked list to process
 *
 * Return Values:
 *
 * The starting node in the linked list.
 *
 * Author: Keith W. Sheffield
 * Date:   06/05/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 * 04/06/04      K.Sheffield        Modified to just remove the first directory
 *                                  node if it is the only one found.
 *
 */
elist_t * remove_ls_dups(elist_t *list)
{
  elist_t *start = list;
  ls_file_t *cd;
  int cnt=0;

  while(list != NULL)
    {
      cd = (ls_file_t *) list->data;
      if (cd == NULL)
        {
          /* remove any empty nodes */
          if (list == start)
            start = list->next;
          list = elist_delete(list, free_ls_file_t);
          continue;
        }
      else if (cd->type == DIRECTORY_TYPE)
        {
          cnt++;
        }
      list = list->next;
    }

  /* if there is only one directory entry, delete it */
  if (cnt == 1)
    start = elist_delete(start, free_ls_file_t);
  
  return(start);
      
} /* end of remove_ls_dups */