4 * ***** BEGIN GPL/BL DUAL 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. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
31 * Reorganised mar-01 nzc
32 * Some really low-level file thingies.
35 #include <sys/types.h>
40 #include "BLI_winstuff.h"
41 #include <sys/types.h>
53 #if defined (__sun__) || defined (__sun)
54 #include <sys/statvfs.h> /* Other modern unix os's should probably use this also */
55 #elif !defined(__FreeBSD__) && !defined(linux) && (defined(__sgi) || defined(__sparc) || defined(__sparc__))
56 #include <sys/statfs.h>
59 #if defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__NetBSD__)
60 #include <sys/param.h>
61 #include <sys/mount.h>
64 #if defined(linux) || defined(__CYGWIN32__) || defined(__hpux)
77 #include <sys/param.h>
78 #include <sys/mount.h>
79 #endif /* __APPLE__ */
83 #if !defined(__BeOS) && !defined(WIN32)
84 #include <sys/mtio.h> /* tape comando's */
86 #include <string.h> /* strcpy etc.. */
89 #include <sys/ioctl.h>
90 #include <unistd.h> /* */
94 #if !defined(__FreeBSD__) && !defined(__APPLE__)
99 #include "MEM_guardedalloc.h"
101 #include "DNA_listBase.h"
102 #include "BLI_blenlib.h"
103 #include "BLI_storage.h"
104 #include "BLI_storage_types.h"
106 #include "BLI_util.h"
107 #include "BLI_linklist.h"
109 #include "BKE_utildefines.h"
112 static int totnum,actnum;
113 static struct direntry *files;
115 static struct ListBase dirbase_={
117 static struct ListBase *dirbase = &dirbase_;
120 char *BLI_getwdN(char *dir)
130 /* 160 is FILE_MAXDIR in filesel.c */
131 return( getcwd(dir, 160) );
137 int BLI_compare(struct direntry *entry1, struct direntry *entry2)
139 /* type is equal to stat.st_mode */
141 if (S_ISDIR(entry1->type)){
142 if (S_ISDIR(entry2->type)==0) return (-1);
144 if (S_ISDIR(entry2->type)) return (1);
146 if (S_ISREG(entry1->type)){
147 if (S_ISREG(entry2->type)==0) return (-1);
149 if (S_ISREG(entry2->type)) return (1);
151 if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
152 if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
154 /* make sure "." and ".." are always first */
155 if( strcmp(entry1->relname, ".")==0 ) return (-1);
156 if( strcmp(entry2->relname, ".")==0 ) return (1);
157 if( strcmp(entry1->relname, "..")==0 ) return (-1);
159 return (BLI_strcasecmp(entry1->relname,entry2->relname));
163 double BLI_diskfree(char *dir)
166 DWORD sectorspc, bytesps, freec, clusters;
169 tmp[0]='\\'; tmp[1]=0; /* Just a failsafe */
170 if (dir[0]=='/' || dir[0]=='\\') {
173 } else if (dir[1]==':') {
180 GetDiskFreeSpace(tmp,§orspc, &bytesps, &freec, &clusters);
182 return (double) (freec*bytesps*sectorspc);
185 #if defined (__sun__) || defined (__sun)
190 char name[FILE_MAXDIR],*slash;
191 int len = strlen(dir);
193 if (len >= FILE_MAXDIR) /* path too long */
199 slash = strrchr(name,'/');
200 if (slash) slash[1] = 0;
201 } else strcpy(name,"/");
203 #if defined (__FreeBSD__) || defined (linux) || defined (__OpenBSD__) || defined (__APPLE__)
204 if (statfs(name, &disk)) return(-1);
210 #if defined (__sun__) || defined (__sun)
211 if (statvfs(name, &disk)) return(-1);
212 #elif !defined(__FreeBSD__) && !defined(linux) && (defined (__sgi) || defined(__sparc) || defined(__sparc__))
213 /* WARNING - This may not be supported by geeneric unix os's - Campbell */
214 if (statfs(name, &disk, sizeof(struct statfs), 0)) return(-1);
217 return ( ((double) disk.f_bsize) * ((double) disk.f_bfree));
221 static int hide_dot= 0;
223 void BLI_hide_dot_files(int set)
229 void BLI_builddir(char *dirname, char *relname)
231 struct dirent *fname;
232 struct dirlink *dlink;
233 int rellen, newnum = 0, seen_ = 0, seen__ = 0;
238 rellen=strlen(relname);
245 if (chdir(dirname) == -1){
250 if ( (dir = (DIR *)opendir(".")) ){
251 while ((fname = (struct dirent*) readdir(dir)) != NULL) {
253 if(hide_dot && fname->d_name[0]=='.' && fname->d_name[1]!='.' && fname->d_name[1]!=0);
256 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
258 strcpy(buf+rellen,fname->d_name);
260 dlink->name = BLI_strdup(buf);
262 if (dlink->name[0] == '.') {
263 if (dlink->name[1] == 0) seen_ = 1;
264 else if (dlink->name[1] == '.') {
265 if (dlink->name[2] == 0) seen__ = 1;
268 BLI_addhead(dirbase,dlink);
276 if (seen_ == 0) { /* Cachefs PATCH */
277 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
278 strcpy(buf+rellen,"./.");
279 dlink->name = BLI_strdup(buf);
280 BLI_addhead(dirbase,dlink);
283 if (seen__ == 0) { /* MAC PATCH */
284 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
285 strcpy(buf+rellen,"./..");
286 dlink->name = BLI_strdup(buf);
287 BLI_addhead(dirbase,dlink);
291 if (seen_ == 0) { /* should only happen for root paths like "C:\" */
292 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
293 strcpy(buf+rellen,".");
294 dlink->name = BLI_strdup(buf);
295 BLI_addhead(dirbase,dlink);
300 if (files) files=(struct direntry *)realloc(files,(totnum+newnum) * sizeof(struct direntry));
301 else files=(struct direntry *)malloc(newnum * sizeof(struct direntry));
304 dlink = (struct dirlink *) dirbase->first;
306 memset(&files[actnum], 0 , sizeof(struct direntry));
307 files[actnum].relname = dlink->name;
308 stat(dlink->name,&files[actnum].s);
309 files[actnum].type=files[actnum].s.st_mode;
310 files[actnum].flags = 0;
316 printf("Couldn't get memory for dir\n");
320 BLI_freelist(dirbase);
321 if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))BLI_compare);
323 printf("%s empty directory\n",dirname);
328 printf("%s non-existant directory\n",dirname);
332 void BLI_adddirstrings()
337 static char * types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
339 off_t num1, num2, num3, num4, num5;
346 struct direntry * file;
352 for(num=0;num<actnum;num++){
355 strcpy(file->mode1, types[0]);
356 strcpy(file->mode2, types[0]);
357 strcpy(file->mode3, types[0]);
359 mode = file->s.st_mode;
361 strcpy(file->mode1, types[(mode & 0700) >> 6]);
362 strcpy(file->mode2, types[(mode & 0070) >> 3]);
363 strcpy(file->mode3, types[(mode & 0007)]);
365 if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2]=='-'))file->mode2[2]='l';
367 if (mode & (S_ISUID | S_ISGID)){
368 if (file->mode1[2]=='x') file->mode1[2]='s';
369 else file->mode1[2]='S';
371 if (file->mode2[2]=='x')file->mode2[2]='s';
375 if (file->mode3[2] == 'x') file->mode3[2] = 't';
376 else file->mode3[2] = 'T';
381 strcpy(files[num].owner,"user");
384 struct passwd *pwuser;
385 pwuser = getpwuid(files[num].s.st_uid);
387 strcpy(files[num].owner, pwuser->pw_name);
389 sprintf(files[num].owner, "%d", files[num].s.st_uid);
394 tm= localtime(&files[num].s.st_mtime);
395 // prevent impossible dates in windows
396 if(tm==NULL) tm= localtime(&zero);
397 strftime(files[num].time, 8, "%H:%M", tm);
398 strftime(files[num].date, 16, "%d-%b-%y", tm);
401 * Seems st_size is signed 32-bit value in *nix and Windows. This
402 * will buy us some time until files get bigger than 4GB or until
403 * everyone starts using __USE_FILE_OFFSET64 or equivalent.
405 st_size= (off_t)files[num].s.st_size;
407 num1= st_size % 1000;
410 num3= st_size/(1000*1000);
412 num4= st_size/(1000*1000*1000);
414 num5= st_size/(1000000000000LL);
418 sprintf(files[num].size, "%1d %03d %03d %03d K", (int)num5, (int)num4, (int)num3, (int)num2);
419 else if(num4) sprintf(files[num].size, "%3d %03d %03d %03d", (int)num4, (int)num3, (int)num2, (int)num1);
420 else if(num3) sprintf(files[num].size, "%7d %03d %03d", (int)num3, (int)num2, (int)num1);
421 else if(num2) sprintf(files[num].size, "%11d %03d", (int)num2, (int)num1);
422 else if(num1) sprintf(files[num].size, "%15d", (int)num1);
423 else sprintf(files[num].size, "0");
425 strftime(datum, 32, "%d-%b-%y %H:%M", tm);
427 if (st_size < 1000) {
428 sprintf(size, "%10d", (int) st_size);
429 } else if (st_size < 1000 * 1000) {
430 sprintf(size, "%6d %03d", (int) (st_size / 1000), (int) (st_size % 1000));
431 } else if (st_size < 100 * 1000 * 1000) {
432 sprintf(size, "%2d %03d %03d", (int) (st_size / (1000 * 1000)), (int) ((st_size / 1000) % 1000), (int) ( st_size % 1000));
434 sprintf(size, "> %4.1f M", (double) (st_size / (1024.0 * 1024.0)));
435 sprintf(size, "%10d", (int) st_size);
438 sprintf(buf,"%s %s %10s %s", files[num].date, files[num].time, size,
441 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,
444 files[num].string=MEM_mallocN(strlen(buf)+1, "filestring");
445 if (files[num].string){
446 strcpy(files[num].string,buf);
453 unsigned int BLI_getdir(char *dirname, struct direntry **filelist)
455 // reset global variables
456 // memory stored in files is free()'d in
457 // filesel.c:freefilelist()
462 BLI_builddir(dirname,"");
468 // keep blender happy. Blender stores this in a variable
469 // where 0 has special meaning.....
470 *(filelist) = files = malloc(sizeof(struct direntry));
477 int BLI_filesize(int file)
481 if (file <= 0) return (-1);
483 return (buf.st_size);
486 int BLI_filepathsize(const char *path)
488 int size, file = open(path, O_BINARY|O_RDONLY);
493 size = BLI_filesize(file);
499 int BLI_exist(char *name)
503 /* in Windows stat doesn't recognize dir ending on a slash
504 To not break code where the ending slash is expected we
505 don't mess with the argument name directly here - elubie */
506 char tmp[FILE_MAXDIR+FILE_MAXFILE];
508 BLI_strncpy(tmp, name, FILE_MAXDIR+FILE_MAXFILE);
510 if (len > 3 && ( tmp[len-1]=='\\' || tmp[len-1]=='/') ) tmp[len-1] = '\0';
511 if (stat(tmp,&st)) return(0);
513 if (stat(name,&st)) return(0);
518 LinkNode *BLI_read_file_as_lines(char *name)
520 FILE *fp= fopen(name, "r");
521 LinkNode *lines= NULL;
525 if (!fp) return NULL;
527 fseek(fp, 0, SEEK_END);
529 fseek(fp, 0, SEEK_SET);
536 * size = because on win32 reading
537 * all the bytes in the file will return
538 * less bytes because of crnl changes.
540 size= fread(buf, 1, size, fp);
541 for (i=0; i<=size; i++) {
542 if (i==size || buf[i]=='\n') {
543 char *line= BLI_strdupn(&buf[last], i-last);
545 BLI_linklist_prepend(&lines, line);
555 BLI_linklist_reverse(&lines);
559 void BLI_free_file_lines(LinkNode *lines)
561 BLI_linklist_free(lines, (void(*)(void*)) MEM_freeN);