Skip to content

Commit 3ffec08

Browse files
Correctly creates directories.
1 parent 628c51c commit 3ffec08

File tree

5 files changed

+90
-39
lines changed

5 files changed

+90
-39
lines changed

code/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ size_t global_file_len;
2121
size_t global_file_cap;
2222
const unsigned VERSION_MAJOR=0;
2323
const unsigned VERSION_MINOR=3;
24-
const unsigned VERSION_PATCH=4;
24+
const unsigned VERSION_PATCH=5;
2525
int initialize_global_file_data(void)
2626
{
2727
int failed=1;

code/cpbuild.c

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,42 @@
99
#include"cpbuild.h"
1010
#include"utils.h"
1111
const char cpb_default_option_list[]="cc\0c++";
12-
const char cpb_accepted_extensions[]="c\0c++\0cpp\0cxx\0";
12+
const char cpb_accepted_extensions[]="c\0c++\0cc\0cpp\0cxx\0";
1313
char cpb_cmd_option_list[]="-c\0-o\0-MM";
14+
struct option_and_files
15+
{
16+
cpbuild_options_t*opt;
17+
struct program_args files;
18+
struct program_args folders;
19+
};
1420
int timespec_compare(const struct timespec*a,const struct timespec*b)
1521
{
1622
long c[2]={a->tv_sec-b->tv_sec,a->tv_nsec-b->tv_nsec};
1723
int64_t result=c[c[0]==0];
1824
return(result>=0)+(result>0)-1;
1925
}
20-
void build_callback(const char*file,void*arg,int isdir)
26+
int build_callback(const char*file,void*arg,int info)
2127
{
22-
struct cpbuild_options*opt=arg;
28+
struct option_and_files*oaf=arg;
29+
struct cpbuild_options*opt=oaf->opt;
30+
int r=0;
31+
int isdir=info&1;
2332
char*periodptr=strrchr(file,'.');
2433
size_t len=strlen(file);
34+
info>>=1;
2535
if(isdir)
2636
{
27-
struct stat objdat,dirdat;
28-
int ores=stat(opt->objdir,&objdat);
29-
int dres=stat(file,&dirdat);
30-
int same=ores==0&&dres==0&&objdat.st_dev==dirdat.st_dev&&objdat.st_ino==dirdat.st_ino;
31-
if(!same)
37+
if(info==3)
3238
{
33-
char*target=changeext_add_prefix(file+opt->pathshift,opt->objdir,"");
34-
mkdir(target,0755);
35-
free(target);
39+
char*directory=changeext_add_prefix(file+opt->pathshift,opt->objdir,"");
40+
if(directory==NULL)
41+
{
42+
perror("directory to be created is NULL, malloc failed");
43+
}
44+
else
45+
{
46+
r=append_program_arg(&oaf->folders,directory)==0;
47+
}
3648
}
3749
}
3850
else if(strcontains(cpb_accepted_extensions,periodptr+1))
@@ -49,11 +61,12 @@ void build_callback(const char*file,void*arg,int isdir)
4961
else
5062
{
5163
memcpy(filename,file,len+1);
64+
append_program_arg(&oaf->files,filename);
5265
append_program_arg(&opt->linkerargs,objname);
53-
buildfile(filename,objname,opt);
54-
free(filename);
66+
r=1;
5567
}
5668
}
69+
return r;
5770
}
5871
int make_command_line(struct program_options*restrict d,const struct program_options*restrict s,char*compiler)
5972
{
@@ -81,6 +94,9 @@ int cpbuild(char**targets,unsigned len,struct cpbuild_options*opt)
8194
if(!cfail&&!cppfail)
8295
{
8396
size_t targetlen=len;
97+
size_t currLinkerLen;
98+
struct option_and_files oaf;
99+
oaf.opt=opt;
84100
init_program_args(&opt->linkerargs,opt->linkerops.len+3);
85101
opt->linkerargs.options[0]=opt->compiler;
86102
opt->linkerargs.options[1]=cpb_cmd_option_list+3;
@@ -97,10 +113,35 @@ int cpbuild(char**targets,unsigned len,struct cpbuild_options*opt)
97113
perror("stat failed");
98114
else if(S_ISDIR(fdat.st_mode))
99115
{
100-
target=changeext_add_prefix(*it,opt->objdir,"");
101-
mkdir(target,0755);
102-
free(target);
103-
iterate_directory(*it,&build_callback,opt);
116+
currLinkerLen=opt->linkerargs.len;
117+
if(init_program_args(&oaf.files,16)==0&&init_program_args(&oaf.folders,4)==0)
118+
{
119+
iterate_directory(*it,&build_callback,&oaf);
120+
for(size_t i=oaf.folders.len;i>0;--i)
121+
{
122+
mkdir(oaf.folders.options[i-1],0755);
123+
free(oaf.folders.options[i-1]);
124+
}
125+
free(oaf.folders.options);
126+
for(size_t i=0;i<oaf.files.len;++i)
127+
{
128+
buildfile(oaf.files.options[i],opt->linkerargs.options[currLinkerLen+i],opt);
129+
free(oaf.files.options[i]);
130+
}
131+
free(oaf.files.options);
132+
}
133+
else
134+
{
135+
if(oaf.files.options!=NULL)
136+
{
137+
free(oaf.files.options);
138+
}
139+
if(oaf.folders.options!=NULL)
140+
{
141+
free(oaf.folders.options);
142+
}
143+
perror("cpbuild: initializing array for files failed");
144+
}
104145
}
105146
else
106147
{

code/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int main(int argl,char**argv)
1616
memset(&defops,0,sizeof defops);
1717
char**argstart=argv+1;
1818
char*conffile=NULL;
19-
char*emptyargs[]={"-b",conffile,NULL};
19+
char*emptyargs[]={"-b",conffile};
2020
int freearti=1;
2121
struct program_args targetArray={NULL,0,0};
2222
if(argl==1)
@@ -47,7 +47,7 @@ int main(int argl,char**argv)
4747
}
4848
char**ptr;
4949
unsigned len;
50-
if(targetArray.options==NULL)
50+
if(targetArray.len==0)
5151
{
5252
ptr=&thisdirp;
5353
len=1;

code/utils.c

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,37 @@
99
#include<sys/wait.h>
1010
#include<unistd.h>
1111
#include"utils.h"
12-
void iterate_directory(const char*dirname,void(*func)(const char*,void*,int),void*arg)
12+
void iterate_directory(const char*dirname,directory_iterator_callback_t func,void*arg)
1313
{
14-
DIR *dirhand = opendir(dirname);
15-
if(dirhand == NULL)
14+
DIR *dirhand=opendir(dirname);
15+
if(dirhand==NULL)
1616
{
1717
fprintf(stderr,"Opening %s",dirname);
1818
perror(" failed");
1919
}
2020
else
2121
{
2222
char currdir[PATH_MAX];
23-
DIR *handarr[128];
24-
struct dirent *en, *enarr[128];
25-
unsigned depth = 1;
26-
size_t namelen, dirlen = strlen(dirname);
27-
memcpy(currdir, dirname, dirlen + 1);
28-
handarr[0] = dirhand;
29-
while(depth > 0)
23+
DIR*handarr[128];
24+
struct dirent*en,*enarr[128];
25+
char boolval[128];
26+
unsigned depth=1;
27+
size_t namelen,dirlen=strlen(dirname);
28+
memcpy(currdir,dirname,dirlen+1);
29+
handarr[0]=dirhand;
30+
boolval[0]=0;
31+
while(depth>0)
3032
{
31-
en = enarr[depth - 1] = readdir(handarr[depth - 1]);
32-
if(en == NULL)
33+
en=enarr[depth-1]=readdir(handarr[depth-1]);
34+
if(en==NULL)
3335
{
34-
for(; dirlen > 0 && currdir[dirlen] != '/'; --dirlen);
35-
currdir[dirlen] = '\0';
36+
boolval[depth-1]|=func(currdir,arg,boolval[depth-1]<<2|3);
37+
if(depth>1)
38+
{
39+
boolval[depth-2]|=boolval[depth-1];
40+
}
41+
for(;dirlen>0&&currdir[dirlen]!='/';--dirlen);
42+
currdir[dirlen]='\0';
3643
closedir(handarr[--depth]);
3744
}
3845
else
@@ -42,7 +49,7 @@ void iterate_directory(const char*dirname,void(*func)(const char*,void*,int),voi
4249
{
4350
currdir[dirlen]='/';
4451
memcpy(currdir+dirlen+1,en->d_name,namelen+1);
45-
dirlen += namelen + 1;
52+
dirlen+=namelen+1;
4653
if(en->d_type==DT_DIR)
4754
{
4855
handarr[depth]=opendir(currdir);
@@ -54,12 +61,14 @@ void iterate_directory(const char*dirname,void(*func)(const char*,void*,int),voi
5461
currdir[dirlen]='\0';
5562
}
5663
else
57-
++depth;
58-
func(currdir,arg,1);
64+
{
65+
boolval[depth-1]|=func(currdir,arg,1);
66+
boolval[depth++]=0;
67+
}
5968
}
6069
else
6170
{
62-
func(currdir,arg,0);
71+
boolval[depth-1]|=func(currdir,arg,0);
6372
for(;dirlen>0&&currdir[dirlen]!='/';--dirlen);
6473
currdir[dirlen]='\0';
6574
}

code/utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ struct vector_char
55
char*str;
66
unsigned len,cap;
77
};
8-
void iterate_directory(const char*dirname,void(*func)(const char*,void*,int),void*arg);
8+
typedef int(*directory_iterator_callback_t)(const char*,void*,int);
9+
void iterate_directory(const char*dirname,directory_iterator_callback_t func,void*arg);
910
int runprogram(unsigned char maxi,char*const*args);
1011
int program_output(struct vector_char*data,char*const*args);
1112
char strcontains(const char*strlist,const char*str);

0 commit comments

Comments
 (0)