2
0

Read the user's name and site name from stdin if not given.

This commit is contained in:
Maarten Billemont 2014-11-10 12:35:31 -05:00
parent 96b482d94b
commit 435d72a509

View File

@ -95,13 +95,24 @@ char *homedir(const char *filename) {
return homefile; return homefile;
} }
char *getlinep(const char *prompt) {
char *buf = NULL;
size_t bufSize = 0;
ssize_t lineSize;
fprintf(stderr, "%s", prompt);
fprintf(stderr, " ");
if ((lineSize = getline(&buf, &bufSize, stdin)) < 0) {
free(buf);
return NULL;
}
buf[lineSize - 1]=0;
return buf;
}
int main(int argc, char *const argv[]) { int main(int argc, char *const argv[]) {
if (argc < 2)
usage();
// Read the environment. // Read the environment.
const char *userName = getenv( MP_env_username ); char *userName = getenv( MP_env_username );
const char *masterPassword = NULL; const char *masterPassword = NULL;
const char *siteName = NULL; const char *siteName = NULL;
MPElementType siteType = MPElementTypeGeneratedLong; MPElementType siteType = MPElementTypeGeneratedLong;
@ -156,14 +167,18 @@ int main(int argc, char *const argv[]) {
// Convert and validate input. // Convert and validate input.
if (!userName) { if (!userName) {
if (!(userName = getlinep("Your user name:"))) {
fprintf(stderr, "Missing user name.\n"); fprintf(stderr, "Missing user name.\n");
return 1; return 1;
} }
}
trc("userName: %s\n", userName); trc("userName: %s\n", userName);
if (!siteName) { if (!siteName) {
if (!(siteName = getlinep("Site name:"))) {
fprintf(stderr, "Missing site name.\n"); fprintf(stderr, "Missing site name.\n");
return 1; return 1;
} }
}
trc("siteName: %s\n", siteName); trc("siteName: %s\n", siteName);
if (siteCounterString) if (siteCounterString)
siteCounter = atoi( siteCounterString ); siteCounter = atoi( siteCounterString );