~ubuntu-branches/ubuntu/edgy/tilp/edgy

« back to all changes in this revision

Viewing changes to src/files.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien BLACHE
  • Date: 2004-05-22 21:12:03 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040522211203-awg2cuw03guyvyz9
Tags: 6.72-2
* debian/control
  + Build-Depends: libticables3 (>= 3.8.4-1).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  tilp - a linking program for TI graphing calculators
2
 
 *  Copyright (C) 1999-2002  Romain Lievin
3
 
 *
4
 
 *  This program is free software; you can redistribute it and/or modify
5
 
 *  it under the terms of the GNU General Public License as published by
6
 
 *  the Free Software Foundation; either version 2 of the License, or
7
 
 *  (at your option) any later version.
8
 
 *
9
 
 *  This program is distributed in the hope that it will be useful,
10
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 *  GNU General Public License for more details.
13
 
 *
14
 
 *  You should have received a copy of the GNU General Public License
15
 
 *  along with this program; if not, write to the Free Software
16
 
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
 
 */
18
 
 
19
 
/*
20
 
  This file contains utility functions about files, attributes,
21
 
  sorting routines for selection.
22
 
  These functions are mainly used by the right window.
23
 
 */
24
 
 
25
 
#include <stdio.h>
26
 
#include <ctype.h>
27
 
#include <stdlib.h>
28
 
#include <string.h>
29
 
#include <time.h>
30
 
#include <sys/stat.h>
31
 
//#include <dirent.h>
32
 
//#include <unistd.h>
33
 
 
34
 
#ifndef __MACOSX__
35
 
#include <glib.h>
36
 
#include "platform.h"
37
 
#else
38
 
#include <dirent.h>
39
 
#include <glib/glib.h>
40
 
#endif
41
 
 
42
 
#include "struct.h"
43
 
#include "defs.h"
44
 
#include "intl.h"
45
 
#include "gui_indep.h"
46
 
#include "error.h"
47
 
#include "vars.h"
48
 
 
49
 
/*******************************/
50
 
/* File manipulation functions */
51
 
/*******************************/
52
 
 
53
 
/*
54
 
  Copy a file from src to dst 
55
 
 */
56
 
int copy_file(char *src, char *dst)
57
 
{
58
 
#ifndef __WIN32__
59
 
  FILE *in, *out;
60
 
  int c;
61
 
 
62
 
   if((in=fopen(src, "rb")) == NULL)
63
 
    {
64
 
      return -1;
65
 
    }
66
 
   if((out=fopen(dst, "wb")) == NULL)
67
 
     {
68
 
      return -2;
69
 
    }
70
 
   while(!feof(in))
71
 
     {
72
 
           c=fgetc(in);
73
 
           if(feof(in)) break;
74
 
       fputc(c, out);
75
 
     }
76
 
   fclose(in);
77
 
   fclose(out);
78
 
#else
79
 
        if(!CopyFile(src, dst, FALSE))
80
 
                return -1;
81
 
#endif
82
 
 
83
 
  return 0;
84
 
}
85
 
 
86
 
/*
87
 
  Move the file
88
 
 */
89
 
int move_file(char *src, char *dst)
90
 
{
91
 
#ifndef __WIN32__
92
 
  int ret;
93
 
 
94
 
  ret=copy_file(src, dst);
95
 
  if(ret) return ret;
96
 
  unlink(src);
97
 
#else
98
 
        if(!MoveFile(src, dst))
99
 
                return -1;
100
 
#endif
101
 
 
102
 
  return 0;
103
 
}
104
 
 
105
 
int delete_file(char *f)
106
 
{
107
 
  if(unlink(f) == -1)
108
 
    {
109
 
#ifdef __WIN32__
110
 
      if(!RemoveDirectory(f))
111
 
#else
112
 
        /* [X91] temporarily drop root privileges */
113
 
#ifdef __LINUX__
114
 
        uid_t   effective;
115
 
      
116
 
      effective = geteuid();
117
 
      seteuid(getuid());
118
 
#endif
119
 
      if(remove(f) == -1)
120
 
#endif
121
 
        {
122
 
          gif->msg_box(_("Information"),
123
 
                       _("Unable to remove the file. You can not delete non empty folders !"));
124
 
          return -1;
125
 
        }
126
 
#ifdef __LINUX__
127
 
      seteuid(effective);
128
 
#endif
129
 
    }
130
 
  return 0;
131
 
}
132
 
 
133
 
