Move to size_t where practical

This commit is contained in:
Shav Kinderlehrer 2023-04-11 12:32:38 -04:00
parent 41487c8ae6
commit da0287ec63
5 changed files with 15 additions and 11 deletions

View File

@ -4,7 +4,7 @@
#define FILE_H #define FILE_H
struct filedata { struct filedata {
int lc; int lc;
unsigned len; size_t len;
char *buf; char *buf;
}; };

View File

@ -1,8 +1,10 @@
#ifndef LIB_H #ifndef LIB_H
#define LIB_H #define LIB_H
#include <stddef.h>
void die(const char *message); void die(const char *message);
char *formatBytes(unsigned bytes, float *rounded); char *formatBytes(size_t bytes, float *rounded);
int intlen(unsigned i); int intlen(size_t i);
#endif #endif

View File

@ -10,7 +10,7 @@ struct filedata readfile(FILE *fp) {
f.lc = 0; f.lc = 0;
f.len = 0; f.len = 0;
unsigned bufsize = 4; size_t bufsize = 4;
f.buf = malloc(bufsize); f.buf = malloc(bufsize);
if (f.buf == NULL) if (f.buf == NULL)

View File

@ -7,12 +7,12 @@ void die(const char *message) {
exit(1); exit(1);
} }
char *formatBytes(unsigned bytes, float *rounded) { char *formatBytes(size_t bytes, float *rounded) {
char *SIZES[] = {"bytes", "kB", "MB", "GB"}; char *SIZES[] = {"bytes", "kB", "MB", "GB"};
unsigned size = bytes; size_t size = bytes;
unsigned div = 0; size_t div = 0;
unsigned rem = 0; size_t rem = 0;
while (size >= 1024 && div < (sizeof SIZES / sizeof *SIZES)) { while (size >= 1024 && div < (sizeof SIZES / sizeof *SIZES)) {
rem = (size % 1024); rem = (size % 1024);
@ -25,7 +25,7 @@ char *formatBytes(unsigned bytes, float *rounded) {
return SIZES[div]; return SIZES[div];
} }
int intlen(unsigned i) { int intlen(size_t i) {
int l = 1; int l = 1;
while (i > 9) { while (i > 9) {

View File

@ -26,16 +26,18 @@ void run(FILE *fp, char *filename) {
f.lc = 0; f.lc = 0;
char pc = '\0'; char pc = '\0';
char c; char c;
for (unsigned i = 0; i < f.len; i++) { for (size_t i = 0; i < f.len; i++) {
c = f.buf[i]; c = f.buf[i];
if (tty && (pc == '\n' || i == 0)) { if (tty && (pc == '\n' || i == 0)) {
f.lc++; f.lc++;
int padlen = lcpad - intlen(f.lc); int padlen = lcpad - intlen(f.lc);
char padding[padlen]; char padding[padlen];
memset(padding, ' ', padlen); memset(padding, ' ', padlen);
fprintf(stderr, "\r%s%s%d:%s ", GREY, padding, f.lc, RESET); fprintf(stderr, "\r%s%s%d:%s ", GREY, padlen > 0 ? padding : "", f.lc,
RESET); // padlen < 1 causes undefined
} }
pc = c; pc = c;