Skip to content

Instantly share code, notes, and snippets.

@MrDOS
Forked from atroche/simple-wc.c
Created March 2, 2017 16:37
Show Gist options
  • Save MrDOS/6ff5e59322dcd7c736ad1bbe210c9a10 to your computer and use it in GitHub Desktop.
Save MrDOS/6ff5e59322dcd7c736ad1bbe210c9a10 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <fcntl.h>
#include <err.h>
#include <unistd.h>
#include <inttypes.h>
#define BUF_SIZE (1048576 * 10)
int main(int argc, char *argv[])
{
static char char_buf[BUF_SIZE];
static ssize_t buf_size = BUF_SIZE;
void *buf = char_buf;
char *p;
ssize_t len;
uint64_t linect;
int fd = open("ten.json", O_RDONLY);
if (fd < 0) {
warn("bad open");
return 1;
}
while((len = read(fd, buf, buf_size))) {
for(p=buf;len--;p++){
if(*p == '\n'){
linect++;
}
}
}
printf("%" PRIu64 "\n", linect);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment