[utils] / wc.c Repository:
ViewVC logotype

View of /wc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (download) (annotate)
Sat Aug 26 18:14:57 2006 UTC (3 years, 2 months ago) by synack
File size: 622 byte(s)
Added scriptable wc
// wc.c
// A script friendly version of wc
// Copyright 2006 Jeremy Grosser
// synack@csh.rit.edu

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char *argv[]) {
	char *buf;
	FILE *fd;
	int len, i, count;

	if(argc < 2) {
		fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
		exit(0);
	}

	fd = fopen(argv[1], "r");

	// Get the file size
	fseek(fd, 0, SEEK_END);
	len = (int)ftell(fd);
	fseek(fd, 0, SEEK_SET);

	buf = malloc(len);
	fread(buf, len, 1, fd);
	fclose(fd);

	for(i = 0; i < len; i++) {
		if(buf[i] == '\n') {
			count++;
		}
	}

	fprintf(stdout, "%i\n", count);
	return 0;
}

synack at csh.rit.edu
ViewVC Help
Powered by ViewVC 1.0.0