all repos — conver-tool @ 99aae0a6e47779e7794bd643e2ae0ce15621db20

A command line tool to manage ConVer projects.

src/conver.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
 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
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
#include "conver.h"

#if defined(_WIN32) || defined(_WIN64)
#  define FS_WINDOWS
#  ifndef WIN32_LEAN_AND_MEAN
#    define WIN32_LEAN_AND_MEAN
#  endif
#  include <windows.h>
#else
#  define FS_POSIX
#  include <sys/stat.h>  
#endif

void print_error(conver_result_t result)
{
    switch (result)
    {
        case CONVER_OK:
            break;
        case CONVER_ERR_OPEN:
            fprintf(stderr, "ERROR: Unable to open file.\n");
            break;
        case CONVER_ERR_WRITE:
            fprintf(stderr, "ERROR: Unable to write file.\n");
            break;
        case CONVER_ERR_READ:
            fprintf(stderr, "ERROR: Unable to read file.\n");
            break;
        case CONVER_ERR_ALLOC:
            fprintf(stderr, "ERROR: Unable to allocate memory for file.\n");
            break;
        case CONVER_ERR_STAT:
            fprintf(stderr, "ERROR: Unable to get file/directory information.\n");
            break;
    }
}

int fs_dir_exists(const char *path) {
#ifdef FS_WINDOWS
    DWORD attr = GetFileAttributesA(path);
    return (attr != INVALID_FILE_ATTRIBUTES) &&
           (attr & FILE_ATTRIBUTE_DIRECTORY);
#else
    struct stat st;
    return (stat(path, &st) == 0) && S_ISDIR(st.st_mode);
#endif
}

int fs_file_exists(const char *path) {
#ifdef FS_WINDOWS
    DWORD attr = GetFileAttributesA(path);
    return (attr != INVALID_FILE_ATTRIBUTES) &&
           !(attr & FILE_ATTRIBUTE_DIRECTORY);
#else
    struct stat st;
    return (stat(path, &st) == 0) && S_ISREG(st.st_mode);
#endif
}

static conver_result_t fs_open_write(const char *path, const char *mode,
                               const void *data, size_t size) {
    FILE *f = fopen(path, mode);
    if (!f) return CONVER_ERR_OPEN;
    if (size > 0) {
        size_t written = fwrite(data, 1, size, f);
        if (written != size) {
            fclose(f);
            return CONVER_ERR_WRITE;
        }
    }
    fclose(f);
    return CONVER_OK;
}

conver_result_t fs_file_create(const char *path) {
    return fs_open_write(path, "w", NULL, 0);
}

conver_result_t fs_file_create_b(const char *path) {
    return fs_open_write(path, "wb", NULL, 0);
}

conver_result_t fs_write(const char *path, const char *text) {
    size_t len = 0;
    const char *p = text;
    while (*p++) len++;           /* strlen without <string.h> dependency */
    return fs_open_write(path, "w", text, len);
}

conver_result_t fs_write_b(const char *path, const void *data, size_t size) {
    return fs_open_write(path, "wb", data, size);
}

conver_result_t fs_append(const char *path, const char *text) {
    size_t len = 0;
    const char *p = text;
    while (*p++) len++;
    return fs_open_write(path, "a", text, len);
}

conver_result_t fs_append_b(const char *path, const void *data, size_t size) {
    return fs_open_write(path, "ab", data, size);
}

static conver_result_t fs_read_all(const char *path, const char *mode,
                             void **out_buf, size_t *out_len, int nul_term) {
    *out_buf = NULL;
    if (out_len) *out_len = 0;

    FILE *f = fopen(path, mode);
    if (!f) return CONVER_ERR_OPEN;

    /* Determine file size via seek (works for both text and binary,
       though on Windows text mode may report a slightly larger size —
       we handle that with the actual bytes-read count below). */
    if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return CONVER_ERR_READ; }
    long file_size = ftell(f);
    if (file_size < 0)              { fclose(f); return CONVER_ERR_READ; }
    rewind(f);

    size_t alloc_size = (size_t)file_size + (nul_term ? 1 : 0);
    unsigned char *buf = (unsigned char *)malloc(alloc_size ? alloc_size : 1);
    if (!buf) { fclose(f); return CONVER_ERR_ALLOC; }

    size_t bytes_read = fread(buf, 1, (size_t)file_size, f);
    /* On Windows, text mode converts \r\n → \n so bytes_read <= file_size.
       Check ferror *before* fclose while the FILE* is still valid. */
    int read_err = ferror(f);
    fclose(f);

    if (read_err || (bytes_read == 0 && file_size > 0)) {
        free(buf);
        return CONVER_ERR_READ;
    }

    if (nul_term) buf[bytes_read] = '\0';

    *out_buf = buf;
    if (out_len) *out_len = bytes_read;
    return CONVER_OK;
}

conver_result_t fs_read(const char *path, char **out_buf, size_t *out_len) {
    return fs_read_all(path, "r", (void **)out_buf, out_len, 1);
}

conver_result_t fs_read_b(const char *path, unsigned char **out_buf, size_t *out_len) {
    return fs_read_all(path, "rb", (void **)out_buf, out_len, 0);
}

//// Commands

void command_init()
{
    int write_all = 0;
    if (!fs_dir_exists(CONVER_DIR))
    {
        write_all = 1;
    }
    if (write_all || !fs_file_exists(CONVER_DRAFT_FILE))
    {
        printf("Creating %s...\n", CONVER_DRAFT_FILE);
        print_error(fs_file_create(CONVER_DRAFT_FILE));
    }
}

//// Main

int main(int argc, char *argv[])
{

    if (argc > 1)
    {
        for (int i = 1; i < argc; i++)
        {
            char *arg = argv[i];
            if ((strcmp(arg, "-v") == 0 || strcmp(arg, "--version") == 0))
            {
                printf("%04X\n", CONVER_VERSION);
                return 0;
            }
            if ((strcmp(arg, "init") == 0))
            {
                command_init();
            }
        }
    }
}