I wrote this code for opening URLs using C.
Is using enum for function result a good practice?
I also thought about sanitizing the URL, but it didn't seem easy and is. Probably, not a part of this function's responsibility.
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static char command[] = "start \"\" \"%s\"";
static size_t commandSize = sizeof(command) - 3ULL;
static size_t calculateCommandBufferSize(char const * url) {
size_t urlSize = strlen(url);
return urlSize + commandSize + 1;
}
/**
* @brief describes possible results of `openURL`.
*/
typedef enum OPEN_URL_RESULT {
OPEN_URL_SUCCESS,
OPEN_URL_NULL_POINTER,
OPEN_URL_OUT_OF_MEMORY,
OPEN_URL_COMMAND_BUFFER_FAIL,
OPEN_URL_CMD_FAIL
} OPEN_URL_RESULT;
typedef struct OpenURLResult {
OPEN_URL_RESULT result;
int exit_code;
} OpenURLResult;
#define return_with(enum_result) return (OpenURLResult){enum_result, 0};
/**
* @brief execute command for opening url in the default browser using Windows
* command line.
* @warning Do not accept url as input from user without sanitizing it as the
* program is prone to injections
*
* @param url url to be opened
* @return true if executed successfully
* @return false if failed to execute
*/
OpenURLResult openURL(char const * url) {
if (url == NULL) {
return_with(OPEN_URL_NULL_POINTER);
}
size_t const buffer_size = calculateCommandBufferSize(url);
char *buffer = malloc(buffer_size * sizeof(char));
if (buffer == NULL) {
return_with(OPEN_URL_OUT_OF_MEMORY);
}
int const print_result = sprintf(buffer, command, url);
if (print_result < 0) {
return_with(OPEN_URL_COMMAND_BUFFER_FAIL);
}
int const exit_code = system(buffer);
free(buffer);
if (!exit_code) {
return_with(OPEN_URL_SUCCESS);
}
OpenURLResult result = {OPEN_URL_CMD_FAIL, exit_code};
return result;
}
#undef return_with
systemcrutch. It should be more efficient. \$\endgroup\$