rtapelib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /* Functions for communicating with a remote tape drive.
  2. Copyright 1988, 92, 94, 96, 97, 99, 2000 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* The man page rmt(8) for /etc/rmt documents the remote mag tape protocol
  15. which rdump and rrestore use. Unfortunately, the man page is *WRONG*.
  16. The author of the routines I'm including originally wrote his code just
  17. based on the man page, and it didn't work, so he went to the rdump source
  18. to figure out why. The only thing he had to change was to check for the
  19. 'F' return code in addition to the 'E', and to separate the various
  20. arguments with \n instead of a space. I personally don't think that this
  21. is much of a problem, but I wanted to point it out. -- Arnold Robbins
  22. Originally written by Jeff Lee, modified some by Arnold Robbins. Redone
  23. as a library that can replace open, read, write, etc., by Fred Fish, with
  24. some additional work by Arnold Robbins. Modified to make all rmt* calls
  25. into macros for speed by Jay Fenlason. Use -DWITH_REXEC for rexec
  26. code, courtesy of Dan Kegel. */
  27. #include "system.h"
  28. #include <safe-read.h>
  29. /* Try hard to get EOPNOTSUPP defined. 486/ISC has it in net/errno.h,
  30. 3B2/SVR3 has it in sys/inet.h. Otherwise, like on MSDOS, use EINVAL. */
  31. #ifndef EOPNOTSUPP
  32. # if HAVE_NET_ERRNO_H
  33. # include <net/errno.h>
  34. # endif
  35. # if HAVE_SYS_INET_H
  36. # include <sys/inet.h>
  37. # endif
  38. # ifndef EOPNOTSUPP
  39. # define EOPNOTSUPP EINVAL
  40. # endif
  41. #endif
  42. #include <signal.h>
  43. #if HAVE_NETDB_H
  44. # include <netdb.h>
  45. #endif
  46. #include "rmt.h"
  47. char *base_name PARAMS ((char const *));
  48. /* Exit status if exec errors. */
  49. #define EXIT_ON_EXEC_ERROR 128
  50. /* FIXME: Size of buffers for reading and writing commands to rmt. */
  51. #define COMMAND_BUFFER_SIZE 64
  52. #ifndef RETSIGTYPE
  53. # define RETSIGTYPE void
  54. #endif
  55. /* FIXME: Maximum number of simultaneous remote tape connections. */
  56. #define MAXUNIT 4
  57. #define PREAD 0 /* read file descriptor from pipe() */
  58. #define PWRITE 1 /* write file descriptor from pipe() */
  59. /* Return the parent's read side of remote tape connection Fd. */
  60. #define READ_SIDE(Fd) (from_remote[Fd][PREAD])
  61. /* Return the parent's write side of remote tape connection Fd. */
  62. #define WRITE_SIDE(Fd) (to_remote[Fd][PWRITE])
  63. /* The pipes for receiving data from remote tape drives. */
  64. static int from_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
  65. /* The pipes for sending data to remote tape drives. */
  66. static int to_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
  67. /* Temporary variable used by macros in rmt.h. */
  68. char *rmt_path__;
  69. /* Close remote tape connection HANDLE, and reset errno to ERRNO_VALUE. */
  70. static void
  71. _rmt_shutdown (int handle, int errno_value)
  72. {
  73. close (READ_SIDE (handle));
  74. close (WRITE_SIDE (handle));
  75. READ_SIDE (handle) = -1;
  76. WRITE_SIDE (handle) = -1;
  77. errno = errno_value;
  78. }
  79. /* Attempt to perform the remote tape command specified in BUFFER on
  80. remote tape connection HANDLE. Return 0 if successful, -1 on
  81. error. */
  82. static int
  83. do_command (int handle, const char *buffer)
  84. {
  85. /* Save the current pipe handler and try to make the request. */
  86. size_t length = strlen (buffer);
  87. RETSIGTYPE (*pipe_handler) () = signal (SIGPIPE, SIG_IGN);
  88. ssize_t written = full_write (WRITE_SIDE (handle), buffer, length);
  89. signal (SIGPIPE, pipe_handler);
  90. if (written == length)
  91. return 0;
  92. /* Something went wrong. Close down and go home. */
  93. _rmt_shutdown (handle, EIO);
  94. return -1;
  95. }
  96. static char *
  97. get_status_string (int handle, char *command_buffer)
  98. {
  99. char *cursor;
  100. int counter;
  101. /* Read the reply command line. */
  102. for (counter = 0, cursor = command_buffer;
  103. counter < COMMAND_BUFFER_SIZE;
  104. counter++, cursor++)
  105. {
  106. if (safe_read (READ_SIDE (handle), cursor, 1) != 1)
  107. {
  108. _rmt_shutdown (handle, EIO);
  109. return 0;
  110. }
  111. if (*cursor == '\n')
  112. {
  113. *cursor = '\0';
  114. break;
  115. }
  116. }
  117. if (counter == COMMAND_BUFFER_SIZE)
  118. {
  119. _rmt_shutdown (handle, EIO);
  120. return 0;
  121. }
  122. /* Check the return status. */
  123. for (cursor = command_buffer; *cursor; cursor++)
  124. if (*cursor != ' ')
  125. break;
  126. if (*cursor == 'E' || *cursor == 'F')
  127. {
  128. errno = atoi (cursor + 1);
  129. /* Skip the error message line. */
  130. /* FIXME: there is better to do than merely ignoring error messages
  131. coming from the remote end. Translate them, too... */
  132. {
  133. char character;
  134. while (safe_read (READ_SIDE (handle), &character, 1) == 1)
  135. if (character == '\n')
  136. break;
  137. }
  138. if (*cursor == 'F')
  139. _rmt_shutdown (handle, errno);
  140. return 0;
  141. }
  142. /* Check for mis-synced pipes. */
  143. if (*cursor != 'A')
  144. {
  145. _rmt_shutdown (handle, EIO);
  146. return 0;
  147. }
  148. /* Got an `A' (success) response. */
  149. return cursor + 1;
  150. }
  151. /* Read and return the status from remote tape connection HANDLE. If
  152. an error occurred, return -1 and set errno. */
  153. static long
  154. get_status (int handle)
  155. {
  156. char command_buffer[COMMAND_BUFFER_SIZE];
  157. const char *status = get_status_string (handle, command_buffer);
  158. return status ? atol (status) : -1L;
  159. }
  160. static off_t
  161. get_status_off (int handle)
  162. {
  163. char command_buffer[COMMAND_BUFFER_SIZE];
  164. const char *status = get_status_string (handle, command_buffer);
  165. if (! status)
  166. return -1;
  167. else
  168. {
  169. /* Parse status, taking care to check for overflow.
  170. We can't use standard functions,
  171. since off_t might be longer than long. */
  172. off_t count = 0;
  173. int negative;
  174. for (; *status == ' ' || *status == '\t'; status++)
  175. continue;
  176. negative = *status == '-';
  177. status += negative || *status == '+';
  178. for (;;)
  179. {
  180. int digit = *status++ - '0';
  181. if (9 < (unsigned) digit)
  182. break;
  183. else
  184. {
  185. off_t c10 = 10 * count;
  186. off_t nc = negative ? c10 - digit : c10 + digit;
  187. if (c10 / 10 != count || (negative ? c10 < nc : nc < c10))
  188. return -1;
  189. count = nc;
  190. }
  191. }
  192. return count;
  193. }
  194. }
  195. #if WITH_REXEC
  196. int rexec ();
  197. /* Execute /etc/rmt as user USER on remote system HOST using rexec.
  198. Return a file descriptor of a bidirectional socket for stdin and
  199. stdout. If USER is zero, use the current username.
  200. By default, this code is not used, since it requires that the user
  201. have a .netrc file in his/her home directory, or that the
  202. application designer be willing to have rexec prompt for login and
  203. password info. This may be unacceptable, and .rhosts files for use
  204. with rsh are much more common on BSD systems. */
  205. static int
  206. _rmt_rexec (char *host, char *user)
  207. {
  208. int saved_stdin = dup (STDIN_FILENO);
  209. int saved_stdout = dup (STDOUT_FILENO);
  210. struct servent *rexecserv;
  211. int result;
  212. /* When using cpio -o < filename, stdin is no longer the tty. But the
  213. rexec subroutine reads the login and the passwd on stdin, to allow
  214. remote execution of the command. So, reopen stdin and stdout on
  215. /dev/tty before the rexec and give them back their original value
  216. after. */
  217. if (! freopen ("/dev/tty", "r", stdin))
  218. freopen ("/dev/null", "r", stdin);
  219. if (! freopen ("/dev/tty", "w", stdout))
  220. freopen ("/dev/null", "w", stdout);
  221. if (rexecserv = getservbyname ("exec", "tcp"), !rexecserv)
  222. error (EXIT_ON_EXEC_ERROR, 0, _("exec/tcp: Service not available"));
  223. result = rexec (&host, rexecserv->s_port, user, 0, "/etc/rmt", 0);
  224. if (fclose (stdin) == EOF)
  225. error (0, errno, _("stdin"));
  226. fdopen (saved_stdin, "r");
  227. if (fclose (stdout) == EOF)
  228. error (0, errno, _("stdout"));
  229. fdopen (saved_stdout, "w");
  230. return result;
  231. }
  232. #endif /* WITH_REXEC */
  233. /* Place into BUF a string representing OFLAG, which must be suitable
  234. as argument 2 of `open'. BUF must be large enough to hold the
  235. result. This function should generate a string that decode_oflag
  236. can parse. */
  237. static void
  238. encode_oflag (char *buf, int oflag)
  239. {
  240. sprintf (buf, "%d ", oflag);
  241. switch (oflag & O_ACCMODE)
  242. {
  243. case O_RDONLY: strcat (buf, "O_RDONLY"); break;
  244. case O_RDWR: strcat (buf, "O_RDWR"); break;
  245. case O_WRONLY: strcat (buf, "O_WRONLY"); break;
  246. default: abort ();
  247. }
  248. #ifdef O_APPEND
  249. if (oflag & O_APPEND) strcat (buf, "|O_APPEND");
  250. #endif
  251. if (oflag & O_CREAT) strcat (buf, "|O_CREAT");
  252. #ifdef O_DSYNC
  253. if (oflag & O_DSYNC) strcat (buf, "|O_DSYNC");
  254. #endif
  255. if (oflag & O_EXCL) strcat (buf, "|O_EXCL");
  256. #ifdef O_LARGEFILE
  257. if (oflag & O_LARGEFILE) strcat (buf, "|O_LARGEFILE");
  258. #endif
  259. #ifdef O_NOCTTY
  260. if (oflag & O_NOCTTY) strcat (buf, "|O_NOCTTY");
  261. #endif
  262. #ifdef O_NONBLOCK
  263. if (oflag & O_NONBLOCK) strcat (buf, "|O_NONBLOCK");
  264. #endif
  265. #ifdef O_RSYNC
  266. if (oflag & O_RSYNC) strcat (buf, "|O_RSYNC");
  267. #endif
  268. #ifdef O_SYNC
  269. if (oflag & O_SYNC) strcat (buf, "|O_SYNC");
  270. #endif
  271. if (oflag & O_TRUNC) strcat (buf, "|O_TRUNC");
  272. }
  273. /* Open a file (a magnetic tape device?) on the system specified in
  274. PATH, as the given user. PATH has the form `[USER@]HOST:FILE'.
  275. OPEN_MODE is O_RDONLY, O_WRONLY, etc. If successful, return the
  276. remote pipe number plus BIAS. REMOTE_SHELL may be overridden. On
  277. error, return -1. */
  278. int
  279. rmt_open__ (const char *path, int open_mode, int bias, const char *remote_shell)
  280. {
  281. int remote_pipe_number; /* pseudo, biased file descriptor */
  282. char *path_copy ; /* copy of path string */
  283. char *remote_host; /* remote host name */
  284. char *remote_file; /* remote file name (often a device) */
  285. char *remote_user; /* remote user name */
  286. /* Find an unused pair of file descriptors. */
  287. for (remote_pipe_number = 0;
  288. remote_pipe_number < MAXUNIT;
  289. remote_pipe_number++)
  290. if (READ_SIDE (remote_pipe_number) == -1
  291. && WRITE_SIDE (remote_pipe_number) == -1)
  292. break;
  293. if (remote_pipe_number == MAXUNIT)
  294. {
  295. errno = EMFILE;
  296. return -1;
  297. }
  298. /* Pull apart the system and device, and optional user. */
  299. {
  300. char *cursor;
  301. path_copy = xstrdup (path);
  302. remote_host = path_copy;
  303. remote_user = 0;
  304. remote_file = 0;
  305. for (cursor = path_copy; *cursor; cursor++)
  306. switch (*cursor)
  307. {
  308. default:
  309. break;
  310. case '\n':
  311. /* Do not allow newlines in the path, since the protocol
  312. uses newline delimiters. */
  313. free (path_copy);
  314. errno = ENOENT;
  315. return -1;
  316. case '@':
  317. if (!remote_user)
  318. {
  319. remote_user = remote_host;
  320. *cursor = '\0';
  321. remote_host = cursor + 1;
  322. }
  323. break;
  324. case ':':
  325. if (!remote_file)
  326. {
  327. *cursor = '\0';
  328. remote_file = cursor + 1;
  329. }
  330. break;
  331. }
  332. }
  333. /* FIXME: Should somewhat validate the decoding, here. */
  334. if (remote_user && *remote_user == '\0')
  335. remote_user = 0;
  336. #if WITH_REXEC
  337. /* Execute the remote command using rexec. */
  338. READ_SIDE (remote_pipe_number) = _rmt_rexec (remote_host, remote_user);
  339. if (READ_SIDE (remote_pipe_number) < 0)
  340. {
  341. int e = errno;
  342. free (path_copy);
  343. errno = e;
  344. return -1;
  345. }
  346. WRITE_SIDE (remote_pipe_number) = READ_SIDE (remote_pipe_number);
  347. #else /* not WITH_REXEC */
  348. {
  349. const char *remote_shell_basename;
  350. pid_t status;
  351. /* Identify the remote command to be executed. */
  352. if (!remote_shell)
  353. {
  354. #ifdef REMOTE_SHELL
  355. remote_shell = REMOTE_SHELL;
  356. #else
  357. free (path_copy);
  358. errno = EIO;
  359. return -1;
  360. #endif
  361. }
  362. remote_shell_basename = base_name (remote_shell);
  363. /* Set up the pipes for the `rsh' command, and fork. */
  364. if (pipe (to_remote[remote_pipe_number]) == -1
  365. || pipe (from_remote[remote_pipe_number]) == -1)
  366. {
  367. int e = errno;
  368. free (path_copy);
  369. errno = e;
  370. return -1;
  371. }
  372. status = fork ();
  373. if (status == -1)
  374. {
  375. int e = errno;
  376. free (path_copy);
  377. errno = e;
  378. return -1;
  379. }
  380. if (status == 0)
  381. {
  382. /* Child. */
  383. close (STDIN_FILENO);
  384. dup (to_remote[remote_pipe_number][PREAD]);
  385. close (to_remote[remote_pipe_number][PREAD]);
  386. close (to_remote[remote_pipe_number][PWRITE]);
  387. close (STDOUT_FILENO);
  388. dup (from_remote[remote_pipe_number][PWRITE]);
  389. close (from_remote[remote_pipe_number][PREAD]);
  390. close (from_remote[remote_pipe_number][PWRITE]);
  391. #if !MSDOS
  392. setuid (getuid ());
  393. setgid (getgid ());
  394. #endif
  395. if (remote_user)
  396. execl (remote_shell, remote_shell_basename, remote_host,
  397. "-l", remote_user, "/etc/rmt", (char *) 0);
  398. else
  399. execl (remote_shell, remote_shell_basename, remote_host,
  400. "/etc/rmt", (char *) 0);
  401. /* Bad problems if we get here. */
  402. /* In a previous version, _exit was used here instead of exit. */
  403. error (EXIT_ON_EXEC_ERROR, errno, _("Cannot execute remote shell"));
  404. }
  405. /* Parent. */
  406. close (from_remote[remote_pipe_number][PWRITE]);
  407. close (to_remote[remote_pipe_number][PREAD]);
  408. }
  409. #endif /* not WITH_REXEC */
  410. /* Attempt to open the tape device. */
  411. {
  412. size_t remote_file_len = strlen (remote_file);
  413. char *command_buffer = xmalloc (remote_file_len + 1000);
  414. sprintf (command_buffer, "O%s\n", remote_file);
  415. encode_oflag (command_buffer + remote_file_len + 2, open_mode);
  416. strcat (command_buffer, "\n");
  417. if (do_command (remote_pipe_number, command_buffer) == -1
  418. || get_status (remote_pipe_number) == -1)
  419. {
  420. int e = errno;
  421. free (command_buffer);
  422. free (path_copy);
  423. _rmt_shutdown (remote_pipe_number, e);
  424. return -1;
  425. }
  426. free (command_buffer);
  427. }
  428. free (path_copy);
  429. return remote_pipe_number + bias;
  430. }
  431. /* Close remote tape connection HANDLE and shut down. Return 0 if
  432. successful, -1 on error. */
  433. int
  434. rmt_close__ (int handle)
  435. {
  436. int status;
  437. if (do_command (handle, "C\n") == -1)
  438. return -1;
  439. status = get_status (handle);
  440. _rmt_shutdown (handle, errno);
  441. return status;
  442. }
  443. /* Read up to LENGTH bytes into BUFFER from remote tape connection HANDLE.
  444. Return the number of bytes read on success, -1 on error. */
  445. ssize_t
  446. rmt_read__ (int handle, char *buffer, size_t length)
  447. {
  448. char command_buffer[COMMAND_BUFFER_SIZE];
  449. ssize_t status, rlen;
  450. size_t counter;
  451. sprintf (command_buffer, "R%lu\n", (unsigned long) length);
  452. if (do_command (handle, command_buffer) == -1
  453. || (status = get_status (handle)) == -1)
  454. return -1;
  455. for (counter = 0; counter < status; counter += rlen, buffer += rlen)
  456. {
  457. rlen = safe_read (READ_SIDE (handle), buffer, status - counter);
  458. if (rlen <= 0)
  459. {
  460. _rmt_shutdown (handle, EIO);
  461. return -1;
  462. }
  463. }
  464. return status;
  465. }
  466. /* Write LENGTH bytes from BUFFER to remote tape connection HANDLE.
  467. Return the number of bytes written on success, -1 on error. */
  468. ssize_t
  469. rmt_write__ (int handle, char *buffer, size_t length)
  470. {
  471. char command_buffer[COMMAND_BUFFER_SIZE];
  472. RETSIGTYPE (*pipe_handler) ();
  473. size_t written;
  474. sprintf (command_buffer, "W%lu\n", (unsigned long) length);
  475. if (do_command (handle, command_buffer) == -1)
  476. return -1;
  477. pipe_handler = signal (SIGPIPE, SIG_IGN);
  478. written = full_write (WRITE_SIDE (handle), buffer, length);
  479. signal (SIGPIPE, pipe_handler);
  480. if (written == length)
  481. return get_status (handle);
  482. /* Write error. */
  483. _rmt_shutdown (handle, EIO);
  484. return -1;
  485. }
  486. /* Perform an imitation lseek operation on remote tape connection
  487. HANDLE. Return the new file offset if successful, -1 if on error. */
  488. off_t
  489. rmt_lseek__ (int handle, off_t offset, int whence)
  490. {
  491. char command_buffer[COMMAND_BUFFER_SIZE];
  492. char operand_buffer[UINTMAX_STRSIZE_BOUND];
  493. uintmax_t u = offset < 0 ? - (uintmax_t) offset : (uintmax_t) offset;
  494. char *p = operand_buffer + sizeof operand_buffer;
  495. do
  496. *--p = '0' + (int) (u % 10);
  497. while ((u /= 10) != 0);
  498. if (offset < 0)
  499. *--p = '-';
  500. switch (whence)
  501. {
  502. case SEEK_SET: whence = 0; break;
  503. case SEEK_CUR: whence = 1; break;
  504. case SEEK_END: whence = 2; break;
  505. default: abort ();
  506. }
  507. sprintf (command_buffer, "L%s\n%d\n", p, whence);
  508. if (do_command (handle, command_buffer) == -1)
  509. return -1;
  510. return get_status_off (handle);
  511. }
  512. /* Perform a raw tape operation on remote tape connection HANDLE.
  513. Return the results of the ioctl, or -1 on error. */
  514. int
  515. rmt_ioctl__ (int handle, int operation, char *argument)
  516. {
  517. switch (operation)
  518. {
  519. default:
  520. errno = EOPNOTSUPP;
  521. return -1;
  522. #ifdef MTIOCTOP
  523. case MTIOCTOP:
  524. {
  525. char command_buffer[COMMAND_BUFFER_SIZE];
  526. char operand_buffer[UINTMAX_STRSIZE_BOUND];
  527. uintmax_t u = (((struct mtop *) argument)->mt_count < 0
  528. ? - (uintmax_t) ((struct mtop *) argument)->mt_count
  529. : (uintmax_t) ((struct mtop *) argument)->mt_count);
  530. char *p = operand_buffer + sizeof operand_buffer;
  531. do
  532. *--p = '0' + (int) (u % 10);
  533. while ((u /= 10) != 0);
  534. if (((struct mtop *) argument)->mt_count < 0)
  535. *--p = '-';
  536. /* MTIOCTOP is the easy one. Nothing is transferred in binary. */
  537. sprintf (command_buffer, "I%d\n%s\n",
  538. ((struct mtop *) argument)->mt_op, p);
  539. if (do_command (handle, command_buffer) == -1)
  540. return -1;
  541. return get_status (handle);
  542. }
  543. #endif /* MTIOCTOP */
  544. #ifdef MTIOCGET
  545. case MTIOCGET:
  546. {
  547. ssize_t status;
  548. ssize_t counter;
  549. /* Grab the status and read it directly into the structure. This
  550. assumes that the status buffer is not padded and that 2 shorts
  551. fit in a long without any word alignment problems; i.e., the
  552. whole struct is contiguous. NOTE - this is probably NOT a good
  553. assumption. */
  554. if (do_command (handle, "S") == -1
  555. || (status = get_status (handle), status == -1))
  556. return -1;
  557. for (; status > 0; status -= counter, argument += counter)
  558. {
  559. counter = safe_read (READ_SIDE (handle), argument, status);
  560. if (counter <= 0)
  561. {
  562. _rmt_shutdown (handle, EIO);
  563. return -1;
  564. }
  565. }
  566. /* Check for byte position. mt_type (or mt_model) is a small integer
  567. field (normally) so we will check its magnitude. If it is larger
  568. than 256, we will assume that the bytes are swapped and go through
  569. and reverse all the bytes. */
  570. if (((struct mtget *) argument)->MTIO_CHECK_FIELD < 256)
  571. return 0;
  572. for (counter = 0; counter < status; counter += 2)
  573. {
  574. char copy = argument[counter];
  575. argument[counter] = argument[counter + 1];
  576. argument[counter + 1] = copy;
  577. }
  578. return 0;
  579. }
  580. #endif /* MTIOCGET */
  581. }
  582. }