Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / pdisk / pathname.c
blob75acd51a1b74d553a0f96dc23e51f5a9bb045b52
1 /*
2 * pathname.c -
4 * Written by Eryk Vershen
5 */
7 /*
8 * Copyright 1997,1998 by Apple Computer, Inc.
9 * All Rights Reserved
11 * Permission to use, copy, modify, and distribute this software and
12 * its documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appears in all copies and
14 * that both the copyright notice and this permission notice appear in
15 * supporting documentation.
17 * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE.
21 * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
22 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
23 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
24 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
25 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 // for strncmp()
30 #include <string.h>
32 #include "pathname.h"
33 #include "file_media.h"
35 #if !defined(__linux__) && !defined(__unix__)
36 #include "SCSI_media.h"
37 #include "ATA_media.h"
38 #endif
42 * Defines
47 * Types
52 * Global Constants
57 * Global Variables
62 * Forward declarations
67 * Routines
71 * Note that open_pathname_as_media() and get_linux_name() have almost
72 * identical structures. If one changes the other must also!
74 MEDIA
75 open_pathname_as_media(char *path, int oflag)
77 MEDIA m = 0;
78 #if !defined(__linux__) && !defined(__unix__)
79 long id;
80 long bus;
82 if (strncmp("/dev/", path, 5) == 0) {
83 if (strncmp("/dev/scsi", path, 9) == 0) {
84 if (path[9] >= '0' && path[9] <= '7' && path[10] == 0) {
85 // scsi[0-7]
86 id = path[9] - '0';
87 m = open_old_scsi_as_media(id);
88 } else if (path[9] >= '0' && path[9] <= '7' && path[10] == '.'
89 && path[11] >= '0' && path[11] <= '7' && path[12] == 0) {
90 // scsi[0-7].[0-7]
91 id = path[11] - '0';
92 bus = path[9] - '0';
93 m = open_scsi_as_media(bus, id);
95 } else if (strncmp("/dev/ata", path, 8) == 0
96 || strncmp("/dev/ide", path, 8) == 0) {
97 if (path[8] >= '0' && path[8] <= '7' && path[9] == 0) {
98 // ata[0-7], ide[0-7]
99 bus = path[8] - '0';
100 m = open_ata_as_media(bus, 0);
101 } else if (path[8] >= '0' && path[8] <= '7' && path[9] == '.'
102 && path[10] >= '0' && path[10] <= '1' && path[11] == 0) {
103 // ata[0-7].[0-1], ide[0-7].[0-1]
104 id = path[10] - '0';
105 bus = path[8] - '0';
106 m = open_ata_as_media(bus, id);
108 } else if (strncmp("/dev/sd", path, 7) == 0) {
109 if (path[7] >= 'a' && path[7] <= 'z' && path[8] == 0) {
110 // sd[a-z]
111 id = path[7] - 'a';
112 m = open_linux_scsi_as_media(id, 0);
113 } else if (path[7] >= 'a' && path[7] <= 'z' && path[8] == '.'
114 && path[9] >= 'a' && path[9] <= 'z' && path[10] == 0) {
115 // sd[a-z][a-z]
116 bus = path[7] - 'a';
117 id = path[9] - 'a';
118 id += bus * 26;
119 m = open_linux_scsi_as_media(id, 0);
121 } else if (strncmp("/dev/scd", path, 8) == 0) {
122 if (path[8] >= '0' && path[8] <= '9' && path[9] == 0) {
123 // scd[0-9]
124 id = path[8] - '0';
125 m = open_linux_scsi_as_media(id, 1);
127 } else if (strncmp("/dev/hd", path, 7) == 0) {
128 if (path[7] >= 'a' && path[7] <= 'z' && path[8] == 0) {
129 // hd[a-z]
130 id = path[7] - 'a';
131 m = open_linux_ata_as_media(id);
134 } else
135 #endif
138 m = open_file_as_media(path, oflag);
140 return m;
144 char *
145 get_linux_name(char *path)
147 char *result = 0;
148 #if !defined(__linux__) && !defined(__unix__)
149 long id;
150 long bus;
152 if (strncmp("/dev/", path, 5) == 0) {
153 if (strncmp("/dev/scsi", path, 9) == 0) {
154 if (path[9] >= '0' && path[9] <= '7' && path[10] == 0) {
155 /* old scsi */
156 // scsi[0-7]
157 id = path[9] - '0';
158 result = linux_old_scsi_name(id);
159 } else if (path[9] >= '0' && path[9] <= '7' && path[10] == '.'
160 && path[11] >= '0' && path[11] <= '7' && path[12] == 0) {
161 /* new scsi */
162 // scsi[0-7].[0-7]
163 id = path[11] - '0';
164 bus = path[9] - '0';
165 result = linux_scsi_name(bus, id);
167 } else if (strncmp("/dev/ata", path, 8) == 0
168 || strncmp("/dev/ide", path, 8) == 0) {
169 if (path[8] >= '0' && path[8] <= '7' && path[9] == 0) {
170 /* ata/ide - master device */
171 // ata[0-7], ide[0-7]
172 bus = path[8] - '0';
173 result = linux_ata_name(bus, 0);
174 } else if (path[8] >= '0' && path[8] <= '7' && path[9] == '.'
175 && path[10] >= '0' && path[10] <= '1' && path[11] == 0) {
176 /* ata/ide */
177 // ata[0-7].[0-1], ide[0-7].[0-1]
178 id = path[10] - '0';
179 bus = path[8] - '0';
180 result = linux_ata_name(bus, id);
184 #endif
186 return result;
190 MEDIA_ITERATOR
191 first_media_kind(long *state)
193 *state = 0;
194 return next_media_kind(state);
198 MEDIA_ITERATOR
199 next_media_kind(long *state)
201 MEDIA_ITERATOR result;
202 long ix;
204 result = 0;
205 ix = *state;
207 switch (ix) {
208 case 0:
209 #if defined(__linux__) || defined(__unix__)
210 result = create_file_iterator();
211 #endif
212 ix = 1;
213 if (result != 0) {
214 break;
216 /* fall through to next interface */
218 case 1:
219 #if !defined(__linux__) && !defined(__unix__)
220 result = create_ata_iterator();
221 #endif
222 ix = 2;
223 if (result != 0) {
224 break;
226 /* fall through to next interface */
228 case 2:
229 #if !defined(__linux__) && !defined(__unix__)
230 result = create_scsi_iterator();
231 #endif
232 ix = 3;
233 if (result != 0) {
234 break;
236 /* fall through to next interface */
238 case 3:
239 default:
240 break;
243 *state = ix;
244 return result;