1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "textfile.h"
36
print_entry(char * key,char * value,void * data)37 static void print_entry(char *key, char *value, void *data)
38 {
39 printf("%s %s\n", key, value);
40 }
41
main(int argc,char * argv[])42 int main(int argc, char *argv[])
43 {
44 char filename[] = "/tmp/textfile";
45 char key[18], value[512], *str;
46 unsigned int i, j, size, max = 10;
47 int fd;
48
49 size = getpagesize();
50 printf("System uses a page size of %d bytes\n\n", size);
51
52 fd = creat(filename, 0644);
53 if (ftruncate(fd, 0) < 0)
54 return -errno;
55
56 memset(value, 0, sizeof(value));
57 for (i = 0; i < (size / sizeof(value)); i++) {
58 if (write(fd, value, sizeof(value)) < 0)
59 return -errno;
60 }
61
62 close(fd);
63
64 sprintf(key, "11:11:11:11:11:11");
65 str = textfile_get(filename, key);
66
67 if (truncate(filename, 0) < 0)
68 return -errno;
69
70 sprintf(key, "00:00:00:00:00:00");
71 if (textfile_del(filename, key) < 0)
72 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
73
74 memset(value, 0, sizeof(value));
75 if (textfile_put(filename, key, value) < 0)
76 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
77
78 str = textfile_get(filename, key);
79 if (!str)
80 fprintf(stderr, "No value for %s\n", key);
81 else
82 free(str);
83
84 snprintf(value, sizeof(value), "Test");
85 if (textfile_put(filename, key, value) < 0)
86 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
87
88 if (textfile_put(filename, key, value) < 0)
89 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
90
91 if (textfile_put(filename, key, value) < 0)
92 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
93
94 if (textfile_del(filename, key) < 0)
95 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
96
97 str = textfile_get(filename, key);
98 if (str) {
99 fprintf(stderr, "Found value for %s\n", key);
100 free(str);
101 }
102
103 for (i = 1; i < max + 1; i++) {
104 sprintf(key, "00:00:00:00:00:%02X", i);
105
106 memset(value, 0, sizeof(value));
107 for (j = 0; j < i; j++)
108 value[j] = 'x';
109
110 printf("%s %s\n", key, value);
111
112 if (textfile_put(filename, key, value) < 0) {
113 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
114 break;
115 }
116
117 str = textfile_get(filename, key);
118 if (!str)
119 fprintf(stderr, "No value for %s\n", key);
120 else
121 free(str);
122 }
123
124
125 sprintf(key, "00:00:00:00:00:%02X", max);
126
127 memset(value, 0, sizeof(value));
128 for (j = 0; j < max; j++)
129 value[j] = 'y';
130
131 if (textfile_put(filename, key, value) < 0)
132 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
133
134 sprintf(key, "00:00:00:00:00:%02X", 1);
135
136 memset(value, 0, sizeof(value));
137 for (j = 0; j < max; j++)
138 value[j] = 'z';
139
140 if (textfile_put(filename, key, value) < 0)
141 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
142
143 printf("\n");
144
145 for (i = 1; i < max + 1; i++) {
146 sprintf(key, "00:00:00:00:00:%02X", i);
147
148 str = textfile_get(filename, key);
149 if (str) {
150 printf("%s %s\n", key, str);
151 free(str);
152 }
153 }
154
155
156 sprintf(key, "00:00:00:00:00:%02X", 2);
157
158 if (textfile_del(filename, key) < 0)
159 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
160
161 sprintf(key, "00:00:00:00:00:%02X", max - 3);
162
163 if (textfile_del(filename, key) < 0)
164 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
165
166 printf("\n");
167
168 textfile_foreach(filename, print_entry, NULL);
169
170
171 sprintf(key, "00:00:00:00:00:%02X", 1);
172
173 if (textfile_del(filename, key) < 0)
174 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
175
176 sprintf(key, "00:00:00:00:00:%02X", max);
177
178 if (textfile_del(filename, key) < 0)
179 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
180
181 sprintf(key, "00:00:00:00:00:%02X", max + 1);
182
183 if (textfile_del(filename, key) < 0)
184 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
185
186 printf("\n");
187
188 textfile_foreach(filename, print_entry, NULL);
189
190 return 0;
191 }
192