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)
74 #include <sys/param.h>
75 #include <sys/mount.h>
76 #endif /* __APPLE__ */
80 #if !defined(__BeOS) && !defined(WIN32)
81 #include <sys/mtio.h> /* tape comando's */
83 #include <string.h> /* strcpy etc.. */
86 #include <sys/ioctl.h>
87 #include <unistd.h> /* */
91 #if !defined(__FreeBSD__) && !defined(__APPLE__)
96 #include "MEM_guardedalloc.h"
98 #include "DNA_listBase.h"
99 #include "BLI_blenlib.h"
100 #include "BLI_storage.h"
101 #include "BLI_storage_types.h"
103 #include "BLI_util.h"
104 #include "BLI_linklist.h"
106 #include "BKE_utildefines.h"
109 static int totnum,actnum;
110 static struct direntry *files;
112 static struct ListBase dirbase_={
114 static struct ListBase *dirbase = &dirbase_;
117 char *BLI_getwdN(char *dir)
127 /* 160 is FILE_MAXDIR in filesel.c */
128 return( getcwd(dir, 160) );
134 int BLI_compare(struct direntry *entry1, struct direntry *entry2)
136 /* type is equal to stat.st_mode */
138 if (S_ISDIR(entry1->type)){
139 if (S_ISDIR(entry2->type)==0) return (-1);
141 if (S_ISDIR(entry2->type)) return (1);
143 if (S_ISREG(entry1->type)){
144 if (S_ISREG(entry2->type)==0) return (-1);
146 if (S_ISREG(entry2->type)) return (1);
148 if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
149 if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
151 /* make sure "." and ".." are always first */
152 if( strcmp(entry1->relname, ".")==0 ) return (-1);
153 if( strcmp(entry2->relname, ".")==0 ) return (1);
154 if( strcmp(entry1->relname, "..")==0 ) return (-1);
156 return (BLI_strcasecmp(entry1->relname,entry2->relname));
160 double BLI_diskfree(char *dir)
163 DWORD sectorspc, bytesps, freec, clusters;
166 tmp[0]='\\'; tmp[1]=0; /* Just a failsafe */
167 if (dir[0]=='/' || dir[0]=='\\') {
170 } else if (dir[1]==':') {
177 GetDiskFreeSpace(tmp,§orspc, &bytesps, &freec, &clusters);
179 return (double) (freec*bytesps*sectorspc);
182 #if defined (__sun__) || defined (__sun) || defined (__sgi)
187 char name[FILE_MAXDIR],*slash;
188 int len = strlen(dir);
190 if (len >= FILE_MAXDIR) /* path too long */
196 slash = strrchr(name,'/');
197 if (slash) slash[1] = 0;
198 } else strcpy(name,"/");
200 #if defined (__FreeBSD__) || defined (linux) || defined (__OpenBSD__) || defined (__APPLE__)
201 if (statfs(name, &disk)) return(-1);
207 #if defined (__sun__) || defined (__sun)
208 if (statvfs(name, &disk)) return(-1);
209 #elif !defined(__FreeBSD__) && !defined(linux) && (defined(__sparc) || defined(__sparc__))
210 /* WARNING - This may not be supported by geeneric unix os's - Campbell */
211 if (statfs(name, &disk, sizeof(struct statfs), 0)) return(-1);
214 return ( ((double) disk.f_bsize) * ((double) disk.f_bfree));
218 static int hide_dot= 0;
220 void BLI_hide_dot_files(int set)
226 void BLI_builddir(char *dirname, char *relname)
228 struct dirent *fname;
229 struct dirlink *dlink;
230 int rellen, newnum = 0, seen_ = 0, seen__ = 0;
235 rellen=strlen(relname);
242 if (chdir(dirname) == -1){
247 if ( (dir = (DIR *)opendir(".")) ){
248 while ((fname = (struct dirent*) readdir(dir)) != NULL) {
250 if(hide_dot && fname->d_name[0]=='.' && fname->d_name[1]!='.' && fname->d_name[1]!=0);
253 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
255 strcpy(buf+rellen,fname->d_name);
257 dlink->name = BLI_strdup(buf);
259 if (dlink->name[0] == '.') {
260 if (dlink->name[1] == 0) seen_ = 1;
261 else if (dlink->name[1] == '.') {
262 if (dlink->name[2] == 0) seen__ = 1;
265 BLI_addhead(dirbase,dlink);
273 if (seen_ == 0) { /* Cachefs PATCH */
274 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
275 strcpy(buf+rellen,"./.");
276 dlink->name = BLI_strdup(buf);
277 BLI_addhead(dirbase,dlink);
280 if (seen__ == 0) { /* MAC PATCH */
281 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
282 strcpy(buf+rellen,"./..");
283 dlink->name = BLI_strdup(buf);
284 BLI_addhead(dirbase,dlink);
288 if (seen_ == 0) { /* should only happen for root paths like "C:\" */
289 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
290 strcpy(buf+rellen,".");
291 dlink->name = BLI_strdup(buf);
292 BLI_addhead(dirbase,dlink);
297 if (files) files=(struct direntry *)realloc(files,(totnum+newnum) * sizeof(struct direntry));
298 else files=(struct direntry *)malloc(newnum * sizeof(struct direntry));
301 dlink = (struct dirlink *) dirbase->first;
303 memset(&files[actnum], 0 , sizeof(struct direntry));
304 files[actnum].relname = dlink->name;
305 stat(dlink->name,&files[actnum].s);
306 files[actnum].type=files[actnum].s.st_mode;
307 files[actnum].flags = 0;
313 printf("Couldn't get memory for dir\n");
317 BLI_freelist(dirbase);
318 if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))BLI_compare);
320 printf("%s empty directory\n",dirname);
325 printf("%s non-existant directory\n",dirname);
329 void BLI_adddirstrings()
334 static char * types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
336 off_t num1, num2, num3, num4, num5;
343 struct direntry * file;
349 for(num=0;num<actnum;num++){
352 strcpy(file->mode1, types[0]);
353 strcpy(file->mode2, types[0]);
354 strcpy(file->mode3, types[0]);
356 mode = file->s.st_mode;
358 strcpy(file->mode1, types[(mode & 0700) >> 6]);
359 strcpy(file->mode2, types[(mode & 0070) >> 3]);
360 strcpy(file->mode3, types[(mode & 0007)]);
362 if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2]=='-'))file->mode2[2]='l';
364 if (mode & (S_ISUID | S_ISGID)){
365 if (file->mode1[2]=='x') file->mode1[2]='s';
366 else file->mode1[2]='S';
368 if (file->mode2[2]=='x')file->mode2[2]='s';
372 if (file->mode3[2] == 'x') file->mode3[2] = 't';
373 else file->mode3[2] = 'T';
378 strcpy(files[num].owner,"user");
381 struct passwd *pwuser;
382 pwuser = getpwuid(files[num].s.st_uid);
384 strcpy(files[num].owner, pwuser->pw_name);
386 sprintf(files[num].owner, "%d", files[num].s.st_uid);
391 tm= localtime(&files[num].s.st_mtime);
392 // prevent impossible dates in windows
393 if(tm==NULL) tm= localtime(&zero);
394 strftime(files[num].time, 8, "%H:%M", tm);
395 strftime(files[num].date, 16, "%d-%b-%y", tm);
398 * Seems st_size is signed 32-bit value in *nix and Windows. This
399 * will buy us some time until files get bigger than 4GB or until
400 * everyone starts using __USE_FILE_OFFSET64 or equivalent.
402 st_size= (off_t)files[num].s.st_size;
404 num1= st_size % 1000;
407 num3= st_size/(1000*1000);
409 num4= st_size/(1000*1000*1000);
411 num5= st_size/(1000000000000LL);
415 sprintf(files[num].size, "%1d %03d %03d %03d K", (int)num5, (int)num4, (int)num3, (int)num2);
416 else if(num4) sprintf(files[num].size, "%3d %03d %03d %03d", (int)num4, (int)num3, (int)num2, (int)num1);
417 else if(num3) sprintf(files[num].size, "%7d %03d %03d", (int)num3, (int)num2, (int)num1);
418 else if(num2) sprintf(files[num].size, "%11d %03d", (int)num2, (int)num1);
419 else if(num1) sprintf(files[num].size, "%15d", (int)num1);
420 else sprintf(files[num].size, "0");
422 strftime(datum, 32, "%d-%b-%y %H:%M", tm);
424 if (st_size < 1000) {
425 sprintf(size, "%10d", (int) st_size);
426 } else if (st_size < 1000 * 1000) {
427 sprintf(size, "%6d %03d", (int) (st_size / 1000), (int) (st_size % 1000));
428 } else if (st_size < 100 * 1000 * 1000) {
429 sprintf(size, "%2d %03d %03d", (int) (st_size / (1000 * 1000)), (int) ((st_size / 1000) % 1000), (int) ( st_size % 1000));
431 sprintf(size, "> %4.1f M", (double) (st_size / (1024.0 * 1024.0)));
432 sprintf(size, "%10d", (int) st_size);
435 sprintf(buf,"%s %s %10s %s", files[num].date, files[num].time, size,
438 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,
441 files[num].string=MEM_mallocN(strlen(buf)+1, "filestring");
442 if (files[num].string){
443 strcpy(files[num].string,buf);
450 unsigned int BLI_getdir(char *dirname, struct direntry **filelist)
452 // reset global variables
453 // memory stored in files is free()'d in
454 // filesel.c:freefilelist()
459 BLI_builddir(dirname,"");
465 // keep blender happy. Blender stores this in a variable
466 // where 0 has special meaning.....
467 *(filelist) = files = malloc(sizeof(struct direntry));
474 int BLI_filesize(int file)
478 if (file <= 0) return (-1);
480 return (buf.st_size);
483 int BLI_filepathsize(const char *path)
485 int size, file = open(path, O_BINARY|O_RDONLY);
490 size = BLI_filesize(file);
496 int BLI_exist(char *name)
500 /* in Windows stat doesn't recognize dir ending on a slash
501 To not break code where the ending slash is expected we
502 don't mess with the argument name directly here - elubie */
503 char tmp[FILE_MAXDIR+FILE_MAXFILE];
505 BLI_strncpy(tmp, name, FILE_MAXDIR+FILE_MAXFILE);
507 if (len > 3 && ( tmp[len-1]=='\\' || tmp[len-1]=='/') ) tmp[len-1] = '\0';
508 if (stat(tmp,&st)) return(0);
510 if (stat(name,&st)) return(0);
515 LinkNode *BLI_read_file_as_lines(char *name)
517 FILE *fp= fopen(name, "r");
518 LinkNode *lines= NULL;
522 if (!fp) return NULL;
524 fseek(fp, 0, SEEK_END);
526 fseek(fp, 0, SEEK_SET);
533 * size = because on win32 reading
534 * all the bytes in the file will return
535 * less bytes because of crnl changes.
537 size= fread(buf, 1, size, fp);
538 for (i=0; i<=size; i++) {
539 if (i==size || buf[i]=='\n') {
540 char *line= BLI_strdupn(&buf[last], i-last);
542 BLI_linklist_prepend(&lines, line);
552 BLI_linklist_reverse(&lines);
556 void BLI_free_file_lines(LinkNode *lines)
558 BLI_linklist_free(lines, (void(*)(void*)) MEM_freeN);