This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
forked from avrdudes/avrdude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.c
1407 lines (1138 loc) · 28.6 KB
/
lists.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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 1990-2004 Brian S. Dean <[email protected]>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
/* $Id$ */
/*----------------------------------------------------------------------
Id: lists.c,v 1.4 2001/08/19 23:26:20 bsd Exp $
----------------------------------------------------------------------*/
/*------------------------------------------------------------------------
lists.c
General purpose linked list routines. These routines implement a
generic doubly linked list. Any data type may be placed in the
lists. Stacking and Queuing routines are provided via #defines
declared in 'lists.h'.
Author : Brian Dean
Date : 10 January, 1990
------------------------------------------------------------------------*/
#include "ac_cfg.h"
#include <stdio.h>
#include <stdlib.h>
#include "libavrdude.h"
#define MAGIC 0xb05b05b0
#define CHECK_MAGIC 0 /* set to 1 to enable memory overwrite detection */
#ifdef BOS
#define MALLOC(size,x) kmalloc(size,x)
#define FREE kfree
#else
#define MALLOC(size,x) malloc(size)
#define FREE free
#endif
/*------------------------------------------------------------
| required private data structures
------------------------------------------------------------*/
typedef struct LISTNODE {
#if CHECK_MAGIC
unsigned int magic1;
#endif
struct LISTNODE * next; /* chain to next item in the list */
struct LISTNODE * prev; /* chain to previous item in the list */
void * data; /* pointer to user data */
#if CHECK_MAGIC
unsigned int magic2;
#endif
} LISTNODE;
typedef struct NODEPOOL {
#if CHECK_MAGIC
unsigned int magic1;
#endif
struct NODEPOOL * chain_next; /* chain to next node pool */
struct NODEPOOL * chain_prev; /* chain to previous node pool */
#if CHECK_MAGIC
unsigned int magic2;
#endif
} NODEPOOL;
typedef struct LIST {
#if CHECK_MAGIC
unsigned int magic1;
#endif
int num; /* number of elements in the list */
short int free_on_close; /* free the LIST memory on close T/F */
short int poolsize; /* list node allocation size */
int n_ln_pool; /* number of listnodes in a pool */
LISTNODE * top; /* top of the list */
LISTNODE * bottom; /* bottom of the list */
LISTNODE * next_ln; /* next available list node */
NODEPOOL * np_top; /* top of the node pool chain */
NODEPOOL * np_bottom; /* bottom of the node pool chain */
#if CHECK_MAGIC
unsigned int magic2;
#endif
} LIST;
/* allocate list nodes in 512 byte chunks, giving 42 elements */
#define DEFAULT_POOLSIZE 512
#if CHECK_MAGIC
#define CKMAGIC(p) { if (p->magic1 != MAGIC) breakpoint(); \
if (p->magic2 != MAGIC) breakpoint(); }
#define CKNPMAGIC(p) cknpmagic(p)
#define CKLNMAGIC(p) cklnmagic(p)
#define CKLMAGIC(p) cklmagic(p)
#else
#define CKMAGIC(p)
#define CKNPMAGIC(p)
#define CKLNMAGIC(p)
#define CKLMAGIC(p)
#endif
static int insert_ln ( LIST * l, LISTNODE * ln, void * data_ptr );
#if CHECK_MAGIC
static int cknpmagic ( LIST * l )
{
NODEPOOL * np;
int i;
i = 0;
np = l->np_top;
while (np) {
i++;
CKMAGIC(np);
np = np->chain_next;
}
i = 0;
np = l->np_bottom;
while (np) {
i++;
CKMAGIC(np);
np = np->chain_prev;
}
return 0;
}
static int cklnmagic ( LIST * l )
{
LISTNODE * ln;
int i;
i = 0;
ln = l->top;
while (ln) {
i++;
CKMAGIC(ln);
ln = ln->next;
}
i = 0;
ln = l->bottom;
while (ln) {
i++;
CKMAGIC(ln);
ln = ln->prev;
}
return 0;
}
static int cklmagic ( LIST * l )
{
CKMAGIC(l);
CKNPMAGIC(l);
CKLNMAGIC(l);
CKMAGIC(l);
return 0;
}
#endif
/*------------------------------------------------------------
| new_node_pool
|
| Create and initialize a new pool of list nodes. This is
| just a big block of memory with the first sizeof(NODEPOOL)
| bytes reserved. The first available list node resides at
| offset sizeof(NODEPOOL).
------------------------------------------------------------*/
static
NODEPOOL *
new_nodepool ( LIST * l )
{
NODEPOOL * np;
LISTNODE * ln;
int i;
CKLMAGIC(l);
/*--------------------------------------------------
| get a block of memory for the new pool
--------------------------------------------------*/
np = (NODEPOOL *) MALLOC ( l->poolsize, "list node pool" );
if (np == NULL) {
return NULL;
}
/*--------------------------------------------------
| initialize the chaining information at the
| beginning of the pool.
--------------------------------------------------*/
#if CHECK_MAGIC
np->magic1 = MAGIC;
#endif
np->chain_next = NULL;
np->chain_prev = NULL;
#if CHECK_MAGIC
np->magic2 = MAGIC;
#endif
/*--------------------------------------------------
| initialize all the list nodes within the node
| pool, which begin just after the NODEPOOL
| structure at the beginning of the memory block
--------------------------------------------------*/
ln = (LISTNODE *) (&np[1]);
#if CHECK_MAGIC
ln[0].magic1 = MAGIC;
#endif
ln[0].data = NULL;
ln[0].next = &ln[1];
ln[0].prev = NULL;
#if CHECK_MAGIC
ln[0].magic2 = MAGIC;
#endif
for (i=1; i<l->n_ln_pool-1; i++) {
#if CHECK_MAGIC
ln[i].magic1 = MAGIC;
#endif
ln[i].data = NULL;
ln[i].next = &ln[i+1];
ln[i].prev = &ln[i-1];
#if CHECK_MAGIC
ln[i].magic2 = MAGIC;
#endif
}
#if CHECK_MAGIC
ln[l->n_ln_pool-1].magic1 = MAGIC;
#endif
ln[l->n_ln_pool-1].data = NULL;
ln[l->n_ln_pool-1].next = NULL;
ln[l->n_ln_pool-1].prev = &ln[l->n_ln_pool-2];
#if CHECK_MAGIC
ln[l->n_ln_pool-1].magic2 = MAGIC;
#endif
CKMAGIC(np);
CKLMAGIC(l);
return np;
}
/*------------------------------------------------------------
| get_listnode
|
| Get the next available list node. If there are no more
| list nodes, another pool of list nodes is allocated. If
| that fails, NULL is returned.
------------------------------------------------------------*/
static
LISTNODE *
get_listnode ( LIST * l )
{
LISTNODE * ln;
NODEPOOL * np;
CKLMAGIC(l);
if (l->next_ln == NULL) {
/*--------------------------------------------------
| allocate a new node pool and chain to the others
--------------------------------------------------*/
np = new_nodepool(l);
if (np == NULL) {
CKLMAGIC(l);
return NULL;
}
if (l->np_top == NULL) {
/*--------------------------------------------------
| this is the first node pool for this list,
| directly assign to the top and bottom.
--------------------------------------------------*/
l->np_top = np;
l->np_bottom = np;
np->chain_next = NULL;
np->chain_prev = NULL;
}
else {
/*--------------------------------------------------
| this is an additional node pool, add it to the
| chain.
--------------------------------------------------*/
np->chain_next = NULL;
np->chain_prev = l->np_bottom;
l->np_bottom->chain_next = np;
l->np_bottom = np;
}
/*--------------------------------------------------
| set the list's pointer to the next available
| list node to the first list node in this new
| pool.
--------------------------------------------------*/
l->next_ln = (LISTNODE *)&np[1];
CKMAGIC(np);
}
/*--------------------------------------------------
| get the next available list node, set the list's
| next available list node to the next one in the
| list.
--------------------------------------------------*/
ln = l->next_ln;
l->next_ln = ln->next;
CKMAGIC(ln);
/*--------------------------------------------------
| initialize the new list node and return
--------------------------------------------------*/
ln->next = NULL;
ln->prev = NULL;
ln->data = NULL;
CKLMAGIC(l);
return ln;
}
/*------------------------------------------------------------
| free_listnode
|
| Return a list node to the pool of list nodes. This puts
| the node at the head of the free list, so that the next
| call to 'get_listnode', with return the most recently
| freed one.
------------------------------------------------------------*/
static
int
free_listnode ( LIST * l, LISTNODE * ln )
{
CKLMAGIC(l);
/*--------------------------------------------------
| insert the list node at the head of the list of
| free list nodes.
--------------------------------------------------*/
ln->prev = NULL;
ln->data = NULL;
ln->next = l->next_ln;
l->next_ln = ln;
CKLMAGIC(l);
return 0;
}
/*----------------------------------------------------------------------
lcreat
Create a new list data structure.
If liststruct is not NULL, it is used to provide the memory space
for the list structure instance, otherwise, the necessary memory is
malloc'd.
If elements is zero, the default poolsize is used, otherwise,
poolsizes of 'elements' elements are malloc'd to obtain the memory
for list nodes. Minimum element count is 5.
The first node pool is not preallocated; instead it is malloc'd at
the time of the first use.
----------------------------------------------------------------------*/
LISTID
lcreat ( void * liststruct, int elements )
{
LIST * l;
if (liststruct == NULL) {
/*--------------------------------------------------
allocate memory for the list itself
--------------------------------------------------*/
l = (LIST *) MALLOC ( sizeof(LIST), "list struct" );
if (l == NULL) {
return NULL;
}
l->free_on_close = 1;
}
else {
/*-----------------------------------------------------------------
use the memory given to us for the list structure
-----------------------------------------------------------------*/
l = liststruct;
l->free_on_close = 0;
}
/*--------------------------------------------------
| initialize the list
--------------------------------------------------*/
#if CHECK_MAGIC
l->magic1 = MAGIC;
l->magic2 = MAGIC;
#endif
l->top = NULL;
l->bottom = NULL;
l->num = 0;
if (elements == 0) {
l->poolsize = DEFAULT_POOLSIZE;
}
else {
l->poolsize = elements*sizeof(LISTNODE)+sizeof(NODEPOOL);
}
l->n_ln_pool = (l->poolsize-sizeof(NODEPOOL))/sizeof(LISTNODE);
if (l->n_ln_pool < 5) {
if (!liststruct) {
FREE(l);
}
return NULL;
}
l->np_top = NULL;
l->np_bottom = NULL;
l->next_ln = NULL;
CKLMAGIC(l);
return (LISTID)l;
}
/*--------------------------------------------------
| ldestroy_cb
|
| destroy an existing list data structure, calling
| the user routine 'ucleanup' on the data pointer
| of each list element. Allows the user to free
| up a list data structure and have this routine
| call their function to free up each list element
| at the same time.
--------------------------------------------------*/
void
ldestroy_cb ( LISTID lid, void (*ucleanup)(void * data_ptr) )
{
LIST * l;
LISTNODE * ln;
l = (LIST *)lid;
CKLMAGIC(l);
ln = l->top;
while (ln != NULL) {
ucleanup ( ln->data );
ln = ln->next;
}
ldestroy ( l );
}
/*--------------------------------------------------
| ldestroy
|
| destroy an existing list data structure.
|
| assumes that each data element does not need to
| be freed.
--------------------------------------------------*/
void
ldestroy ( LISTID lid )
{
LIST * l;
NODEPOOL * p1, * p2;
l = (LIST *)lid;
CKLMAGIC(l);
/*--------------------------------------------------
| free each node pool - start at the first node
| pool and free each successive until there are
| no more.
--------------------------------------------------*/
p1 = l->np_top;
while (p1 != NULL) {
p2 = p1->chain_next;
FREE(p1);
p1 = p2;
}
/*--------------------------------------------------
| now free the memory occupied by the list itself
--------------------------------------------------*/
if (l->free_on_close) {
FREE ( l );
}
}
/*------------------------------------------------------------
| ladd
|
| add list - add item p to the list
------------------------------------------------------------*/
int
ladd ( LISTID lid, void * p )
{
LIST * l;
LISTNODE *lnptr;
l = (LIST *)lid;
CKLMAGIC(l);
lnptr = get_listnode(l);
if (lnptr==NULL) {
#ifdef BOS
breakpoint();
#endif
return -1;
}
CKMAGIC(lnptr);
lnptr->data = p;
if (l->top == NULL) {
l->top = lnptr;
l->bottom = lnptr;
lnptr->next = NULL;
lnptr->prev = NULL;
}
else {
lnptr->prev = l->bottom;
lnptr->next = NULL;
l->bottom->next = lnptr;
l->bottom = lnptr;
}
l->num++;
CKLMAGIC(l);
return 0;
}
/*------------------------------------------------------------
| laddo
|
| add list, ordered - add item p to the list, use 'compare'
| function to place 'p' when the comparison 'p' is less than
| the next item. Return 0 if this was a unique entry,
| else return 1 indicating a duplicate entry, i.e., the
| compare function returned 0 while inserting the element.
------------------------------------------------------------*/
int
laddo ( LISTID lid, void * p, int (*compare)(const void *p1,const void *p2),
LNODEID * firstdup )
{
LIST * l;
LISTNODE * ln;
int dup, cmp;
l = (LIST *)lid;
CKLMAGIC(l);
dup = 0;
ln = l->top;
while (ln!=NULL) {
CKMAGIC(ln);
cmp = compare(p,ln->data);
if (cmp == 0) {
dup = 1;
if (firstdup)
*firstdup = ln;
}
if (cmp < 0) {
insert_ln(l,ln,p);
CKLMAGIC(l);
return dup;
}
else {
ln = ln->next;
}
}
ladd(l,p);
CKLMAGIC(l);
return dup;
}
/*---------------------------------------------------------------------------
| laddu
|
| add list, ordered, unique - add item p to the list, use 'compare'
| function to place 'p' when the comparison 'p' is less than the next
| item. Return 1 if the item was added, 0 if not.
|
--------------------------------------------------------------------------*/
int
laddu ( LISTID lid, void * p, int (*compare)(const void *p1,const void *p2) )
{
LIST * l;
LISTNODE * ln;
int cmp;
l = (LIST *)lid;
CKLMAGIC(l);
ln = l->top;
while (ln!=NULL) {
CKMAGIC(ln);
cmp = compare(p,ln->data);
if (cmp == 0) {
CKLMAGIC(l);
return 0;
}
if (cmp < 0) {
insert_ln(l,ln,p);
CKLMAGIC(l);
return 1;
}
else {
ln = ln->next;
}
}
ladd(l,p);
CKLMAGIC(l);
return 1;
}
LNODEID
lfirst ( LISTID lid )
{
CKLMAGIC(((LIST *)lid));
return ((LIST *)lid)->top;
}
LNODEID
llast ( LISTID lid )
{
CKLMAGIC(((LIST *)lid));
return ((LIST *)lid)->bottom;
}
LNODEID
lnext ( LNODEID lnid )
{
CKMAGIC(((LISTNODE *)lnid));
return ((LISTNODE *)lnid)->next;
}
LNODEID
lprev ( LNODEID lnid )
{
CKMAGIC(((LISTNODE *)lnid));
return ((LISTNODE *)lnid)->prev;
}
void *
ldata ( LNODEID lnid )
{
CKMAGIC(((LISTNODE *)lnid));
return ((LISTNODE *)lnid)->data;
}
int
lsize ( LISTID lid )
{
CKLMAGIC(((LIST *)lid));
return ((LIST *)lid)->num;
}
/*------------------------------------------------------------
| lcat
|
| catenate - catenate l2 to l1, return pointer to l1.
------------------------------------------------------------*/
LISTID
lcat ( LISTID lid1, LISTID lid2 )
{
CKLMAGIC(((LIST *)lid1));
CKLMAGIC(((LIST *)lid2));
while (lsize(lid2)) {
ladd ( lid1, lrmv_n(lid2,1) );
}
CKLMAGIC(((LIST *)lid1));
CKLMAGIC(((LIST *)lid2));
return lid1;
}
/*----------------------------------------------------------------------
| lget
|
| get from list, last item - return pointer to the data of the last
| item in the list, non-destructive
----------------------------------------------------------------------*/
void *
lget ( LISTID lid )
{
LIST * l;
LISTNODE * p;
l = (LIST *)lid;
CKLMAGIC(l);
p = l->bottom;
if (p == NULL) {
CKLMAGIC(l);
return NULL;
}
else {
CKLMAGIC(l);
return p->data;
}
}
/*---------------------------------------------------------------
| lget_n
|
| get from list, index - return the nth list item,
| non-destructive
---------------------------------------------------------------*/
void *
lget_n ( LISTID lid, unsigned int n )
{
int i;
LIST * l;
LISTNODE * ln;
l = (LIST *)lid;
CKLMAGIC(l);
if ((n<1)||(n>lsize(l))) {
return NULL;
}
ln = l->top;
i = 1;
while (ln && (i!=n)) {
CKMAGIC(ln);
ln = ln->next;
i++;
}
if (ln) {
CKLMAGIC(l);
return ln->data;
}
else {
CKLMAGIC(l);
return NULL;
}
}
/*---------------------------------------------------------------
| lget_ln
|
| get from list, listnode - return the nth list item, the
| listnode is returned instead of the data, non-destructive
---------------------------------------------------------------*/
LNODEID
lget_ln ( LISTID lid, unsigned int n )
{
int i;
LIST * l;
LISTNODE * ln;
l = (LIST *)lid;
CKLMAGIC(l);
if ((n<1)||(n>lsize(l))) {
return NULL;
}
ln = l->top;
i = 1;
while (i!=n) {
CKMAGIC(ln);
ln = ln->next;
i++;
}
CKLMAGIC(l);
return (LNODEID)ln;
}
/*----------------------------------------------------------------------
| insert_ln
|
| insert data, listnode - insert data just before the list item
| pointed to by 'ln'.
|
| This routine is not intended to be called directly by the user
| because the validity of ln is not checked. This routine is called
| by list manipulation routines within this module, in which ln is
| known to point to a valid list node.
----------------------------------------------------------------------*/
static
int
insert_ln ( LIST * l, LISTNODE * ln, void * data_ptr )
{
LISTNODE * lnptr;
CKLMAGIC(l);
if (ln==NULL) {
ladd ( l, data_ptr );
CKLMAGIC(l);
return 0;
}
lnptr = get_listnode(l);
if (lnptr == NULL) {
#ifdef BOS
breakpoint();
#endif
return -1;
}
CKMAGIC(lnptr);
lnptr->data = data_ptr;
if (ln==l->top) {
/*------------------------------
| insert before the list head
------------------------------*/
lnptr->next = ln;
lnptr->prev = NULL;
ln->prev = lnptr;
l->top = lnptr;
}
else if (ln==NULL) {
/*-----------------
| list was empty
-----------------*/
lnptr->next = NULL;
lnptr->prev = l->bottom;
l->bottom->next = lnptr;
l->bottom = lnptr;
}
else {
/*-----------------------------------
| insert in the middle of the list
-----------------------------------*/
lnptr->next = ln;
lnptr->prev = ln->prev;
lnptr->next->prev = lnptr;
lnptr->prev->next = lnptr;
}
l->num++;
CKLMAGIC(l);
return 0;
}
/*-----------------------------------------------------------------
| lins_n
|
| Insert data before the nth item in the list.
-----------------------------------------------------------------*/
int
lins_n ( LISTID lid, void * data_ptr, unsigned int n )
{
int i;
LIST * l;
LISTNODE * ln;
l = (LIST *)lid;
CKLMAGIC(l);
if ((n<1)||(n>(l->num+1))) {
return -1;
}
if (l->num == 0) {
return ladd ( lid, data_ptr );
}
/*----------------------------------
| locate the nth item in the list
----------------------------------*/
ln = l->top;
i = 1;
while (ln && (i!=n)) {
CKMAGIC(ln);
ln = ln->next;
i++;
}
if (!ln) {
CKLMAGIC(l);
return -1;
}
CKLMAGIC(l);
/*-----------------------------------------
| insert before the nth item in the list
-----------------------------------------*/
return insert_ln ( l, ln, data_ptr );
}
/*-----------------------------------------------------------------
| lins_ln
|
| Insert data before the list node pointed to by ln.
-----------------------------------------------------------------*/
int
lins_ln ( LISTID lid, LNODEID lnid, void * data_ptr )
{
LIST * l;
LISTNODE * ln;
LISTNODE * ln_ptr;
l = (LIST *)lid;