~ubuntu-branches/ubuntu/intrepid/bluez/intrepid

« back to all changes in this revision

Viewing changes to common/test_textfile.c

  • Committer: Bazaar Package Importer
  • Author(s): Mario Limonciello
  • Date: 2008-10-07 12:10:29 UTC
  • Revision ID: james.westby@ubuntu.com-20081007121029-4gup4fmmh2vfo5nh
Tags: upstream-4.12
ImportĀ upstreamĀ versionĀ 4.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  BlueZ - Bluetooth protocol stack for Linux
 
4
 *
 
5
 *  Copyright (C) 2004-2008  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
 
 
37
static void print_entry(char *key, char *value, void *data)
 
38
{
 
39
        printf("%s %s\n", key, value);
 
40
}
 
41
 
 
42
int main(int argc, char *argv[])
 
43
{
 
44
        char filename[] = "/tmp/textfile";
 
45
        char key[18], value[512], *str;
 
46
        int i, j, fd, err, size, max = 10;
 
47
 
 
48
        size = getpagesize();
 
49
        printf("System uses a page size of %d bytes\n\n", size);
 
50
 
 
51
        fd = creat(filename, 0644);
 
52
        err = ftruncate(fd, 0);
 
53
 
 
54
        memset(value, 0, sizeof(value));
 
55
        for (i = 0; i < (size / sizeof(value)); i++)
 
56
                err = write(fd, value, sizeof(value));
 
57
 
 
58
        close(fd);
 
59
 
 
60
        sprintf(key, "11:11:11:11:11:11");
 
61
        str = textfile_get(filename, key);
 
62
 
 
63
        err = truncate(filename, 0);
 
64
 
 
65
 
 
66
        sprintf(key, "00:00:00:00:00:00");
 
67
        if (textfile_del(filename, key) < 0) 
 
68
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
69
 
 
70
        memset(value, 0, sizeof(value));
 
71
        if (textfile_put(filename, key, value) < 0)
 
72
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
73
 
 
74
        str = textfile_get(filename, key);
 
75
        if (!str)
 
76
                fprintf(stderr, "No value for %s\n", key);
 
77
        else
 
78
                free(str);
 
79
 
 
80
        snprintf(value, sizeof(value), "Test");
 
81
        if (textfile_put(filename, key, value) < 0)
 
82
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
83
 
 
84
        if (textfile_put(filename, key, value) < 0)
 
85
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
86
 
 
87
        if (textfile_put(filename, key, value) < 0)
 
88
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
89
 
 
90
        if (textfile_del(filename, key) < 0) 
 
91
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
92
 
 
93
        str = textfile_get(filename, key);
 
94
        if (str) {
 
95
                fprintf(stderr, "Found value for %s\n", key);
 
96
                free(str);
 
97
        }
 
98
 
 
99
        for (i = 1; i < max + 1; i++) {
 
100
                sprintf(key, "00:00:00:00:00:%02X", i);
 
101
 
 
102
                memset(value, 0, sizeof(value));
 
103
                for (j = 0; j < i; j++)
 
104
                        value[j] = 'x';
 
105
 
 
106
                printf("%s %s\n", key, value);
 
107
 
 
108
                if (textfile_put(filename, key, value) < 0) {
 
109
                        fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
110
                        break;
 
111
                }
 
112
 
 
113
                str = textfile_get(filename, key);
 
114
                if (!str)
 
115
                        fprintf(stderr, "No value for %s\n", key);
 
116
                else
 
117
                        free(str);
 
118
        }
 
119
 
 
120
 
 
121
        sprintf(key, "00:00:00:00:00:%02X", max);
 
122
 
 
123
        memset(value, 0, sizeof(value));
 
124
        for (j = 0; j < max; j++)
 
125
                value[j] = 'y';
 
126
 
 
127
        if (textfile_put(filename, key, value) < 0)
 
128
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
129
 
 
130
        sprintf(key, "00:00:00:00:00:%02X", 1);
 
131
 
 
132
        memset(value, 0, sizeof(value));
 
133
        for (j = 0; j < max; j++)
 
134
                value[j] = 'z';
 
135
 
 
136
        if (textfile_put(filename, key, value) < 0)
 
137
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
138
 
 
139
        printf("\n");
 
140
 
 
141
        for (i = 1; i < max + 1; i++) {
 
142
                sprintf(key, "00:00:00:00:00:%02X", i);
 
143
 
 
144
                str = textfile_get(filename, key);
 
145
                if (str) {
 
146
                        printf("%s %s\n", key, str);
 
147
                        free(str);
 
148
                }
 
149
        }
 
150
 
 
151
 
 
152
        sprintf(key, "00:00:00:00:00:%02X", 2);
 
153
 
 
154
        if (textfile_del(filename, key) < 0)
 
155
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
156
 
 
157
        sprintf(key, "00:00:00:00:00:%02X", max - 3);
 
158
 
 
159
        if (textfile_del(filename, key) < 0)
 
160
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
161
 
 
162
        printf("\n");
 
163
 
 
164
        textfile_foreach(filename, print_entry, NULL);
 
165
 
 
166
 
 
167
        sprintf(key, "00:00:00:00:00:%02X", 1);
 
168
 
 
169
        if (textfile_del(filename, key) < 0)
 
170
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
171
 
 
172
        sprintf(key, "00:00:00:00:00:%02X", max);
 
173
 
 
174
        if (textfile_del(filename, key) < 0)
 
175
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
176
 
 
177
        sprintf(key, "00:00:00:00:00:%02X", max + 1);
 
178
 
 
179
        if (textfile_del(filename, key) < 0)
 
180
                fprintf(stderr, "%s (%d)\n", strerror(errno), errno);
 
181
 
 
182
        printf("\n");
 
183
 
 
184
        textfile_foreach(filename, print_entry, NULL);
 
185
 
 
186
        return 0;
 
187
}