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>
37 #include <stdlib.h> /* voorkomt dat je bij malloc type moet aangeven */
42 #include "BLI_winstuff.h"
43 #include <sys/types.h>
55 #if defined(__sgi) || defined(__sun__)
56 #include <sys/statfs.h>
60 #include <sys/param.h>
61 #include <sys/mount.h>
64 #if defined(linux) || defined(__CYGWIN32__)
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 /* MAART: #ifndef __FreeBSD__ */
95 #if !defined(__FreeBSD__) && !defined(__APPLE__)
100 #include "MEM_guardedalloc.h"
102 #include "DNA_listBase.h"
103 #include "BLI_blenlib.h"
104 #include "BLI_storage.h"
105 #include "BLI_storage_types.h"
107 #include "BLI_util.h"
108 #include "BLI_linklist.h"
111 void BLI_buildpwtable(struct passwd **pwtable);
112 void BLI_freepwtable(struct passwd *pwtable);
113 char *BLI_findpwtable(struct passwd *pwtable, unsigned short user);
116 static int totnum,actnum;
117 static struct direntry *files;
119 static struct ListBase dirbase_={
121 static struct ListBase *dirbase = &dirbase_;
124 char *BLI_getwdN(char *dir)
134 /* 160 is FILE_MAXDIR in filesel.c */
135 return( getcwd(dir, 160) );
141 int BLI_compare(struct direntry *entry1, struct direntry *entry2)
143 /* type is gelijk aan stat.st_mode */
145 if (S_ISDIR(entry1->type)){
146 if (S_ISDIR(entry2->type)==0) return (-1);
148 if (S_ISDIR(entry2->type)) return (1);
150 if (S_ISREG(entry1->type)){
151 if (S_ISREG(entry2->type)==0) return (-1);
153 if (S_ISREG(entry2->type)) return (1);
155 if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
156 if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
157 return (strcasecmp(entry1->relname,entry2->relname));
161 double BLI_diskfree(char *dir)
164 DWORD sectorspc, bytesps, freec, clusters;
167 tmp[0]='\\'; tmp[1]=0; /* Just a failsafe */
168 if (dir[0]=='/' || dir[0]=='\\') {
171 } else if (dir[1]==':') {
178 GetDiskFreeSpace(tmp,§orspc, &bytesps, &freec, &clusters);
180 return (double) (freec*bytesps*sectorspc);
183 char name[100],*slash;
189 slash = strrchr(name,'/');
190 if (slash) slash[1] = 0;
191 } else strcpy(name,"/");
193 #if defined __FreeBSD__ || defined linux
194 if (statfs(name, &disk)) return(-1);
199 #if defined __sgi || defined __sun__
201 if (statfs(name, &disk, sizeof(struct statfs), 0)){
202 /* printf("diskfree: Couldn't get information about %s.\n",dir); */
207 return ( ((double) disk.f_bsize) * ((double) disk.f_bfree));
211 static int hide_dot= 0;
213 void BLI_hide_dot_files(int set)
219 void BLI_builddir(char *dirname, char *relname)
221 struct dirent *fname;
222 struct dirlink *dlink;
223 int rellen, newnum = 0, seen_ = 0, seen__ = 0;
228 rellen=strlen(relname);
235 if (chdir(dirname) == -1){
240 if (dir = (DIR *)opendir(".")){
241 while ((fname = (struct dirent*) readdir(dir)) != NULL) {
243 if(hide_dot && fname->d_name[0]=='.' && fname->d_name[1]!='.' && fname->d_name[1]!=0);
246 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
248 strcpy(buf+rellen,fname->d_name);
250 dlink->name = strdup(buf);
252 if (dlink->name[0] == '.') {
253 if (dlink->name[1] == 0) seen_ = 1;
254 else if (dlink->name[1] == '.') {
255 if (dlink->name[2] == 0) seen__ = 1;
258 BLI_addhead(dirbase,dlink);
266 if (seen_ == 0) { /* Cachefs PATCH */
267 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
268 strcpy(buf+rellen,"./.");
269 dlink->name = strdup(buf);
270 BLI_addhead(dirbase,dlink);
273 if (seen__ == 0) { /* MAC PATCH */
274 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
275 strcpy(buf+rellen,"./..");
276 dlink->name = strdup(buf);
277 BLI_addhead(dirbase,dlink);
282 if (files) files=(struct direntry *)realloc(files,(totnum+newnum) * sizeof(struct direntry));
283 else files=(struct direntry *)malloc(newnum * sizeof(struct direntry));
286 dlink = (struct dirlink *) dirbase->first;
288 memset(&files[actnum], 0 , sizeof(struct direntry));
289 files[actnum].relname = dlink->name;
290 stat(dlink->name,&files[actnum].s);
291 files[actnum].type=files[actnum].s.st_mode;
292 files[actnum].flags = 0;
298 printf("Couldn't get memory for dir\n");
302 BLI_freelist(dirbase);
303 if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))BLI_compare);
305 printf("%s empty directory\n",dirname);
310 printf("%s non-existant directory\n",dirname);
315 void BLI_buildpwtable(struct passwd **pwtable)
318 struct passwd *pw,*pwtab;
328 pwtab = (struct passwd *)calloc(count+1,sizeof(struct passwd));
334 slen = strlen(pw->pw_name);
335 pwtab[count].pw_name = malloc(slen+1);
336 strcpy(pwtab[count].pw_name,pw->pw_name);
340 pwtab[count].pw_name = 0;
346 void BLI_freepwtable(struct passwd *pwtable)
351 if (pwtable[count].pw_name) free(pwtable[count].pw_name);
360 char *BLI_findpwtable(struct passwd *pwtable, ushort user)
362 static char string[32];
364 while (pwtable->pw_name){
365 if (pwtable->pw_uid == user){
366 return (pwtable->pw_name);
370 sprintf(string, "%d", user);
376 void BLI_adddirstrings()
381 static char * types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
383 int num1, num2, num3, num4, st_size;
385 struct passwd *pwtable;
387 struct direntry * file;
391 BLI_buildpwtable(&pwtable);
396 for(num=0;num<actnum;num++){
399 strcpy(file->mode1, types[0]);
400 strcpy(file->mode2, types[0]);
401 strcpy(file->mode3, types[0]);
403 mode = file->s.st_mode;
405 strcpy(file->mode1, types[(mode & 0700) >> 6]);
406 strcpy(file->mode2, types[(mode & 0070) >> 3]);
407 strcpy(file->mode3, types[(mode & 0007)]);
409 if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2]=='-'))file->mode2[2]='l';
411 if (mode & (S_ISUID | S_ISGID)){
412 if (file->mode1[2]=='x') file->mode1[2]='s';
413 else file->mode1[2]='S';
415 if (file->mode2[2]=='x')file->mode2[2]='s';
419 if (file->mode3[2] == 'x') file->mode3[2] = 't';
420 else file->mode3[2] = 'T';
425 strcpy(files[num].owner,"user");
427 strcpy(files[num].owner, BLI_findpwtable(pwtable,files[num].s.st_uid));
430 tm= localtime(&files[num].s.st_mtime);
431 strftime(files[num].time, 8, "%H:%M", tm);
432 strftime(files[num].date, 16, "%d-%b-%y", tm);
434 st_size= (int)files[num].s.st_size;
436 num1= st_size % 1000;
439 num3= st_size/(1000*1000);
441 num4= st_size/(1000*1000*1000);
444 if(num4) sprintf(files[num].size, "%3d %03d %03d %03d", num4, num3, num2, num1);
445 else if(num3) sprintf(files[num].size, "%7d %03d %03d", num3, num2, num1);
446 else if(num2) sprintf(files[num].size, "%11d %03d", num2, num1);
447 else if(num1) sprintf(files[num].size, "%15d", num1);
448 else sprintf(files[num].size, "");
450 strftime(datum, 32, "%d-%b-%y %R", tm);
452 if (st_size < 1000) {
453 sprintf(size, "%10d", st_size);
454 } else if (st_size < 1000 * 1000) {
455 sprintf(size, "%6d %03d", st_size / 1000, st_size % 1000);
456 } else if (st_size < 100 * 1000 * 1000) {
457 sprintf(size, "%2d %03d %03d", st_size / (1000 * 1000), (st_size / 1000) % 1000, st_size % 1000);
459 sprintf(size, "> %4.1f M", st_size / (1024.0 * 1024.0));
460 sprintf(size, "%10d", st_size);
463 sprintf(buf,"%s %s %10s %s", files[num].date, files[num].time, size,
466 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,
469 files[num].string=malloc(strlen(buf)+1);
470 if (files[num].string){
471 strcpy(files[num].string,buf);
477 BLI_freepwtable(pwtable);
481 unsigned int BLI_getdir(char *dirname, struct direntry **filelist)
483 // reset global variables
484 // memory stored in files is free()'d in
485 // filesel.c:freefilelist()
490 BLI_builddir(dirname,"");
496 // keep blender happy. Blender stores this in a variable
497 // where 0 has special meaning.....
498 *(filelist) = files = malloc(sizeof(struct direntry));
505 int BLI_filesize(int file)
509 if (file <= 0) return (-1);
511 return (buf.st_size);
516 int BLI_exist(char *name)
520 if (stat(name,&st)) return(0);
524 LinkNode *BLI_read_file_as_lines(char *name)
526 FILE *fp= fopen(name, "r");
527 LinkNode *lines= NULL;
531 if (!fp) return NULL;
533 fseek(fp, 0, SEEK_END);
535 fseek(fp, 0, SEEK_SET);
542 * size = because on win32 reading
543 * all the bytes in the file will return
544 * less bytes because of crnl changes.
546 size= fread(buf, 1, size, fp);
547 for (i=0; i<=size; i++) {
548 if (i==size || buf[i]=='\n') {
549 char *line= BLI_strdupn(&buf[last], i-last);
551 BLI_linklist_prepend(&lines, line);
561 BLI_linklist_reverse(&lines);
565 void BLI_free_file_lines(LinkNode *lines)
567 BLI_linklist_free(lines, (void(*)(void*)) MEM_freeN);