Browse Source

Use typedefs uint and byte

June 3 weeks ago
parent
commit
3021962725
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
7 changed files with 53 additions and 39 deletions
  1. 3
    1
      bin/bri.c
  2. 6
    4
      bin/hnel.c
  3. 5
    2
      bin/modem.c
  4. 4
    2
      bin/pbd.c
  5. 25
    22
      bin/scheme.c
  6. 3
    2
      bin/wake.c
  7. 7
    6
      bin/xx.c

+ 3
- 1
bin/bri.c View File

@@ -23,6 +23,8 @@
23 23
 #include <sysexits.h>
24 24
 #include <unistd.h>
25 25
 
26
+typedef unsigned uint;
27
+
26 28
 static const char *Class = "/sys/class/backlight";
27 29
 
28 30
 int main(int argc, char *argv[]) {
@@ -52,7 +54,7 @@ int main(int argc, char *argv[]) {
52 54
 	FILE *actual = fopen("actual_brightness", "r");
53 55
 	if (!actual) err(EX_OSFILE, "actual_brightness");
54 56
 
55
-	unsigned value;
57
+	uint value;
56 58
 	int match = fscanf(actual, "%u", &value);
57 59
 	if (match == EOF) err(EX_IOERR, "actual_brightness");
58 60
 	if (match < 1) errx(EX_DATAERR, "actual_brightness");

+ 6
- 4
bin/hnel.c View File

@@ -33,6 +33,8 @@
33 33
 #include <util.h>
34 34
 #endif
35 35
 
36
+typedef unsigned char byte;
37
+
36 38
 static struct termios saveTerm;
37 39
 static void restoreTerm(void) {
38 40
 	tcsetattr(STDIN_FILENO, TCSADRAIN, &saveTerm);
@@ -43,10 +45,10 @@ int main(int argc, char *argv[]) {
43 45
 
44 46
 	if (argc < 4) return EX_USAGE;
45 47
 
46
-	char table[256] = {0};
48
+	byte table[256] = {0};
47 49
 	if (strlen(argv[1]) != strlen(argv[2])) return EX_USAGE;
48 50
 	for (const char *from = argv[1], *to = argv[2]; *from; ++from, ++to) {
49
-		table[(unsigned)*from] = *to;
51
+		table[(byte)*from] = *to;
50 52
 	}
51 53
 
52 54
 	error = tcgetattr(STDERR_FILENO, &saveTerm);
@@ -73,7 +75,7 @@ int main(int argc, char *argv[]) {
73 75
 
74 76
 	bool enable = true;
75 77
 
76
-	char buf[4096];
78
+	byte buf[4096];
77 79
 	struct pollfd fds[2] = {
78 80
 		{ .fd = STDIN_FILENO, .events = POLLIN },
79 81
 		{ .fd = pty, .events = POLLIN },
@@ -89,7 +91,7 @@ int main(int argc, char *argv[]) {
89 91
 					continue;
90 92
 				}
91 93
 
92
-				unsigned char c = buf[0];
94
+				byte c = buf[0];
93 95
 				if (enable && table[c]) buf[0] = table[c];
94 96
 			}
95 97
 

+ 5
- 2
bin/modem.c View File

@@ -31,7 +31,10 @@
31 31
 #include <util.h>
32 32
 #endif
33 33
 
34
-static const int BaudRate = 19200;
34
+typedef unsigned uint;
35
+typedef unsigned char byte;
36
+
37
+static const uint BaudRate = 19200;
35 38
 
36 39
 static struct termios saveTerm;
37 40
 static void restoreTerm(void) {
@@ -65,7 +68,7 @@ int main(int argc, char *argv[]) {
65 68
 		err(EX_NOINPUT, "%s", argv[1]);
66 69
 	}
67 70
 
68
-	char c;
71
+	byte c;
69 72
 	struct pollfd fds[2] = {
70 73
 		{ .events = POLLIN, .fd = STDIN_FILENO },
71 74
 		{ .events = POLLIN, .fd = pty },

+ 4
- 2
bin/pbd.c View File

@@ -27,6 +27,8 @@
27 27
 #include <sysexits.h>
28 28
 #include <unistd.h>
29 29
 
30
+typedef unsigned char byte;
31
+
30 32
 static void spawn(const char *cmd, const char *arg, int dest, int src) {
31 33
 	pid_t pid = fork();
32 34
 	if (pid < 0) err(EX_OSERR, "fork");
@@ -106,7 +108,7 @@ static int pbdClient(char c) {
106 108
 }
107 109
 
108 110
 static void copy(int out, int in) {
109
-	char buf[4096];
111
+	byte buf[4096];
110 112
 	ssize_t readSize;
111 113
 	while (0 < (readSize = read(in, buf, sizeof(buf)))) {
112 114
 		ssize_t writeSize = write(out, buf, readSize);
@@ -127,7 +129,7 @@ static int pbpaste(void) {
127 129
 	return EX_OK;
128 130
 }
129 131
 
130
-static int open1(char *url) {
132
+static int open1(const char *url) {
131 133
 	if (!url) return EX_USAGE;
132 134
 	int client = pbdClient('o');
133 135
 	ssize_t size = write(client, url, strlen(url));

+ 25
- 22
bin/scheme.c View File

@@ -25,6 +25,9 @@
25 25
 #include <unistd.h>
26 26
 #include <zlib.h>
27 27
 
28
+typedef unsigned uint;
29
+typedef unsigned char byte;
30
+
28 31
 static const struct HSV { double h, s, v; }
29 32
 	R = {   0.0, 1.0, 1.0 },
30 33
 	Y = {  60.0, 1.0, 1.0 },
@@ -33,7 +36,7 @@ static const struct HSV { double h, s, v; }
33 36
 	B = { 240.0, 1.0, 1.0 },
34 37
 	M = { 300.0, 1.0, 1.0 };
35 38
 
36
-static struct RGB { uint8_t r, g, b; } toRGB(struct HSV hsv) {
39
+static struct RGB { byte r, g, b; } toRGB(struct HSV hsv) {
37 40
 	double c = hsv.v * hsv.s;
38 41
 	double h = hsv.h / 60.0;
39 42
 	double x = c * (1.0 - fabs(fmod(h, 2.0) - 1.0));
@@ -88,7 +91,7 @@ static void generate(void) {
88 91
 
89 92
 	scheme[Dark + Black] = x(scheme[Light + Black], 0.0, 1.0, 0.3);
90 93
 	scheme[Dark + White] = x(scheme[Light + White], 0.0, 1.0, 0.6);
91
-	for (int i = Red; i < White; ++i) {
94
+	for (uint i = Red; i < White; ++i) {
92 95
 		scheme[Dark + i] = x(scheme[Light + i], 0.0, 1.0, 0.8);
93 96
 	}
94 97
 
@@ -99,7 +102,7 @@ static void generate(void) {
99 102
 	scheme[Cursor]     = x(scheme[Dark + White],    0.0, 1.0, 0.8);
100 103
 }
101 104
 
102
-static void swap(int a, int b) {
105
+static void swap(uint a, uint b) {
103 106
 	struct HSV t = scheme[a];
104 107
 	scheme[a] = scheme[b];
105 108
 	scheme[b] = t;
@@ -110,13 +113,13 @@ static void invert(void) {
110 113
 	swap(Light + Black, Dark + White);
111 114
 }
112 115
 
113
-static void printHSV(int n) {
116
+static void printHSV(uint n) {
114 117
 	printf("%g,%g,%g\n", scheme[n].h, scheme[n].s, scheme[n].v);
115 118
 }
116 119
 
117
-static void printRGB(int n) {
120
+static void printRGB(uint n) {
118 121
 	struct RGB rgb = toRGB(scheme[n]);
119
-	printf("%02X%02X%02X\n", rgb.r, rgb.g, rgb.b);
122
+	printf("%02hhX%02hhX%02hhX\n", rgb.r, rgb.g, rgb.b);
120 123
 }
121 124
 
122 125
 static const char *CNames[SchemeLen] = {
@@ -145,17 +148,17 @@ static const char *CNames[SchemeLen] = {
145 148
 static void printCHead(void) {
146 149
 	printf("enum {\n");
147 150
 }
148
-static void printC(int n) {
151
+static void printC(uint n) {
149 152
 	struct RGB rgb = toRGB(scheme[n]);
150
-	printf("\t%s = 0x%02X%02X%02X,\n", CNames[n], rgb.r, rgb.g, rgb.b);
153
+	printf("\t%s = 0x%02hhX%02hhX%02hhX,\n", CNames[n], rgb.r, rgb.g, rgb.b);
151 154
 }
152 155
 static void printCTail(void) {
153 156
 	printf("};\n");
154 157
 }
155 158
 
156
-static void printLinux(int n) {
159
+static void printLinux(uint n) {
157 160
 	struct RGB rgb = toRGB(scheme[n]);
158
-	printf("\x1B]P%X%02X%02X%02X", n, rgb.r, rgb.g, rgb.b);
161
+	printf("\x1B]P%X%02hhX%02hhX%02hhX", n, rgb.r, rgb.g, rgb.b);
159 162
 }
160 163
 
161 164
 static const char *MinttyNames[SchemeLen] = {
@@ -179,10 +182,10 @@ static const char *MinttyNames[SchemeLen] = {
179 182
 	[Foreground]      = "ForegroundColour",
180 183
 	[Cursor]          = "CursorColour",
181 184
 };
182
-static void printMintty(int n) {
185
+static void printMintty(uint n) {
183 186
 	if (!MinttyNames[n]) return;
184 187
 	struct RGB rgb = toRGB(scheme[n]);
185
-	printf("%s=%d,%d,%d\n", MinttyNames[n], rgb.r, rgb.g, rgb.b);
188
+	printf("%s=%hhd,%hhd,%hhd\n", MinttyNames[n], rgb.r, rgb.g, rgb.b);
186 189
 }
187 190
 
188 191
 static uint32_t crc;
@@ -201,7 +204,7 @@ static void pngChunk(const char *type, uint32_t size) {
201 204
 	pngWrite(type, 4);
202 205
 }
203 206
 
204
-static void png(int at, int to) {
207
+static void png(uint at, uint to) {
205 208
 	if (to - at > 256) to = at + 256;
206 209
 
207 210
 	uint32_t len = to - at;
@@ -221,7 +224,7 @@ static void png(int at, int to) {
221 224
 	pngInt(crc);
222 225
 
223 226
 	pngChunk("PLTE", 3 * len);
224
-	for (int i = at; i < to; ++i) {
227
+	for (uint i = at; i < to; ++i) {
225 228
 		struct RGB rgb = toRGB(scheme[i]);
226 229
 		pngWrite(&rgb, 3);
227 230
 	}
@@ -233,16 +236,16 @@ static void png(int at, int to) {
233 236
 		enum { None, Sub, Up, Average, Paeth };
234 237
 		data[y][0] = (y % swatchHeight) ? Up : Sub;
235 238
 	}
236
-	for (int i = at; i < to; ++i) {
237
-		int p = i - at;
239
+	for (uint i = at; i < to; ++i) {
240
+		uint p = i - at;
238 241
 		uint32_t y = swatchHeight * (p / cols);
239 242
 		uint32_t x = swatchWidth * (p % cols);
240 243
 		data[y][1 + x] = x ? 1 : p;
241 244
 	}
242 245
 
243 246
 	uLong size = compressBound(sizeof(data));
244
-	uint8_t deflate[size];
245
-	int error = compress(deflate, &size, (Byte *)data, sizeof(data));
247
+	byte deflate[size];
248
+	int error = compress(deflate, &size, (byte *)data, sizeof(data));
246 249
 	if (error != Z_OK) errx(EX_SOFTWARE, "compress: %d", error);
247 250
 
248 251
 	pngChunk("IDAT", size);
@@ -253,16 +256,16 @@ static void png(int at, int to) {
253 256
 	pngInt(crc);
254 257
 }
255 258
 
256
-static void print(void (*fn)(int), int at, int to) {
257
-	for (int i = at; i < to; ++i) {
259
+static void print(void (*fn)(uint), uint at, uint to) {
260
+	for (uint i = at; i < to; ++i) {
258 261
 		fn(i);
259 262
 	}
260 263
 }
261 264
 
262 265
 int main(int argc, char *argv[]) {
263 266
 	generate();
264
-	int at = 0;
265
-	int to = Background;
267
+	uint at = 0;
268
+	uint to = Background;
266 269
 	char out = 'x';
267 270
 
268 271
 	int opt;

+ 3
- 2
bin/wake.c View File

@@ -18,13 +18,14 @@
18 18
 
19 19
 #include <err.h>
20 20
 #include <netinet/in.h>
21
-#include <stdint.h>
22 21
 #include <stdlib.h>
23 22
 #include <sys/socket.h>
24 23
 #include <sysexits.h>
25 24
 
25
+typedef unsigned char byte;
26
+
26 27
 #define MAC 0x04, 0x7D, 0x7B, 0xD5, 0x6A, 0x53
27
-static const uint8_t Payload[102] = {
28
+static const byte Payload[102] = {
28 29
 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
29 30
 	MAC, MAC, MAC, MAC, MAC, MAC, MAC, MAC,
30 31
 	MAC, MAC, MAC, MAC, MAC, MAC, MAC, MAC,

+ 7
- 6
bin/xx.c View File

@@ -17,13 +17,14 @@
17 17
 #include <ctype.h>
18 18
 #include <err.h>
19 19
 #include <stdbool.h>
20
-#include <stdint.h>
21 20
 #include <stdio.h>
22 21
 #include <stdlib.h>
23 22
 #include <sysexits.h>
24 23
 #include <unistd.h>
25 24
 
26
-static bool zero(const uint8_t *ptr, size_t size) {
25
+typedef unsigned char byte;
26
+
27
+static bool zero(const byte *ptr, size_t size) {
27 28
 	for (size_t i = 0; i < size; ++i) {
28 29
 		if (ptr[i]) return false;
29 30
 	}
@@ -41,7 +42,7 @@ static struct {
41 42
 static void dump(FILE *file) {
42 43
 	bool skip = false;
43 44
 
44
-	uint8_t buf[options.cols];
45
+	byte buf[options.cols];
45 46
 	size_t offset = 0;
46 47
 	for (
47 48
 		size_t size;
@@ -92,10 +93,10 @@ static void dump(FILE *file) {
92 93
 }
93 94
 
94 95
 static void undump(FILE *file) {
95
-	uint8_t byte;
96
+	byte c;
96 97
 	int match;
97
-	while (0 < (match = fscanf(file, " %hhx", &byte))) {
98
-		printf("%c", byte);
98
+	while (0 < (match = fscanf(file, " %hhx", &c))) {
99
+		printf("%c", c);
99 100
 	}
100 101
 	if (!match) errx(EX_DATAERR, "invalid input");
101 102
 }

Loading…
Cancel
Save