summaryrefslogtreecommitdiff
path: root/sbin/pdisk/pathname.c
blob: 81198433f36959822eb13e2e4b53131e5f5c9beb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * pathname.c -
 *
 * Written by Eryk Vershen (eryk@apple.com)
 */

/*
 * Copyright 1997,1998 by Apple Computer, Inc.
 *              All Rights Reserved
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appears in all copies and
 * that both the copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */


// for strncmp()
#include <string.h>

#include "pathname.h"
#include "file_media.h"

#ifndef __linux__
#include "SCSI_media.h"
#include "ATA_media.h"
#endif


/*
 * Defines
 */


/*
 * Types
 */


/*
 * Global Constants
 */


/*
 * Global Variables
 */


/*
 * Forward declarations
 */


/*
 * Routines
 */

/*
 * Note that open_pathname_as_media() and get_mklinux_name() have almost
 * identical structures.  If one changes the other must also!
 */
MEDIA
open_pathname_as_media(char *path, int oflag)
{
    MEDIA	m = 0;
#if !defined(__linux__) && !defined(__unix__)
    long	id;
    long	bus;
	
    if (strncmp("/dev/", path, 5) == 0) {
	if (strncmp("/dev/scsi", path, 9) == 0) {
	    if (path[9] >= '0' && path[9] <= '7' && path[10] == 0) {
		id = path[9] - '0';
		m = open_old_scsi_as_media(id);
	    } else if (path[9] >= '0' && path[9] <= '7' && path[10] == '.'
		    && path[11] >= '0' && path[11] <= '7' && path[12] == 0) {
		id = path[11] - '0';
		bus = path[9] - '0';
		m = open_scsi_as_media(bus, id);
	    }
	} else if (strncmp("/dev/ata", path, 8) == 0
		|| strncmp("/dev/ide", path, 8) == 0) {
	    if (path[8] >= '0' && path[8] <= '7' && path[9] == 0) {
		bus = path[8] - '0';
		m = open_ata_as_media(bus, 0);
	    } else if (path[8] >= '0' && path[8] <= '7' && path[9] == '.'
		    && path[10] >= '0' && path[10] <= '1' && path[11] == 0) {
		id = path[10] - '0';
		bus = path[8] - '0';
		m = open_ata_as_media(bus, id);
	    }
	} else if (strncmp("/dev/sd", path, 7) == 0) {
	    if (path[7] >= 'a' && path[7] <= 'z' && path[8] == 0) {
		id = path[7] - 'a';
		m = open_mklinux_scsi_as_media(id, 0);
	    }
	} else if (strncmp("/dev/scd", path, 8) == 0) {
	    if (path[8] >= '0' && path[8] <= '9' && path[9] == 0) {
		id = path[8] - '0';
		m = open_mklinux_scsi_as_media(id, 1);
	    }
	} else if (strncmp("/dev/hd", path, 7) == 0) {
	    if (path[7] >= 'a' && path[7] <= 'z' && path[8] == 0) {
		id = path[7] - 'a';
		m = open_mklinux_ata_as_media(id);
	    }
	}
    } else
#endif

    {
	m = open_file_as_media(path, oflag);
    }
    return m;
}


char *
get_mklinux_name(char *path)
{
    char	*result = 0;
#if !defined(__linux__) && !defined(__unix__)
    long	id;
    long	bus;

    if (strncmp("/dev/", path, 5) == 0) {
	if (strncmp("/dev/scsi", path, 9) == 0) {
	    if (path[9] >= '0' && path[9] <= '7' && path[10] == 0) {
		/* old scsi */
		id = path[9] - '0';
		result = mklinux_old_scsi_name(id);
	    } else if (path[9] >= '0' && path[9] <= '7' && path[10] == '.'
		    && path[11] >= '0' && path[11] <= '7' && path[12] == 0) {
		/* new scsi */
		id = path[11] - '0';
		bus = path[9] - '0';
		result = mklinux_scsi_name(bus, id);
	    }
	} else if (strncmp("/dev/ata", path, 8) == 0
		|| strncmp("/dev/ide", path, 8) == 0) {
	    if (path[8] >= '0' && path[8] <= '7' && path[9] == 0) {
		/* ata/ide - master device */
		bus = path[8] - '0';
		result = mklinux_ata_name(bus, 0);
	    } else if (path[8] >= '0' && path[8] <= '7' && path[9] == '.'
		    && path[10] >= '0' && path[10] <= '1' && path[11] == 0) {
		/* ata/ide */
		id = path[10] - '0';
		bus = path[8] - '0';
		result = mklinux_ata_name(bus, id);
	    }
	}
    }
#endif

    return result;
}


MEDIA_ITERATOR
first_media_kind(long *state)
{
    *state = 0;
    return next_media_kind(state);
}


MEDIA_ITERATOR
next_media_kind(long *state)
{
    MEDIA_ITERATOR result;
    long index;

    result = 0;
    index = *state;

    switch (index) {
    case 0:
#if defined(__linux__) || defined(__unix__)
	result = create_file_iterator();
#endif
	index = 1;
	if (result != 0) {
		break;
	}
	/* fall through to next interface */

    case 1:
#if !defined(__linux__) && !defined(__unix__)
	result = create_ata_iterator();
#endif
	index = 2;
	if (result != 0) {
		break;
	}
	/* fall through to next interface */

    case 2:
#if !defined(__linux__) && !defined(__unix__)
	result = create_scsi_iterator();
#endif
	index = 3;
	if (result != 0) {
		break;
	}
	/* fall through to next interface */

    case 3:
    default:
	break;
    }

    *state = index;
    return result;
}