Subversion Repositories HelenOS

Rev

Rev 2404 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2404 Rev 2435
1
/*
1
/*
2
 * Copyright (c) 1987,1997, Prentice Hall
2
 * Copyright (c) 1987,1997, Prentice Hall
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use of the MINIX operating system in source and
5
 * Redistribution and use of the MINIX operating system in source and
6
 * binary forms, with or without modification, are permitted provided
6
 * binary forms, with or without modification, are permitted provided
7
 * that the following conditions are met:
7
 * that the following conditions are met:
8
 
8
 
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 
11
 
12
 * - Redistributions in binary form must reproduce the above
12
 * - Redistributions in binary form must reproduce the above
13
 *   copyright notice, this list of conditions and the following
13
 *   copyright notice, this list of conditions and the following
14
 *   disclaimer in the documentation and/or other materials provided
14
 *   disclaimer in the documentation and/or other materials provided
15
 *   with the distribution.
15
 *   with the distribution.
16
 
16
 
17
 * - Neither the name of Prentice Hall nor the names of the software
17
 * - Neither the name of Prentice Hall nor the names of the software
18
 *   authors or contributors may be used to endorse or promote
18
 *   authors or contributors may be used to endorse or promote
19
 *   products derived from this software without specific prior
19
 *   products derived from this software without specific prior
20
 *   written permission.
20
 *   written permission.
21
 
21
 
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
23
 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26
 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
26
 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
27
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
 */
34
 */
-
 
35
 
-
 
36
/** @addtogroup FileSystemImpl
-
 
37
* @{
-
 
38
*/
-
 
39
 
-
 
40
/**
-
 
41
 * @file    utility.c
-
 
42
 * @brief   This is the master header for fs.  It includes some other files
-
 
43
*         and defines the principal constants.
-
 
44
*/
35
 
45
 
36
 
46
 
37
/* This file contains a few general purpose utility routines. */
47
/* This file contains a few general purpose utility routines. */
38
 
48
 
39
/* Methods:
-
 
40
 * fetch_name:  go get a path name from user space
-
 
41
 * no_sys:      reject a system call that FS does not handle
-
 
42
 * conv2:       does byte swapping on a 16-bit int
-
 
43
 * conv4:       does byte swapping on a 32-bit long
-
 
44
 * fs_strncmp:  same like strncmp
-
 
45
 */
-
 
46
 
-
 
47
 
-
 
48
#include <unistd.h>
49
#include <unistd.h>
49
#include "fs.h"
50
#include "fs.h"
50
#include "block.h"
51
#include "block.h"
51
#include "file.h"
52
#include "file.h"
52
#include "fproc.h"
53
#include "fproc.h"
53
#include "inode.h"
54
#include "inode.h"
54
 
55
 
-
 
56
/**
-
 
57
 * Go get a path name from user space
55
 
58
 */
56
int fetch_name(char* path, int len)
59
int fetch_name(char* path, int len)
57
{
60
{
58
   
61
   
59
    /* Go get path and put it in 'user_path' */
62
    /* Go get path and put it in 'user_path' */
60
 
63
 
61
    register char *rpu, *rpm;
64
    register char *rpu, *rpm;
62
 
65
 
63
    /* Check name length for validity */
66
    /* Check name length for validity */
64
    if (len <= 0) {
67
    if (len <= 0) {
65
        err_code = FS_EINVAL;
68
        err_code = FS_EINVAL;
66
        return FS_EGENERIC;
69
        return FS_EGENERIC;
67
    }
70
    }
68
 
71
 
69
    if (len > PATH_MAX) {
72
    if (len > PATH_MAX) {
70
        err_code = FS_ENAMETOOLONG;
73
        err_code = FS_ENAMETOOLONG;
71
        return FS_EGENERIC;
74
        return FS_EGENERIC;
72
    }
75
    }
73
 
76
 
74
    rpu = &user_path[0];
77
    rpu = &user_path[0];
75
    rpm = path;
78
    rpm = path;
76
    do { *rpu++ = *rpm++;
79
    do { *rpu++ = *rpm++;
77
        } while (--len);
80
        } while (--len);
78
     
81
     
79
    return OK;
82
    return OK;
80
}
83
}
81
   
84
 
-
 
85
/**
-
 
86
 * Reject a system call that FS does not handle
-
 
87
 */
