-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.c
More file actions
90 lines (71 loc) · 2.08 KB
/
setup.c
File metadata and controls
90 lines (71 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int create_directory_if_not_exists(const char* dirName){
struct stat st = {0};
if (stat(dirName, &st) == -1){
// folder doesn;t exist
return mkdir(dirName, 0700);
}
return -1;
}
char * concat(const char *a , const char * b){
int len = strlen(a) + strlen(b) + 1;
char * result = malloc(len);
if (!result) return NULL;
strcpy(result, a);
strcat(result, b);
return result;
}
int main(){
printf("Please enter your file Name: ");
char rootDir[256];
scanf("%255s", rootDir);
char * path;
struct stat st = {0};
if (stat(rootDir, &st) == -1){
// Directory does not exist, attempt to create it
if (mkdir(rootDir, 0700) == -1) {
perror("Failed to create root directory");
return -1; // If creation fails, exit
}
}
path = concat(rootDir, "/src");
if (create_directory_if_not_exists(path) == -1){
printf("Error structuring ur learning\n.");
return -1;
};
free(path);
path = concat(rootDir, "/src/util");
if (create_directory_if_not_exists(path) == -1){
printf("Error structuring ur learning.");
};
free(path);
path = concat(rootDir, "/src/shared");
if (create_directory_if_not_exists(path) == -1){
printf("Error structuring ur learning.");
};
free(path);
path = concat(rootDir, "/bin");
if (create_directory_if_not_exists(path) == -1){
printf("Error structuring ur learning.");
};
free(path);
path = concat(rootDir, "/tests");
if (create_directory_if_not_exists(path) == -1){
printf("Error structuring ur learning.");
};
free(path);
FILE * fp;
path = concat(rootDir, "/README.MD");
fp = fopen(path, "w");
if (fp == NULL){
printf("Unable to create README.md file.");
return 1;
}
fclose(fp);
return 0;
}