/* Remove '\r' characters for GtkText */
134
 
void process_buffer(gchar *buf)
135
 
{
136
 
  gint i;
137
 
  
138
 
  for(i=0; i<strlen(buf); i++)
139
 
  {
140
 
    if(buf[i]=='\r') buf[i]=' ';
141
 
  }
142
 
}
143
 
 
144
 
/* Replace a '\r\n' or a '\n' by '\r' */
145
 
void process_unix2dos(gchar *buf)
146
 
{
147
 
        int i;
148
 
        int l = strlen(buf);
149
 
 
150
 
        for(i=0; i<l; i++)
151
 
        {/*
152
 
                if( (buf[i] == '\r') && (buf[i+1] == '\n') )
153
 
                {
154
 
                        buf[i] = '\r';
155
 
                        buf[i+1] = '\n';
156
 
                }
157
 
                if( (buf[i] == '\n') && (buf[i+1] == '\r') )
158
 
                {
159
 
                        buf[i] = '\r';
160
 
                        buf[i+1] = '\n';
161
 
                }
162
 
                if(buf[i] == '\r') buf[i] = '\n';
163
 
                */
164
 
                if( (buf[i] == '\r') || (buf[i] == '\n') )
165
 
                {
166
 
                        buf[i]='\r';
167
 
                        buf[i+1]='\n';
168
 
                        i++;
169
 
                }
170
 
        }
171
 
        buf[i]='\0';
172
 
 
173
 
}
174
 
 
175
 
 
176
 
/*************************************/
177
 
/* Extracting informations functions */
178
 
/*************************************/
179
 
 
180
 
/*
181
 
  Retrieve informations about attributes from a file info structure
182
 
  and returns a string
183
 
 */
184
 
char *get_attributes(TilpFileInfo f_info)
185
 
{
186
 
  char *s;
187
 
 
188
 
  s=g_strdup(" ---------- ");
189
 
 
190
 
  if(f_info.attrib & S_IRUSR) s[2]='r';
191
 
  if(f_info.attrib & S_IWUSR) s[3]='w';
192
 
  if(f_info.attrib & S_ISUID) 
193
 
    {
194
 
      if(f_info.attrib & S_IXUSR) s[4]='s';
195
 
      else s[4]='S';
196
 
    }
197
 
  else if(f_info.attrib & S_IXUSR) s[4]='x';
198
 
  
199
 
  if(f_info.attrib & S_IRGRP) s[5]='r';
200
 
  if(f_info.attrib & S_IWGRP) s[6]='w';
201
 
  if(f_info.attrib & S_ISGID) 
202
 
    {
203
 
      if(f_info.attrib & S_IXGRP) s[7]='s';
204
 
      else s[7]='S';
205
 
    }
206
 
  else if(f_info.attrib & S_IXGRP) s[7]='x';
207
 
  
208
 
  if(f_info.attrib & S_IROTH) s[8]='r';
209
 
  if(f_info.attrib & S_IWOTH) s[9]='w';
210
 
  if(f_info.attrib & S_ISVTX) 
211
 
    {
212
 
      if(f_info.attrib & S_IXOTH) s[10]='t';
213
 
      else s[10]='T';
214
 
    }
215
 
  else if(f_info.attrib & S_IXOTH) s[10]='x';
216
 
#ifndef __WIN32__
217
 
  if(S_ISLNK(f_info.attrib)) 
218
 
    {
219
 
      s[1]='l';
220
 
      return s;
221
 
    }
222
 
#endif
223
 
 
224
 
  switch(S_IFMT & f_info.attrib)  
225
 
    {
226
 
    case S_IFBLK:       s[1]='b';
227
 
      break;
228
 
    case S_IFDIR:       s[1]='d';
229
 
      break;
230
 
    case S_IFCHR:       s[1]='c';
231
 
      break;
232
 
    case S_IFIFO:       s[1]='p';
233
 
      break;
234
 
    case S_IFSOCK:      s[1]='s';
235
 
      break;
236
 
    }
237
 
 
238
 
  return s;
239
 
}
240
 
 
241
 
/*
242
 
  Returns the user's name
243
 
*/
244
 
void get_user_name(TilpFileInfo f_info, char **name)
245
 
