4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * Reorganised mar-01 nzc
29 * Some really low-level file thingies.
32 #include <sys/types.h>
37 #include "BLI_winstuff.h"
38 #include <sys/types.h>
50 #if defined (__sun__) || defined (__sun) || defined (__sgi)
51 #include <sys/statvfs.h> /* Other modern unix os's should probably use this also */
52 #elif !defined(__FreeBSD__) && !defined(linux) && (defined(__sparc) || defined(__sparc__))
53 #include <sys/statfs.h>
56 #if defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__NetBSD__)
57 #include <sys/param.h>
58 #include <sys/mount.h>
61 #if defined(linux) || defined(__CYGWIN32__) || defined(__hpux)
67 #include <sys/param.h>
68 #include <sys/mount.h>
69 #endif /* __APPLE__ */
74 #include <sys/mtio.h> /* tape comando's */
76 #include <string.h> /* strcpy etc.. */
79 #include <sys/ioctl.h>
80 #include <unistd.h> /* */
84 #if !defined(__FreeBSD__) && !defined(__APPLE__)
89 #include "MEM_guardedalloc.h"
91 #include "DNA_listBase.h"
92 #include "BLI_blenlib.h"
93 #include "BLI_storage.h"
94 #include "BLI_storage_types.h"
97 #include "BLI_linklist.h"
99 #include "BKE_utildefines.h"
102 static int totnum,actnum;
103 static struct direntry *files;
105 static struct ListBase dirbase_={
107 static struct ListBase *dirbase = &dirbase_;
110 char *BLI_getwdN(char *dir)
120 /* 160 is FILE_MAXDIR in filesel.c */
121 return( getcwd(dir, 160) );
127 int BLI_compare(struct direntry *entry1, struct direntry *entry2)
129 /* type is equal to stat.st_mode */
131 if (S_ISDIR(entry1->type)){
132 if (S_ISDIR(entry2->type)==0) return (-1);
134 if (S_ISDIR(entry2->type)) return (1);
136 if (S_ISREG(entry1->type)){
137 if (S_ISREG(entry2->type)==0) return (-1);
139 if (S_ISREG(entry2->type)) return (1);
141 if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
142 if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
144 /* make sure "." and ".." are always first */
145 if( strcmp(entry1->relname, ".")==0 ) return (-1);
146 if( strcmp(entry2->relname, ".")==0 ) return (1);
147 if( strcmp(entry1->relname, "..")==0 ) return (-1);
148 if( strcmp(entry2->relname, "..")==0 ) return (1);
150 return (BLI_natstrcmp(entry1->relname,entry2->relname));
154 double BLI_diskfree(char *dir)
157 DWORD sectorspc, bytesps, freec, clusters;
160 tmp[0]='\\'; tmp[1]=0; /* Just a failsafe */
161 if (dir[0]=='/' || dir[0]=='\\') {
164 } else if (dir[1]==':') {
171 GetDiskFreeSpace(tmp,§orspc, &bytesps, &freec, &clusters);
173 return (double) (freec*bytesps*sectorspc);
176 #if defined (__sun__) || defined (__sun) || defined (__sgi)
181 char name[FILE_MAXDIR],*slash;
182 int len = strlen(dir);
184 if (len >= FILE_MAXDIR) /* path too long */
190 slash = strrchr(name,'/');
191 if (slash) slash[1] = 0;
192 } else strcpy(name,"/");
194 #if defined (__FreeBSD__) || defined (linux) || defined (__OpenBSD__) || defined (__APPLE__)
195 if (statfs(name, &disk)) return(-1);
198 #if defined (__sun__) || defined (__sun) || defined (__sgi)
199 if (statvfs(name, &disk)) return(-1);
200 #elif !defined(__FreeBSD__) && !defined(linux) && (defined(__sparc) || defined(__sparc__))
201 /* WARNING - This may not be supported by geeneric unix os's - Campbell */
202 if (statfs(name, &disk, sizeof(struct statfs), 0)) return(-1);
205 return ( ((double) disk.f_bsize) * ((double) disk.f_bfree));
209 static int hide_dot= 0;
211 void BLI_hide_dot_files(int set)
217 void BLI_builddir(char *dirname, char *relname)
219 struct dirent *fname;
220 struct dirlink *dlink;
221 int rellen, newnum = 0;
226 rellen=strlen(relname);
233 if (chdir(dirname) == -1){
238 if ( (dir = (DIR *)opendir(".")) ){
239 while ((fname = (struct dirent*) readdir(dir)) != NULL) {
241 if(hide_dot && fname->d_name[0]=='.' && fname->d_name[1]!='.' && fname->d_name[1]!=0) {
243 else if ( ( (fname->d_name[0] == '.') && (fname->d_name[1] == 0) ) ||
244 ( (fname->d_name[0] == '.') && (fname->d_name[1] == '.') && (fname->d_name[2] == 0)) ) {
245 /* ignore '.' and '..' */
248 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
250 strcpy(buf+rellen,fname->d_name);
251 dlink->name = BLI_strdup(buf);
252 BLI_addhead(dirbase,dlink);
260 if (files) files=(struct direntry *)realloc(files,(totnum+newnum) * sizeof(struct direntry));
261 else files=(struct direntry *)malloc(newnum * sizeof(struct direntry));
264 dlink = (struct dirlink *) dirbase->first;
266 memset(&files[actnum], 0 , sizeof(struct direntry));
267 files[actnum].relname = dlink->name;
268 stat(dlink->name,&files[actnum].s);
269 files[actnum].type=files[actnum].s.st_mode;
270 files[actnum].flags = 0;
276 printf("Couldn't get memory for dir\n");
280 BLI_freelist(dirbase);
281 if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))BLI_compare);
283 printf("%s empty directory\n",dirname);
288 printf("%s non-existant directory\n",dirname);
292 void BLI_adddirstrings()
297 static char * types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
305 struct direntry * file;
311 for(num=0;num<actnum;num++){
314 strcpy(file->mode1, types[0]);
315 strcpy(file->mode2, types[0]);
316 strcpy(file->mode3, types[0]);
318 mode = file->s.st_mode;
320 strcpy(file->mode1, types[(mode & 0700) >> 6]);
321 strcpy(file->mode2, types[(mode & 0070) >> 3]);
322 strcpy(file->mode3, types[(mode & 0007)]);
324 if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2]=='-'))file->mode2[2]='l';
326 if (mode & (S_ISUID | S_ISGID)){
327 if (file->mode1[2]=='x') file->mode1[2]='s';
328 else file->mode1[2]='S';
330 if (file->mode2[2]=='x')file->mode2[2]='s';
334 if (file->mode3[2] == 'x') file->mode3[2] = 't';
335 else file->mode3[2] = 'T';
340 strcpy(files[num].owner,"user");
343 struct passwd *pwuser;
344 pwuser = getpwuid(files[num].s.st_uid);
346 strcpy(files[num].owner, pwuser->pw_name);
348 sprintf(files[num].owner, "%d", files[num].s.st_uid);
353 tm= localtime(&files[num].s.st_mtime);
354 // prevent impossible dates in windows
355 if(tm==NULL) tm= localtime(&zero);
356 strftime(files[num].time, 8, "%H:%M", tm);
357 strftime(files[num].date, 16, "%d-%b-%y", tm);
360 * Seems st_size is signed 32-bit value in *nix and Windows. This
361 * will buy us some time until files get bigger than 4GB or until
362 * everyone starts using __USE_FILE_OFFSET64 or equivalent.
364 st_size= (off_t)files[num].s.st_size;
366 if (st_size > 1024*1024*1024) {
367 sprintf(files[num].size, "%.2f GB", ((double)st_size)/(1024*1024*1024));
369 else if (st_size > 1024*1024) {
370 sprintf(files[num].size, "%.1f MB", ((double)st_size)/(1024*1024));
372 else if (st_size > 1024) {
373 sprintf(files[num].size, "%d KB", (int)(st_size/1024));
376 sprintf(files[num].size, "%d B", (int)st_size);
379 strftime(datum, 32, "%d-%b-%y %H:%M", tm);
381 if (st_size < 1000) {
382 sprintf(size, "%10d", (int) st_size);
383 } else if (st_size < 1000 * 1000) {
384 sprintf(size, "%6d %03d", (int) (st_size / 1000), (int) (st_size % 1000));
385 } else if (st_size < 100 * 1000 * 1000) {
386 sprintf(size, "%2d %03d %03d", (int) (st_size / (1000 * 1000)), (int) ((st_size / 1000) % 1000), (int) ( st_size % 1000));
388 sprintf(size, "> %4.1f M", (double) (st_size / (1024.0 * 1024.0)));
389 sprintf(size, "%10d", (int) st_size);
392 sprintf(buf,"%s %s %s %7s %s %s %10s %s", file->mode1, file->mode2, file->mode3, files[num].owner, files[num].date, files[num].time, size,
395 files[num].string=MEM_mallocN(strlen(buf)+1, "filestring");
396 if (files[num].string){
397 strcpy(files[num].string,buf);
404 unsigned int BLI_getdir(char *dirname, struct direntry **filelist)
406 // reset global variables
407 // memory stored in files is free()'d in
408 // filesel.c:freefilelist()
413 BLI_builddir(dirname,"");
419 // keep blender happy. Blender stores this in a variable
420 // where 0 has special meaning.....
421 *(filelist) = files = malloc(sizeof(struct direntry));
428 int BLI_filesize(int file)
432 if (file <= 0) return (-1);
434 return (buf.st_size);
437 int BLI_filepathsize(const char *path)
439 int size, file = open(path, O_BINARY|O_RDONLY);
444 size = BLI_filesize(file);
450 int BLI_exist(char *name)
454 /* in Windows stat doesn't recognize dir ending on a slash
455 To not break code where the ending slash is expected we
456 don't mess with the argument name directly here - elubie */
457 char tmp[FILE_MAXDIR+FILE_MAXFILE];
459 BLI_strncpy(tmp, name, FILE_MAXDIR+FILE_MAXFILE);
461 if (len > 3 && ( tmp[len-1]=='\\' || tmp[len-1]=='/') ) tmp[len-1] = '\0';
462 if (stat(tmp,&st)) return(0);
464 if (stat(name,&st)) return(0);
469 LinkNode *BLI_read_file_as_lines(char *name)
471 FILE *fp= fopen(name, "r");
472 LinkNode *lines= NULL;
476 if (!fp) return NULL;
478 fseek(fp, 0, SEEK_END);
480 fseek(fp, 0, SEEK_SET);
487 * size = because on win32 reading
488 * all the bytes in the file will return
489 * less bytes because of crnl changes.
491 size= fread(buf, 1, size, fp);
492 for (i=0; i<=size; i++) {
493 if (i==size || buf[i]=='\n') {
494 char *line= BLI_strdupn(&buf[last], i-last);
496 BLI_linklist_prepend(&lines, line);
506 BLI_linklist_reverse(&lines);
510 void BLI_free_file_lines(LinkNode *lines)
512 BLI_linklist_free(lines, (void(*)(void*)) MEM_freeN);