summaryrefslogtreecommitdiff
path: root/src/Check.cc
blob: 49996bf4b959b5bb582fa860e8d18ab5e13baf30 (plain)
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
#include <config.h>
#include "Command.h"
#include "Errors.h"
#include <cstdio>
#include <cstdarg>
#include <cerrno>

bool check(const char *format, ...) {
  if(command.force)
    return true;
  char buffer[64];
  for(;;) {
    va_list ap;
    va_start(ap, format);
    vfprintf(stdout, format, ap);
    va_end(ap);
    printf("yes/no> ");
    fflush(stdout);
    fgets(buffer, sizeof buffer, stdin);
    if(feof(stdin))
      throw IOError("unexpected EOF reading stdin");
    if(ferror(stdin))
      throw IOError("reading stdin", errno);
    std::string result = buffer;
    if(result == "yes\n")
      return true;
    if(result == "no\n")
      return false;
    printf("Please answer 'yes' or 'no'.\n");
  }
}