{
246
 
#if defined(__LINUX__)
247
 
  struct passwd *pwuid;
248
 
  
249
 
  if((pwuid=getpwuid(f_info.user)) == NULL)
250
 
    {
251
 
      *name=NULL;
252
 
    }
253
 
  else
254
 
    {
255
 
      *name=g_strdup(pwuid->pw_name);
256
 
    }
257
 
#else
258
 
  *name = NULL;
259
 
#endif
260
 
}
261
 
 
262
 
/*
263
 
  Returns the group's name
264
 
 */
265
 
void get_group_name(TilpFileInfo f_info, char **name)
266
 
{
267
 
#if defined(__LINUX__)
268
 
  struct group *grpid;
269
 
  
270
 
  if((grpid=getgrgid(f_info.group)) == NULL)
271
 
    {
272
 
      *name=NULL;
273
 
    }
274
 
  else
275
 
    {
276
 
      *name=g_strdup(grpid->gr_name);
277
 
    }
278
 
    #else
279
 
          *name = NULL;
280
 
    #endif
281
 
}
282
 
 
283
 
/*
284
 
        Return the date of file
285
 
*/
286
 
void get_date(TilpFileInfo f_info, char **s)
287
 
{
288
 
  char *p;
289
 
  char buffer[32];
290
 
  int i;
291
 
 
292
 
  p=ctime(&(f_info.date));
293
 
  for(i=4; i<11; i++)
294
 
    {
295
 
      buffer[i-4]=*(p+i);
296
 
    }
297
 
  for(i=20; i<24; i++)
298
 
    {
299
 
      buffer[i-13]=*(p+i);
300
 
    }
301
 
  buffer[i-13]='\0';
302
 
  *s=g_strdup(buffer);
303
 
}
304
 
 
305
 
/*
306
 
  Returns the user's home directory
307
 
*/
308
 
int get_home_path(char **path)
309
 
{
310
 
#if defined(__LINUX__)
311
 
  uid_t uid;
312
 
  struct passwd *p;
313
 
  
314
 
  uid = getuid();
315
 
  //fprintf(stderr, "UID: %i\n", uid);
316
 
  
317
 
  if((p = getpwuid(uid)) == NULL)
318
 
    {
319
 
      *path=NULL;
320
 
      return 0;
321
 
    }
322
 
  else
323
 
    {
324
 
      *path = g_strdup(p->pw_dir);
325
 
      return 1;
326
 
    }
327
 
#endif
328
 
  return 0;
329
 
}
330
 
 
331
 
 
332
 
/****************************/
333
 
/* Directory list functions */
334
 
/****************************/
335
 
 
336
 
/* Used by the function below */
337
 
void free_file_info_struct(gpointer data)
338
 
{
339
 
  g_free(((TilpFileInfo *)data)->filename);
340
 
  g_free(data);
341
 
}
342
 
 
343
 
/* Make a directory listing of the current directory and place the result 
344
 
   in the clist_win.dirlist GList 
345
 
*/
346
 
void l_directory_list()
347
 
{
348
 
  DIR *dir;
349
 
  struct dirent *file;
350
 
  struct stat f_info;
351
 
  TilpFileInfo *fi;
352
 
 
353
 
  if(clist_win.dirlist!=NULL)
354
 
    {
355
 
      g_list_foreach(clist_win.dirlist, (GFunc) free_file_info_struct, NULL);
356
 
      g_list_free(clist_win.dirlist);
357
 
      clist_win.dirlist=NULL;
358
 
    }  
359
 
  
360
 
  if( (dir=opendir(clist_win.cur_dir)) == NULL) 
361
 
    {
362
 
      fprintf(stderr, _("Opendir error\n"));
363
 
    }
364
 
  while( (file=readdir(dir)) != NULL) 
365
 
    {
366
 
      if(strcmp(file->d_name, ".")==0) { continue; }
367
 
      if(strcmp(file->d_name, ".."))
368
 
         {
369
 
           if( ((file->d_name)[0]=='.') && (options.show == HIDE) ) { continue; }
370
 
         }
371
 
      fi=(TilpFileInfo *)g_malloc(sizeof(TilpFileInfo));
372
 
      fi->filename=g_strdup(file->d_name);
373
 
      if(stat(file->d_name, &f_info)!=0)
374
 
        {
375
 
          fi->date=0;
376
 
          fi->size=0;
377
 
          fi->user=0;
378
 
          fi->group=0;
379
 
          fi->attrib=0;
380
 
        }
381
 
      else
382
 
        {
383
 
          fi->date=f_info.st_mtime;
384
 
          fi->size=f_info.st_size;
385
 
          fi->user=f_info.st_uid;
386
 
          fi->group=f_info.st_gid;
387
 
          fi->attrib=f_info.st_mode;
388
 
        }
389
 
      clist_win.dirlist=g_list_prepend(clist_win.dirlist, (gpointer)fi);
390
 
    }
391
 
  if(closedir(dir)==-1)
392
 
    {
393
 
      fprintf(stderr, _("Closedir error\n"));
394
 
    }
395
 
}
396
 
 
397
 
