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
|
#define _XOPEN_SOURCE 700
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char credentials_file[] = "../../test_credentials";
static int read_config(char **line, int order) {
FILE *file;
size_t length = 0;
char *eol;
if (!line) return -1;
free(*line);
*line = NULL;
file = fopen(credentials_file, "r");
if (!file) {
fprintf(stderr, "Could open %s\n", credentials_file);
return -1;
}
for (int i = 0; i < order; i++) {
if (-1 == getline(line, &length, file)) {
fprintf(stderr, "Could not read line #%d from %s: ",
i + 1, credentials_file);
if (ferror(file))
fprintf(stderr, "error occured\n");
else if (feof(file))
fprintf(stderr, "end of file reached\n");
else
fprintf(stderr, "I don't know why\n");
fclose(file);
free(*line);
*line = NULL;
return -1;
}
}
fclose(file);
eol = strpbrk(*line, "\r\n");
if (eol) *eol = '\0';
return 0;
}
const char *username(void) {
static char *username;
if (!username) {
username = getenv("ISDS_USERNAME");
if (!username)
read_config(&username, 1);
}
return username;
}
const char *password(void) {
static char *password;
if (!password) {
password = getenv("ISDS_PASSWORD");
if (!password)
read_config(&password, 2);
}
return password;
}
|