File: rm.c

package info (click to toggle)
yafc 1.1.1.dfsg.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,244 kB
  • ctags: 1,679
  • sloc: ansic: 19,338; sh: 10,365; makefile: 155; perl: 38; ruby: 33
file content (199 lines) | stat: -rw-r--r-- 4,701 bytes parent folder | download | duplicates (4)
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
/* $Id: rm.c,v 1.5 2003/07/12 10:25:41 mhe Exp $
 *
 * rm.c -- remove files, the 'rm' command
 *
 * Yet Another FTP Client
 * Copyright (C) 1998-2001, Martin Hedenfalk <mhe@stacken.kth.se>
 *
 * 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. See COPYING for more details.
 */

#include "syshdr.h"
#include "ftp.h"
#include "gvars.h"
#include "shortpath.h"
#include "strq.h"
#include "input.h"
#include "commands.h"
#include "bashline.h"

#define RM_INTERACTIVE 1
#define RM_FORCE 2
#define RM_VERBOSE 4
#define RM_RECURSIVE 8
#define RM_TAGGED 16

static void print_rm_syntax(void)
{
	printf(_("Remove file(s).  Usage:\n"
			 "  rm [options] file ...\n"
			 "Options:\n"
			 "  -f, --force           never prompt\n"
			 "  -i, --interactive     prompt before any removal\n"
			 "  -r, --recursive       remove the contents of"
			 " directories recursively\n"
			 "                          CAREFUL!\n"
			 "  -t, --tagged          remove tagged files\n"
			 "  -v, --verbose         explain what is being done\n"
			 "      --help            display this help\n"));
}

static bool rm_quit = false;
static bool rm_batch = false;

static void remove_file(const rfile *f, unsigned opt)
{
	ftp_set_tmp_verbosity(vbNone);
	ftp_unlink(f->path);
	if(test(opt, RM_VERBOSE)) {
		fprintf(stderr, "%s", shortpath(f->path, 40, ftp->homedir));
		if(ftp->code != ctComplete)
			fprintf(stderr, ": %s", ftp_getreply(false));
		fprintf(stderr, "\n");
	}
}

static void remove_files(const list *gl, unsigned opt)
{
	listitem *li;

	for(li=gl->first; li && !rm_quit; li=li->next) {
		rfile *f = (rfile *)li->data;

		if(gvInterrupted)
			break;

		/* skip dotdirs */
		if(strcmp(base_name_ptr(f->path), ".") == 0
		   || strcmp(base_name_ptr(f->path), "..") == 0)
			continue;

		if(test(opt, RM_INTERACTIVE) && !rm_batch) {
			int a = ask(ASKYES|ASKNO|ASKCANCEL|ASKALL, ASKYES,
						_("Remove remote file '%s'?"),
						shortpath(f->path, 42, ftp->homedir));
			if(a == ASKNO)
				continue;
			if(a == ASKCANCEL) {
				rm_quit = true;
				break;
			}
			if(a == ASKALL)
				rm_batch = true;
			/* else a==ASKYES */
		}

		if(risdir(f)) {
			if(test(opt, RM_RECURSIVE)) {
				char *recurs_mask;
				list *rgl;
				char *q_recurs_mask;

				rgl = rglob_create();
				asprintf(&recurs_mask, "%s/*", f->path);
				q_recurs_mask = bash_backslash_quote(recurs_mask);
				rglob_glob(rgl, q_recurs_mask, false, true, 0);
				free(q_recurs_mask);
				if(list_numitem(rgl) > 0)
					remove_files(rgl, opt);
				rglob_destroy(rgl);
				free(recurs_mask);
				ftp_rmdir(f->path);
				if(test(opt, RM_VERBOSE)) {
					fprintf(stderr, "%s", shortpath(f->path, 40,
													ftp->homedir));
					if(ftp->code != ctComplete)
						fprintf(stderr, "%s", ftp_getreply(false));
					fprintf(stderr, "\n");
				}

			} else
				fprintf(stderr, _("%s  omitting directory\n"),
						shortpath(f->path, 40, ftp->homedir));
			continue;
		}
		remove_file(f, opt);
	}
}

void cmd_rm(int argc, char **argv)
{
	int c, opt=0;
	struct option longopts[] = {
		{"force", no_argument, 0, 'f'},
		{"interactive", no_argument, 0, 'i'},
		{"verbose", no_argument, 0, 'v'},
		{"recursive", no_argument, 0, 'r'},
		{"tagged", no_argument, 0, 't'},
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0}
	};
	list *gl;

	optind = 0; /* force getopt() to re-initialize */
	while((c = getopt_long(argc, argv, "fivrt", longopts, 0)) != EOF) {
		switch(c) {
		case 'f':
			opt |= RM_FORCE;
			opt &= ~RM_INTERACTIVE;
			break;
		case 'i':
			opt |= RM_INTERACTIVE;
			break;
		case 'v':
			opt |= RM_VERBOSE;
			break;
		case 'r':
			opt |= RM_RECURSIVE;
			break;
		case 't':
			opt |= RM_TAGGED;
			break;
		case 'h':
			print_rm_syntax();
			return;
		  case '?':
			return;
		}
	}

	if(!test(opt, RM_TAGGED))
		minargs(optind);

	need_connected();
	need_loggedin();

	gl = rglob_create();
	while(optind < argc) {
		stripslash(argv[optind]);
		rglob_glob(gl, argv[optind], true, true, 0);
		optind++;
	}

	if(list_numitem(gl) == 0 && !test(opt, RM_TAGGED)) {
		fprintf(stderr, _("no files found\n"));
		rglob_destroy(gl);
		return;
	}
	if(test(opt, RM_TAGGED)
	   && (!ftp->taglist || list_numitem(ftp->taglist) == 0)) {
		fprintf(stderr, _("no tagged files\n"));
		if(list_numitem(gl) == 0) {
			rglob_destroy(gl);
			return;
		}
	}
	rm_quit = false;
	rm_batch = test(opt, RM_FORCE);
	if(test(opt, RM_FORCE))
		opt &= ~RM_INTERACTIVE;

	remove_files(gl, opt);
	if(test(opt, RM_TAGGED)) {
		remove_files(ftp->taglist, opt);
		list_clear(ftp->taglist);
	}
}