aboutsummaryrefslogtreecommitdiffstats
path: root/prompt.c
blob: 9d099c7739f69b058e22bec3d81d4f38b740e95e (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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
  Licensed under the GNU Public License.
  Copyright (C) 1998-2009 by Thomas M. Vier, Jr. All Rights Reserved.

  wipe is free software.
  See LICENSE for more information.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "config.h"

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#ifdef STAT_MACROS_BROKEN
/* just in case, so we don't unlink a directory,
   we don't currently handle broken stat macros */
# define unlink(x) remove(x)
#endif

#ifndef HAVE_UNLINK
# define unlink(x) remove(x)
#endif

#include "std.h"
#include "percent.h"
#include "file.h"
#include "dir.h"
#include "main.h"
#include "wipe.h"
#include "blkdev.h"
#include "prompt.h"

extern char *argvzero;
extern struct opt_s options;

/*
  prompt_destroy -- prompt user before destroying target
*/

public void prompt_destroy(struct file_s *f, const int perm)
{
  int permdenied;
  char prompt[4];

#ifdef SANITY
  if (!S_ISBLK(f->st.st_mode) && !S_ISREG(f->st.st_mode))
    {
      fprintf(stderr, "\r%s: prompt_destroy: not a file or block dev, `%s' mode %o\n",
	     argvzero, f->name, f->st.st_mode);
      abort();
    }
#endif

  if (options.force) goto destroy;

  permdenied = access(f->name, perm);
  if (options.interactive) /* force overrides interaction */
    {
      prompt[0] = 0; /* clear prompt */

      if (!permdenied)
	{
	  while (1)
	    {
	      switch (f->st.st_mode & S_IFMT)
		{
		case S_IFBLK:
		  printf("\r%s: destroy block device `%s'? ", argvzero, f->name);
		  break;
		case S_IFCHR:
		  printf("\r%s: destory character device `%s'? ", argvzero, f->name);
		  break;
		default:
		  printf("\r%s: destroy file `%s'? ", argvzero, f->name);
		  break;
		}

	      fgets(prompt, sizeof(prompt), stdin);

	      if (prompt[0] == 'y' || prompt[0] == 'Y') goto destroy;
	      if (prompt[0] == 'n' || prompt[0] == 'N') return;
	    }
	}
      else /* perm denied */
	{
	  while (1)
	    {
	      switch (f->st.st_mode & S_IFMT)
		{
		case S_IFBLK:
		  printf("\r%s: destroy block device `%s', "
			 "overriding mode %04o? ",
			 argvzero, f->name, (unsigned int) f->st.st_mode & 07777);
		  break;
		default:
		  printf("\r%s: destroy file `%s', "
			 "overriding mode %04o? ",
			 argvzero, f->name, (unsigned int)  f->st.st_mode & 07777);
		  break;
		}

	      fgets(prompt, sizeof(prompt), stdin);

	      if (prompt[0] == 'y' || prompt[0] == 'Y') goto destroy;
	      if (prompt[0] == 'n' || prompt[0] == 'N') return;
	    }
	}
    }

 destroy:
  switch (f->st.st_mode & S_IFMT)
    {
    case S_IFBLK:
      options.delete = 0;
      destroy_blkdev(f); return;
      break;

    case S_IFCHR:
      options.delete = 0;
      destroy_file(f); return;
      break;

    case S_IFREG:
      destroy_file(f); return;
      break;

    default:
      abort();
    }
}

/*
  prompt_unlink -- prompt for removal of non-wiped files
*/

public void prompt_unlink(const char name[], const char type[], const int perm, const mode_t mode)
{
  int permdenied;
  char prompt[4];

  if (!options.delete) return;

  if (!options.rmspcl)
    {
      if (options.verbose)
	fprintf(stderr, "\r%s: `%s' is a %s -- skipping unlink\n", argvzero, name, type);
      return;
    }

  if (options.force) goto unlink;

  permdenied = access(name, perm);

  if (options.interactive)
    {
      prompt[0] = 0; /* clear prompt */

      if (!permdenied)
	{
	  while (1)
	    {
	      printf("\r%s: remove %s `%s'? ",
		     argvzero, type, name);

	      fgets(prompt, sizeof(prompt), stdin);

	      if (prompt[0] == 'y' || prompt[0] == 'Y') goto unlink;
	      if (prompt[0] == 'n' || prompt[0] == 'N') return; /* skip to next file */
	    }
	}
      else /* permdenied */
	{
	  while (1)
	    {
	      printf("\r%s: remove %s `%s', "
		     "overriding mode %04o? ",
		     argvzero, type, name, (unsigned int) mode & 07777);

	      fgets(prompt, sizeof(prompt), stdin);

	      if (prompt[0] == 'y' || prompt[0] == 'Y') goto unlink;
	      if (prompt[0] == 'n' || prompt[0] == 'N') return;
	    }
	}
    }

 unlink:
  if (unlink(name))
    {
      if (options.verbose)
	{
	  fprintf(stderr, "\r%s: cannot remove %s `%s': %s\n",
		  argvzero, type, name, strerror(errno));
	}
    }
}

/*
  prompt_recursion -- prompt for directory recursion
*/

public void prompt_recursion(const char name[], const int perm, const mode_t mode)
{
  int permdenied;
  char prompt[4];

  if (!options.recursion)
    {
      fprintf(stderr, "\r%s: `%s' is a directory -- skipping\n", argvzero, name);
      return;      
    }

#ifdef SANITY
  if (!S_ISDIR(mode))
    {
      fprintf(stderr, "\r%s: prompt_recursion: not a directory, `%s' mode %o\n",
	     argvzero, name, mode);
      abort();
    }
#endif

  if (options.force) goto recurse;

  permdenied = access(name, perm);

  if (options.interactive)
    {
      prompt[0] = 0; /* clear prompt */

      if (!permdenied)
	{
	  while (1)
	    {
	      printf("\r%s: destroy files in `%s'? ",
		     argvzero, name);

	      fgets(prompt, sizeof(prompt), stdin);

	      if (prompt[0] == 'y' || prompt[0] == 'Y') goto recurse;
	      if (prompt[0] == 'n' || prompt[0] == 'N') return; /* skip to next file */
	    }
	}
      else /* permdenied */
	{
	  while (1)
	    {
	      printf("\r%s: remove files in `%s', "
		     "overriding mode %04o? ",
		     argvzero, name, (unsigned int) mode & 07777);

	      fgets(prompt, sizeof(prompt), stdin);

	      if (prompt[0] == 'y' || prompt[0] == 'Y') goto recurse;
	      if (prompt[0] == 'n' || prompt[0] == 'N') return;
	    }
	}
    }

 recurse:
  drill_down(name);
}