• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2004-2009  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, err;
48 
49 	size = getpagesize();
50 	printf("System uses a page size of %d bytes\n\n", size);
51 
52 	fd = creat(filename, 0644);
53 	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 
59 	close(fd);
60 
61 	sprintf(key, "11:11:11:11:11:11");
62 	str = textfile_get(filename, key);
63 
64 	err = truncate(filename, 0);
65 
66 
67 	sprintf(key, "00:00:00:00:00:00");
68 	if (textfile_del(filename, key) < 0)
69 		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 
81 	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 
91 	if (textfile_del(filename, key) < 0)
92 		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 	}
99 
100 	for (i = 1; i < max + 1; i++) {
101 		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 		}
113 
114 		str = textfile_get(filename, key);
115 		if (!str)
116 			fprintf(stderr, "No value for %s\n", key);
117 		else
118 			free(str);
119 	}
120 
121 
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 
165 	textfile_foreach(filename, print_entry, NULL);
166 
167 
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 
185 	textfile_foreach(filename, print_entry, NULL);
186 
187 	return 0;
188 }
189