Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This is a tool created to start C projects like cargo new or smith for C++
// Author: Antonio Souza
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -31,6 +32,12 @@ void cinit_put_str_color(const char *str, cinit_color_t color)

#define CINIT_VERSION "0.1.0"

bool validate_project_name(const char *project_name) {
const char *allowed_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
size_t len = strlen(project_name);
return ((len > 0) && (strspn(project_name, allowed_chars) == len));
}

int main_file(const char *project_name) {
char main_path[256];
snprintf(main_path, sizeof(main_path), "%s/src/main.c", project_name);
Expand Down Expand Up @@ -126,6 +133,11 @@ int main(int argc, char *argv[]) {
return 1;
}

if(!validate_project_name(argv[2])) {
cinit_put_str_color("Your project name should use alphanumeric (A-Z, a-z, 0-9), underscore (_) or hyphen (-) characters only \n", CINIT_COLOR_RED);
return 1;
}

// creating dir
int dir = mkdir(argv[2], 0755);
if (dir != 0) {
Expand Down