utils.h (1331B)
1 #ifndef GOUT_UTILS_H_ 2 #define GOUT_UTILS_H_ 3 4 #include <stdbool.h> 5 #include <stdio.h> 6 7 /* Concatenates path parts to "p1/p2". Exits on failure or truncation. */ 8 char* path_concat(char* out, size_t out_len, const char* p1, const char* p2); 9 10 /* Recursively creates the directories specified by path, like mkdir -p. */ 11 int mkdirp(const char* path); 12 13 /* Behaves as calloc but exits on failure. */ 14 void* ecalloc(size_t count, size_t size); 15 16 /* Behaves as strdup but exits on failure. */ 17 char* estrdup(const char* s); 18 19 /* Behaves as strlcpy but exits on failure or truncation. */ 20 size_t estrlcpy(char* dst, const char* src, size_t dsize); 21 22 /* Behaves as strlcat but exits on failure or truncation. */ 23 size_t estrlcat(char* dst, const char* src, size_t dsize); 24 25 /* Opens the specified file. Terminates with error on failure. */ 26 FILE* efopen(const char* filename, const char* flags); 27 28 /* Exits with error if the specified FILE* has an error. */ 29 void checkfileerror(FILE* fp, const char* name, char mode); 30 31 /* Validates that a path is safe to use. Returns true if safe. */ 32 bool is_safe_repo_path(const char* path); 33 34 /* Returns true if the URL has a safe scheme. */ 35 bool is_safe_url(const char* url); 36 37 /* Returns true if the email address is safe to use in a mailto: link. */ 38 bool is_safe_mailto(const char* email); 39 40 #endif // GOUT_UTILS_H_