int c_directory_list(void)
398
 
{
399
 
  TicalcVarInfo varlist;
400
 
  int n;
401
 
  int ret = 0;
402
 
 
403
 
  gif->create_pbar_type2(_("Directory list"), 
404
 
                         _("Reading variables"));
405
 
  if(tilp_error(ti_calc.directorylist(&varlist, &n)))
406
 
    {
407
 
      varlist.next=NULL;
408
 
      n=0;
409
 
      ret = -1;
410
 
    }
411
 
  gif->destroy_pbar();
412
 
 
413
 
  varlist_to_glist(varlist);
414
 
  
415
 
  return ret;
416
 
}
417
 
 
418
 
/*********************/
419
 
/* Sorting functions */
420
 
/*********************/
421
 
 
422
 
/* 
423
 
   For these routines I have used the worst sorting method but the easiest: 
424
 
   the bubble sort algorithm !!! 
425
 
*/
426
 
 
427
 
 
428
 
void sort_lfiles_by_type(GList *list)
429
 
{
430
 
  GList *p, *q;
431
 
  int i, j, end, max;
432
 
  gpointer tmp;
433
 
  TilpFileInfo *fi_p, *fi_q;
434
 
 
435
 
  max=g_list_length(list);
436
 
  for (i=max-1; i>0; i=end)
437
 
    {
438
 
      end=0;
439
 
      for(j=0, p=list; j<i; j++, p=p->next)
440
 
        {
441
 
          q=p->next;
442
 
          fi_p=p->data;
443
 
          fi_q=q->data;
444
 
          if( (fi_q->attrib & S_IFMT) == S_IFDIR  )
445
 
            {
446
 
              end=j;
447
 
              tmp=p->data;
448
 
              p->data=q->data;
449
 
              q->data=tmp;
450
 
            }
451
 
        }
452
 
    }
453
 
}
454
 
 
455
 
void sort_lfiles_by_name(GList *list)
456
 
{
457
 
  GList *p, *q;
458
 
  int i, j, end, max;
459
 
  gpointer tmp;
460
 
  TilpFileInfo *fi_p, *fi_q;
461
 
 
462
 
  sort_lfiles_by_type(list);
463
 
 
464
 
  max=g_list_length(list);
465
 
  for (i=max-1; i>0; i=end)
466
 
    {
467
 
      end=0;
468
 
      for(j=0, p=list; j<i; j++, p=p->next)
469
 
        {
470
 
          q=p->next;
471
 
          fi_p=p->data;
472
 
          fi_q=q->data;
473
 
          if( (((fi_p->attrib & S_IFMT) == S_IFDIR) && ((fi_q->attrib & S_IFMT) == S_IFDIR)) || 
474
 
              (((fi_p->attrib & S_IFMT) != S_IFDIR) && ((fi_q->attrib & S_IFMT) != S_IFDIR)) )
475
 
            {
476
 
              if(strcmp(fi_p->filename, fi_q->filename) > 0)
477
 
                {
478
 
                  end=j;
479
 
                  tmp=p->data;
480
 
                  p->data=q->data;
481
 
                  q->data=tmp;
482
 
                }
483
 
            }
484
 
          else
485
 
            {
486
 
              if( ((fi_q->attrib & S_IFMT) == S_IFDIR) && (strcmp(fi_p->filename, fi_q->filename) > 0) )
487
 
                {
488
 
                  end=j;
489
 
                  tmp=p->data;
490
 
                  p->data=q->data;
491
 
                  q->data=tmp;
492
 
                }
493
 
            }
494
 
        }
495
 
    }
496
 
}
497
 
 
498
 
/* Sort files by date (smallest to biggest size) */
499
 
void sort_lfiles_by_date(GList *list)
500
 
