annotate rogue3/list.c @ 140:856017d63519

xrogue: don't segfault when backstabbing while empty-handed. The code for backstabbing checked the weapon's properties without making sure it was not NULL.
author John "Elwin" Edwards
date Tue, 05 May 2015 12:12:20 -0400
parents 527e2150eaf0
children 0250220d8cdd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
1 /*
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
2 * Functions for dealing with linked lists of goodies
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
3 *
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
4 * @(#)list.c 3.3 (Berkeley) 6/15/81
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
5 *
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
6 * Rogue: Exploring the Dungeons of Doom
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
7 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
8 * All rights reserved.
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
9 *
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
10 * See the file LICENSE.TXT for full copyright and licensing information.
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
11 */
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
12
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
13 #include "curses.h"
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
14 #include <stdlib.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
15 #include <string.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
16 #include "machdep.h"
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
17 #include "rogue.h"
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
18
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
19 /*
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
20 * detach:
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
21 * Takes an item out of whatever linked list it might be in
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
22 */
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
23
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
24 void
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
25 _detach(struct linked_list **list, struct linked_list *item)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
26 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
27 if (*list == item)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
28 *list = next(item);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
29 if (prev(item) != NULL) item->l_prev->l_next = next(item);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
30 if (next(item) != NULL) item->l_next->l_prev = prev(item);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
31 item->l_next = NULL;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
32 item->l_prev = NULL;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
33 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
34
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
35 /*
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
36 * _attach:
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
37 * add an item to the head of a list
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
38 */
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
39
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
40 void
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
41 _attach(struct linked_list **list, struct linked_list *item)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
42 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
43 if (*list != NULL)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
44 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
45 item->l_next = *list;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
46 (*list)->l_prev = item;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
47 item->l_prev = NULL;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
48 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
49 else
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
50 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
51 item->l_next = NULL;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
52 item->l_prev = NULL;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
53 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
54
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
55 *list = item;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
56 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
57
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
58 /*
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
59 * _free_list:
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
60 * Throw the whole blamed thing away
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
61 */
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
62
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
63 void
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
64 _free_list(struct linked_list **ptr)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
65 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
66 struct linked_list *item;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
67
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
68 while (*ptr != NULL)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
69 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
70 item = *ptr;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
71 *ptr = next(item);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
72 discard(item);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
73 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
74 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
75
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
76 /*
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
77 * discard:
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
78 * free up an item
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
79 */
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
80
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
81 void
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
82 discard(struct linked_list *item)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
83 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
84 total -= 2;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
85 FREE(item->l_data);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
86 FREE(item);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
87 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
88
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
89 /*
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
90 * new_item
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
91 * get a new item with a specified size
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
92 */
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
93
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
94 struct linked_list *
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
95 new_item(int size)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
96 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
97 struct linked_list *item;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
98
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
99 if ((item = (struct linked_list *) _new(sizeof *item)) == NULL)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
100 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
101 msg("Ran out of memory for header after %d items", total);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
102 return NULL;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
103 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
104
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
105 if ((item->l_data = _new(size)) == NULL)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
106 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
107 msg("Ran out of memory for data after %d items", total);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)