source: golgotha/src/i4/file/linux/get_dir.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 2.8 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "file/get_dir.hh"
10#include "memory/malloc.hh"
11
12#error "don't use this file!"
13
14#include <sys/types.h>
15#include <dirent.h>
16#include <unistd.h>
17#include <sys/stat.h>
18
19class i4_dir_string : public i4_str
20{
21public:
22  i4_dir_string(char *st) : i4_str(strlen(st)+1)
23  {
24    strcpy(ptr,st);
25    len=strlen(st);
26  }
27
28  char *buffer() { return ptr; }
29  void set_len(w16 _len) { len = _len; }
30};
31
32extern char *i4_os_string(const i4_const_str &name, char *buf);
33
34
35i4_bool i4_get_directory(const i4_const_str &path,
36                         i4_str **&files, w32 &tfiles,
37                         i4_str **&dirs, w32 &tdirs,
38                         i4_file_status_struct **file_status)
39
40  char buf[256];
41  struct dirent *de;
42  files=NULL;
43  dirs=NULL;
44  if (file_status) *file_status=0;
45  tfiles=0;
46  tdirs=0;
47  DIR *d=opendir(i4_os_string(path,buf,sizeof(buf)));
48  if (!d) return i4_F;
49
50  i4_dir_string **tlist=NULL;
51  sw32 t=0;
52  char curdir[200];
53  getcwd(curdir,200);
54  if (chdir(i4_os_string(path,buf,sizeof(buf)))!=0)
55    return i4_F;
56
57  do
58  {
59    de=readdir(d);
60    if (de)
61    {
62      t++;
63      tlist=(i4_dir_string **)i4_realloc(tlist,sizeof(i4_dir_string *)*t,"tmp file list");
64      tlist[t-1]=new i4_dir_string(de->d_name);
65    }
66  } while (de);
67  closedir(d);
68
69  for (int i=0;i<t;i++)
70  {
71    d=opendir(((i4_dir_string *)tlist[i])->buffer() );   // see if the file is a directory
72    if (d)
73    {
74      tdirs++;
75      dirs=(i4_str **)i4_realloc(dirs,sizeof(i4_str *)*tdirs,"dir list");
76      dirs[tdirs-1]=tlist[i];
77      closedir(d);
78    } else
79    {
80      tfiles++;
81      files=(i4_str **)i4_realloc(files,sizeof(i4_str *)*tfiles,"dir list");
82      files[tfiles-1]=tlist[i];
83    }
84  }
85  if (t)
86    i4_free(tlist);
87
88
89  if (file_status)
90  {
91    if (!tfiles)
92      *file_status=0;
93    else
94    {
95      i4_file_status_struct *sa;
96      sa=(i4_file_status_struct *)i4_malloc(sizeof (i4_file_status_struct) * tfiles, "stat array");
97      for (int j=0; j<tfiles; j++)
98      {
99        struct stat s;
100        stat( ((i4_dir_string *)files[j])->buffer(), &s);
101        sa[j].last_accessed=s.st_atime;
102        sa[j].last_modified=s.st_mtime;
103        sa[j].created=s.st_ctime;
104      }
105
106      *file_status=sa;
107    }
108  }
109
110  chdir(curdir);
111  return i4_T;
112}
113
114
115
116
117
118
119
120
Note: See TracBrowser for help on using the repository browser.