bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / file_io / os2 / seek.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr_arch_file_io.h"
18 #include "apr_file_io.h"
19 #include "apr_lib.h"
20 #include <string.h>
21 #include <io.h>
22
23
24 static apr_status_t setptr(apr_file_t *thefile, unsigned long pos )
25 {
26     long newbufpos;
27     ULONG rc;
28
29     if (thefile->direction == 1) {
30         apr_status_t rv = apr_file_flush(thefile);
31
32         if (rv != APR_SUCCESS) {
33             return rv;
34         }
35
36         thefile->bufpos = thefile->direction = thefile->dataRead = 0;
37     }
38
39     newbufpos = pos - (thefile->filePtr - thefile->dataRead);
40     if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
41         thefile->bufpos = newbufpos;
42         rc = 0;
43     } else {
44         rc = DosSetFilePtr(thefile->filedes, pos, FILE_BEGIN, &thefile->filePtr );
45
46         if ( !rc )
47             thefile->bufpos = thefile->dataRead = 0;
48     }
49
50     return APR_FROM_OS_ERROR(rc);
51 }
52
53
54
55 APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *thefile, apr_seek_where_t where, apr_off_t *offset)
56 {
57     if (!thefile->isopen) {
58         return APR_EBADF;
59     }
60
61     thefile->eof_hit = 0;
62
63     if (thefile->buffered) {
64         int rc = EINVAL;
65         apr_finfo_t finfo;
66
67         switch (where) {
68         case APR_SET:
69             rc = setptr(thefile, *offset);
70             break;
71
72         case APR_CUR:
73             rc = setptr(thefile, thefile->filePtr - thefile->dataRead + thefile->bufpos + *offset);
74             break;
75
76         case APR_END:
77             rc = apr_file_info_get(&finfo, APR_FINFO_NORM, thefile);
78             if (rc == APR_SUCCESS)
79                 rc = setptr(thefile, finfo.size + *offset);
80             break;
81         }
82
83         *offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
84         return rc;
85     } else {
86         switch (where) {
87         case APR_SET:
88             where = FILE_BEGIN;
89             break;
90
91         case APR_CUR:
92             where = FILE_CURRENT;
93             break;
94
95         case APR_END:
96             where = FILE_END;
97             break;
98         }
99
100         return APR_FROM_OS_ERROR(DosSetFilePtr(thefile->filedes, *offset, where, (ULONG *)offset));
101     }
102 }
103
104
105
106 APR_DECLARE(apr_status_t) apr_file_trunc(apr_file_t *fp, apr_off_t offset)
107 {
108     int rc = DosSetFileSize(fp->filedes, offset);
109
110     if (rc != 0) {
111         return APR_FROM_OS_ERROR(rc);
112     }
113
114     if (fp->buffered) {
115         return setptr(fp, offset);
116     }
117
118     return APR_SUCCESS;
119 }