new round of warning fixes. we are now down to 24 with Xcode on blender
[blender.git] / source / blender / blenlib / intern / storage.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
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
12  * about this.
13  *
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.
18  *
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.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Reorganised mar-01 nzc
32  * Some really low-level file thingies.
33  */
34
35 #include <sys/types.h>
36 #include <stdio.h>
37 #include <stdlib.h>     
38
39 #ifdef WIN32
40 #include "BLI_winstuff.h"
41 #include <sys/types.h>
42 #include <io.h>
43 #include <direct.h>
44 #endif
45
46 #ifndef WIN32
47 #include <dirent.h>
48 #endif
49
50 #include <time.h>
51 #include <sys/stat.h>
52
53 #if !defined(linux) && (defined(__sgi) || defined(__sun__) || defined(__sun) || defined(__sparc) || defined(__sparc__))
54 #include <sys/statfs.h>
55 #endif
56
57 #if defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__NetBSD__)
58 #include <sys/param.h>
59 #include <sys/mount.h>
60 #endif
61
62 #if defined(linux) || defined(__CYGWIN32__) || defined(__hpux)
63 #include <sys/vfs.h>
64 #endif
65
66 #ifdef __BeOS
67 struct statfs {
68         int f_bsize;
69         int f_bfree;
70 };
71 #endif
72
73 #ifdef __APPLE__
74 /* For statfs */
75 #include <sys/param.h>
76 #include <sys/mount.h>
77 #endif /* __APPLE__ */
78
79
80 #include <fcntl.h>
81 #if !defined(__BeOS) && !defined(WIN32)
82 #include <sys/mtio.h>                   /* tape comando's */
83 #endif
84 #include <string.h>                     /* strcpy etc.. */
85
86 #ifndef WIN32
87 #include <sys/ioctl.h>
88 #include <unistd.h>                     /*  */
89 #include <pwd.h>
90 #endif
91
92 #if !defined(__FreeBSD__) && !defined(__APPLE__)
93 #include <malloc.h>
94 #endif
95
96 /* lib includes */
97 #include "MEM_guardedalloc.h"
98
99 #include "DNA_listBase.h"
100 #include "BLI_blenlib.h"
101 #include "BLI_storage.h"
102 #include "BLI_storage_types.h"
103
104 #include "BLI_util.h"
105 #include "BLI_linklist.h"
106
107 /* vars: */
108 static int totnum,actnum;
109 static struct direntry *files;
110
111 static struct ListBase dirbase_={
112         0,0};
113 static struct ListBase *dirbase = &dirbase_;
114
115
116 char *BLI_getwdN(char *dir)
117 {
118         char *pwd;
119
120         if (dir) {
121                 pwd = getenv("PWD");
122                 if (pwd){
123                         strcpy(dir, pwd);
124                         return(dir);
125                 }
126                 /* 160 is FILE_MAXDIR in filesel.c */
127                 return( getcwd(dir, 160) );
128         }
129         return(0);
130 }
131
132
133 int BLI_compare(struct direntry *entry1, struct direntry *entry2)
134 {
135         /* type is equal to stat.st_mode */
136
137         if (S_ISDIR(entry1->type)){
138                 if (S_ISDIR(entry2->type)==0) return (-1);
139         } else{
140                 if (S_ISDIR(entry2->type)) return (1);
141         }
142         if (S_ISREG(entry1->type)){
143                 if (S_ISREG(entry2->type)==0) return (-1);
144         } else{
145                 if (S_ISREG(entry2->type)) return (1);
146         }
147         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
148         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
149         
150         /* make sure "." and ".." are always first */
151         if( strcmp(entry1->relname, ".")==0 ) return (-1);
152         if( strcmp(entry2->relname, ".")==0 ) return (1);
153         if( strcmp(entry1->relname, "..")==0 ) return (-1);
154         
155         return (BLI_strcasecmp(entry1->relname,entry2->relname));
156 }
157
158
159 double BLI_diskfree(char *dir)
160 {
161 #ifdef WIN32
162         DWORD sectorspc, bytesps, freec, clusters;
163         char tmp[4];
164         
165         tmp[0]='\\'; tmp[1]=0; /* Just a failsafe */
166         if (dir[0]=='/' || dir[0]=='\\') {
167                 tmp[0]='\\';
168                 tmp[1]=0;
169         } else if (dir[1]==':') {
170                 tmp[0]=dir[0];
171                 tmp[1]=':';
172                 tmp[2]='\\';
173                 tmp[3]=0;
174         }
175
176         GetDiskFreeSpace(tmp,&sectorspc, &bytesps, &freec, &clusters);
177
178         return (double) (freec*bytesps*sectorspc);
179 #else
180         struct statfs disk;
181         char name[100],*slash;
182
183
184         strcpy(name,dir);
185
186         if(strlen(name)){
187                 slash = strrchr(name,'/');
188                 if (slash) slash[1] = 0;
189         } else strcpy(name,"/");
190
191 #if defined (__FreeBSD__) || defined (linux) || defined (__OpenBSD__) || defined (__APPLE__) 
192         if (statfs(name, &disk)) return(-1);
193 #endif
194 #ifdef __BeOS
195         return -1;
196 #endif
197 #if !defined(linux) && (defined (__sgi) || defined (__sun__) || defined (__sun) || defined(__sparc) || defined(__sparc__))
198
199         if (statfs(name, &disk, sizeof(struct statfs), 0)){
200                 /* printf("diskfree: Couldn't get information about %s.\n",dir); */
201                 return(-1);
202         }
203 #endif
204
205         return ( ((double) disk.f_bsize) * ((double) disk.f_bfree));
206 #endif
207 }
208
209 static int hide_dot= 0;
210
211 void BLI_hide_dot_files(int set)
212 {
213         if(set) hide_dot= 1;
214         else hide_dot= 0;
215 }
216
217 void BLI_builddir(char *dirname, char *relname)
218 {
219         struct dirent *fname;
220         struct dirlink *dlink;
221         int rellen, newnum = 0, seen_ = 0, seen__ = 0;
222         char buf[256];
223         DIR *dir;
224
225         strcpy(buf,relname);
226         rellen=strlen(relname);
227
228         if (rellen){
229                 buf[rellen]='/';
230                 rellen++;
231         }
232
233         if (chdir(dirname) == -1){
234                 perror(dirname);
235                 return;
236         }
237
238         if ( (dir = (DIR *)opendir(".")) ){
239                 while ((fname = (struct dirent*) readdir(dir)) != NULL) {
240                         
241                         if(hide_dot && fname->d_name[0]=='.' && fname->d_name[1]!='.' && fname->d_name[1]!=0);
242                         else {
243                                 
244                                 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
245                                 if (dlink){
246                                         strcpy(buf+rellen,fname->d_name);
247         
248                                         dlink->name = BLI_strdup(buf);
249         
250                                         if (dlink->name[0] == '.') {
251                                                 if (dlink->name[1] == 0) seen_ = 1;
252                                                 else if (dlink->name[1] == '.') {
253                                                         if (dlink->name[2] == 0) seen__ = 1;
254                                                 }
255                                         }
256                                         BLI_addhead(dirbase,dlink);
257                                         newnum++;
258                                 }
259                         }
260                 }
261                 
262                 if (newnum){
263 #ifndef WIN32           
264                         if (seen_ == 0) {       /* Cachefs PATCH */
265                                 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
266                                 strcpy(buf+rellen,"./.");
267                                 dlink->name = BLI_strdup(buf);
268                                 BLI_addhead(dirbase,dlink);
269                                 newnum++;
270                         }
271                         if (seen__ == 0) {      /* MAC PATCH */
272                                 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
273                                 strcpy(buf+rellen,"./..");
274                                 dlink->name = BLI_strdup(buf);
275                                 BLI_addhead(dirbase,dlink);
276                                 newnum++;
277                         }
278 #endif                  
279
280                         if (files) files=(struct direntry *)realloc(files,(totnum+newnum) * sizeof(struct direntry));
281                         else files=(struct direntry *)malloc(newnum * sizeof(struct direntry));
282
283                         if (files){
284                                 dlink = (struct dirlink *) dirbase->first;
285                                 while(dlink){
286                                         memset(&files[actnum], 0 , sizeof(struct direntry));
287                                         files[actnum].relname = dlink->name;
288                                         stat(dlink->name,&files[actnum].s);
289                                         files[actnum].type=files[actnum].s.st_mode;
290                                         files[actnum].flags = 0;
291                                         totnum++;
292                                         actnum++;
293                                         dlink = dlink->next;
294                                 }
295                         } else{
296                                 printf("Couldn't get memory for dir\n");
297                                 exit(1);
298                         }
299
300                         BLI_freelist(dirbase);
301                         if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))BLI_compare);
302                 } else {
303                         printf("%s empty directory\n",dirname);
304                 }
305
306                 closedir(dir);
307         } else {
308                 printf("%s non-existant directory\n",dirname);
309         }
310 }
311
312 void BLI_adddirstrings()
313 {
314         char datum[100];
315         char buf[250];
316         char size[250];
317         static char * types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
318         int num, mode;
319         int num1, num2, num3, num4;
320 #ifdef WIN32
321         __int64 st_size;
322 #else
323         long long st_size;
324 #endif
325         
326         struct direntry * file;
327         struct tm *tm;
328         time_t zero= 0;
329
330         file = &files[0];
331         
332         for(num=0;num<actnum;num++){
333 #ifdef WIN32
334                 mode = 0;
335                 strcpy(file->mode1, types[0]);
336                 strcpy(file->mode2, types[0]);
337                 strcpy(file->mode3, types[0]);
338 #else
339                 mode = file->s.st_mode;
340
341                 strcpy(file->mode1, types[(mode & 0700) >> 6]);
342                 strcpy(file->mode2, types[(mode & 0070) >> 3]);
343                 strcpy(file->mode3, types[(mode & 0007)]);
344                 
345                 if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2]=='-'))file->mode2[2]='l';
346
347                 if (mode & (S_ISUID | S_ISGID)){
348                         if (file->mode1[2]=='x') file->mode1[2]='s';
349                         else file->mode1[2]='S';
350
351                         if (file->mode2[2]=='x')file->mode2[2]='s';
352                 }
353
354                 if (mode & S_ISVTX){
355                         if (file->mode3[2] == 'x') file->mode3[2] = 't';
356                         else file->mode3[2] = 'T';
357                 }
358 #endif
359
360 #ifdef WIN32
361                 strcpy(files[num].owner,"user");
362 #else
363                 {
364                         struct passwd *pwuser;
365                         pwuser = getpwuid(files[num].s.st_uid);
366                         if ( pwuser ) {
367                         strcpy(files[num].owner, pwuser->pw_name);
368                         } else {
369                                 sprintf(files[num].owner, "%d", files[num].s.st_uid);
370             }
371                 }
372 #endif
373
374                 tm= localtime(&files[num].s.st_mtime);
375                 // prevent impossible dates in windows
376                 if(tm==NULL) tm= localtime(&zero);
377                 strftime(files[num].time, 8, "%H:%M", tm);
378                 strftime(files[num].date, 16, "%d-%b-%y", tm);
379
380                 st_size= files[num].s.st_size;
381                 
382                 num1= st_size % 1000;
383                 num2= st_size/1000;
384                 num2= num2 % 1000;
385                 num3= st_size/(1000*1000);
386                 num3= num3 % 1000;
387                 num4= st_size/(1000*1000*1000);
388                 num4= num4 % 1000;
389
390                 if(num4) sprintf(files[num].size, "%3d %03d %03d %03d", num4, num3, num2, num1);
391                 else if(num3) sprintf(files[num].size, "%7d %03d %03d", num3, num2, num1);
392                 else if(num2) sprintf(files[num].size, "%11d %03d", num2, num1);
393                 else if(num1) sprintf(files[num].size, "%15d", num1);
394                 else sprintf(files[num].size, "0");
395
396                 strftime(datum, 32, "%d-%b-%y %R", tm);
397
398                 if (st_size < 1000) {
399                         sprintf(size, "%10d", (int) st_size);
400                 } else if (st_size < 1000 * 1000) {
401                         sprintf(size, "%6d %03d", (int) (st_size / 1000), (int) (st_size % 1000));
402                 } else if (st_size < 100 * 1000 * 1000) {
403                         sprintf(size, "%2d %03d %03d", (int) (st_size / (1000 * 1000)), (int) ((st_size / 1000) % 1000), (int) ( st_size % 1000));
404                 } else {
405                         sprintf(size, "> %4.1f M", (double) (st_size / (1024.0 * 1024.0)));
406                         sprintf(size, "%10d", (int) st_size);
407                 }
408
409                 sprintf(buf,"%s %s %10s %s", files[num].date, files[num].time, size,
410                         files[num].relname);
411
412                 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,
413                         files[num].relname);
414
415                 files[num].string=MEM_mallocN(strlen(buf)+1, "filestring");
416                 if (files[num].string){
417                         strcpy(files[num].string,buf);
418                 }
419
420                 file++;
421         }
422 }
423
424 unsigned int BLI_getdir(char *dirname,  struct direntry **filelist)
425 {
426         // reset global variables
427         // memory stored in files is free()'d in
428         // filesel.c:freefilelist()
429
430         actnum = totnum = 0;
431         files = 0;
432
433         BLI_builddir(dirname,"");
434         BLI_adddirstrings();
435
436         if (files) {
437                 *(filelist) = files;
438         } else {
439                 // keep blender happy. Blender stores this in a variable
440                 // where 0 has special meaning.....
441                 *(filelist) = files = malloc(sizeof(struct direntry));
442         }
443
444         return(actnum);
445 }
446
447
448 int BLI_filesize(int file)
449 {
450         struct stat buf;
451
452         if (file <= 0) return (-1);
453         fstat(file, &buf);
454         return (buf.st_size);
455 }
456
457
458
459 int BLI_exist(char *name)
460 {
461         struct stat st;
462
463         if (stat(name,&st)) return(0);
464         return(st.st_mode);
465 }
466
467 LinkNode *BLI_read_file_as_lines(char *name)
468 {
469         FILE *fp= fopen(name, "r");
470         LinkNode *lines= NULL;
471         char *buf;
472         int size;
473
474         if (!fp) return NULL;
475                 
476         fseek(fp, 0, SEEK_END);
477         size= ftell(fp);
478         fseek(fp, 0, SEEK_SET);
479
480         buf= malloc(size);
481         if (buf) {
482                 int i, last= 0;
483                 
484                         /* 
485                          * size = because on win32 reading
486                          * all the bytes in the file will return
487                          * less bytes because of crnl changes.
488                          */
489                 size= fread(buf, 1, size, fp);
490                 for (i=0; i<=size; i++) {
491                         if (i==size || buf[i]=='\n') {
492                                 char *line= BLI_strdupn(&buf[last], i-last);
493
494                                 BLI_linklist_prepend(&lines, line);
495                                 last= i+1;
496                         }
497                 }
498                 
499                 free(buf);
500         }
501         
502         fclose(fp);
503         
504         BLI_linklist_reverse(&lines);
505         return lines;
506 }
507
508 void BLI_free_file_lines(LinkNode *lines)
509 {
510         BLI_linklist_free(lines, (void(*)(void*)) MEM_freeN);
511 }