soft-exp/ex03/mixed/mixed01.c
2025-05-22 10:44:24 +02:00

52 lines
1.1 KiB
C
Executable File

#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct creds creds_t;
typedef struct creds {
char name[16];
void (*welcome)(void);
} creds_t;
static creds_t *creds;
/* internal prototypes */
static void login(void);
static void s3cr3t(void);
/* entry point */
// ./mixed01 aaaaaaaaaaaaaaaaa 65536
int main(int argc, char **argv) {
if (argc < 3) {
printf("usage: %s <name> <length of name>\n", argv[0]);
return -1;
}
int32_t size = atoi(argv[2]);
uint16_t s = size;
creds = (creds_t *)calloc(1, sizeof(creds_t));
if (!creds) {
perror("calloc");
return -1;
}
if (s >= sizeof(creds->name)) {
fprintf(stderr, "length exceeds buffer size\n");
free(creds);
return -1;
}
creds->welcome = login;
snprintf(creds->name, size + 1, "%s", argv[1]);
creds->welcome();
free(creds);
return 0;
}
/* internal function definitions */
static void login(void) { printf("Hello %s.\n", creds->name); }
static void s3cr3t(void) { printf("You've gathered secret material!\n"); }