all repos — conver-tool @ 8e89942a0b72db7185ce063a57a90c281cf09575

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
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
#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 <errno.h>
#  include <sys/stat.h>  
#endif

void print_result(conver_result_t result)
{
    if (result == 0)
    {
        fprintf(stdout, "-> %s\n", conver_errors[result]);
        return;
    }
    fprintf(stderr, "ERROR: %s\n", conver_errors[result]);
}

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
}

conver_result_t fs_dir_create(const char *path, int ok_if_exists) {
#ifdef FS_WINDOWS
    if (CreateDirectoryA(path, NULL)) {
        return CONVER_OK;
    }
    if (ok_if_exists && GetLastError() == ERROR_ALREADY_EXISTS && fs_dir_exists(path))
    {
        return CONVER_OK;
    }
    return CONVER_ERR_MKDIR;
#else
    if (mkdir(path, 0755) == 0) {
        return CONVER_OK;
    }
    if (ok_if_exists && errno == EEXIST && fs_dir_exists(path))
    {
        return CONVER_OK;
    }
    return CONVER_ERR_MKDIR;
#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;
        fs_dir_create(CONVER_DIR, 1);
    }
    if (write_all || !fs_file_exists(CONVER_DRAFT_FILE))
    {
        printf("Creating %s...\n", CONVER_DRAFT_FILE);
        print_result(fs_file_create(CONVER_DRAFT_FILE));
    }
    if (write_all || !fs_file_exists(CONVER_RELEASE_FILE))
    {
        printf("Creating %s...\n", CONVER_RELEASE_FILE);
        print_result(fs_file_create(CONVER_RELEASE_FILE));
    }
    if (write_all || !fs_file_exists(CONVER_LEGACY_FILE))
    {
        printf("Creating %s...\n", CONVER_LEGACY_FILE);
        print_result(fs_file_create(CONVER_LEGACY_FILE));
    }
    if (write_all || !fs_file_exists(CONVER_HISTORY_FILE))
    {
        printf("Creating %s...\n", CONVER_HISTORY_FILE);
        print_result(fs_file_create(CONVER_HISTORY_FILE));
    }
}

void command_draft()
{
    char score[16];
    int valid = 0;

    while (!valid) {
        printf("Specify the dependability score of the release [001-FFF]: ");
        fgets(score, sizeof(score), stdin);
        if (strlen(score) > 4) {
            print_result(CONVER_ERR_INVALID_VALUE);
            continue;
        }
        int invalid_digit = 0;
        for (int i=0; i<strlen(score)-1; i++){
            if (!isxdigit(score[i])){
                invalid_digit = 1;
                printf("Invalid: %c", score[i]);
                break;
            }
        }
        if (invalid_digit) {
            print_result(CONVER_ERR_INVALID_VALUE);
            continue;
        }
        valid = 1;
    }
    printf("Score: %s\n", score);
    valid = 0;
    char size[16];
    while (!valid) {
        printf("Specify the size of the release [S, M, L, X]: ");
        fgets(size, sizeof(size), stdin);
        if (strlen(size)> 2) {
            print_result(CONVER_ERR_INVALID_VALUE);
            continue;
        }
        if (size[0] != 'S' && size[0] != 's' && size[0] != 'M' && size[0] != 'm' && 
            size[0] != 'L' && size[0] != 'l' && size[0] != 'X' && size[0] != 'x')
        {
            print_result(CONVER_ERR_INVALID_VALUE);
            continue;
        }
        valid = 1;
    }
    printf("Size: %s\n", size);
}

//// 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();
            }
            if ((strcmp(arg, "draft") == 0))
            {
                command_draft();
            }
        }
    }
}