65 lines
1.3 KiB
C
Executable File
65 lines
1.3 KiB
C
Executable File
#include <errno.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define BUF_SIZE 128
|
|
|
|
typedef struct creds creds_t;
|
|
|
|
typedef struct creds {
|
|
char str[BUF_SIZE];
|
|
void (*welcome)(void);
|
|
} creds_t;
|
|
static creds_t *creds;
|
|
|
|
/* internal prototypes */
|
|
static void copy(const char *s, int32_t len1);
|
|
|
|
static void print(void);
|
|
|
|
static void s3cr3t(void);
|
|
|
|
/* entry point */
|
|
int main(int argc, char **argv) {
|
|
if (argc < 3) {
|
|
printf("usage: %s <string> <length>\n", argv[0]);
|
|
return -1;
|
|
}
|
|
|
|
char *endptr;
|
|
errno = 0;
|
|
int32_t len = (int32_t)strtol(argv[2], &endptr, 10);
|
|
if (errno || *endptr) {
|
|
fprintf(stderr, "Conversion error, non-convertible part: %s\n", endptr);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
creds = (creds_t *)calloc(1, sizeof(creds_t));
|
|
if (!creds) {
|
|
perror("calloc");
|
|
return -1;
|
|
}
|
|
|
|
creds->welcome = print;
|
|
copy(argv[1], len);
|
|
creds->welcome();
|
|
free(creds);
|
|
return 0;
|
|
}
|
|
|
|
/* internal function definitions */
|
|
static void copy(const char *s, int32_t len) {
|
|
if (len >= BUF_SIZE) {
|
|
fprintf(stderr, "string length exceeds buffer size\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
snprintf(creds->str, len + 1, "%s", s);
|
|
}
|
|
|
|
static void print(void) { printf("%s\n", creds->str); }
|
|
|
|
static void s3cr3t(void) { printf("You've gathered secret material!\n"); }
|