blob: fd47b5e4828b31cf8196ad36b0d8b1c35dec81b4 (
plain)
1 // GPGEdit edits GPG encrypted files
2 // Copyright (C) 2018 Aaron Ball <nullspoon@oper.io>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 #include "strll.h"
18
19 struct strll* strll_new() {
20 struct strll* out;
21 out = malloc(sizeof(struct strll));
22 out->next = NULL;
23 out->str = NULL;
24
25 return out;
26 }
27
28
29 struct strll* strll_add(struct strll* listitem, char* str) {
30 struct strll* cur = listitem;
31
32 // If str == null, we're inside the first item of a new list
33 if(cur->str == NULL) {
34 cur->str = malloc(sizeof(char) * (strlen(str) + 1));
35 strcpy(cur->str, str);
36 cur->next = NULL;
37 return cur;
38 }
39
40 // Traverse to the last item in the list
41 while(cur->next != NULL)
42 cur = cur->next;
43
44 // Allocate the next item and update the next pointer
45 cur->next = malloc(sizeof(struct strll));
46 // Repoint cur to the newly allocated struct
47 cur = cur->next;
48
49 // Allocate string, copy in, and set next to null
50 cur->str = malloc(sizeof(char) * (strlen(str) + 1));
51 strcpy(cur->str, str);
52 cur->next = NULL;
53
54 return cur;
55 }
56
57
58 void strll_release(struct strll* listitem) {
59 struct strll* item = listitem;
60 struct strll* next = NULL;
61
62 while(item != NULL) {
63 next = item->next;
64 free(item->str);
65 free(item);
66 item = next;
67 }
68 }
69
70
71 void strll_dump(struct strll* listitem) {
72 struct strll* cur = listitem;
73 while(cur != NULL) {
74 printf("%s\n", cur->str);
75 cur = cur->next;
76 }
77 }
|