| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: snprintb.c,v 1.3 2009/01/14 21:33:22 pooka Exp $ */ | | 1 | /* $NetBSD: snprintb.c,v 1.4 2009/01/18 12:05:49 lukem Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2002 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2002 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -31,37 +31,37 @@ | | | @@ -31,37 +31,37 @@ |
31 | * | | 31 | * |
32 | * => returns the length of the buffer that would be required to print the | | 32 | * => returns the length of the buffer that would be required to print the |
33 | * string minus the terminating NUL. | | 33 | * string minus the terminating NUL. |
34 | */ | | 34 | */ |
35 | #ifndef _STANDALONE | | 35 | #ifndef _STANDALONE |
36 | # ifndef _KERNEL | | 36 | # ifndef _KERNEL |
37 | | | 37 | |
38 | # if HAVE_NBTOOL_CONFIG_H | | 38 | # if HAVE_NBTOOL_CONFIG_H |
39 | # include "nbtool_config.h" | | 39 | # include "nbtool_config.h" |
40 | # endif | | 40 | # endif |
41 | | | 41 | |
42 | # include <sys/cdefs.h> | | 42 | # include <sys/cdefs.h> |
43 | # if defined(LIBC_SCCS) && !defined(lint) | | 43 | # if defined(LIBC_SCCS) && !defined(lint) |
44 | __RCSID("$NetBSD: snprintb.c,v 1.3 2009/01/14 21:33:22 pooka Exp $"); | | 44 | __RCSID("$NetBSD: snprintb.c,v 1.4 2009/01/18 12:05:49 lukem Exp $"); |
45 | # endif | | 45 | # endif |
46 | | | 46 | |
47 | # include <sys/types.h> | | 47 | # include <sys/types.h> |
48 | # include <sys/inttypes.h> | | 48 | # include <sys/inttypes.h> |
49 | # include <stdio.h> | | 49 | # include <stdio.h> |
50 | # include <util.h> | | 50 | # include <util.h> |
51 | # include <errno.h> | | 51 | # include <errno.h> |
52 | # else | | 52 | # else |
53 | # include <sys/cdefs.h> | | 53 | # include <sys/cdefs.h> |
54 | __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.3 2009/01/14 21:33:22 pooka Exp $"); | | 54 | __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.4 2009/01/18 12:05:49 lukem Exp $"); |
55 | # include <sys/param.h> | | 55 | # include <sys/param.h> |
56 | # include <sys/inttypes.h> | | 56 | # include <sys/inttypes.h> |
57 | # include <sys/systm.h> | | 57 | # include <sys/systm.h> |
58 | # include <lib/libkern/libkern.h> | | 58 | # include <lib/libkern/libkern.h> |
59 | # endif | | 59 | # endif |
60 | | | 60 | |
61 | int | | 61 | int |
62 | snprintb(char *buf, size_t buflen, const char *bitfmt, uint64_t val) | | 62 | snprintb(char *buf, size_t buflen, const char *bitfmt, uint64_t val) |
63 | { | | 63 | { |
64 | char *bp = buf; | | 64 | char *bp = buf; |
65 | const char *sbase; | | 65 | const char *sbase; |
66 | int bit, ch, len, sep, flen; | | 66 | int bit, ch, len, sep, flen; |
67 | uint64_t field; | | 67 | uint64_t field; |
| @@ -83,39 +83,39 @@ snprintb(char *buf, size_t buflen, const | | | @@ -83,39 +83,39 @@ snprintb(char *buf, size_t buflen, const |
83 | sbase = "%" PRId64; | | 83 | sbase = "%" PRId64; |
84 | break; | | 84 | break; |
85 | case 16: | | 85 | case 16: |
86 | sbase = "0x%" PRIx64; | | 86 | sbase = "0x%" PRIx64; |
87 | break; | | 87 | break; |
88 | default: | | 88 | default: |
89 | goto internal; | | 89 | goto internal; |
90 | } | | 90 | } |
91 | | | 91 | |
92 | len = snprintf(bp, buflen, sbase, val); | | 92 | len = snprintf(bp, buflen, sbase, val); |
93 | if (len < 0) | | 93 | if (len < 0) |
94 | goto internal; | | 94 | goto internal; |
95 | | | 95 | |
96 | if (len < buflen) | | 96 | if ((size_t)len < buflen) |
97 | bp += len; | | 97 | bp += len; |
98 | else | | 98 | else |
99 | bp += buflen - 1; | | 99 | bp += buflen - 1; |
100 | | | 100 | |
101 | /* | | 101 | /* |
102 | * If the value we printed was 0 and we're using the old-style format, | | 102 | * If the value we printed was 0 and we're using the old-style format, |
103 | * we're done. | | 103 | * we're done. |
104 | */ | | 104 | */ |
105 | if ((val == 0) && (ch != '\177')) | | 105 | if ((val == 0) && (ch != '\177')) |
106 | goto terminate; | | 106 | goto terminate; |
107 | | | 107 | |
108 | #define PUTC(c) if (++len < buflen) *bp++ = (c) | | 108 | #define PUTC(c) if ((size_t)(++len) < buflen) *bp++ = (c) |
109 | #define PUTS(s) while ((ch = *(s)++) != 0) PUTC(ch) | | 109 | #define PUTS(s) while ((ch = *(s)++) != 0) PUTC(ch) |
110 | | | 110 | |
111 | /* | | 111 | /* |
112 | * Chris Torek's new bitmask format is identified by a leading \177 | | 112 | * Chris Torek's new bitmask format is identified by a leading \177 |
113 | */ | | 113 | */ |
114 | sep = '<'; | | 114 | sep = '<'; |
115 | if (ch != '\177') { | | 115 | if (ch != '\177') { |
116 | /* old (standard) format. */ | | 116 | /* old (standard) format. */ |
117 | for (;(bit = *bitfmt++) != 0;) { | | 117 | for (;(bit = *bitfmt++) != 0;) { |
118 | if (val & (1 << (bit - 1))) { | | 118 | if (val & (1 << (bit - 1))) { |
119 | PUTC(sep); | | 119 | PUTC(sep); |
120 | for (; (ch = *bitfmt) > ' '; ++bitfmt) | | 120 | for (; (ch = *bitfmt) > ' '; ++bitfmt) |
121 | PUTC(ch); | | 121 | PUTC(ch); |
| @@ -142,27 +142,27 @@ snprintb(char *buf, size_t buflen, const | | | @@ -142,27 +142,27 @@ snprintb(char *buf, size_t buflen, const |
142 | flen = *bitfmt++; /* field length */ | | 142 | flen = *bitfmt++; /* field length */ |
143 | field = (val >> bit) & | | 143 | field = (val >> bit) & |
144 | (((uint64_t)1 << flen) - 1); | | 144 | (((uint64_t)1 << flen) - 1); |
145 | if (ch == 'F') /* just extract */ | | 145 | if (ch == 'F') /* just extract */ |
146 | break; | | 146 | break; |
147 | PUTC(sep); | | 147 | PUTC(sep); |
148 | sep = ','; | | 148 | sep = ','; |
149 | PUTS(bitfmt); | | 149 | PUTS(bitfmt); |
150 | PUTC('='); | | 150 | PUTC('='); |
151 | flen = snprintf(bp, buflen - len, sbase, field); | | 151 | flen = snprintf(bp, buflen - len, sbase, field); |
152 | if (flen < 0) | | 152 | if (flen < 0) |
153 | goto internal; | | 153 | goto internal; |
154 | len += flen; | | 154 | len += flen; |
155 | if (len < buflen) | | 155 | if ((size_t)len < buflen) |
156 | bp += flen; | | 156 | bp += flen; |
157 | break; | | 157 | break; |
158 | case '=': | | 158 | case '=': |
159 | case ':': | | 159 | case ':': |
160 | /* | | 160 | /* |
161 | * Here "bit" is actually a value instead, | | 161 | * Here "bit" is actually a value instead, |
162 | * to be compared against the last field. | | 162 | * to be compared against the last field. |
163 | * This only works for values in [0..255], | | 163 | * This only works for values in [0..255], |
164 | * of course. | | 164 | * of course. |
165 | */ | | 165 | */ |
166 | if ((int)field != bit) | | 166 | if ((int)field != bit) |
167 | goto skip; | | 167 | goto skip; |
168 | if (ch == '=') { | | 168 | if (ch == '=') { |