Skip to content

Instantly share code, notes, and snippets.

@WaltHP
Created February 11, 2016 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WaltHP/f7b50b9ad6e415262aa0 to your computer and use it in GitHub Desktop.
Save WaltHP/f7b50b9ad6e415262aa0 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <getopt.h>
static int path_prepend(char **path, const char *fmt, ...)
{
va_list va;
char *pre;
int err = 0;
va_start(va, fmt);
err = vasprintf(&pre, fmt, va);
va_end(va);
if (err < 0)
goto out;
if (*path != NULL) {
char *new;
err = asprintf(&new, "%s-%s", pre, *path);
free(pre);
if (err < 0)
goto out;
free(*path);
*path = new;
} else {
*path = pre;
}
out:
return err;
}
/*
** Linux only supports 32 bit luns.
** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
*/
static int format_lun_number(unsigned long lun, char **path) {
/* address method 0, peripheral device addressing with bus id of zero */
if (lun < 256)
return path_prepend(path, "lun-%d", lun);
/* handle all other lun addressing methods by using a variant of the original lun format */
return path_prepend(path, "lun-0x%04x%04x00000000", (lun & 0xffff), (lun >> 16) & 0xffff);
}
int main() {
unsigned long lun;
int converted_lun;
for( lun = 0; lun < 65536; lun = lun + 1) {
char *lun_str = NULL;
converted_lun = format_lun_number(lun, &lun_str);
printf("LUN(%lu) = %s\n", lun, lun_str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment