rmtlib.c revision 1.1 1 1.1 jtc #ifndef lint
2 1.1 jtc static char *RCSid = "$Header: /tank/opengrok/rsync2/NetBSD/src/lib/librmt/rmtlib.c,v 1.1 1996/08/09 03:35:19 jtc Exp $";
3 1.1 jtc #endif
4 1.1 jtc
5 1.1 jtc /*
6 1.1 jtc * $Log: rmtlib.c,v $
7 1.1 jtc * Revision 1.1 1996/08/09 03:35:19 jtc
8 1.1 jtc * Remote mag tape library from volume 18 of comp.sources.unix.
9 1.1 jtc *
10 1.1 jtc * Revision 1.7 89/03/23 14:09:51 root
11 1.1 jtc * Fix from haynes (at) ucscc.ucsc.edu for use w/compat. ADR.
12 1.1 jtc *
13 1.1 jtc * Revision 1.6 88/10/25 17:04:29 root
14 1.1 jtc * rexec code and a bug fix from srs!dan, miscellanious cleanup. ADR.
15 1.1 jtc *
16 1.1 jtc * Revision 1.5 88/10/25 16:30:17 root
17 1.1 jtc * Fix from jeff (at) gatech.edu for getting user@host:dev right. ADR.
18 1.1 jtc *
19 1.1 jtc * Revision 1.4 87/10/30 10:36:12 root
20 1.1 jtc * Made 4.2 syntax a compile time option. ADR.
21 1.1 jtc *
22 1.1 jtc * Revision 1.3 87/04/22 11:16:48 root
23 1.1 jtc * Two fixes from parmelee (at) wayback.cs.cornell.edu to correctly
24 1.1 jtc * do fd biasing and rmt protocol on 'S' command. ADR.
25 1.1 jtc *
26 1.1 jtc * Revision 1.2 86/10/09 16:38:53 root
27 1.1 jtc * Changed to reflect 4.3BSD rcp syntax. ADR.
28 1.1 jtc *
29 1.1 jtc * Revision 1.1 86/10/09 16:17:35 root
30 1.1 jtc * Initial revision
31 1.1 jtc *
32 1.1 jtc */
33 1.1 jtc
34 1.1 jtc /*
35 1.1 jtc * rmt --- remote tape emulator subroutines
36 1.1 jtc *
37 1.1 jtc * Originally written by Jeff Lee, modified some by Arnold Robbins
38 1.1 jtc *
39 1.1 jtc * WARNING: The man page rmt(8) for /etc/rmt documents the remote mag
40 1.1 jtc * tape protocol which rdump and rrestore use. Unfortunately, the man
41 1.1 jtc * page is *WRONG*. The author of the routines I'm including originally
42 1.1 jtc * wrote his code just based on the man page, and it didn't work, so he
43 1.1 jtc * went to the rdump source to figure out why. The only thing he had to
44 1.1 jtc * change was to check for the 'F' return code in addition to the 'E',
45 1.1 jtc * and to separate the various arguments with \n instead of a space. I
46 1.1 jtc * personally don't think that this is much of a problem, but I wanted to
47 1.1 jtc * point it out.
48 1.1 jtc * -- Arnold Robbins
49 1.1 jtc *
50 1.1 jtc * Redone as a library that can replace open, read, write, etc, by
51 1.1 jtc * Fred Fish, with some additional work by Arnold Robbins.
52 1.1 jtc */
53 1.1 jtc
54 1.1 jtc /*
55 1.1 jtc * MAXUNIT --- Maximum number of remote tape file units
56 1.1 jtc *
57 1.1 jtc * READ --- Return the number of the read side file descriptor
58 1.1 jtc * WRITE --- Return the number of the write side file descriptor
59 1.1 jtc */
60 1.1 jtc
61 1.1 jtc #define RMTIOCTL 1
62 1.1 jtc /* #define USE_REXEC 1 /* rexec code courtesy of Dan Kegel, srs!dan */
63 1.1 jtc
64 1.1 jtc #include <stdio.h>
65 1.1 jtc #include <signal.h>
66 1.1 jtc #include <sys/types.h>
67 1.1 jtc
68 1.1 jtc #ifdef RMTIOCTL
69 1.1 jtc #include <sys/ioctl.h>
70 1.1 jtc #include <sys/mtio.h>
71 1.1 jtc #endif
72 1.1 jtc
73 1.1 jtc #ifdef USE_REXEC
74 1.1 jtc #include <netdb.h>
75 1.1 jtc #endif
76 1.1 jtc
77 1.1 jtc #include <errno.h>
78 1.1 jtc #include <setjmp.h>
79 1.1 jtc #include <sys/stat.h>
80 1.1 jtc
81 1.1 jtc #define BUFMAGIC 64 /* a magic number for buffer sizes */
82 1.1 jtc #define MAXUNIT 4
83 1.1 jtc
84 1.1 jtc #define READ(fd) (Ctp[fd][0])
85 1.1 jtc #define WRITE(fd) (Ptc[fd][1])
86 1.1 jtc
87 1.1 jtc static int Ctp[MAXUNIT][2] = { -1, -1, -1, -1, -1, -1, -1, -1 };
88 1.1 jtc static int Ptc[MAXUNIT][2] = { -1, -1, -1, -1, -1, -1, -1, -1 };
89 1.1 jtc
90 1.1 jtc static jmp_buf Jmpbuf;
91 1.1 jtc extern int errno;
92 1.1 jtc
93 1.1 jtc /*
94 1.1 jtc * abort --- close off a remote tape connection
95 1.1 jtc */
96 1.1 jtc
97 1.1 jtc static void abort(fildes)
98 1.1 jtc int fildes;
99 1.1 jtc {
100 1.1 jtc close(READ(fildes));
101 1.1 jtc close(WRITE(fildes));
102 1.1 jtc READ(fildes) = -1;
103 1.1 jtc WRITE(fildes) = -1;
104 1.1 jtc }
105 1.1 jtc
106 1.1 jtc
107 1.1 jtc
108 1.1 jtc /*
109 1.1 jtc * command --- attempt to perform a remote tape command
110 1.1 jtc */
111 1.1 jtc
112 1.1 jtc static int command(fildes, buf)
113 1.1 jtc int fildes;
114 1.1 jtc char *buf;
115 1.1 jtc {
116 1.1 jtc register int blen;
117 1.1 jtc int (*pstat)();
118 1.1 jtc
119 1.1 jtc /*
120 1.1 jtc * save current pipe status and try to make the request
121 1.1 jtc */
122 1.1 jtc
123 1.1 jtc blen = strlen(buf);
124 1.1 jtc pstat = signal(SIGPIPE, SIG_IGN);
125 1.1 jtc if (write(WRITE(fildes), buf, blen) == blen)
126 1.1 jtc {
127 1.1 jtc signal(SIGPIPE, pstat);
128 1.1 jtc return(0);
129 1.1 jtc }
130 1.1 jtc
131 1.1 jtc /*
132 1.1 jtc * something went wrong. close down and go home
133 1.1 jtc */
134 1.1 jtc
135 1.1 jtc signal(SIGPIPE, pstat);
136 1.1 jtc abort(fildes);
137 1.1 jtc
138 1.1 jtc errno = EIO;
139 1.1 jtc return(-1);
140 1.1 jtc }
141 1.1 jtc
142 1.1 jtc
143 1.1 jtc
144 1.1 jtc /*
145 1.1 jtc * status --- retrieve the status from the pipe
146 1.1 jtc */
147 1.1 jtc
148 1.1 jtc static int status(fildes)
149 1.1 jtc int fildes;
150 1.1 jtc {
151 1.1 jtc int i;
152 1.1 jtc char c, *cp;
153 1.1 jtc char buffer[BUFMAGIC];
154 1.1 jtc
155 1.1 jtc /*
156 1.1 jtc * read the reply command line
157 1.1 jtc */
158 1.1 jtc
159 1.1 jtc for (i = 0, cp = buffer; i < BUFMAGIC; i++, cp++)
160 1.1 jtc {
161 1.1 jtc if (read(READ(fildes), cp, 1) != 1)
162 1.1 jtc {
163 1.1 jtc abort(fildes);
164 1.1 jtc errno = EIO;
165 1.1 jtc return(-1);
166 1.1 jtc }
167 1.1 jtc if (*cp == '\n')
168 1.1 jtc {
169 1.1 jtc *cp = 0;
170 1.1 jtc break;
171 1.1 jtc }
172 1.1 jtc }
173 1.1 jtc
174 1.1 jtc if (i == BUFMAGIC)
175 1.1 jtc {
176 1.1 jtc abort(fildes);
177 1.1 jtc errno = EIO;
178 1.1 jtc return(-1);
179 1.1 jtc }
180 1.1 jtc
181 1.1 jtc /*
182 1.1 jtc * check the return status
183 1.1 jtc */
184 1.1 jtc
185 1.1 jtc for (cp = buffer; *cp; cp++)
186 1.1 jtc if (*cp != ' ')
187 1.1 jtc break;
188 1.1 jtc
189 1.1 jtc if (*cp == 'E' || *cp == 'F')
190 1.1 jtc {
191 1.1 jtc errno = atoi(cp + 1);
192 1.1 jtc while (read(READ(fildes), &c, 1) == 1)
193 1.1 jtc if (c == '\n')
194 1.1 jtc break;
195 1.1 jtc
196 1.1 jtc if (*cp == 'F')
197 1.1 jtc abort(fildes);
198 1.1 jtc
199 1.1 jtc return(-1);
200 1.1 jtc }
201 1.1 jtc
202 1.1 jtc /*
203 1.1 jtc * check for mis-synced pipes
204 1.1 jtc */
205 1.1 jtc
206 1.1 jtc if (*cp != 'A')
207 1.1 jtc {
208 1.1 jtc abort(fildes);
209 1.1 jtc errno = EIO;
210 1.1 jtc return(-1);
211 1.1 jtc }
212 1.1 jtc
213 1.1 jtc return(atoi(cp + 1));
214 1.1 jtc }
215 1.1 jtc
216 1.1 jtc #ifdef USE_REXEC
217 1.1 jtc
218 1.1 jtc /*
219 1.1 jtc * _rmt_rexec
220 1.1 jtc *
221 1.1 jtc * execute /etc/rmt on a remote system using rexec().
222 1.1 jtc * Return file descriptor of bidirectional socket for stdin and stdout
223 1.1 jtc * If username is NULL, or an empty string, uses current username.
224 1.1 jtc *
225 1.1 jtc * ADR: By default, this code is not used, since it requires that
226 1.1 jtc * the user have a .netrc file in his/her home directory, or that the
227 1.1 jtc * application designer be willing to have rexec prompt for login and
228 1.1 jtc * password info. This may be unacceptable, and .rhosts files for use
229 1.1 jtc * with rsh are much more common on BSD systems.
230 1.1 jtc */
231 1.1 jtc
232 1.1 jtc static int
233 1.1 jtc _rmt_rexec(host, user)
234 1.1 jtc char *host;
235 1.1 jtc char *user; /* may be NULL */
236 1.1 jtc {
237 1.1 jtc struct servent *rexecserv;
238 1.1 jtc
239 1.1 jtc rexecserv = getservbyname("exec", "tcp");
240 1.1 jtc if (NULL == rexecserv) {
241 1.1 jtc fprintf (stderr, "? exec/tcp: service not available.");
242 1.1 jtc exit (-1);
243 1.1 jtc }
244 1.1 jtc if ((user != NULL) && *user == '\0')
245 1.1 jtc user = (char *) NULL;
246 1.1 jtc return rexec (&host, rexecserv->s_port, user, NULL,
247 1.1 jtc "/etc/rmt", (int *)NULL);
248 1.1 jtc }
249 1.1 jtc #endif /* USE_REXEC */
250 1.1 jtc
251 1.1 jtc /*
252 1.1 jtc * _rmt_open --- open a magtape device on system specified, as given user
253 1.1 jtc *
254 1.1 jtc * file name has the form [user@]system:/dev/????
255 1.1 jtc #ifdef COMPAT
256 1.1 jtc * file name has the form system[.user]:/dev/????
257 1.1 jtc #endif
258 1.1 jtc */
259 1.1 jtc
260 1.1 jtc #define MAXHOSTLEN 257 /* BSD allows very long host names... */
261 1.1 jtc
262 1.1 jtc static int _rmt_open (path, oflag, mode)
263 1.1 jtc char *path;
264 1.1 jtc int oflag;
265 1.1 jtc int mode;
266 1.1 jtc {
267 1.1 jtc int i, rc;
268 1.1 jtc char buffer[BUFMAGIC];
269 1.1 jtc char system[MAXHOSTLEN];
270 1.1 jtc char device[BUFMAGIC];
271 1.1 jtc char login[BUFMAGIC];
272 1.1 jtc char *sys, *dev, *user;
273 1.1 jtc
274 1.1 jtc sys = system;
275 1.1 jtc dev = device;
276 1.1 jtc user = login;
277 1.1 jtc
278 1.1 jtc /*
279 1.1 jtc * first, find an open pair of file descriptors
280 1.1 jtc */
281 1.1 jtc
282 1.1 jtc for (i = 0; i < MAXUNIT; i++)
283 1.1 jtc if (READ(i) == -1 && WRITE(i) == -1)
284 1.1 jtc break;
285 1.1 jtc
286 1.1 jtc if (i == MAXUNIT)
287 1.1 jtc {
288 1.1 jtc errno = EMFILE;
289 1.1 jtc return(-1);
290 1.1 jtc }
291 1.1 jtc
292 1.1 jtc /*
293 1.1 jtc * pull apart system and device, and optional user
294 1.1 jtc * don't munge original string
295 1.1 jtc * if COMPAT is defined, also handle old (4.2) style person.site notation.
296 1.1 jtc */
297 1.1 jtc
298 1.1 jtc while (*path != '@'
299 1.1 jtc #ifdef COMPAT
300 1.1 jtc && *path != '.'
301 1.1 jtc #endif
302 1.1 jtc && *path != ':') {
303 1.1 jtc *sys++ = *path++;
304 1.1 jtc }
305 1.1 jtc *sys = '\0';
306 1.1 jtc path++;
307 1.1 jtc
308 1.1 jtc if (*(path - 1) == '@')
309 1.1 jtc {
310 1.1 jtc (void) strcpy (user, system); /* saw user part of user@host */
311 1.1 jtc sys = system; /* start over */
312 1.1 jtc while (*path != ':') {
313 1.1 jtc *sys++ = *path++;
314 1.1 jtc }
315 1.1 jtc *sys = '\0';
316 1.1 jtc path++;
317 1.1 jtc }
318 1.1 jtc #ifdef COMPAT
319 1.1 jtc else if (*(path - 1) == '.')
320 1.1 jtc {
321 1.1 jtc while (*path != ':') {
322 1.1 jtc *user++ = *path++;
323 1.1 jtc }
324 1.1 jtc *user = '\0';
325 1.1 jtc path++;
326 1.1 jtc }
327 1.1 jtc #endif
328 1.1 jtc else
329 1.1 jtc *user = '\0';
330 1.1 jtc
331 1.1 jtc while (*path) {
332 1.1 jtc *dev++ = *path++;
333 1.1 jtc }
334 1.1 jtc *dev = '\0';
335 1.1 jtc
336 1.1 jtc #ifdef USE_REXEC
337 1.1 jtc /*
338 1.1 jtc * Execute the remote command using rexec
339 1.1 jtc */
340 1.1 jtc READ(i) = WRITE(i) = _rmt_rexec(system, login);
341 1.1 jtc if (READ(i) < 0)
342 1.1 jtc return -1;
343 1.1 jtc #else
344 1.1 jtc /*
345 1.1 jtc * setup the pipes for the 'rsh' command and fork
346 1.1 jtc */
347 1.1 jtc
348 1.1 jtc if (pipe(Ptc[i]) == -1 || pipe(Ctp[i]) == -1)
349 1.1 jtc return(-1);
350 1.1 jtc
351 1.1 jtc if ((rc = fork()) == -1)
352 1.1 jtc return(-1);
353 1.1 jtc
354 1.1 jtc if (rc == 0)
355 1.1 jtc {
356 1.1 jtc close(0);
357 1.1 jtc dup(Ptc[i][0]);
358 1.1 jtc close(Ptc[i][0]); close(Ptc[i][1]);
359 1.1 jtc close(1);
360 1.1 jtc dup(Ctp[i][1]);
361 1.1 jtc close(Ctp[i][0]); close(Ctp[i][1]);
362 1.1 jtc (void) setuid (getuid ());
363 1.1 jtc (void) setgid (getgid ());
364 1.1 jtc if (*login)
365 1.1 jtc {
366 1.1 jtc execl("/usr/ucb/rsh", "rsh", system, "-l", login,
367 1.1 jtc "/etc/rmt", (char *) 0);
368 1.1 jtc execl("/usr/bin/remsh", "remsh", system, "-l", login,
369 1.1 jtc "/etc/rmt", (char *) 0);
370 1.1 jtc }
371 1.1 jtc else
372 1.1 jtc {
373 1.1 jtc execl("/usr/ucb/rsh", "rsh", system,
374 1.1 jtc "/etc/rmt", (char *) 0);
375 1.1 jtc execl("/usr/bin/remsh", "remsh", system,
376 1.1 jtc "/etc/rmt", (char *) 0);
377 1.1 jtc }
378 1.1 jtc
379 1.1 jtc /*
380 1.1 jtc * bad problems if we get here
381 1.1 jtc */
382 1.1 jtc
383 1.1 jtc perror("exec");
384 1.1 jtc exit(1);
385 1.1 jtc }
386 1.1 jtc
387 1.1 jtc close(Ptc[i][0]); close(Ctp[i][1]);
388 1.1 jtc #endif
389 1.1 jtc
390 1.1 jtc /*
391 1.1 jtc * now attempt to open the tape device
392 1.1 jtc */
393 1.1 jtc
394 1.1 jtc sprintf(buffer, "O%s\n%d\n", device, oflag);
395 1.1 jtc if (command(i, buffer) == -1 || status(i) == -1)
396 1.1 jtc return(-1);
397 1.1 jtc
398 1.1 jtc return(i);
399 1.1 jtc }
400 1.1 jtc
401 1.1 jtc
402 1.1 jtc
403 1.1 jtc /*
404 1.1 jtc * _rmt_close --- close a remote magtape unit and shut down
405 1.1 jtc */
406 1.1 jtc
407 1.1 jtc static int _rmt_close(fildes)
408 1.1 jtc int fildes;
409 1.1 jtc {
410 1.1 jtc int rc;
411 1.1 jtc
412 1.1 jtc if (command(fildes, "C\n") != -1)
413 1.1 jtc {
414 1.1 jtc rc = status(fildes);
415 1.1 jtc
416 1.1 jtc abort(fildes);
417 1.1 jtc return(rc);
418 1.1 jtc }
419 1.1 jtc
420 1.1 jtc return(-1);
421 1.1 jtc }
422 1.1 jtc
423 1.1 jtc
424 1.1 jtc
425 1.1 jtc /*
426 1.1 jtc * _rmt_read --- read a buffer from a remote tape
427 1.1 jtc */
428 1.1 jtc
429 1.1 jtc static int _rmt_read(fildes, buf, nbyte)
430 1.1 jtc int fildes;
431 1.1 jtc char *buf;
432 1.1 jtc unsigned int nbyte;
433 1.1 jtc {
434 1.1 jtc int rc, i;
435 1.1 jtc char buffer[BUFMAGIC];
436 1.1 jtc
437 1.1 jtc sprintf(buffer, "R%d\n", nbyte);
438 1.1 jtc if (command(fildes, buffer) == -1 || (rc = status(fildes)) == -1)
439 1.1 jtc return(-1);
440 1.1 jtc
441 1.1 jtc for (i = 0; i < rc; i += nbyte, buf += nbyte)
442 1.1 jtc {
443 1.1 jtc nbyte = read(READ(fildes), buf, rc);
444 1.1 jtc if (nbyte <= 0)
445 1.1 jtc {
446 1.1 jtc abort(fildes);
447 1.1 jtc errno = EIO;
448 1.1 jtc return(-1);
449 1.1 jtc }
450 1.1 jtc }
451 1.1 jtc
452 1.1 jtc return(rc);
453 1.1 jtc }
454 1.1 jtc
455 1.1 jtc
456 1.1 jtc
457 1.1 jtc /*
458 1.1 jtc * _rmt_write --- write a buffer to the remote tape
459 1.1 jtc */
460 1.1 jtc
461 1.1 jtc static int _rmt_write(fildes, buf, nbyte)
462 1.1 jtc int fildes;
463 1.1 jtc char *buf;
464 1.1 jtc unsigned int nbyte;
465 1.1 jtc {
466 1.1 jtc int rc;
467 1.1 jtc char buffer[BUFMAGIC];
468 1.1 jtc int (*pstat)();
469 1.1 jtc
470 1.1 jtc sprintf(buffer, "W%d\n", nbyte);
471 1.1 jtc if (command(fildes, buffer) == -1)
472 1.1 jtc return(-1);
473 1.1 jtc
474 1.1 jtc pstat = signal(SIGPIPE, SIG_IGN);
475 1.1 jtc if (write(WRITE(fildes), buf, nbyte) == nbyte)
476 1.1 jtc {
477 1.1 jtc signal (SIGPIPE, pstat);
478 1.1 jtc return(status(fildes));
479 1.1 jtc }
480 1.1 jtc
481 1.1 jtc signal (SIGPIPE, pstat);
482 1.1 jtc abort(fildes);
483 1.1 jtc errno = EIO;
484 1.1 jtc return(-1);
485 1.1 jtc }
486 1.1 jtc
487 1.1 jtc
488 1.1 jtc
489 1.1 jtc /*
490 1.1 jtc * _rmt_lseek --- perform an imitation lseek operation remotely
491 1.1 jtc */
492 1.1 jtc
493 1.1 jtc static long _rmt_lseek(fildes, offset, whence)
494 1.1 jtc int fildes;
495 1.1 jtc long offset;
496 1.1 jtc int whence;
497 1.1 jtc {
498 1.1 jtc char buffer[BUFMAGIC];
499 1.1 jtc
500 1.1 jtc sprintf(buffer, "L%d\n%d\n", offset, whence);
501 1.1 jtc if (command(fildes, buffer) == -1)
502 1.1 jtc return(-1);
503 1.1 jtc
504 1.1 jtc return(status(fildes));
505 1.1 jtc }
506 1.1 jtc
507 1.1 jtc
508 1.1 jtc /*
509 1.1 jtc * _rmt_ioctl --- perform raw tape operations remotely
510 1.1 jtc */
511 1.1 jtc
512 1.1 jtc #ifdef RMTIOCTL
513 1.1 jtc static _rmt_ioctl(fildes, op, arg)
514 1.1 jtc int fildes, op;
515 1.1 jtc char *arg;
516 1.1 jtc {
517 1.1 jtc char c;
518 1.1 jtc int rc, cnt;
519 1.1 jtc char buffer[BUFMAGIC];
520 1.1 jtc
521 1.1 jtc /*
522 1.1 jtc * MTIOCOP is the easy one. nothing is transfered in binary
523 1.1 jtc */
524 1.1 jtc
525 1.1 jtc if (op == MTIOCTOP)
526 1.1 jtc {
527 1.1 jtc sprintf(buffer, "I%d\n%d\n", ((struct mtop *) arg)->mt_op,
528 1.1 jtc ((struct mtop *) arg)->mt_count);
529 1.1 jtc if (command(fildes, buffer) == -1)
530 1.1 jtc return(-1);
531 1.1 jtc return(status(fildes));
532 1.1 jtc }
533 1.1 jtc
534 1.1 jtc /*
535 1.1 jtc * we can only handle 2 ops, if not the other one, punt
536 1.1 jtc */
537 1.1 jtc
538 1.1 jtc if (op != MTIOCGET)
539 1.1 jtc {
540 1.1 jtc errno = EINVAL;
541 1.1 jtc return(-1);
542 1.1 jtc }
543 1.1 jtc
544 1.1 jtc /*
545 1.1 jtc * grab the status and read it directly into the structure
546 1.1 jtc * this assumes that the status buffer is (hopefully) not
547 1.1 jtc * padded and that 2 shorts fit in a long without any word
548 1.1 jtc * alignment problems, ie - the whole struct is contiguous
549 1.1 jtc * NOTE - this is probably NOT a good assumption.
550 1.1 jtc */
551 1.1 jtc
552 1.1 jtc if (command(fildes, "S") == -1 || (rc = status(fildes)) == -1)
553 1.1 jtc return(-1);
554 1.1 jtc
555 1.1 jtc for (; rc > 0; rc -= cnt, arg += cnt)
556 1.1 jtc {
557 1.1 jtc cnt = read(READ(fildes), arg, rc);
558 1.1 jtc if (cnt <= 0)
559 1.1 jtc {
560 1.1 jtc abort(fildes);
561 1.1 jtc errno = EIO;
562 1.1 jtc return(-1);
563 1.1 jtc }
564 1.1 jtc }
565 1.1 jtc
566 1.1 jtc /*
567 1.1 jtc * now we check for byte position. mt_type is a small integer field
568 1.1 jtc * (normally) so we will check its magnitude. if it is larger than
569 1.1 jtc * 256, we will assume that the bytes are swapped and go through
570 1.1 jtc * and reverse all the bytes
571 1.1 jtc */
572 1.1 jtc
573 1.1 jtc if (((struct mtget *) arg)->mt_type < 256)
574 1.1 jtc return(0);
575 1.1 jtc
576 1.1 jtc for (cnt = 0; cnt < rc; cnt += 2)
577 1.1 jtc {
578 1.1 jtc c = arg[cnt];
579 1.1 jtc arg[cnt] = arg[cnt+1];
580 1.1 jtc arg[cnt+1] = c;
581 1.1 jtc }
582 1.1 jtc
583 1.1 jtc return(0);
584 1.1 jtc }
585 1.1 jtc #endif /* RMTIOCTL */
586 1.1 jtc
587 1.1 jtc /*
588 1.1 jtc * Added routines to replace open(), close(), lseek(), ioctl(), etc.
589 1.1 jtc * The preprocessor can be used to remap these the rmtopen(), etc
590 1.1 jtc * thus minimizing source changes:
591 1.1 jtc *
592 1.1 jtc * #ifdef <something>
593 1.1 jtc * # define access rmtaccess
594 1.1 jtc * # define close rmtclose
595 1.1 jtc * # define creat rmtcreat
596 1.1 jtc * # define dup rmtdup
597 1.1 jtc * # define fcntl rmtfcntl
598 1.1 jtc * # define fstat rmtfstat
599 1.1 jtc * # define ioctl rmtioctl
600 1.1 jtc * # define isatty rmtisatty
601 1.1 jtc * # define lseek rmtlseek
602 1.1 jtc * # define lstat rmtlstat
603 1.1 jtc * # define open rmtopen
604 1.1 jtc * # define read rmtread
605 1.1 jtc * # define stat rmtstat
606 1.1 jtc * # define write rmtwrite
607 1.1 jtc * #endif
608 1.1 jtc *
609 1.1 jtc * -- Fred Fish
610 1.1 jtc *
611 1.1 jtc * ADR --- I set up a <rmt.h> include file for this
612 1.1 jtc *
613 1.1 jtc */
614 1.1 jtc
615 1.1 jtc /*
616 1.1 jtc * Note that local vs remote file descriptors are distinquished
617 1.1 jtc * by adding a bias to the remote descriptors. This is a quick
618 1.1 jtc * and dirty trick that may not be portable to some systems.
619 1.1 jtc */
620 1.1 jtc
621 1.1 jtc #define REM_BIAS 128
622 1.1 jtc
623 1.1 jtc
624 1.1 jtc /*
625 1.1 jtc * Test pathname to see if it is local or remote. A remote device
626 1.1 jtc * is any string that contains ":/dev/". Returns 1 if remote,
627 1.1 jtc * 0 otherwise.
628 1.1 jtc */
629 1.1 jtc
630 1.1 jtc static int remdev (path)
631 1.1 jtc register char *path;
632 1.1 jtc {
633 1.1 jtc #define strchr index
634 1.1 jtc extern char *strchr ();
635 1.1 jtc
636 1.1 jtc if ((path = strchr (path, ':')) != NULL)
637 1.1 jtc {
638 1.1 jtc if (strncmp (path + 1, "/dev/", 5) == 0)
639 1.1 jtc {
640 1.1 jtc return (1);
641 1.1 jtc }
642 1.1 jtc }
643 1.1 jtc return (0);
644 1.1 jtc }
645 1.1 jtc
646 1.1 jtc
647 1.1 jtc /*
648 1.1 jtc * Open a local or remote file. Looks just like open(2) to
649 1.1 jtc * caller.
650 1.1 jtc */
651 1.1 jtc
652 1.1 jtc int rmtopen (path, oflag, mode)
653 1.1 jtc char *path;
654 1.1 jtc int oflag;
655 1.1 jtc int mode;
656 1.1 jtc {
657 1.1 jtc int fd;
658 1.1 jtc
659 1.1 jtc if (remdev (path))
660 1.1 jtc {
661 1.1 jtc fd = _rmt_open (path, oflag, mode);
662 1.1 jtc
663 1.1 jtc return (fd == -1) ? -1 : (fd + REM_BIAS);
664 1.1 jtc }
665 1.1 jtc else
666 1.1 jtc {
667 1.1 jtc return (open (path, oflag, mode));
668 1.1 jtc }
669 1.1 jtc }
670 1.1 jtc
671 1.1 jtc /*
672 1.1 jtc * Test pathname for specified access. Looks just like access(2)
673 1.1 jtc * to caller.
674 1.1 jtc */
675 1.1 jtc
676 1.1 jtc int rmtaccess (path, amode)
677 1.1 jtc char *path;
678 1.1 jtc int amode;
679 1.1 jtc {
680 1.1 jtc if (remdev (path))
681 1.1 jtc {
682 1.1 jtc return (0); /* Let /etc/rmt find out */
683 1.1 jtc }
684 1.1 jtc else
685 1.1 jtc {
686 1.1 jtc return (access (path, amode));
687 1.1 jtc }
688 1.1 jtc }
689 1.1 jtc
690 1.1 jtc
691 1.1 jtc /*
692 1.1 jtc * Read from stream. Looks just like read(2) to caller.
693 1.1 jtc */
694 1.1 jtc
695 1.1 jtc int rmtread (fildes, buf, nbyte)
696 1.1 jtc int fildes;
697 1.1 jtc char *buf;
698 1.1 jtc unsigned int nbyte;
699 1.1 jtc {
700 1.1 jtc if (isrmt (fildes))
701 1.1 jtc {
702 1.1 jtc return (_rmt_read (fildes - REM_BIAS, buf, nbyte));
703 1.1 jtc }
704 1.1 jtc else
705 1.1 jtc {
706 1.1 jtc return (read (fildes, buf, nbyte));
707 1.1 jtc }
708 1.1 jtc }
709 1.1 jtc
710 1.1 jtc
711 1.1 jtc /*
712 1.1 jtc * Write to stream. Looks just like write(2) to caller.
713 1.1 jtc */
714 1.1 jtc
715 1.1 jtc int rmtwrite (fildes, buf, nbyte)
716 1.1 jtc int fildes;
717 1.1 jtc char *buf;
718 1.1 jtc unsigned int nbyte;
719 1.1 jtc {
720 1.1 jtc if (isrmt (fildes))
721 1.1 jtc {
722 1.1 jtc return (_rmt_write (fildes - REM_BIAS, buf, nbyte));
723 1.1 jtc }
724 1.1 jtc else
725 1.1 jtc {
726 1.1 jtc return (write (fildes, buf, nbyte));
727 1.1 jtc }
728 1.1 jtc }
729 1.1 jtc
730 1.1 jtc /*
731 1.1 jtc * Perform lseek on file. Looks just like lseek(2) to caller.
732 1.1 jtc */
733 1.1 jtc
734 1.1 jtc long rmtlseek (fildes, offset, whence)
735 1.1 jtc int fildes;
736 1.1 jtc long offset;
737 1.1 jtc int whence;
738 1.1 jtc {
739 1.1 jtc if (isrmt (fildes))
740 1.1 jtc {
741 1.1 jtc return (_rmt_lseek (fildes - REM_BIAS, offset, whence));
742 1.1 jtc }
743 1.1 jtc else
744 1.1 jtc {
745 1.1 jtc return (lseek (fildes, offset, whence));
746 1.1 jtc }
747 1.1 jtc }
748 1.1 jtc
749 1.1 jtc
750 1.1 jtc /*
751 1.1 jtc * Close a file. Looks just like close(2) to caller.
752 1.1 jtc */
753 1.1 jtc
754 1.1 jtc int rmtclose (fildes)
755 1.1 jtc int fildes;
756 1.1 jtc {
757 1.1 jtc if (isrmt (fildes))
758 1.1 jtc {
759 1.1 jtc return (_rmt_close (fildes - REM_BIAS));
760 1.1 jtc }
761 1.1 jtc else
762 1.1 jtc {
763 1.1 jtc return (close (fildes));
764 1.1 jtc }
765 1.1 jtc }
766 1.1 jtc
767 1.1 jtc /*
768 1.1 jtc * Do ioctl on file. Looks just like ioctl(2) to caller.
769 1.1 jtc */
770 1.1 jtc
771 1.1 jtc int rmtioctl (fildes, request, arg)
772 1.1 jtc int fildes;
773 1.1 jtc unsigned long request;
774 1.1 jtc char *arg;
775 1.1 jtc {
776 1.1 jtc if (isrmt (fildes))
777 1.1 jtc {
778 1.1 jtc #ifdef RMTIOCTL
779 1.1 jtc return (_rmt_ioctl (fildes - REM_BIAS, request, arg));
780 1.1 jtc #else
781 1.1 jtc errno = EOPNOTSUPP;
782 1.1 jtc return (-1); /* For now (fnf) */
783 1.1 jtc #endif
784 1.1 jtc }
785 1.1 jtc else
786 1.1 jtc {
787 1.1 jtc return (ioctl (fildes, request, arg));
788 1.1 jtc }
789 1.1 jtc }
790 1.1 jtc
791 1.1 jtc
792 1.1 jtc /*
793 1.1 jtc * Duplicate an open file descriptor. Looks just like dup(2)
794 1.1 jtc * to caller.
795 1.1 jtc */
796 1.1 jtc
797 1.1 jtc int rmtdup (fildes)
798 1.1 jtc int fildes;
799 1.1 jtc {
800 1.1 jtc if (isrmt (fildes))
801 1.1 jtc {
802 1.1 jtc errno = EOPNOTSUPP;
803 1.1 jtc return (-1); /* For now (fnf) */
804 1.1 jtc }
805 1.1 jtc else
806 1.1 jtc {
807 1.1 jtc return (dup (fildes));
808 1.1 jtc }
809 1.1 jtc }
810 1.1 jtc
811 1.1 jtc /*
812 1.1 jtc * Get file status. Looks just like fstat(2) to caller.
813 1.1 jtc */
814 1.1 jtc
815 1.1 jtc int rmtfstat (fildes, buf)
816 1.1 jtc int fildes;
817 1.1 jtc struct stat *buf;
818 1.1 jtc {
819 1.1 jtc if (isrmt (fildes))
820 1.1 jtc {
821 1.1 jtc errno = EOPNOTSUPP;
822 1.1 jtc return (-1); /* For now (fnf) */
823 1.1 jtc }
824 1.1 jtc else
825 1.1 jtc {
826 1.1 jtc return (fstat (fildes, buf));
827 1.1 jtc }
828 1.1 jtc }
829 1.1 jtc
830 1.1 jtc
831 1.1 jtc /*
832 1.1 jtc * Get file status. Looks just like stat(2) to caller.
833 1.1 jtc */
834 1.1 jtc
835 1.1 jtc int rmtstat (path, buf)
836 1.1 jtc char *path;
837 1.1 jtc struct stat *buf;
838 1.1 jtc {
839 1.1 jtc if (remdev (path))
840 1.1 jtc {
841 1.1 jtc errno = EOPNOTSUPP;
842 1.1 jtc return (-1); /* For now (fnf) */
843 1.1 jtc }
844 1.1 jtc else
845 1.1 jtc {
846 1.1 jtc return (stat (path, buf));
847 1.1 jtc }
848 1.1 jtc }
849 1.1 jtc
850 1.1 jtc
851 1.1 jtc
852 1.1 jtc /*
853 1.1 jtc * Create a file from scratch. Looks just like creat(2) to the caller.
854 1.1 jtc */
855 1.1 jtc
856 1.1 jtc #include <sys/file.h> /* BSD DEPENDANT!!! */
857 1.1 jtc /* #include <fcntl.h> /* use this one for S5 with remote stuff */
858 1.1 jtc
859 1.1 jtc int rmtcreat (path, mode)
860 1.1 jtc char *path;
861 1.1 jtc int mode;
862 1.1 jtc {
863 1.1 jtc if (remdev (path))
864 1.1 jtc {
865 1.1 jtc return (rmtopen (path, 1 | O_CREAT, mode));
866 1.1 jtc }
867 1.1 jtc else
868 1.1 jtc {
869 1.1 jtc return (creat (path, mode));
870 1.1 jtc }
871 1.1 jtc }
872 1.1 jtc
873 1.1 jtc /*
874 1.1 jtc * Isrmt. Let a programmer know he has a remote device.
875 1.1 jtc */
876 1.1 jtc
877 1.1 jtc int isrmt (fd)
878 1.1 jtc int fd;
879 1.1 jtc {
880 1.1 jtc return (fd >= REM_BIAS);
881 1.1 jtc }
882 1.1 jtc
883 1.1 jtc /*
884 1.1 jtc * Rmtfcntl. Do a remote fcntl operation.
885 1.1 jtc */
886 1.1 jtc
887 1.1 jtc int rmtfcntl (fd, cmd, arg)
888 1.1 jtc int fd, cmd, arg;
889 1.1 jtc {
890 1.1 jtc if (isrmt (fd))
891 1.1 jtc {
892 1.1 jtc errno = EOPNOTSUPP;
893 1.1 jtc return (-1);
894 1.1 jtc }
895 1.1 jtc else
896 1.1 jtc {
897 1.1 jtc return (fcntl (fd, cmd, arg));
898 1.1 jtc }
899 1.1 jtc }
900 1.1 jtc
901 1.1 jtc /*
902 1.1 jtc * Rmtisatty. Do the isatty function.
903 1.1 jtc */
904 1.1 jtc
905 1.1 jtc int rmtisatty (fd)
906 1.1 jtc int fd;
907 1.1 jtc {
908 1.1 jtc if (isrmt (fd))
909 1.1 jtc return (0);
910 1.1 jtc else
911 1.1 jtc return (isatty (fd));
912 1.1 jtc }
913 1.1 jtc
914 1.1 jtc
915 1.1 jtc /*
916 1.1 jtc * Get file status, even if symlink. Looks just like lstat(2) to caller.
917 1.1 jtc */
918 1.1 jtc
919 1.1 jtc int rmtlstat (path, buf)
920 1.1 jtc char *path;
921 1.1 jtc struct stat *buf;
922 1.1 jtc {
923 1.1 jtc if (remdev (path))
924 1.1 jtc {
925 1.1 jtc errno = EOPNOTSUPP;
926 1.1 jtc return (-1); /* For now (fnf) */
927 1.1 jtc }
928 1.1 jtc else
929 1.1 jtc {
930 return (lstat (path, buf));
931 }
932 }
933