OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [test/] [unistd/] [preadwrite.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* Tests for pread and pwrite.
2
   Copyright (C) 1998, 2000 Free Software Foundation, Inc.
3
   This file is part of the GNU C Library.
4
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
 
6
   The GNU C Library is free software; you can redistribute it and/or
7
   modify it under the terms of the GNU Lesser General Public
8
   License as published by the Free Software Foundation; either
9
   version 2.1 of the License, or (at your option) any later version.
10
 
11
   The GNU C Library is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
   Lesser General Public License for more details.
15
 
16
   You should have received a copy of the GNU Lesser General Public
17
   License along with the GNU C Library; if not, write to the Free
18
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19
   02111-1307 USA.  */
20
 
21
#define _GNU_SOURCE
22
#include <stdlib.h>
23
#include <stdio.h>
24
#include <search.h>
25
#include <errno.h>
26
#include <error.h>
27
#include <string.h>
28
#include <unistd.h>
29
 
30
#define TESTFILE_NAME "CRAP.XXXXXX"
31
#define STRINGIFY(s) STRINGIFY2 (s)
32
#define STRINGIFY2(s) #s
33
 
34
/* These are for the temporary file we generate.  */
35
char *name;
36
int fd;
37
 
38
 
39
/* Test the 32-bit versions first.  */
40
#define PREAD pread
41
#define PWRITE pwrite
42
 
43
int test(int argc, char *argv[])
44
{
45
    char buf[1000];
46
    char res[1000];
47
    int i;
48
 
49
    memset (buf, '\0', sizeof (buf));
50
    memset (res, '\xff', sizeof (res));
51
 
52
    if (write (fd, buf, sizeof (buf)) != sizeof (buf))
53
        error (EXIT_FAILURE, errno, "during write");
54
 
55
    for (i = 100; i < 200; ++i)
56
        buf[i] = i;
57
    if (PWRITE (fd, buf + 100, 100, 100) != 100)
58
        error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PWRITE));
59
 
60
    for (i = 450; i < 600; ++i)
61
        buf[i] = i;
62
    if (PWRITE (fd, buf + 450, 150, 450) != 150)
63
        error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PWRITE));
64
 
65
    if (PREAD (fd, res, sizeof (buf) - 50, 50) != sizeof (buf) - 50)
66
        error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PREAD));
67
 
68
    close (fd);
69
    unlink (name);
70
    return memcmp (buf + 50, res, sizeof (buf) - 50);
71
}
72
 
73
/* Test the 64-bit versions as well.  */
74
#if defined __UCLIBC_HAS_LFS__ 
75
 
76
#undef PREAD
77
#undef PWRITE
78
#define PREAD pread64
79
#define PWRITE pwrite64
80
 
81
 
82
int test64(int argc, char *argv[])
83
{
84
    char buf[1000];
85
    char res[1000];
86
    int i;
87
 
88
    memset (buf, '\0', sizeof (buf));
89
    memset (res, '\xff', sizeof (res));
90
 
91
    if (write (fd, buf, sizeof (buf)) != sizeof (buf))
92
        error (EXIT_FAILURE, errno, "during write");
93
 
94
    for (i = 100; i < 200; ++i)
95
        buf[i] = i;
96
    if (PWRITE (fd, buf + 100, 100, 100) != 100)
97
        error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PWRITE));
98
 
99
    for (i = 450; i < 600; ++i)
100
        buf[i] = i;
101
    if (PWRITE (fd, buf + 450, 150, 450) != 150)
102
        error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PWRITE));
103
 
104
    if (PREAD (fd, res, sizeof (buf) - 50, 50) != sizeof (buf) - 50)
105
        error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PREAD));
106
 
107
    close (fd);
108
    unlink (name);
109
    return memcmp (buf + 50, res, sizeof (buf) - 50);
110
}
111
#endif
112
 
113
void prepare(void)
114
{
115
    if (!name) {
116
        name = malloc (BUFSIZ);
117
        if (name == NULL)
118
            error (EXIT_FAILURE, errno, "cannot allocate file name");
119
    }
120
    strncpy(name, TESTFILE_NAME, BUFSIZ);
121
 
122
    /* Open our test file.   */
123
    fd = mkstemp (name);
124
    if (fd == -1)
125
        error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
126
}
127
 
128
int main (int argc, char **argv)
129
{
130
    int result = 0;
131
 
132
    prepare();
133
    result+=test(argc, argv);
134
    if (result) {
135
        fprintf(stderr, "pread/pwrite test failed.\n");
136
        return(EXIT_FAILURE);
137
    }
138
    fprintf(stderr, "pread/pwrite test successful.\n");
139
 
140
#if defined __UCLIBC_HAS_LFS__ 
141
    prepare();
142
    result+=test64(argc, argv);
143
    if (result) {
144
        fprintf(stderr, "pread64/pwrite64 test failed.\n");
145
        return(EXIT_FAILURE);
146
    }
147
    fprintf(stderr, "pread64/pwrite64 test successful.\n");
148
#endif
149
    return(EXIT_SUCCESS);
150
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.