1
/* tilp - a linking program for TI graphing calculators
2
* Copyright (C) 1999-2002 Romain Lievin
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.
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.
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.
20
This file contains utility functions about files, attributes,
21
sorting routines for selection.
22
These functions are mainly used by the right window.
39
#include <glib/glib.h>
45
#include "gui_indep.h"
49
/*******************************/
50
/* File manipulation functions */
51
/*******************************/
54
Copy a file from src to dst
56
int copy_file(char *src, char *dst)
62
if((in=fopen(src, "rb")) == NULL)
66
if((out=fopen(dst, "wb")) == NULL)
79
if(!CopyFile(src, dst, FALSE))
89
int move_file(char *src, char *dst)
94
ret=copy_file(src, dst);
98
if(!MoveFile(src, dst))
105
int delete_file(char *f)
110
if(!RemoveDirectory(f))
112
/* [X91] temporarily drop root privileges */
116
effective = geteuid();
122
gif->msg_box(_("Information"),
123
_("Unable to remove the file. You can not delete non empty folders !"));
133
/* Remove '\r' characters for GtkText */
134
void process_buffer(gchar *buf)
138
for(i=0; i<strlen(buf); i++)
140
if(buf[i]=='\r') buf[i]=' ';
144
/* Replace a '\r\n' or a '\n' by '\r' */
145
void process_unix2dos(gchar *buf)
152
if( (buf[i] == '\r') && (buf[i+1] == '\n') )
157
if( (buf[i] == '\n') && (buf[i+1] == '\r') )
162
if(buf[i] == '\r') buf[i] = '\n';
164
if( (buf[i] == '\r') || (buf[i] == '\n') )
176
/*************************************/
177
/* Extracting informations functions */
178
/*************************************/
181
Retrieve informations about attributes from a file info structure
184
char *get_attributes(TilpFileInfo f_info)
188
s=g_strdup(" ---------- ");
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)
194
if(f_info.attrib & S_IXUSR) s[4]='s';
197
else if(f_info.attrib & S_IXUSR) s[4]='x';
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)
203
if(f_info.attrib & S_IXGRP) s[7]='s';
206
else if(f_info.attrib & S_IXGRP) s[7]='x';
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)
212
if(f_info.attrib & S_IXOTH) s[10]='t';
215
else if(f_info.attrib & S_IXOTH) s[10]='x';
217
if(S_ISLNK(f_info.attrib))
224
switch(S_IFMT & f_info.attrib)
226
case S_IFBLK: s[1]='b';
228
case S_IFDIR: s[1]='d';
230
case S_IFCHR: s[1]='c';
232
case S_IFIFO: s[1]='p';
234
case S_IFSOCK: s[1]='s';
242
Returns the user's name
244
void get_user_name(TilpFileInfo f_info, char **name)
246
#if defined(__LINUX__)
247
struct passwd *pwuid;
249
if((pwuid=getpwuid(f_info.user)) == NULL)
255
*name=g_strdup(pwuid->pw_name);
263
Returns the group's name
265
void get_group_name(TilpFileInfo f_info, char **name)
267
#if defined(__LINUX__)
270
if((grpid=getgrgid(f_info.group)) == NULL)
276
*name=g_strdup(grpid->gr_name);
284
Return the date of file
286
void get_date(TilpFileInfo f_info, char **s)
292
p=ctime(&(f_info.date));
306
Returns the user's home directory
308
int get_home_path(char **path)
310
#if defined(__LINUX__)
315
//fprintf(stderr, "UID: %i\n", uid);
317
if((p = getpwuid(uid)) == NULL)
324
*path = g_strdup(p->pw_dir);
332
/****************************/
333
/* Directory list functions */
334
/****************************/
336
/* Used by the function below */
337
void free_file_info_struct(gpointer data)
339
g_free(((TilpFileInfo *)data)->filename);
343
/* Make a directory listing of the current directory and place the result
344
in the clist_win.dirlist GList
346
void l_directory_list()
353
if(clist_win.dirlist!=NULL)
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;
360
if( (dir=opendir(clist_win.cur_dir)) == NULL)
362
fprintf(stderr, _("Opendir error\n"));
364
while( (file=readdir(dir)) != NULL)
366
if(strcmp(file->d_name, ".")==0) { continue; }
367
if(strcmp(file->d_name, ".."))
369
if( ((file->d_name)[0]=='.') && (options.show == HIDE) ) { continue; }
371
fi=(TilpFileInfo *)g_malloc(sizeof(TilpFileInfo));
372
fi->filename=g_strdup(file->d_name);
373
if(stat(file->d_name, &f_info)!=0)
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;
389
clist_win.dirlist=g_list_prepend(clist_win.dirlist, (gpointer)fi);
391
if(closedir(dir)==-1)
393
fprintf(stderr, _("Closedir error\n"));
397
int c_directory_list(void)
399
TicalcVarInfo varlist;
403
gif->create_pbar_type2(_("Directory list"),
404
_("Reading variables"));
405
if(tilp_error(ti_calc.directorylist(&varlist, &n)))
413
varlist_to_glist(varlist);
418
/*********************/
419
/* Sorting functions */
420
/*********************/
423
For these routines I have used the worst sorting method but the easiest:
424
the bubble sort algorithm !!!
428
void sort_lfiles_by_type(GList *list)
433
TilpFileInfo *fi_p, *fi_q;
435
max=g_list_length(list);
436
for (i=max-1; i>0; i=end)
439
for(j=0, p=list; j<i; j++, p=p->next)
444
if( (fi_q->attrib & S_IFMT) == S_IFDIR )
455
void sort_lfiles_by_name(GList *list)
460
TilpFileInfo *fi_p, *fi_q;
462
sort_lfiles_by_type(list);
464
max=g_list_length(list);
465
for (i=max-1; i>0; i=end)
468
for(j=0, p=list; j<i; j++, p=p->next)
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)) )
476
if(strcmp(fi_p->filename, fi_q->filename) > 0)
486
if( ((fi_q->attrib & S_IFMT) == S_IFDIR) && (strcmp(fi_p->filename, fi_q->filename) > 0) )
498
/* Sort files by date (smallest to biggest size) */
499
void sort_lfiles_by_date(GList *list)
504
TilpFileInfo *fi_p, *fi_q;
506
max=g_list_length(list);
507
for (i=max-1; i>0; i=end)
510
for(j=0, p=list; j<i; j++, p=p->next)
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)) )
518
if(fi_p->date > fi_q->date)
528
if( ((fi_q->attrib & S_IFMT) == S_IFDIR) && (fi_p->date > fi_q->date) )
540
void sort_lfiles_by_size2(GList *list);
542
void sort_lfiles_by_size(GList *list)
544
sort_lfiles_by_size2(list);
545
//g_list_sort(list, GCompareComputerSizes);
548
void sort_lfiles_by_size2(GList *list)
553
TilpFileInfo *fi_p, *fi_q;
555
max=g_list_length(list);
556
for (i=max-1; i>0; i=end)
559
for(j=0, p=list; j<i; j++, p=p->next)
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)) )
569
if(fi_p->size > fi_q->size)
577
else if( ((fi_q->attrib & S_IFMT) == S_IFDIR))
579
if(fi_p->size > fi_q->size)
591
/* Sort files by user (smallest to biggest size) */
592
void sort_lfiles_by_user(GList *list)
597
TilpFileInfo *fi_p, *fi_q;
599
max=g_list_length(list);
600
for (i=max-1; i>0; i=end)
603
for(j=0, p=list; j<i; j++, p=p->next)
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)) )
611
if(fi_p->user > fi_q->user)
621
if( ((fi_q->attrib & S_IFMT) == S_IFDIR) && (fi_p->user > fi_q->user) )
633
/* Sort files by group (smallest to biggest size) */
634
void sort_lfiles_by_group(GList *list)
639
TilpFileInfo *fi_p, *fi_q;
641
max=g_list_length(list);
642
for (i=max-1; i>0; i=end)
645
for(j=0, p=list; j<i; j++, p=p->next)
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)) )
653
if(fi_p->group > fi_q->group)
663
if( ((fi_q->attrib & S_IFMT) == S_IFDIR) && (fi_p->group > fi_q->group) )
675
/* Sort files by attributes (smallest to biggest size) */
676
void sort_lfiles_by_attrib(GList *list)
681
TilpFileInfo *fi_p, *fi_q;
683
max=g_list_length(list);
684
for (i=max-1; i>0; i=end)
687
for(j=0, p=list; j<i; j++, p=p->next)
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)) )
695
if(fi_p->attrib > fi_q->attrib)
705
if( ((fi_q->attrib & S_IFMT) == S_IFDIR) && (fi_p->attrib > fi_q->attrib) )
717
/* Return the filename or its extension if it has one */
718
char *file_extension(char *filename)
723
for(i=strlen(filename); i > 0; i--)
725
if(filename[i] == '.') break;