rtapelib.c 18 KB

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