-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
61 lines (59 loc) · 2.12 KB
/
main.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fsandel <fsandel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/30 11:40:06 by fsandel #+# #+# */
/* Updated: 2023/05/26 18:11:42 by fsandel ### ########.fr */
/* */
/* ************************************************************************** */
#include "template.h"
int main(int argc, char *argv[]) {
if (argc != 2) {
dprintf(STDERR_FILENO, "Exactly one argument required\n");
return (1);
}
if (strchr(argv[1], '.')) {
dprintf(STDERR_FILENO,
"Only add class name as argument, not file extension\n");
return (2);
}
char *cpp_name = strjoin(argv[1], ".cpp");
if (!cpp_name) {
dprintf(STDERR_FILENO, "First allocation failed\n");
return (6);
}
char *hpp_name = strjoin(argv[1], ".hpp");
if (!hpp_name) {
dprintf(STDERR_FILENO, "Second allocation failed\n");
return (7);
}
if (!access(cpp_name, F_OK) || !access(hpp_name, F_OK)) {
dprintf(STDERR_FILENO, "File already exists\n");
free(cpp_name);
free(hpp_name);
return (3);
}
int cpp_fd = open(cpp_name, O_WRONLY | O_CREAT, 0644);
if (cpp_fd < 0) {
dprintf(STDERR_FILENO, "CPP File open failed\n");
free(cpp_name);
free(hpp_name);
return (4);
}
int hpp_fd = open(hpp_name, O_WRONLY | O_CREAT, 0644);
if (hpp_fd < 0) {
dprintf(STDERR_FILENO, "HPP File open failed\n");
free(cpp_name);
free(hpp_name);
close(cpp_fd);
return (5);
}
create_hpp(hpp_fd, argv[1]);
create_cpp(cpp_fd, argv[1]);
close(cpp_fd);
close(hpp_fd);
return (0);
}