82
int no_sys()
88
int no_sys()
83
{
89
{
84
   
90
   
85
    /* Somebody has used an illegal system call number */
91
    /* Somebody has used an illegal system call number */
86
         
92
         
87
    print_console("FS_NOSYS called, illegal system call number!\n");
93
    print_console("FS_NOSYS called, illegal system call number!\n");
88
   
94
   
89
    return FS_EINVAL;
95
    re\turn FS_EINVAL;
90
}
96
}
91
   
97
 
-
 
98
/**
-
 
99
 * Does byte swapping on a 16-bit int
-
 
100
 */
92
unsigned conv2(int norm, int w)
101
unsigned conv2(int norm, int w)
93
{
102
{
94
   
103
   
95
    /* Possibly swap a 16-bit word between 8086 and 68000 byte order. */
104
    /* Possibly swap a 16-bit word between 8086 and 68000 byte order. */
96
 
105
 
97
    if (norm)   /* TRUE if no swap, FALSE for byte swap */
106
    if (norm)   /* TRUE if no swap, FALSE for byte swap */
98
        return((unsigned) w & 0xFFFF);
107
        return((unsigned) w & 0xFFFF);
99
       
108
       
100
    return(((w&BYTE) << 8) | ( (w>>8) & BYTE));
109
    return(((w&BYTE) << 8) | ( (w>>8) & BYTE));
101
}
110
}
102
 
111
 
-
 
112
/**
-
 
113
 * Does byte swapping on a 32-bit long
-
 
114
 */
103
long conv4(int norm, long x)
115
long conv4(int norm, long x)
104
{
116
{
105
   
117
   
106
    /* Possibly swap a 32-bit long between 8086 and 68000 byte order. */
118
    /* Possibly swap a 32-bit long between 8086 and 68000 byte order. */
107
   
119
   
108
    unsigned lo, hi;
120
    unsigned lo, hi;
109
    long l;
121
    long l;
110
     
122
     
111
    if (norm)       /* byte order was already ok */
123
    if (norm)       /* byte order was already ok */
112
        return(x);
124
        return(x);
113
                       
125
                       
114
    lo = conv2(FALSE, (int) x & 0xFFFF);        /* low-order half, byte swapped */
126
    lo = conv2(FALSE, (int) x & 0xFFFF);        /* low-order half, byte swapped */
115
    hi = conv2(FALSE, (int) (x>>16) & 0xFFFF);  /* high-order half, swapped */
127
    hi = conv2(FALSE, (int) (x>>16) & 0xFFFF);  /* high-order half, swapped */
116
    l = ((long) lo <<16) | hi;
128
    l = ((long) lo <<16) | hi;
117
     
129
     
118
    return l;
130
    return l;
119
}
131
}
120
 
132
 
-
 
133
/**
-
 
134
 * Same like strncmp
-
 
135
 */
121
int fs_strncmp(const char *src, const char *dst, size_t len)
136
int fs_strncmp(const char *src, const char *dst, size_t len)
122
{
137
{
123
   
138
   
124
    int i, src_len, dst_len;
139
    int i, src_len, dst_len;
125
       
140
       
126
    if (src == NULL) {
141
    if (src == NULL) {
127
        if (dst == NULL)
142
        if (dst == NULL)
128
            return 0;
143
            return 0;
129
        return -1; 
144
        return -1; 
130
    }
145
    }
131
   
146
   
132
    if (dst == NULL)
147
    if (dst == NULL)
133
        return 1;
148
        return 1;
134
 
149
 
135
    src_len = strlen(src);
150
    src_len = strlen(src);
136
    dst_len = strlen(dst);
151
    dst_len = strlen(dst);
137
 
152
 
138
    for (i = 0; i < len && i < src_len && i < dst_len; i++) {
153
    for (i = 0; i < len && i < src_len && i < dst_len; i++) {
139
        if (src[i] > dst[i])
154
        if (src[i] > dst[i])
140
            return 1;
155
            return 1;
141
        if (src[i] < dst[i])
156
        if (src[i] < dst[i])
142
            return -1;
157
            return -1;
143
    }
158
    }
144
 
159
 
145
    return 0;
160
    return 0;
146
}
161
}
-
 
162
 
-
 
163
/**
-
 
164
 * }
-
 
165
 */
-
 
166
 
147
 
167