opkg: Fix a bug that ap may undefined.
[oweals/opkg-lede.git] / libopkg / user.c
1 /* user.c - the opkg package management system
2
3    Jamey Hicks
4
5    Copyright (C) 2002 Hewlett Packard Company
6    Copyright (C) 2001 University of Southern California
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17 */
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <stdarg.h>
21 #include <string.h>
22 #include "file_util.h"
23 #include "str_util.h"
24 #include "user.h"
25
26 static char *question = NULL;
27 static int question_len = 255;
28
29 opkg_response_callback opkg_cb_response = NULL;
30
31 char *get_user_response(const char *format, ...)
32 {
33      int len = question_len;
34      va_list ap;
35      char *response;
36
37      do {
38           if (question == NULL || len > question_len) {
39                question = realloc(question, len + 1);
40                question_len = len;
41           }
42
43           va_start(ap, format);
44           len = vsnprintf(question,question_len,format,ap);
45           va_end(ap);
46      } while (len > question_len);
47      response = strdup(opkg_cb_response(question));
48      str_chomp(response);
49      str_tolower(response);
50
51      return response;
52 }