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.h"
18#include "apr_arch_file_io.h"
19#include "apr_strings.h"
20#include "apr_lib.h"
21#include <ctype.h>
22
23/* OS/2 Exceptions:
24 *
25 * Note that trailing spaces and trailing periods are never recorded
26 * in the file system.
27 *
28 * Leading spaces and periods are accepted, however.
29 * The * ? < > codes all have wildcard side effects
30 * The " / \ : are exclusively component separator tokens
31 * The system doesn't accept | for any (known) purpose
32 * Oddly, \x7f _is_ acceptable ;)
33 */
34
35const char c_is_fnchar[256] =
36{/* Reject all ctrl codes...                                         */
37    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
38 /*     "               *         /                      :   <   > ? */
39    1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,0, 1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,0,
40 /*                                                          \       */
41    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,
42 /*                                                          |       */
43    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,
44 /* High bit codes are accepted                                      */
45    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
46    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
47    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
48    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
49};
50
51
52#define IS_SLASH(c) (c == '/' || c == '\\')
53
54
55apr_status_t filepath_root_test(char *path, apr_pool_t *p)
56{
57    char drive = apr_toupper(path[0]);
58
59    if (drive >= 'A' && drive <= 'Z' && path[1] == ':' && IS_SLASH(path[2]))
60        return APR_SUCCESS;
61
62    return APR_EBADPATH;
63}
64
65
66apr_status_t filepath_drive_get(char **rootpath, char drive,
67                                apr_int32_t flags, apr_pool_t *p)
68{
69    char path[APR_PATH_MAX];
70    char *pos;
71    ULONG rc;
72    ULONG bufsize = sizeof(path) - 3;
73
74    path[0] = drive;
75    path[1] = ':';
76    path[2] = '/';
77
78    rc = DosQueryCurrentDir(apr_toupper(drive) - 'A', path+3, &bufsize);
79
80    if (rc) {
81        return APR_FROM_OS_ERROR(rc);
82    }
83
84    if (!(flags & APR_FILEPATH_NATIVE)) {
85        for (pos=path; *pos; pos++) {
86            if (*pos == '\\')
87                *pos = '/';
88        }
89    }
90
91    *rootpath = apr_pstrdup(p, path);
92    return APR_SUCCESS;
93}
94
95
96apr_status_t filepath_root_case(char **rootpath, char *root, apr_pool_t *p)
97{
98    if (root[0] && apr_islower(root[0]) && root[1] == ':') {
99        *rootpath = apr_pstrdup(p, root);
100        (*rootpath)[0] = apr_toupper((*rootpath)[0]);
101    }
102    else {
103       *rootpath = root;
104    }
105    return APR_SUCCESS;
106}
107
108
109APR_DECLARE(apr_status_t) apr_filepath_get(char **defpath, apr_int32_t flags,
110                                           apr_pool_t *p)
111{
112    char path[APR_PATH_MAX];
113    ULONG drive;
114    ULONG drivemap;
115    ULONG rv, pathlen = sizeof(path) - 3;
116    char *pos;
117
118    DosQueryCurrentDisk(&drive, &drivemap);
119    path[0] = '@' + drive;
120    strcpy(path+1, ":\\");
121    rv = DosQueryCurrentDir(drive, path+3, &pathlen);
122
123    *defpath = apr_pstrdup(p, path);
124
125    if (!(flags & APR_FILEPATH_NATIVE)) {
126        for (pos=*defpath; *pos; pos++) {
127            if (*pos == '\\')
128                *pos = '/';
129        }
130    }
131
132    return APR_SUCCESS;
133}
134
135
136
137APR_DECLARE(apr_status_t) apr_filepath_set(const char *path, apr_pool_t *p)
138{
139    ULONG rv = 0;
140
141    if (path[1] == ':')
142        rv = DosSetDefaultDisk(apr_toupper(path[0]) - '@');
143
144    if (rv == 0)
145        rv = DosSetCurrentDir(path);
146
147    return APR_FROM_OS_ERROR(rv);
148}
149