blob: 970e9e77b08442c15f9ac645eea58e412da20258 [file] [log] [blame]
Marcel Holtmann6419dca2005-08-05 11:07:34 +00001/*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
Marcel Holtmann9184e2e2010-01-01 17:08:17 -08005 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
Marcel Holtmann6419dca2005-08-05 11:07:34 +00006 *
7 *
8 * This program is free software; you can redistribute it and/or modify
Marcel Holtmann632a9432005-10-29 22:36:31 +00009 * 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.
Marcel Holtmann6419dca2005-08-05 11:07:34 +000012 *
Marcel Holtmann632a9432005-10-29 22:36:31 +000013 * 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.
Marcel Holtmann6419dca2005-08-05 11:07:34 +000017 *
Marcel Holtmann632a9432005-10-29 22:36:31 +000018 * 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
Marcel Holtmann6419dca2005-08-05 11:07:34 +000021 *
Marcel Holtmann6419dca2005-08-05 11:07:34 +000022 */
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>
Marcel Holtmanna8b5fad2005-09-07 17:23:00 +000032#include <stdlib.h>
Marcel Holtmann6419dca2005-08-05 11:07:34 +000033#include <string.h>
34
35#include "textfile.h"
36
Marcel Holtmann85cc0c52006-02-08 05:38:38 +000037static void print_entry(char *key, char *value, void *data)
38{
39 printf("%s %s\n", key, value);
40}
41
Marcel Holtmann6419dca2005-08-05 11:07:34 +000042int main(int argc, char *argv[])
43{
44 char filename[] = "/tmp/textfile";
Marcel Holtmann3e6da0a2005-08-06 06:27:40 +000045 char key[18], value[512], *str;
Marcel Holtmannbbbb1ad2009-01-29 17:32:08 +010046 unsigned int i, j, size, max = 10;
47 int fd, err;
Marcel Holtmann0b5513d2007-11-26 12:12:00 +000048
49 size = getpagesize();
50 printf("System uses a page size of %d bytes\n\n", size);
Marcel Holtmann6419dca2005-08-05 11:07:34 +000051
52 fd = creat(filename, 0644);
Marcel Holtmann0b5513d2007-11-26 12:12:00 +000053 err = ftruncate(fd, 0);
54
55 memset(value, 0, sizeof(value));
56 for (i = 0; i < (size / sizeof(value)); i++)
57 err = write(fd, value, sizeof(value));
58
Marcel Holtmann6419dca2005-08-05 11:07:34 +000059 close(fd);
60
Marcel Holtmann0b5513d2007-11-26 12:12:00 +000061 sprintf(key, "11:11:11:11:11:11");
62 str = textfile_get(filename, key);
63
64 err = truncate(filename, 0);
65
66
Eduardo Rocha8bc26372006-03-24 14:36:27 +000067 sprintf(key, "00:00:00:00:00:00");
Jose Antonio Santos Cadenas6968c522010-05-07 00:27:10 +020068 if (textfile_del(filename, key) < 0)
Eduardo Rocha8bc26372006-03-24 14:36:27 +000069 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
70
71 memset(value, 0, sizeof(value));
72 if (textfile_put(filename, key, value) < 0)
73 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
74
75 str = textfile_get(filename, key);
76 if (!str)
77 fprintf(stderr, "No value for %s\n", key);
78 else
79 free(str);
80
Marcel Holtmann65614472007-01-21 18:01:39 +000081 snprintf(value, sizeof(value), "Test");
82 if (textfile_put(filename, key, value) < 0)
83 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
84
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
Jose Antonio Santos Cadenas6968c522010-05-07 00:27:10 +020091 if (textfile_del(filename, key) < 0)
Eduardo Rocha8bc26372006-03-24 14:36:27 +000092 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
93
94 str = textfile_get(filename, key);
95 if (str) {
96 fprintf(stderr, "Found value for %s\n", key);
97 free(str);
98 }
Marcel Holtmann71dc4062006-02-07 09:10:07 +000099
100 for (i = 1; i < max + 1; i++) {
Marcel Holtmann6419dca2005-08-05 11:07:34 +0000101 sprintf(key, "00:00:00:00:00:%02X", i);
102
103 memset(value, 0, sizeof(value));
104 for (j = 0; j < i; j++)
105 value[j] = 'x';
106
107 printf("%s %s\n", key, value);
108
109 if (textfile_put(filename, key, value) < 0) {
110 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
111 break;
112 }
Marcel Holtmann3e6da0a2005-08-06 06:27:40 +0000113
114 str = textfile_get(filename, key);
115 if (!str)
116 fprintf(stderr, "No value for %s\n", key);
Marcel Holtmanna8b5fad2005-09-07 17:23:00 +0000117 else
118 free(str);
Marcel Holtmann6419dca2005-08-05 11:07:34 +0000119 }
120
Marcel Holtmann71dc4062006-02-07 09:10:07 +0000121
122 sprintf(key, "00:00:00:00:00:%02X", max);
123
124 memset(value, 0, sizeof(value));
125 for (j = 0; j < max; j++)
126 value[j] = 'y';
127
128 if (textfile_put(filename, key, value) < 0)
129 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
130
131 sprintf(key, "00:00:00:00:00:%02X", 1);
132
133 memset(value, 0, sizeof(value));
134 for (j = 0; j < max; j++)
135 value[j] = 'z';
136
137 if (textfile_put(filename, key, value) < 0)
138 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
139
140 printf("\n");
141
142 for (i = 1; i < max + 1; i++) {
143 sprintf(key, "00:00:00:00:00:%02X", i);
144
145 str = textfile_get(filename, key);
146 if (str) {
147 printf("%s %s\n", key, str);
148 free(str);
149 }
150 }
151
152
153 sprintf(key, "00:00:00:00:00:%02X", 2);
154
155 if (textfile_del(filename, key) < 0)
156 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
157
158 sprintf(key, "00:00:00:00:00:%02X", max - 3);
159
160 if (textfile_del(filename, key) < 0)
161 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
162
163 printf("\n");
164
Marcel Holtmann85cc0c52006-02-08 05:38:38 +0000165 textfile_foreach(filename, print_entry, NULL);
Marcel Holtmann71dc4062006-02-07 09:10:07 +0000166
Marcel Holtmann71dc4062006-02-07 09:10:07 +0000167
168 sprintf(key, "00:00:00:00:00:%02X", 1);
169
170 if (textfile_del(filename, key) < 0)
171 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
172
173 sprintf(key, "00:00:00:00:00:%02X", max);
174
175 if (textfile_del(filename, key) < 0)
176 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
177
178 sprintf(key, "00:00:00:00:00:%02X", max + 1);
179
180 if (textfile_del(filename, key) < 0)
181 fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
182
183 printf("\n");
184
Marcel Holtmann85cc0c52006-02-08 05:38:38 +0000185 textfile_foreach(filename, print_entry, NULL);
Marcel Holtmann71dc4062006-02-07 09:10:07 +0000186
Marcel Holtmann6419dca2005-08-05 11:07:34 +0000187 return 0;
188}