{
501
 
  GList *p, *q;
502
 
  int i, j, end, max;
503
 
  gpointer tmp;
504
 
  TilpFileInfo *fi_p, *fi_q;
505
 
 
506
 
  max=g_list_length(list);
507
 
  for (i=max-1; i>0; i=end)
508
 
    {
509
 
      end=0;
510
 
      for(j=0, p=list; j<i; j++, p=p->next)
511
 
        {
512
 
          q=p->next;
513
 
          fi_p=p->data;
514
 
          fi_q=q->data;
515
 
          if( (((fi_p->attrib & S_IFMT) == S_IFDIR) && ((fi_q->attrib & S_IFMT) == S_IFDIR)) || 
516
 
              (((fi_p->attrib & S_IFMT) != S_IFDIR) && ((fi_q->attrib & S_IFMT) != S_IFDIR)) )
517
 
            {
518
 
              if(fi_p->date > fi_q->date)
519
 
                {
520
 
                  end=j;
521
 
                  tmp=p->data;
522
 
                  p->data=q->data;
523
 
                  q->data=tmp;
524
 
                }
525
 
            }
526
 
          else
527
 
            {
528
 
              if( ((fi_q->attrib & S_IFMT) == S_IFDIR) && (fi_p->date > fi_q->date) )
529
 
                {
530
 
                  end=j;
531
 
                  tmp=p->data;
532
 
                  p->data=q->data;
533
 
                  q->data=tmp;
534
 
                }
535
 
            }
536
 
        }
537
 
    }
538
 
}
539
 
 
540
 
void sort_lfiles_by_size2(GList *list);
541
 
 
542
 
void sort_lfiles_by_size(GList *list)
543
 
{
544
 
  sort_lfiles_by_size2(list);
545
 
  //g_list_sort(list, GCompareComputerSizes);
546
 
}
547
 
 
548
 
void sort_lfiles_by_size2(GList *list)
549
 
{
550
 
  GList *p, *q;
551
 
  int i, j, end, max;
552
 
  gpointer tmp;
553
 
  TilpFileInfo *fi_p, *fi_q;
554
 
 
555
 
  max=g_list_length(list);
556
 
  for (i=max-1; i>0; i=end)
557
 
    {
558
 
      end=0;
559
 
      for(j=0, p=list; j<i; j++, p=p->next)
560
 
        {
561
 
          q=p->next;
562
 
          fi_p=p->data;
563
 
          fi_q=q->data;
564
 
          if( (((fi_p->attrib & S_IFMT) == S_IFDIR) && 
565
 
               ((fi_q->attrib & S_IFMT) == S_IFDIR)) || 
566
 
              (((fi_p->attrib & S_IFMT) != S_IFDIR) && 
567
 
               ((fi_q->attrib & S_IFMT) != S_IFDIR)) )
568
 
            {
569
 
              if(fi_p->size > fi_q->size)
570
 
                {
571
 
                  end=j;
572
 
                  tmp=p->data;
573
 
                  p->data=q->data;
574
 
                  q->data=tmp;
575
 
                }
576
 
            }
577
 
          else if( ((fi_q->attrib & S_IFMT) == S_IFDIR))
578
 
            {
579
 
              if(fi_p->size > fi_q->size)
580
 
                {
581
 
                  end=j;
582
 
                  tmp=p->data;
583
 
                  p->data=q->data;
584
 
                  q->data=tmp;
585
 
                }
586
 
            }
587
 
        }
588
 
    }
589
 
}
590
 
 
591
 
/* Sort files by user (smallest to biggest size) */
592
 
void sort_lfiles_by_user(GList *list)
593
 
{
594
 
  GList *p, *q;
595
 
  int i, j, end, max;
596
 
  gpointer tmp;
597
 
  TilpFileInfo *fi_p, *fi_q;
598
 
 
599
 
  max=g_list_length(list);
600
 
  for (i=max-1; i>0; i=end)
601
 
    {
602
 
      end=0;
603
 
      for(j=0, p=list; j<i; j++, p=p->next)
604
 
        {
605
 
          q=p->next;
606
 
          fi_p=p->data;
607
 
          fi_q=q->data;
608
 
          if( (((fi_p->attrib & S_IFMT) == S_IFDIR) && ((fi_q->attrib & S_IFMT) == S_IFDIR)) || 
609
 
              (((fi_p->attrib & S_IFMT) != S_IFDIR) && ((fi_q->attrib & S_IFMT) != S_IFDIR)) )
610
 
            {
611
 
              if(fi_p->user > fi_q->user)
612
 
                {
613
 
                  end=j;
614
 
                  tmp=p->data;
615
 
                  p->data=q->data;
616
 
                  q->data=tmp;
617
 
                }
618
 
            }
619
 
          else
620
 
            {
621
 
              if( ((fi_q->attrib & S_IFMT) == S_IFDIR) && (fi_p->user > fi_q->user) )
622
 
                {
623
 
                  end=j;
624
 
                  tmp=p->data;
625
 
                  p->data=q->data;
626
 
                  q->data=tmp;
627
 
                }
628
 
            }
629
 
        }
630
 
    }
631
 
}
632
 
 
633
 
