rtapelib.c 20 KB

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