75 lines
1.5 KiB
C
Executable File
75 lines
1.5 KiB
C
Executable File
/* includes */
|
|
#include <errno.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
/* internal protoypes */
|
|
static void print(void);
|
|
|
|
static void insert(const char *name, uint32_t index);
|
|
|
|
typedef struct cred {
|
|
char name[16];
|
|
} cred_t;
|
|
|
|
static cred_t creds[8];
|
|
|
|
#define ADMIN "root"
|
|
|
|
// ./int03 huan 4294967296
|
|
|
|
/* entry point */
|
|
int main(int argc, char **argv) {
|
|
if (argc < 3) {
|
|
printf("usage: %s <name> <index>\n", argv[0]);
|
|
return -1;
|
|
}
|
|
char *endptr;
|
|
errno = 0;
|
|
uint32_t index = (uint32_t)strtoull(argv[2], &endptr, 10);
|
|
if (errno || *endptr) {
|
|
fprintf(stderr, "Conversion error, non-convertible part: %s\n", endptr);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
snprintf(creds[0].name, 16, "%s", ADMIN);
|
|
if (index == 0) {
|
|
fprintf(stderr, "ALERT: someone tried to overwrite admin account!\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
insert(argv[1], index);
|
|
print();
|
|
return 0;
|
|
}
|
|
|
|
/* internal function definitions */
|
|
static void insert(const char *name, uint32_t index) {
|
|
if (index > 7) {
|
|
fprintf(stderr, "Invalid index\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (strlen(name) > 15) {
|
|
fprintf(stderr, "Name must not exceed 15 characters\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
snprintf(creds[index].name, 16, "%s", name);
|
|
}
|
|
|
|
static void print(void) {
|
|
for (int i = 0; i < 8; ++i) {
|
|
if (i == 0) {
|
|
printf("Admin: ");
|
|
} else {
|
|
printf("User: ");
|
|
}
|
|
if (strcmp(creds[i].name, "")) {
|
|
printf("%s\n", creds[i].name);
|
|
} else {
|
|
printf("unknown\n");
|
|
}
|
|
}
|
|
}
|