"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "lib/sg_lib.c" between
sg3_utils-1.45r830.tar.xz and sg3_utils-1.45r831.tar.xz

About: sg3_utils contains utilities that send SCSI commands to devices. Beta version.

sg_lib.c  (sg3_utils-1.45r830.tar.xz):sg_lib.c  (sg3_utils-1.45r831.tar.xz)
skipping to change at line 3064 skipping to change at line 3064
a += 8; a += 8;
memset(buff, ' ', 80); memset(buff, ' ', 80);
k = sg_scnpr(buff + 1, blen - 1, "%.2x", a); k = sg_scnpr(buff + 1, blen - 1, "%.2x", a);
buff[k + 1] = ' '; buff[k + 1] = ' ';
} }
} }
if (cpos > cpstart) if (cpos > cpstart)
printf("%.76s\n", buff); printf("%.76s\n", buff);
} }
/* If the number in 'buf' can be decoded or the multiplier is unknown /* If the number in 'buf' can not be decoded or the multiplier is unknown
* then -1 is returned. Accepts a hex prefix (0x or 0X) or a decimal * then -1 is returned. Accepts a hex prefix (0x or 0X) or a decimal
* multiplier suffix (as per GNU's dd (since 2002: SI and IEC 60027-2)). * multiplier suffix (as per GNU's dd (since 2002: SI and IEC 60027-2)).
* Main (SI) multipliers supported: K, M, G. Ignore leading spaces and * Main (SI) multipliers supported: K, M, G. Ignore leading spaces and
* tabs; accept comma, hyphen, space, tab and hash as terminator. */ * tabs; accept comma, hyphen, space, tab and hash as terminator.
* Handles zero and positive values up to 2**31-1 .
* Experimental: left argument (must in with hexadecimal digit) added
* to, or multiplied, by right argument. No embedded spaces.
* Examples: '3+1k' (evaluates to 1027) and '0x34+1m'. */
int int
sg_get_num(const char * buf) sg_get_num(const char * buf)
{ {
bool is_hex = false;
int res, num, n, len; int res, num, n, len;
unsigned int unum; unsigned int unum;
char * cp; char * cp;
const char * b; const char * b;
const char * b2p;
char c = 'c'; char c = 'c';
char c2 = '\0'; /* keep static checker happy */ char c2 = '\0'; /* keep static checker happy */
char c3 = '\0'; /* keep static checker happy */ char c3 = '\0'; /* keep static checker happy */
char lb[16]; char lb[16];
if ((NULL == buf) || ('\0' == buf[0])) if ((NULL == buf) || ('\0' == buf[0]))
return -1; return -1;
len = strlen(buf); len = strlen(buf);
n = strspn(buf, " \t"); n = strspn(buf, " \t");
if (n > 0) { if (n > 0) {
skipping to change at line 3102 skipping to change at line 3108
cp = strpbrk((char *)buf, " \t,#-"); cp = strpbrk((char *)buf, " \t,#-");
if (cp) { if (cp) {
len = cp - buf; len = cp - buf;
n = (int)sizeof(lb) - 1; n = (int)sizeof(lb) - 1;
len = (len < n) ? len : n; len = (len < n) ? len : n;
memcpy(lb, buf, len); memcpy(lb, buf, len);
lb[len] = '\0'; lb[len] = '\0';
b = lb; b = lb;
} else } else
b = buf; b = buf;
b2p = b;
if (('0' == b[0]) && (('x' == b[1]) || ('X' == b[1]))) { if (('0' == b[0]) && (('x' == b[1]) || ('X' == b[1]))) {
res = sscanf(b + 2, "%x", &unum); res = sscanf(b + 2, "%x%c", &unum, &c);
num = unum; num = unum;
is_hex = true;
b2p = b + 2;
} else if ('H' == toupper((int)b[len - 1])) { } else if ('H' == toupper((int)b[len - 1])) {
res = sscanf(b, "%x", &unum); res = sscanf(b, "%x", &unum);
num = unum; num = unum;
} else } else
res = sscanf(b, "%d%c%c%c", &num, &c, &c2, &c3); res = sscanf(b, "%d%c%c%c", &num, &c, &c2, &c3);
if (res < 1) if (res < 1)
return -1LL; return -1;
else if (1 == res) else if (1 == res)
return num; return num;
else { else {
c = toupper((int)c);
if (is_hex) {
if (! ((c == '+') || (c == 'X')))
return -1;
}
if (res > 2) if (res > 2)
c2 = toupper((int)c2); c2 = toupper((int)c2);
if (res > 3) if (res > 3)
c3 = toupper((int)c3); c3 = toupper((int)c3);
switch (toupper((int)c)) {
switch (c) {
case 'C': case 'C':
return num; return num;
case 'W': case 'W':
return num * 2; return num * 2;
case 'B': case 'B':
return num * 512; return num * 512;
case 'K': case 'K':
if (2 == res) if (2 == res)
return num * 1024; return num * 1024;
if (('B' == c2) || ('D' == c2)) if (('B' == c2) || ('D' == c2))
skipping to change at line 3150 skipping to change at line 3167
return num * 1048576; return num * 1048576;
return -1; return -1;
case 'G': case 'G':
if (2 == res) if (2 == res)
return num * 1073741824; return num * 1073741824;
if (('B' == c2) || ('D' == c2)) if (('B' == c2) || ('D' == c2))
return num * 1000000000; return num * 1000000000;
if (('I' == c2) && (4 == res) && ('B' == c3)) if (('I' == c2) && (4 == res) && ('B' == c3))
return num * 1073741824; return num * 1073741824;
return -1; return -1;
case 'X': case 'X': /* experimental: multiplication */
cp = (char *)strchr(b, 'x'); /* left argument must end with hexadecimal digit */
cp = (char *)strchr(b2p, 'x');
if (NULL == cp) if (NULL == cp)
cp = (char *)strchr(b, 'X'); cp = (char *)strchr(b2p, 'X');
if (cp) { if (cp) {
n = sg_get_num(cp + 1); n = sg_get_num(cp + 1);
if (-1 != n) if (-1 != n)
return num * n; return num * n;
} }
return -1; return -1;
case '+': /* experimental: addition */
/* left argument must end with hexadecimal digit */
cp = (char *)strchr(b2p, '+');
if (cp) {
n = sg_get_num(cp + 1);
if (-1 != n)
return num + n;
}
return -1;
default: default:
pr2ws("unrecognized multiplier\n"); pr2ws("unrecognized multiplier\n");
return -1; return -1;
} }
} }
} }
/* If the number in 'buf' can not be decoded then -1 is returned. Accepts a /* If the number in 'buf' can not be decoded then -1 is returned. Accepts a
* hex prefix (0x or 0X) or a 'h' (or 'H') suffix; otherwise decimal is * hex prefix (0x or 0X) or a 'h' (or 'H') suffix; otherwise decimal is
* assumed. Does not accept multipliers. Accept a comma (","), hyphen ("-"), * assumed. Does not accept multipliers. Accept a comma (","), hyphen ("-"),
skipping to change at line 3199 skipping to change at line 3226
res = sscanf(buf, "%x", &unum); res = sscanf(buf, "%x", &unum);
num = unum; num = unum;
} else } else
res = sscanf(buf, "%d", &num); res = sscanf(buf, "%d", &num);
if (1 == res) if (1 == res)
return num; return num;
else else
return -1; return -1;
} }
/* If the number in 'buf' can be decoded or the multiplier is unknown /* If the number in 'buf' can not be decoded or the multiplier is unknown
* then -1LL is returned. Accepts a hex prefix (0x or 0X) or a decimal * then -1LL is returned. Accepts a hex prefix (0x or 0X), hex suffix
* multiplier suffix (as per GNU's dd (since 2002: SI and IEC 60027-2)). * (h or H), or a decimal multiplier suffix (as per GNU's dd (since 2002:
* Main (SI) multipliers supported: K, M, G, T, P. Ignore leading spaces * SI and IEC 60027-2)). Main (SI) multipliers supported: K, M, G, T, P
* and tabs; accept comma, hyphen, space, tab and hash as terminator. */ * and E. Ignore leading spaces and tabs; accept comma, hyphen, space, tab
* and hash as terminator. Handles zero and positive values up to 2**63-1 .
* Experimental: left argument (must in with hexadecimal digit) added
* to, or multiplied by right argument. No embedded spaces.
* Examples: '3+1k' (evaluates to 1027) and '0x34+1m'. */
int64_t int64_t
sg_get_llnum(const char * buf) sg_get_llnum(const char * buf)
{ {
bool is_hex = false;
int res, len, n; int res, len, n;
int64_t num, ll; int64_t num, ll;
uint64_t unum; uint64_t unum;
char * cp; char * cp;
const char * b; const char * b;
const char * b2p;
char c = 'c'; char c = 'c';
char c2 = '\0'; /* keep static checker happy */ char c2 = '\0'; /* keep static checker happy */
char c3 = '\0'; /* keep static checker happy */ char c3 = '\0'; /* keep static checker happy */
char lb[32]; char lb[32];
if ((NULL == buf) || ('\0' == buf[0])) if ((NULL == buf) || ('\0' == buf[0]))
return -1LL; return -1LL;
len = strlen(buf); len = strlen(buf);
n = strspn(buf, " \t"); n = strspn(buf, " \t");
if (n > 0) { if (n > 0) {
skipping to change at line 3238 skipping to change at line 3271
cp = strpbrk((char *)buf, " \t,#-"); cp = strpbrk((char *)buf, " \t,#-");
if (cp) { if (cp) {
len = cp - buf; len = cp - buf;
n = (int)sizeof(lb) - 1; n = (int)sizeof(lb) - 1;
len = (len < n) ? len : n; len = (len < n) ? len : n;
memcpy(lb, buf, len); memcpy(lb, buf, len);
lb[len] = '\0'; lb[len] = '\0';
b = lb; b = lb;
} else } else
b = buf; b = buf;
b2p = b;
if (('0' == b[0]) && (('x' == b[1]) || ('X' == b[1]))) { if (('0' == b[0]) && (('x' == b[1]) || ('X' == b[1]))) {
res = sscanf(b + 2, "%" SCNx64 , &unum); res = sscanf(b + 2, "%" SCNx64 "%c", &unum, &c);
num = unum; num = unum;
is_hex = true;
b2p = b + 2;
} else if ('H' == toupper((int)b[len - 1])) { } else if ('H' == toupper((int)b[len - 1])) {
res = sscanf(b, "%" SCNx64 , &unum); res = sscanf(b, "%" SCNx64 , &unum);
num = unum; num = unum;
} else } else
res = sscanf(b, "%" SCNd64 "%c%c%c", &num, &c, &c2, &c3); res = sscanf(b, "%" SCNd64 "%c%c%c", &num, &c, &c2, &c3);
if (res < 1) if (res < 1)
return -1LL; return -1LL;
else if (1 == res) else if (1 == res)
return num; return num;
else { else {
c = toupper((int)c);
if (is_hex) {
if (! ((c == '+') || (c == 'X')))
return -1;
}
if (res > 2) if (res > 2)
c2 = toupper((int)c2); c2 = toupper((int)c2);
if (res > 3) if (res > 3)
c3 = toupper((int)c3); c3 = toupper((int)c3);
switch (toupper((int)c)) {
switch (c) {
case 'C': case 'C':
return num; return num;
case 'W': case 'W':
return num * 2; return num * 2;
case 'B': case 'B':
return num * 512; return num * 512;
case 'K': case 'K': /* kilo or kibi */
if (2 == res) if (2 == res)
return num * 1024; return num * 1024;
if (('B' == c2) || ('D' == c2)) if (('B' == c2) || ('D' == c2))
return num * 1000; return num * 1000;
if (('I' == c2) && (4 == res) && ('B' == c3)) if (('I' == c2) && (4 == res) && ('B' == c3))
return num * 1024; return num * 1024; /* KiB */
return -1LL; return -1LL;
case 'M': case 'M': /* mega or mebi */
if (2 == res) if (2 == res)
return num * 1048576; return num * 1048576; /* M */
if (('B' == c2) || ('D' == c2)) if (('B' == c2) || ('D' == c2))
return num * 1000000; return num * 1000000; /* MB */
if (('I' == c2) && (4 == res) && ('B' == c3)) if (('I' == c2) && (4 == res) && ('B' == c3))
return num * 1048576; return num * 1048576; /* MiB */
return -1LL; return -1LL;
case 'G': case 'G': /* giga or gibi */
if (2 == res) if (2 == res)
return num * 1073741824; return num * 1073741824; /* G */
if (('B' == c2) || ('D' == c2)) if (('B' == c2) || ('D' == c2))
return num * 1000000000; return num * 1000000000; /* GB */
if (('I' == c2) && (4 == res) && ('B' == c3)) if (('I' == c2) && (4 == res) && ('B' == c3))
return num * 1073741824; return num * 1073741824; /* GiB */
return -1LL; return -1LL;
case 'T': case 'T': /* tera or tebi */
if (2 == res) if (2 == res)
return num * 1099511627776LL; return num * 1099511627776LL; /* T */
if (('B' == c2) || ('D' == c2)) if (('B' == c2) || ('D' == c2))
return num * 1000000000000LL; return num * 1000000000000LL; /* TB */
if (('I' == c2) && (4 == res) && ('B' == c3)) if (('I' == c2) && (4 == res) && ('B' == c3))
return num * 1099511627776LL; return num * 1099511627776LL; /* TiB */
return -1LL; return -1LL;
case 'P': case 'P': /* peta or pebi */
if (2 == res) if (2 == res)
return num * 1099511627776LL * 1024; return num * 1099511627776LL * 1024;
if (('B' == c2) || ('D' == c2)) if (('B' == c2) || ('D' == c2))
return num * 1000000000000LL * 1000; return num * 1000000000000LL * 1000;
if (('I' == c2) && (4 == res) && ('B' == c3)) if (('I' == c2) && (4 == res) && ('B' == c3))
return num * 1099511627776LL * 1024; return num * 1099511627776LL * 1024;
return -1LL; return -1LL;
case 'X': case 'E': /* exa or exbi */
cp = (char *)strchr(b, 'x'); if (2 == res)
return num * 1099511627776LL * 1024 * 1024;
if (('B' == c2) || ('D' == c2))
return num * 1000000000000LL * 1000 * 1000;
if (('I' == c2) && (4 == res) && ('B' == c3))
return num * 1099511627776LL * 1024 * 1024;
return -1LL;
case 'X': /* experimental: decimal (left arg) multiplication */
cp = (char *)strchr(b2p, 'x');
if (NULL == cp) if (NULL == cp)
cp = (char *)strchr(b, 'X'); cp = (char *)strchr(b2p, 'X');
if (cp) { if (cp) {
ll = sg_get_llnum(cp + 1); ll = sg_get_llnum(cp + 1);
if (-1LL != ll) if (-1LL != ll)
return num * ll; return num * ll;
} }
return -1LL; return -1LL;
case '+': /* experimental: decimal (left arg) addition */
cp = (char *)strchr(b2p, '+');
if (cp) {
ll = sg_get_llnum(cp + 1);
if (-1LL != ll)
return num + ll;
}
return -1LL;
default: default:
pr2ws("unrecognized multiplier\n"); pr2ws("unrecognized multiplier\n");
return -1LL; return -1LL;
} }
} }
} }
/* If the number in 'buf' can not be decoded then -1 is returned. Accepts a /* If the number in 'buf' can not be decoded then -1 is returned. Accepts a
* hex prefix (0x or 0X) or a 'h' (or 'H') suffix; otherwise decimal is * hex prefix (0x or 0X) or a 'h' (or 'H') suffix; otherwise decimal is
* assumed. Does not accept multipliers. Accept a comma (","), hyphen ("-"), * assumed. Does not accept multipliers. Accept a comma (","), hyphen ("-"),
 End of changes. 41 change blocks. 
33 lines changed or deleted 93 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)