1 /*
2 * linsertl.c
3 * al_linsertl()
4 */
5
6
7 /*
8 This file is part of Atclib.
9
10 Atclib is Copyright � 1995-1999 Andr� Majorel.
11
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Library General Public
14 License as published by the Free Software Foundation; either
15 version 2 of the License, or (at your option) any later version.
16
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Library General Public License for more details.
21
22 You should have received a copy of the GNU Library General Public
23 License along with this library; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 02111-1307, USA.
26 */
27
28
29 #include <stdlib.h>
30 #include <memory.h>
31
32 #define AL_AILLEGAL_ACCESS
33 #include "atclib.h"
34
35
al_linsertl(al_llist_t * l,const void * buf,size_t length)36 int al_linsertl (al_llist_t *l, const void *buf, size_t length)
37 {
38 al_lelt_t *new_element;
39
40 al_lcheckmagic (l);
41 if (l->current == NULL || l->ateol)
42 return al_lpoke (l, buf);
43 new_element = malloc (sizeof (al_lelt_t) - 1 + length);
44 if (new_element == NULL)
45 {
46 al_aerrno = AL_ANOMEM;
47 return AL_ANOMEM;
48 }
49 l->total++;
50 memcpy (new_element->f.data, buf, l->length);
51
52 /* update forward link to new element */
53 if (l->current == l->first)
54 l->first = new_element;
55 else
56 l->prev->next = new_element;
57 /* update forward link from new element */
58 new_element->next = l->current;
59 /* update list pointer */
60 l->current = new_element;
61 return 0;
62 }
63
64