1/*
2 * pathname.c -
3 *
4 * Written by Eryk Vershen
5 */
6
7/*
8 * Copyright 1997,1998 by Apple Computer, Inc.
9 *              All Rights Reserved
10 *
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.
16 *
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.
20 *
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.
26 */
27
28
29// for strncmp()
30#include <string.h>
31
32#include "pathname.h"
33#include "file_media.h"
34
35#if !defined(__linux__) && !defined(__unix__)
36#include "SCSI_media.h"
37#include "ATA_media.h"
38#endif
39
40
41/*
42 * Defines
43 */
44
45
46/*
47 * Types
48 */
49
50
51/*
52 * Global Constants
53 */
54
55
56/*
57 * Global Variables
58 */
59
60
61/*
62 * Forward declarations
63 */
64
65
66/*
67 * Routines
68 */
69
70/*
71 * Note that open_pathname_as_media() and get_linux_name() have almost
72 * identical structures.  If one changes the other must also!
73 */
74MEDIA
75open_pathname_as_media(char *path, int oflag)
76{
77    MEDIA	m = 0;
78#if !defined(__linux__) && !defined(__unix__)
79    long	id;
80    long	bus;
81
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);
94	    }
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);
107	    }
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);
120	    }
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);
126	    }
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);
132	    }
133	}
134    } else
135#endif
136
137    {
138	m = open_file_as_media(path, oflag);
139    }
140    return m;
141}
142
143
144char *
145get_linux_name(char *path)
146{
147    char	*result = 0;
148#if !defined(__linux__) && !defined(__unix__)
149    long	id;
150    long	bus;
151
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);
166	    }
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);
181	    }
182	}
183    }
184#endif
185
186    return result;
187}
188
189
190MEDIA_ITERATOR
191first_media_kind(long *state)
192{
193    *state = 0;
194    return next_media_kind(state);
195}
196
197
198MEDIA_ITERATOR
199next_media_kind(long *state)
200{
201    MEDIA_ITERATOR result;
202    long ix;
203
204    result = 0;
205    ix = *state;
206
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;
215	}
216	/* fall through to next interface */
217
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;
225	}
226	/* fall through to next interface */
227
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;
235	}
236	/* fall through to next interface */
237
238    case 3:
239    default:
240	break;
241    }
242
243    *state = ix;
244    return result;
245}
246
247
248