/* Sort files by group (smallest to biggest size) */
634
 
void sort_lfiles_by_group(GList *list)
635
 
{
636
 
  GList *p, *q;
637
 
  int i, j, end, max;
638
 
  gpointer tmp;
639
 
  TilpFileInfo *fi_p, *fi_q;
640
 
 
641
 
  max=g_list_length(list);
642
 
  for (i=max-1; i>0; i=end)
643
 
    {
644
 
      end=0;
645
 
      for(j=0, p=list; j<i; j++, p=p->next)
646
 
        {
647
 
          q=p->next;
648
 
          fi_p=p->data;
649
 
          fi_q=q->data;
650
 
          if( (((fi_p->attrib & S_IFMT) == S_IFDIR) && ((fi_q->attrib & S_IFMT) == S_IFDIR)) || 
651
 
              (((fi_p->attrib & S_IFMT) != S_IFDIR) && ((fi_q->attrib & S_IFMT) != S_IFDIR)) )
652
 
            {
653
 
              if(fi_p->group > fi_q->group)
654
 
                {
655
 
                  end=j;
656
 
                  tmp=p->data;
657
 
                  p->data=q->data;
658
 
                  q->data=tmp;
659
 
                }
660
 
            }
661
 
          else
662
 
            {
663
 
              if( ((fi_q->attrib & S_IFMT) == S_IFDIR) && (fi_p->group > fi_q->group) )
664
 
                {
665
 
                  end=j;
666
 
                  tmp=p->data;
667
 
                  p->data=q->data;
668
 
                  q->data=tmp;
669
 
                }
670
 
            }
671
 
        }
672
 
    }
673
 
}
674
 
 
675
 
/* Sort files by attributes (smallest to biggest size) */
676
 
void sort_lfiles_by_attrib(GList *list)
677
 
{
678
 
  GList *p, *q;
679
 
  int i, j, end, max;
680
 
  gpointer tmp;
681
 
  TilpFileInfo *fi_p, *fi_q;
682
 
 
683
 
  max=g_list_length(list);
684
 
  for (i=max-1; i>0; i=end)
685
 
    {
686
 
      end=0;
687
 
      for(j=0, p=list; j<i; j++, p=p->next)
688
 
        {
689
 
          q=p->next;
690
 
          fi_p=p->data;
691
 
          fi_q=q->data;
692
 
          if( (((fi_p->attrib & S_IFMT) == S_IFDIR) && ((fi_q->attrib & S_IFMT) == S_IFDIR)) || 
693
 
              (((fi_p->attrib & S_IFMT) != S_IFDIR) && ((fi_q->attrib & S_IFMT) != S_IFDIR)) )
694
 
            {
695
 
              if(fi_p->attrib > fi_q->attrib)
696
 
                {
697
 
                  end=j;
698
 
                  tmp=p->data;
699
 
                  p->data=q->data;
700
 
                  q->data=tmp;
701
 
                }
702
 
            }
703
 
          else
704
 
            {
705
 
              if( ((fi_q->attrib & S_IFMT) == S_IFDIR) && (fi_p->attrib > fi_q->attrib) )
706
 
                {
707
 
                  end=j;
708
 
                  tmp=p->data;
709
 
                  p->data=q->data;
710
 
                  q->data=tmp;
711
 
                }
712
 
            }
713
 
        }
714
 
    }
715
 
}
716
 
 
717
 
/* Return the filename or its extension if it has one */
718
 
char *file_extension(char *filename)
719
 
{
720
 
  int i;
721
 
  char *p;
722
 
  
723
 
  for(i=strlen(filename); i > 0; i--)
724
 
    {
725
 
      if(filename[i] == '.') break;
726
 
    }
727
 
  p=filename+i+1;
728
 
  
729
 
  return p;
730
 
}