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>
43 #if defined (__sun__) || defined (__sun) || defined (__sgi) || defined (__NetBSD__)
44 #include <sys/statvfs.h> /* Other modern unix os's should probably use this also */
45 #elif !defined(__FreeBSD__) && !defined(linux) && (defined(__sparc) || defined(__sparc__))
46 #include <sys/statfs.h>
49 #if defined (__FreeBSD__) || defined (__OpenBSD__)
50 #include <sys/param.h>
51 #include <sys/mount.h>
54 #if defined(linux) || defined(__CYGWIN32__) || defined(__hpux)
60 #include <sys/param.h>
61 #include <sys/mount.h>
62 #endif /* __APPLE__ */
66 #include <string.h> /* strcpy etc.. */
69 #include <sys/ioctl.h>
70 #include <unistd.h> /* */
74 #if !defined(__FreeBSD__) && !defined(__APPLE__)
79 #include <sys/types.h>
82 #include "BLI_winstuff.h"
87 #include "MEM_guardedalloc.h"
89 #include "DNA_listBase.h"
91 #include "BLI_listbase.h"
92 #include "BLI_linklist.h"
93 #include "BLI_path_util.h"
94 #include "BLI_storage.h"
95 #include "BLI_storage_types.h"
96 #include "BLI_string.h"
97 #include "BKE_utildefines.h"
100 static int totnum,actnum;
101 static struct direntry *files;
103 static struct ListBase dirbase_={
105 static struct ListBase *dirbase = &dirbase_;
108 char *BLI_getwdN(char *dir)
118 /* 160 is FILE_MAXDIR in filesel.c */
119 return( getcwd(dir, 160) );
125 int BLI_compare(struct direntry *entry1, struct direntry *entry2)
127 /* type is equal to stat.st_mode */
129 if (S_ISDIR(entry1->type)){
130 if (S_ISDIR(entry2->type)==0) return (-1);
132 if (S_ISDIR(entry2->type)) return (1);
134 if (S_ISREG(entry1->type)){
135 if (S_ISREG(entry2->type)==0) return (-1);
137 if (S_ISREG(entry2->type)) return (1);
139 if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
140 if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
142 /* make sure "." and ".." are always first */
143 if( strcmp(entry1->relname, ".")==0 ) return (-1);
144 if( strcmp(entry2->relname, ".")==0 ) return (1);
145 if( strcmp(entry1->relname, "..")==0 ) return (-1);
146 if( strcmp(entry2->relname, "..")==0 ) return (1);
148 return (BLI_natstrcmp(entry1->relname,entry2->relname));
152 double BLI_diskfree(char *dir)
155 DWORD sectorspc, bytesps, freec, clusters;
158 tmp[0]='\\'; tmp[1]=0; /* Just a failsafe */
159 if (dir[0]=='/' || dir[0]=='\\') {
162 } else if (dir[1]==':') {
169 GetDiskFreeSpace(tmp,§orspc, &bytesps, &freec, &clusters);
171 return (double) (freec*bytesps*sectorspc);
174 #if defined (__sun__) || defined (__sun) || defined (__sgi) || defined (__NetBSD__)
179 char name[FILE_MAXDIR],*slash;
180 int len = strlen(dir);
182 if (len >= FILE_MAXDIR) /* path too long */
188 slash = strrchr(name,'/');
189 if (slash) slash[1] = 0;
190 } else strcpy(name,"/");
192 #if defined (__FreeBSD__) || defined (linux) || defined (__OpenBSD__) || defined (__APPLE__)
193 if (statfs(name, &disk)) return(-1);
196 #if defined (__sun__) || defined (__sun) || defined (__sgi) || defined (__NetBSD__)
197 if (statvfs(name, &disk)) return(-1);
198 #elif !defined(__FreeBSD__) && !defined(linux) && (defined(__sparc) || defined(__sparc__))
199 /* WARNING - This may not be supported by geeneric unix os's - Campbell */
200 if (statfs(name, &disk, sizeof(struct statfs), 0)) return(-1);
203 return ( ((double) disk.f_bsize) * ((double) disk.f_bfree));
207 static int hide_dot= 0;
209 void BLI_hide_dot_files(int set)
215 void BLI_builddir(char *dirname, char *relname)
217 struct dirent *fname;
218 struct dirlink *dlink;
219 int rellen, newnum = 0, len;
224 rellen=strlen(relname);
231 if (chdir(dirname) == -1){
236 if ( (dir = (DIR *)opendir(".")) ){
237 while ((fname = (struct dirent*) readdir(dir)) != NULL) {
238 len= strlen(fname->d_name);
240 if(hide_dot && fname->d_name[0]=='.' && fname->d_name[1]!='.' && fname->d_name[1]!=0); /* ignore .file */
241 else if(hide_dot && len && fname->d_name[len-1]=='~'); /* ignore file~ */
242 else if (((fname->d_name[0] == '.') && (fname->d_name[1] == 0) )); /* ignore . */
244 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
246 strcpy(buf+rellen,fname->d_name);
247 dlink->name = BLI_strdup(buf);
248 BLI_addhead(dirbase,dlink);
256 if (files) files=(struct direntry *)realloc(files,(totnum+newnum) * sizeof(struct direntry));
257 else files=(struct direntry *)malloc(newnum * sizeof(struct direntry));
260 dlink = (struct dirlink *) dirbase->first;
262 memset(&files[actnum], 0 , sizeof(struct direntry));
263 files[actnum].relname = dlink->name;
264 // use 64 bit file size, only needed for WIN32 and WIN64.
265 // Excluding other than current MSVC compiler until able to test.
266 #if (defined(WIN32) || defined(WIN64)) && (_MSC_VER>=1500)
267 _stat64(dlink->name,&files[actnum].s);
269 stat(dlink->name,&files[actnum].s);
271 files[actnum].type=files[actnum].s.st_mode;
272 files[actnum].flags = 0;
278 printf("Couldn't get memory for dir\n");
282 BLI_freelist(dirbase);
283 if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))BLI_compare);
285 printf("%s empty directory\n",dirname);
290 printf("%s non-existant directory\n",dirname);
294 void BLI_adddirstrings()
299 static char * types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
307 struct direntry * file;
313 for(num=0;num<actnum;num++){
316 strcpy(file->mode1, types[0]);
317 strcpy(file->mode2, types[0]);
318 strcpy(file->mode3, types[0]);
320 mode = file->s.st_mode;
322 strcpy(file->mode1, types[(mode & 0700) >> 6]);
323 strcpy(file->mode2, types[(mode & 0070) >> 3]);
324 strcpy(file->mode3, types[(mode & 0007)]);
326 if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2]=='-'))file->mode2[2]='l';
328 if (mode & (S_ISUID | S_ISGID)){
329 if (file->mode1[2]=='x') file->mode1[2]='s';
330 else file->mode1[2]='S';
332 if (file->mode2[2]=='x')file->mode2[2]='s';
336 if (file->mode3[2] == 'x') file->mode3[2] = 't';
337 else file->mode3[2] = 'T';
342 strcpy(files[num].owner,"user");
345 struct passwd *pwuser;
346 pwuser = getpwuid(files[num].s.st_uid);
348 strcpy(files[num].owner, pwuser->pw_name);
350 sprintf(files[num].owner, "%d", files[num].s.st_uid);
355 tm= localtime(&files[num].s.st_mtime);
356 // prevent impossible dates in windows
357 if(tm==NULL) tm= localtime(&zero);
358 strftime(files[num].time, 8, "%H:%M", tm);
359 strftime(files[num].date, 16, "%d-%b-%y", tm);
362 * Seems st_size is signed 32-bit value in *nix and Windows. This
363 * will buy us some time until files get bigger than 4GB or until
364 * everyone starts using __USE_FILE_OFFSET64 or equivalent.
366 st_size= files[num].s.st_size;
368 if (st_size > 1024*1024*1024) {
369 sprintf(files[num].size, "%.2f GB", ((double)st_size)/(1024*1024*1024));
371 else if (st_size > 1024*1024) {
372 sprintf(files[num].size, "%.1f MB", ((double)st_size)/(1024*1024));
374 else if (st_size > 1024) {
375 sprintf(files[num].size, "%d KB", (int)(st_size/1024));
378 sprintf(files[num].size, "%d B", (int)st_size);
381 strftime(datum, 32, "%d-%b-%y %H:%M", tm);
383 if (st_size < 1000) {
384 sprintf(size, "%10d", (int) st_size);
385 } else if (st_size < 1000 * 1000) {
386 sprintf(size, "%6d %03d", (int) (st_size / 1000), (int) (st_size % 1000));
387 } else if (st_size < 100 * 1000 * 1000) {
388 sprintf(size, "%2d %03d %03d", (int) (st_size / (1000 * 1000)), (int) ((st_size / 1000) % 1000), (int) ( st_size % 1000));
390 sprintf(size, "> %4.1f M", (double) (st_size / (1024.0 * 1024.0)));
391 sprintf(size, "%10d", (int) st_size);
394 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,
397 files[num].string=MEM_mallocN(strlen(buf)+1, "filestring");
398 if (files[num].string){
399 strcpy(files[num].string,buf);
406 unsigned int BLI_getdir(char *dirname, struct direntry **filelist)
408 // reset global variables
409 // memory stored in files is free()'d in
410 // filesel.c:freefilelist()
415 BLI_builddir(dirname,"");
421 // keep blender happy. Blender stores this in a variable
422 // where 0 has special meaning.....
423 *(filelist) = files = malloc(sizeof(struct direntry));
430 int BLI_filesize(int file)
434 if (file <= 0) return (-1);
436 return (buf.st_size);
439 int BLI_filepathsize(const char *path)
441 int size, file = open(path, O_BINARY|O_RDONLY);
446 size = BLI_filesize(file);
452 int BLI_exist(char *name)
456 /* in Windows stat doesn't recognize dir ending on a slash
457 To not break code where the ending slash is expected we
458 don't mess with the argument name directly here - elubie */
459 char tmp[FILE_MAXDIR+FILE_MAXFILE];
461 BLI_strncpy(tmp, name, FILE_MAXDIR+FILE_MAXFILE);
463 if (len > 3 && ( tmp[len-1]=='\\' || tmp[len-1]=='/') ) tmp[len-1] = '\0';
464 if (stat(tmp,&st)) return(0);
466 if (stat(name,&st)) return(0);
471 /* would be better in fileops.c except that it needs stat.h so add here */
472 int BLI_is_dir(char *file) {
473 return S_ISDIR(BLI_exist(file));
476 LinkNode *BLI_read_file_as_lines(char *name)
478 FILE *fp= fopen(name, "r");
479 LinkNode *lines= NULL;
483 if (!fp) return NULL;
485 fseek(fp, 0, SEEK_END);
487 fseek(fp, 0, SEEK_SET);
494 * size = because on win32 reading
495 * all the bytes in the file will return
496 * less bytes because of crnl changes.
498 size= fread(buf, 1, size, fp);
499 for (i=0; i<=size; i++) {
500 if (i==size || buf[i]=='\n') {
501 char *line= BLI_strdupn(&buf[last], i-last);
503 BLI_linklist_prepend(&lines, line);
513 BLI_linklist_reverse(&lines);
517 void BLI_free_file_lines(LinkNode *lines)
519 BLI_linklist_free(lines, (void(*)(void*)) MEM_freeN);