-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
612 lines (486 loc) · 12.9 KB
/
list.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* ************************************************************************
*
* functions for filelist specific data management
*
* (c) 1994-2012 by Markus Reschke
*
* ************************************************************************ */
/*
* local constants
*/
#define LIST_C
/*
* include header files
*/
/* local header files */
#include "common.h" /* common stuff */
#include "variables.h" /* global variables */
#include "functions.h" /* external functions */
/* ************************************************************************
* file information data (linked list)
* ************************************************************************ */
/*
* free list of info elements
*/
void FreeInfoList(Info_Type *List)
{
Info_Type *Next;
/* sanity check */
if (List == NULL) return;
while (List)
{
Next = List->Next; /* save pointer to next element */
/* free data */
if (List->Name) free(List->Name);
if (List->Infos) FreeTokenlist(List->Infos);
/* free structure */
free(List);
List = Next; /* move to next element */
}
}
/*
* create and add new info element to global list
*
* returns:
* - 1 on success
* - 0 on error
*/
_Bool AddInfoElement(char *Name, off_t Size, time_t Time)
{
_Bool Flag = False; /* return value */
Info_Type *Element; /* new element */
/* sanity check */
if (Name == NULL) return Flag;
Element = malloc(sizeof(Info_Type)); /* allocate memory */
if (Element) /* success */
{
/* set defaults */
Element->Counter = 0;
Element->Status = STAT_NONE;
Element->Infos = NULL;
Element->LastInfo = NULL;
Element->Next = NULL;
/* copy data */
Element->Name = CopyString(Name);
Element->Size = Size;
Element->Time = Time;
/* add new element to list */
if (Env->LastInfo) Env->LastInfo->Next = Element; /* just link */
else Env->InfoList = Element; /* start list */
Env->LastInfo = Element; /* save new list end */
Flag = True; /* signal success */
}
else /* error */
{
Log(L_ERR, "Couldn't allocate memory!");
}
return Flag;
}
/*
* search for info element matching a specific name
*
* returns:
* - pointer to element on match
* - NULL on error or mismatch
*/
Info_Type *SearchInfoList(Info_Type *List, char *Name)
{
Info_Type *Element = NULL; /* return value */
/* sanity check */
if (Name == NULL) return Element;
while (List) /* follow list */
{
if (List->Name &&
(strcmp(Name, List->Name) == 0))
{
Element = List;
List = NULL;
}
else
{
List = List->Next; /* next element */
}
}
return Element;
}
/*
* create and add new description line to Info element
*
* returns:
* - 1 on success
* - 0 on error
*/
_Bool AddDesc2Info(Info_Type *Info, char *Data)
{
_Bool Flag = False; /* return value */
Token_Type *Token = NULL;
/* sanity checks */
if ((Info == NULL) || (Data == NULL)) return Flag;
Token = malloc(sizeof(Token_Type));
if (Token)
{
/* setup element */
Token->Next = NULL;
Token->String = CopyString(Data);
/* and add it to list */
if (Info->LastInfo) Info->LastInfo->Next = Token;
else Info->Infos = Token;
Info->LastInfo = Token;
Flag = True; /* signal success */
}
else
{
Log(L_ERR, "Could not allocate memory!");
}
return Flag;
}
/* ************************************************************************
* file description fields
* ************************************************************************ */
/*
* free list of field elements
*/
void FreeFieldList(Field_Type *List)
{
Field_Type *Next;
/* sanity check */
if (List == NULL) return;
while (List)
{
Next = List->Next; /* save pointer to next element */
/* free structure */
free(List);
List = Next; /* move to next element */
}
}
/*
* create and insert new field element to a global list
* based on line and position (-> sorted list)
*
* returns:
* - 1 on success
* - 0 on error
*/
_Bool InsertField(Fields, Type, Line, Pos, Width, Align, Format)
Field_Type **Fields;
unsigned short Type;
unsigned short Line;
unsigned short Pos;
unsigned short Width;
unsigned short Align;
unsigned short Format;
{
_Bool Flag = False; /* return value */
Field_Type *Field; /* new element */
Field_Type *List;
Field_Type *Prev = NULL;
Field_Type *Next = NULL;
unsigned short Test;
/* sanity check */
if (Fields == NULL) return Flag;
Field = malloc(sizeof(Field_Type)); /* allocate memory */
if (Field)
{
/* set defaults */
Field->Next = NULL;
/* copy values */
Field->Type = Type;
Field->Line = Line;
Field->Pos = Pos;
Field->Width = Width;
Field->Align = Align;
Field->Format = Format;
Flag = True;
/* search for neighbours */
List = *Fields;
while (List)
{
if (List->Line < Field->Line) /* line above */
{
Prev = List;
List = List->Next;
}
else if (List->Line == Field->Line) /* same line */
{
/* take care about positions */
if (List->Pos < Field->Pos) /* left neighbour */
{
Prev = List;
List = List->Next;
}
else /* right neighbour */
{
Next = List;
List = NULL;
}
}
else /* line below */
{
Next = List;
List = NULL;
}
}
/* link new element */
if (Prev) /* got left neighbour */
{
/* insert after element in front of */
Field->Next = Prev->Next;
Prev->Next = Field;
}
else /* no left neighbour */
{
/* insert at list start */
Field->Next = *Fields;
*Fields = Field;
}
/* check offset to left field in same line */
if (Prev && (Prev->Line == Field->Line))
{
Test = Prev->Pos + Prev->Width;
if (Field->Pos <= Test) Flag = False;
}
/* check offset to right field in same line */
if (Next && (Next->Line == Field->Line))
{
Test = Field->Pos + Field->Width;
if (Next->Pos <= Test) Flag = False;
}
if (!Flag)
{
Log(L_WARN, "Overlapping data fields!");
}
}
else /* error */
{
Log(L_ERR, "Couldn't allocate memory!");
}
return Flag;
}
/* ************************************************************************
* file description fields
* ************************************************************************ */
/*
* convert date sub-string into struct tm
*
* returns:
* - 1 on success
* - 0 on error
*/
_Bool Strings2DateTime(char *Year, char *Month, char *Day, struct tm *DateTime)
{
_Bool Flag = False; /* return value */
int Value; /* temp value */
int Counter = 0;
char *Test = NULL; /* test pointer */
/* sanity checks */
if ((DateTime == NULL) || (Year == NULL) ||
(Month == NULL) || (Day == NULL)) return Flag;
/* set default values */
DateTime->tm_sec = 0;
DateTime->tm_min = 0;
DateTime->tm_hour = 0;
DateTime->tm_mday = 0;
DateTime->tm_mon = 0;
DateTime->tm_year = 0;
DateTime->tm_wday = 0;
DateTime->tm_yday = 0;
DateTime->tm_isdst = 0;
/* year */
Value = strtol(Year, &Test, 10); /* convert */
if ((Test != NULL) && (Test[0] == 0)) /* got valid value */
{
if ((Value >= 0) && (Value <= 99)) /* 2 digits */
{
/* todo: select century based on current year (Env->DateTime) */
if (Value < 70) Value += 100; /* before or past 2000? */
DateTime->tm_year = Value; /* years since 1900 */
Counter++;
}
else if (Value >= 1000) /* 4 digits or more */
{
DateTime->tm_year = Value - 1900; /* years since 1900 */
Counter++;
}
}
/* month */
Value = strtol(Month, &Test, 10); /* convert */
if ((Test != NULL) && (Test[0] == 0)) /* got valid value */
{
if ((Value >= 1) && (Value <= 12)) /* check range */
{
DateTime->tm_mon = Value - 1; /* 0 to 11 */
Counter++;
}
}
/* day */
Value = strtol(Day, &Test, 10); /* convert */
if ((Test != NULL) && (Test[0] == 0)) /* got valid value */
{
if ((Value >= 1) && (Value <= 31)) /* check range */
{
DateTime->tm_mday = Value;
Counter++;
}
}
if (Counter == 3) Flag = True; /* got all three parts */
return Flag;
}
/* ************************************************************************
* output support functions
* ************************************************************************ */
/*
* fill string with a given character
*/
void FillString(char *String, char Char, unsigned short Number, unsigned short Max)
{
/* sanity check */
if (String == NULL) return;
if (Number > Max) Number = Max; /* limit */
while (Number > 0)
{
String[0] = Char;
String++;
Number--;
}
String[0] = 0; /* end string */
}
/*
* limit digits of a number
*/
long long LimitNumber(long long Number, unsigned short Digits)
{
long long Value = 1; /* return value */
/* calculate limit */
while (Digits > 0)
{
Value = Value * 10;
Digits--;
}
/* limit number to maximum if limit is exeeded */
if (Number < Value) Value = Number;
else Value--;
return Value;
}
/*
* convert long long integer into string with byte unit
*
* returns:
* - 1 on success
* - 0 on any problem
*/
_Bool Bytes2String(long long Bytes, int Width, char *Buffer, size_t Size)
{
_Bool Flag = False; /* return value */
long long Limit = 1;
unsigned int Counter;
char Unit;
/* sanity checks */
if ((Buffer == NULL) || (Width < 4) || (Width >= Size)) return Flag;
/* calculate limit */
Counter = Width;
while (Counter > 0)
{
Limit = Limit * 10;
Counter--;
}
/* crunch bytes */
Counter = 0;
while (Bytes >= Limit)
{
Bytes = Bytes / 1024;
Counter++;
if (Counter == 1) Limit = Limit / 10; /* save one char for unit */
}
/* unit */
switch (Counter)
{
case 1:
Unit = 'k';
break;
case 2:
Unit = 'M';
break;
case 3:
Unit = 'G';
break;
}
/* build string */
if (Counter > 0) /* with unit */
{
if (sprintf(Buffer, "%*lld%c", Width - 1, Bytes, Unit) > 0) Flag = True;
}
else /* no unit */
{
if (sprintf(Buffer, "%*lld", Width, Bytes) > 0) Flag = True;
}
return Flag;
}
/* ************************************************************************
* test functions
* ************************************************************************ */
/*
* check if filename matches 8.3 DOS-style
*
* returns:
* - 1 on match
* - 0 on mismatch or any problem
*/
_Bool CheckDosFilename(char* Name)
{
_Bool Flag = False; /* return value */
unsigned short Counter = 0;
unsigned short Dot = 0;
unsigned short Check = 0;
/* sanity check */
if (Name == NULL) return Flag;
/* search for dots and invalid characters */
while (Name[0] != 0)
{
Counter++; /* got another char */
if (Name[0] == '.') /* dot */
{
if (Dot == 0) Dot = Counter; /* save position for first dot */
else Check = 1; /* another dot */
}
else if (Name[0] == ' ') /* space */
{
Check = 2;
}
Name++; /* next char */
}
if (Check == 0) /* if ok so far */
{
if (Dot == 0) /* no dot */
{
/* check length of name */
if ((Counter > 0) && (Counter <= 8))
{
Flag = True;
}
}
else /* single dot */
{
/* check length of name and extension */
Check = Dot - 1; /* length of name */
if ((Check > 0) && (Check <= 8))
{
Check = Counter - Dot; /* length of extension */
if ((Check > 0) && (Check <= 3))
{
Flag = True;
}
}
}
}
return Flag;
}
/* ************************************************************************
* clean-up of local constants
* ************************************************************************ */
/*
* undo local constants
*/
#undef LIST_C
/* ************************************************************************
* EOF
* ************************************************************************ */