Revert "auth4: auth_session_info_fill_unix only needs a tevent_context"
[Samba.git] / source3 / utils / interact.c
blobf8fed6de19891163ed70cc09b1c257d1f0267a4a
1 /*
2 * Samba Unix/Linux SMB client library
4 * Copyright (C) Gregor Beck 2011
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /**
21 * @brief Functions to interact with an user.
22 * @author Gregor Beck <gb@sernet.de>
23 * @date Aug 2011
27 #include "includes.h"
28 #include "system/filesys.h"
30 #include "interact.h"
32 #include <termios.h>
34 static const char* get_editor(void) {
35 static char editor[64] = {0};
37 if (editor[0] == '\0') {
38 const char *tmp = getenv("VISUAL");
39 if (tmp == NULL) {
40 tmp = getenv("EDITOR");
42 if (tmp == NULL) {
43 tmp = "vi";
45 snprintf(editor, sizeof(editor), "%s", tmp);
48 return editor;
51 int interact_prompt(const char* msg, const char* acc, char def) {
52 struct termios old_tio, new_tio;
53 int c;
55 tcgetattr(STDIN_FILENO, &old_tio);
56 new_tio=old_tio;
57 new_tio.c_lflag &=(~ICANON & ~ECHO);
58 tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
60 do {
61 d_printf("%s? [%c]\n", msg, def);
62 fflush(stdout);
63 c = getchar();
64 if (c == '\n') {
65 c = def;
66 break;
68 else if (strchr(acc, tolower(c)) != NULL) {
69 break;
71 d_printf("Invalid input '%c'\n", c);
72 } while(c != EOF);
73 tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
74 return c;
78 char* interact_edit(TALLOC_CTX* mem_ctx, const char* str) {
79 char fname[] = "/tmp/net_idmap_check.XXXXXX";
80 char buf[128];
81 char* ret = NULL;
82 FILE* file;
83 mode_t mask;
84 int fd;
86 mask = umask(S_IRWXO | S_IRWXG);
87 fd = mkstemp(fname);
88 umask(mask);
89 if (fd == -1) {
90 DEBUG(0, ("failed to mkstemp %s: %s\n", fname,
91 strerror(errno)));
92 return NULL;
95 file = fdopen(fd, "w");
96 if (!file) {
97 DEBUG(0, ("failed to open %s for writing: %s\n", fname,
98 strerror(errno)));
99 close(fd);
100 unlink(fname);
101 return NULL;
104 fprintf(file, "%s", str);
105 fclose(file);
107 snprintf(buf, sizeof(buf), "%s %s\n", get_editor(), fname);
108 if (system(buf) != 0) {
109 DEBUG(0, ("failed to start editor %s: %s\n", buf,
110 strerror(errno)));
111 unlink(fname);
112 return NULL;
115 file = fopen(fname, "r");
116 if (!file) {
117 DEBUG(0, ("failed to open %s for reading: %s\n", fname,
118 strerror(errno)));
119 unlink(fname);
120 return NULL;
122 while ( fgets(buf, sizeof(buf), file) ) {
123 ret = talloc_strdup_append(ret, buf);
125 fclose(file);
126 unlink(fname);
128 return talloc_steal(mem_ctx, ret);
133 /*Local Variables:*/
134 /*mode: c*/
135 /*End:*/