/* * This is an example of using mrd_read_element_status(3mrd) * to find out what a particular element has media that is * inverted or now. It will simply print "Inverted" or * "Not-inverted" based on the result. * * Usage: * * mrd_inverted robot type element * * A quirk of this example, is that if we use an invalid slot * it may print "Not-inverted". Many medium changers don't * fail Read Element Status for invalid elements, it just * doesn't return any data. We don't check the data length, * so all we see is the initially clear media byte. */ #ifndef lint static char SccsId[] = "@(#)mrd_inverted.c 1.2 (mrd-example) 3/5/97" ; #endif #include #include #include #include #include /* * Given a string, resembling one of the element types, * return the SCSI type code for it. */ struct { int code ; char *string ; } etypes[] = { TRANSPORT, "transport", SLOT, "slot", DRIVE, "drive", PORT, "port", } ; convert_type(char *etype) { register i ; /* * For each entry in the array. */ for(i = 0; i < sizeof(etypes)/sizeof(etypes[0]); i++) /* * Do a case insensitive comparison, allowing * abbreviations. Return as soon as a match is * found. Return -1 if one isn't found. */ #ifdef vms if( strncmp(etypes[i].string, etype, strlen(etype)) == 0 ) #else if( strncasecmp(etypes[i].string, etype, strlen(etype)) == 0 ) #endif return etypes[i].code ; return -1 ; } main(int argc, char *argv[]) { int status ; /* return status */ int type ; /* Element type */ int element ; /* Element number */ char *robot ; /* Robot to open */ robot_info_t robot_info ; /* Robot data */ dev_status_t dev_status ; /* Device status */ unsigned char data[READ_ELEMENT_STATUS_DATA_LENGTH] ; char log_info[MRD_MAX_LOG_STRING+1] ; /* * Check that there are enough arguments. */ if( argc < 4 ) { printf("usage: %s robot type element\n", argv[0]) ; exit(1) ; } else { robot = argv[1] ; element = atoi(argv[3]) ; if((type = convert_type(argv[2])) == -1 ) { printf("Unrecognized element type: %s\n", argv[2]) ; exit(1) ; } } /* * Initialize the channel field of the robot_info, so * mrd_startup(3mrd) will actually open the robot. */ robot_info.channel = BAD_CHANNEL ; status = mrd_startup(robot, &robot_info, log_info) ; if( status != MRD_STATUS_SUCCESS ) { printf("Startup failed: %s: %s.\n", mrd_strstatus(status), log_info[0] ? log_info : "none") ; exit(1) ; } switch( type ) { case SLOT: element += robot_info.slot_start ; break ; case PORT: element += robot_info.port_start ; break ; case TRANSPORT: element += robot_info.transport_start ; break ; case DRIVE: element += robot_info.device_start ; break ; } /* * Read Element Status commands rarely fail, they just * succeed, but return no data. The only byte of * interest is MEDIA_LOC which has the INVERT bit * in it. Clear it and check when done. */ data[MEDIA_LOC] = '\0' ; status = mrd_read_element_status(&robot_info, type, element, 1, data, READ_ELEMENT_STATUS_DATA_LENGTH, &dev_status) ; /* * But sometimes they do fail. */ if( status != MRD_STATUS_SUCCESS ) { printf("Read Element Status failed on %s: %s.\n", robot, mrd_strstatus(status)) ; exit(1) ; } if( data[MEDIA_LOC] & ELEMENT_INVERT ) printf("Inverted\n") ; else printf("Not-inverted\n") ; (void)mrd_shutdown(&robot_info) ; return 0 ; }