FreeBSD/bin/chio/chio.c
/*
 * find_element()
 * 
 * Given a <voltag> find the chager element and unit, or exit
 * with an error if it isn't found.  We grab the changer status
 * and iterate until we find a match, or crap out.
 */


Given a  find the chager element and unit, or exit
with an error if it isn't found.  We grab the changer status
and iterate until we find a match, or crap out.
Given a find the chager element and unit or exit with an error if it isn ' t found . We grab the changer status and iterate until we find a match or crap out .
static void
find_element(char *voltag, uint16_t *et, uint16_t *eu)
{
	struct changer_params cp;
	struct changer_element_status_request cesr;
	struct changer_element_status *ch_ces, *ces;
	int found = 0;
	size_t elem, total_elem;
	if (ioctl(changer_fd, CHIOGPARAMS, (char *)&cp))
		err(1, "%s: CHIOGPARAMS", changer_name);
	total_elem = (cp.cp_nslots + cp.cp_ndrives
	    + cp.cp_npickers + cp.cp_nportals);
	ch_ces = (struct changer_element_status *)
	    calloc(total_elem, sizeof(struct changer_element_status));
	if (NULL == ch_ces)
		errx(1, "can't allocate status storage");
	ces = ch_ces;
	if (cp.cp_nslots > 0) {
		(void) memset(&cesr, 0, sizeof(cesr));
		cesr.cesr_element_type = CHET_ST;
		cesr.cesr_element_base = 0;
		cesr.cesr_element_count = cp.cp_nslots;
		cesr.cesr_flags |= CESR_VOLTAGS;
		cesr.cesr_element_status = ces;
		if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
			free(ch_ces);
			err(1, "%s: CHIOGSTATUS", changer_name);
		}
		ces += cp.cp_nslots;
	}	
	if (cp.cp_ndrives > 0 ) {
		(void) memset(&cesr, 0, sizeof(cesr));
		cesr.cesr_element_type = CHET_DT;
		cesr.cesr_element_base = 0;
		cesr.cesr_element_count = cp.cp_ndrives;
		cesr.cesr_flags |= CESR_VOLTAGS;
		cesr.cesr_element_status = ces;
		if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
			free(ch_ces);
			err(1, "%s: CHIOGSTATUS", changer_name);
		}
		ces += cp.cp_ndrives;
	}
	if (cp.cp_nportals > 0 ) {
		(void) memset(&cesr, 0, sizeof(cesr));
		cesr.cesr_element_type = CHET_IE;
		cesr.cesr_element_base = 0;
		cesr.cesr_element_count = cp.cp_nportals;
		cesr.cesr_flags |= CESR_VOLTAGS;
		cesr.cesr_element_status = ces;
		if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
			free(ch_ces);
			err(1, "%s: CHIOGSTATUS", changer_name);
		}
		ces += cp.cp_nportals;
	}
	if (cp.cp_npickers > 0) {
		(void) memset(&cesr, 0, sizeof(cesr));
		cesr.cesr_element_type = CHET_MT;
		cesr.cesr_element_base = 0;
		cesr.cesr_element_count = cp.cp_npickers;
		cesr.cesr_flags |= CESR_VOLTAGS;
		cesr.cesr_element_status = ces;
		if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
			free(ch_ces);
			err(1, "%s: CHIOGSTATUS", changer_name);
		}
	}
	for (elem = 0; elem <= total_elem; ++elem) {
		ces = &ch_ces[elem];
		if ((ces->ces_flags & (CES_STATUS_ACCESS|CES_STATUS_FULL))
		    != (CES_STATUS_ACCESS|CES_STATUS_FULL))
			continue;
		if (strcasecmp(voltag,
		    (const char *)ces->ces_pvoltag.cv_volid) == 0) {
			*et = ces->ces_type;
			*eu = ces->ces_addr;
			++found;
			break;
		}
	}
	if (!found) {
		errx(1, "%s: unable to locate voltag: %s", changer_name,
		     voltag);
	}
	free(ch_ces);
	return;
}
static void find_element ( char * voltag , uint16_t * et , uint16_t * eu ) { struct changer_params cp ; struct changer_element_status_request cesr ; struct changer_element_status * ch_ces , * ces ; int found = 0 ; size_t elem , total_elem ; if ( ioctl ( changer_fd , CHIOGPARAMS , ( char * ) & cp ) ) err ( 1 , " " , changer_name ) ; total_elem = ( cp . cp_nslots + cp . cp_ndrives + cp . cp_npickers + cp . cp_nportals ) ; ch_ces = ( struct changer_element_status * ) calloc ( total_elem , sizeof ( struct changer_element_status ) ) ; if ( NULL == ch_ces ) errx ( 1 , " " ) ; ces = ch_ces ; if ( cp . cp_nslots > 0 ) { ( void ) memset ( & cesr , 0 , sizeof ( cesr ) ) ; cesr . cesr_element_type = CHET_ST ; cesr . cesr_element_base = 0 ; cesr . cesr_element_count = cp . cp_nslots ; cesr . cesr_flags |= CESR_VOLTAGS ; cesr . cesr_element_status = ces ; if ( ioctl ( changer_fd , CHIOGSTATUS , ( char * ) & cesr ) == -1 ) { free ( ch_ces ) ; err ( 1 , " " , changer_name ) ; } ces += cp . cp_nslots ; } if ( cp . cp_ndrives > 0 ) { ( void ) memset ( & cesr , 0 , sizeof ( cesr ) ) ; cesr . cesr_element_type = CHET_DT ; cesr . cesr_element_base = 0 ; cesr . cesr_element_count = cp . cp_ndrives ; cesr . cesr_flags |= CESR_VOLTAGS ; cesr . cesr_element_status = ces ; if ( ioctl ( changer_fd , CHIOGSTATUS , ( char * ) & cesr ) == -1 ) { free ( ch_ces ) ; err ( 1 , " " , changer_name ) ; } ces += cp . cp_ndrives ; } if ( cp . cp_nportals > 0 ) { ( void ) memset ( & cesr , 0 , sizeof ( cesr ) ) ; cesr . cesr_element_type = CHET_IE ; cesr . cesr_element_base = 0 ; cesr . cesr_element_count = cp . cp_nportals ; cesr . cesr_flags |= CESR_VOLTAGS ; cesr . cesr_element_status = ces ; if ( ioctl ( changer_fd , CHIOGSTATUS , ( char * ) & cesr ) == -1 ) { free ( ch_ces ) ; err ( 1 , " " , changer_name ) ; } ces += cp . cp_nportals ; } if ( cp . cp_npickers > 0 ) { ( void ) memset ( & cesr , 0 , sizeof ( cesr ) ) ; cesr . cesr_element_type = CHET_MT ; cesr . cesr_element_base = 0 ; cesr . cesr_element_count = cp . cp_npickers ; cesr . cesr_flags |= CESR_VOLTAGS ; cesr . cesr_element_status = ces ; if ( ioctl ( changer_fd , CHIOGSTATUS , ( char * ) & cesr ) == -1 ) { free ( ch_ces ) ; err ( 1 , " " , changer_name ) ; } } for ( elem = 0 ; elem <= total_elem ; ++ elem ) { ces = & ch_ces [ elem ] ; if ( ( ces -> ces_flags & ( CES_STATUS_ACCESS | CES_STATUS_FULL ) ) != ( CES_STATUS_ACCESS | CES_STATUS_FULL ) ) continue ; if ( strcasecmp ( voltag , ( const char * ) ces -> ces_pvoltag . cv_volid ) == 0 ) { * et = ces -> ces_type ; * eu = ces -> ces_addr ; ++ found ; break ; } } if ( ! found ) { errx ( 1 , " " , changer_name , voltag ) ; } free ( ch_ces ) ; return ; }
find_element()
find_element ()
/*
	 * Get the changer parameters, we're interested in the counts
	 * for all types of elements to perform our search.
	 */ /* Allocate some memory for the results */ /* Read in the changer slots */ /* Read in the drive information */ /* Read in the portal information */ /* Read in the picker information */ /*
	 * Now search the list the specified <voltag>
	 */ /* Make sure we have a tape in this element */ /* Check to see if it is our target */