rtapelib.c 19 KB

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