Home | History | Annotate | Line # | Download | only in scsictl
scsi_subr.c revision 1.1
      1 /*	$NetBSD: scsi_subr.c,v 1.1 1998/10/15 21:44:39 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
      9  * Simulation Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * SCSI support subroutines.
     42  *
     43  * XXX THESE SHOULD BE IN A LIBRARY!
     44  */
     45 
     46 #include <sys/param.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/scsiio.h>
     49 #include <err.h>
     50 #include <errno.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 
     56 #include "extern.h"
     57 
     58 void
     59 scsi_command(fd, cmd, cmdlen, data, datalen, timeout, flags)
     60 	int fd;
     61 	void *cmd;
     62 	size_t cmdlen;
     63 	void *data;
     64 	size_t datalen;
     65 	int timeout, flags;
     66 {
     67 	scsireq_t req;
     68 
     69 	memset(&req, 0, sizeof(req));
     70 
     71 	memcpy(req.cmd, cmd, cmdlen);
     72 	req.cmdlen = cmdlen;
     73 	req.databuf = data;
     74 	req.datalen = datalen;
     75 	req.timeout = timeout;
     76 	req.flags = flags;
     77 	req.senselen = SENSEBUFLEN;
     78 
     79 	if (ioctl(fd, SCIOCCOMMAND, &req) == -1)
     80 		err(1, "SCIOCCOMMAND");
     81 
     82 	if (req.retsts == SCCMD_OK)
     83 		return;
     84 
     85 	/* Some problem; report it and exit. */
     86 	if (req.retsts & SCCMD_TIMEOUT)
     87 		fprintf(stderr, "%s: SCSI command timed out\n", dvname);
     88 	if (req.retsts & SCCMD_BUSY)
     89 		fprintf(stderr, "%s: device is busy\n", dvname);
     90 	if (req.retsts & SCCMD_SENSE)
     91 		scsi_print_sense(dvname, &req, 1);
     92 
     93 	exit(1);
     94 }
     95 
     96 void
     97 scsi_strvis(sdst, dlen, ssrc, slen)
     98 	char *sdst;
     99 	size_t dlen;
    100 	const char *ssrc;
    101 	size_t slen;
    102 {
    103 	u_char *dst = (u_char *)sdst;
    104 	const u_char *src = (const u_char *)ssrc;
    105 
    106 	/* Trim leading and trailing blanks and NULs. */
    107 	while (slen > 0 && (src[0] == ' ' || src[0] == '\0'))
    108 		++src, --slen;
    109 	while (slen > 0 && (src[slen-1] == ' ' || src[slen-1] == '\0'))
    110 		--slen;
    111 
    112 	while (slen > 0) {
    113 		if (*src < 0x20 || *src >= 0x80) {
    114 			/* non-printable characters */
    115 			dlen -= 4;
    116 			if (dlen < 1)
    117 				break;
    118 			*dst++ = '\\';
    119 			*dst++ = ((*src & 0300) >> 6) + '0';
    120 			*dst++ = ((*src & 0070) >> 3) + '0';
    121 			*dst++ = ((*src & 0007) >> 0) + '0';
    122 		} else if (*src == '\\') {
    123 			/* quote characters */
    124 			dlen -= 2;
    125 			if (dlen < 1)
    126 				break;
    127 			*dst++ = '\\';
    128 			*dst++ = '\\';
    129 		} else {
    130 			/* normal characters */
    131 			if (--dlen < 1)
    132 				break;
    133 			*dst++ = *src;
    134 		}
    135 		++src, --slen;
    136 	}
    137 
    138 	*dst++ = 0;
    139 }
    140