BarryServer : Git

All the code for all my projects
// BarryServer : Git / OrionUserland / blob / 19aefaad8af9de8719ba1e5b5340e0a1b9c68853 / cat / main.c

// Related

OrionUserland

Barry Importing existing Orion Userland 19aefaa (2 years, 4 months ago)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>

/* Main function */
int
main(int argc, char *argv[])
{
	int fd, sz, i;
	char *buf;
	struct stat statbuf;

	if (argc < 2) {
		printf("Usage: %s <file> [<file>...]\n");
		return 0;
	}

	for (i = 1; i < argc; i++) {
		fd = open(argv[i], O_RDONLY);
		if (fd < 0) {
			printf("%s: %s: ", argv[0], argv[i]);
			perror(NULL);
			continue;
		}

		stat(argv[i], &statbuf);
		if (S_ISREG(statbuf.mode)) {
			sz = lseek(fd, 0, SEEK_END);
			buf = malloc(sz + 1);
			lseek(fd, 0, SEEK_SET);
			read(fd, buf, sz);
			printf("%s", buf);
			free(buf);
		}

		close(fd);
	}

	return 0;
}