-
Notifications
You must be signed in to change notification settings - Fork 1
/
tag.c
290 lines (219 loc) · 6.04 KB
/
tag.c
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
280
281
282
283
284
285
286
287
288
289
290
/*
$NiH: tag.c,v 1.37 2003/05/13 15:46:08 dillo Exp $
tag.c -- tagging
Copyright (C) 1996-2002 Dieter Baron
This file is part of cftp, a fullscreen ftp client
The author can be contacted at <[email protected]>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "tag.h"
#include "directory.h"
#include "options.h"
#include "list.h"
#include "util.h"
#include "bindings.h"
struct taglist tags;
struct tagentry tags_s;
static int _tag_insert(int n, struct tagentry *t, char *file,
off_t size, char type);
static int _tag_update_curdir(struct tagentry *u, enum tagopt what);
int
tag_init(void)
{
tags_s.next = tags_s.prev = &tags_s;
tags.len = tags.cur = tags.top = 0;
tags.size = sizeof(struct tagentry);
tags.reallen = 1024;
if ((tags.line=(struct tagentry *)malloc(tags.reallen
*sizeof(struct tagentry)))
== NULL)
return -1;
return 0;
}
void
change_curdir(directory *dir)
{
struct tagentry *t;
int i, cmp;
if (curdir) {
for (i=0; i<curdir->len; i++)
curdir->line[i].line[0] = ' ';
cmp = 1;
for (t=tags_s.next; t != &tags_s && cmp >= 0; t = t->next) {
cmp = strncmp(dir->path, t->name, t->dirl);
if (cmp == 0 && (i=dir_find(dir, t->file)) >= 0) {
dir->line[i].line[0] = opt_tagchar;
}
}
curdir->top = curdir->cur = 0;
}
curdir = dir;
if (binding_state == bs_remote)
list = (struct list *)curdir;
}
int
tag_file(char *dir, char *file, off_t size, char type, enum tagopt what)
{
char *canon;
struct tagentry *t;
int cmp;
canon = NULL;
if (dir == NULL)
canon = canonical(file, NULL);
else {
if ((canon=(char *)malloc(strlen(dir)+strlen(file)+2)) == NULL)
return 0;
sprintf(canon, "%s%s%s", dir,
(strcmp(dir, "/") == 0 ? "" : "/"), file);
}
cmp = 1;
for (t=tags_s.next; t != &tags_s; t = t->next) {
if ((cmp=strcmp(canon, t->name)) <= 0)
break;
}
if (t != &tags_s && cmp == 0) {
if (what == TAG_ON) {
free(canon);
return 0;
}
tag_delete(t-tags.line);
return -1;
}
else {
if (what == TAG_OFF) {
free(canon);
return 0;
}
_tag_insert(tags.len, t->prev, canon, size, type);
return 1;
}
}
void
tag_clear(void)
{
int i;
tags_s.next = tags_s.prev = &tags_s;
for (i=0; i<tags.len; i++) {
_tag_update_curdir(&tags.line[i], TAG_OFF);
free(tags.line[i].line);
free(tags.line[i].name);
}
tags.len = 0;
}
void
tag_delete(int n)
{
int i;
struct tagentry *t;
t = tags.line+n;
_tag_update_curdir(t, TAG_OFF);
free(t->line);
free(t->name);
t->next->prev = t->prev;
t->prev->next = t->next;
--tags.len;
for (i=n; i<tags.len; i++) {
tags.line[i] = tags.line[i+1];
tags.line[i].prev->next--;
tags.line[i].next->prev--;
}
}
static int
_tag_insert(int n, struct tagentry *t, char *file, off_t size, char type)
{
int i;
char *line;
struct tagentry *u;
if ((line=(char *)malloc(strlen(file)+14)) == NULL)
return -1;
if (tags.len >= tags.reallen) {
tags.reallen += 1024;
if ((u=(struct tagentry *)realloc(tags.line, tags.reallen
*sizeof(struct tagentry)))
== NULL)
return -1;
if (u != tags.line) {
/* tags cannot be empty since tags.line is full */
tags_s.next = u + (tags_s.next - &tags.line[0]);
tags_s.prev = u + (tags_s.prev - &tags.line[0]);
for (i=0; i<tags.len; i++) {
if (u[i].prev != &tags_s)
u[i].prev = u + (tags.line[i].prev - &tags.line[0]);
if (u[i].next != &tags_s)
u[i].next = u + (tags.line[i].next - &tags.line[0]);
}
tags.line = u;
}
}
for (i=tags.len; i>n; --i) {
tags.line[i].prev->next++;
tags.line[i].next->prev++;
tags.line[i+1] = tags.line[i];
}
sprintf(line, "%8lld %c %s", size, type, file);
u = tags.line+n;
u->line = line;
u->name = file;
u->file = noalloc_basename(file);
u->dirl = tags.line[n].file - file;
if (u->dirl > 1)
--u->dirl;
u->size = size;
u->type = type;
u->next = t->next;
u->prev = t;
t->next = u;
u->next->prev = u;
_tag_update_curdir(u, TAG_ON);
tags.len++;
return 0;
}
static int
_tag_update_curdir(struct tagentry *u, enum tagopt what)
{
int i;
if (strlen(curdir->path) == u->dirl
&& strncmp(curdir->path, u->name, u->dirl) == 0) {
i = dir_find(curdir, u->file);
if (i >= 0
&& (curdir->line[i].line[0] !=
(what == TAG_ON) ? opt_tagchar : ' ')) {
curdir->line[i].line[0] =
(what == TAG_ON) ? opt_tagchar : ' ';
if (binding_state == bs_remote)
list_reline(i);
}
}
return 0;
}
int
tag_anytags(void)
{
return (tags_s.next != &tags_s);
}