From 209c0401deac3be21f149482310e3e81c73e6782 Mon Sep 17 00:00:00 2001 From: Mid Favila Date: Sat, 13 Aug 2022 19:43:46 -0400 Subject: [PATCH] Update LICENSE, LOG, TODO and Makefile. Add iso646.h to common.h. Add POSIX implementation of cat(1) (it's really bad.). --- LICENSE | 4 +- LOG | 2 +- TODO | 4 +- src/Makefile | 24 ++++++++ src/cat.c | 112 ++++++++++++++++++++++++++++++++++++ src/common.h | 7 ++- src/{truefalse.c => true.c} | 0 7 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 src/Makefile create mode 100644 src/cat.c rename src/{truefalse.c => true.c} (100%) diff --git a/LICENSE b/LICENSE index d41c0bd..21037fd 100644 --- a/LICENSE +++ b/LICENSE @@ -209,7 +209,7 @@ If you develop a new program, and you want it to be of the greatest possible use To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found. - Copyright (C) + Copyright (C) 2021 Mid Favila This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. @@ -221,7 +221,7 @@ Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: - Copyright (C) + Copyright (C) 2021 Mid Favila This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. diff --git a/LOG b/LOG index 787be80..dbc9b30 100644 --- a/LOG +++ b/LOG @@ -1 +1 @@ -2021/11/23 - Added true and false, as well as a skeleton for wc. Created LOG. \ No newline at end of file +2021/11/23 - Added true and false, as well as a skeleton for wc. Created LOG. diff --git a/TODO b/TODO index 532f5e5..8dfbfcd 100644 --- a/TODO +++ b/TODO @@ -9,10 +9,10 @@ Write the following utilities: batch(?) bc cal - cat + cat (in progress) chgrp chmod - chownn + chown cksum cmp comm diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..375ba60 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,24 @@ +.POSIX: +.SUFFIXES: +CC = cc +CFLAGS = -Wall -O2 +LDFLAGS = -static +LDLIBS = +PREFIX = /usr/local/bin/ +DESTDIR = + +all: echo wc yes true false cat +echo: echo.c +wc: wc.c +yes: yes.c +true: true.c +false: + ln -s true false +cat: cat.c + +clean: + rm echo true wc yes false cat + +.SUFFIXES: .c .o +.c.o: + ${CC} ${CFLAGS} ${LDFLAGS} -c $< diff --git a/src/cat.c b/src/cat.c new file mode 100644 index 0000000..68addf6 --- /dev/null +++ b/src/cat.c @@ -0,0 +1,112 @@ +/* cat.c -- provides a routine to output the contents of the named files in either a buffered or unbuffered fashion. */ +/* Accepts the following parameters: */ +/* -u: Output should be unbuffered. */ + +/* Returns the following values: */ +/* 0: All files were printed successfully. */ +/* 1: One or more files could not be opened. */ + + +#include "common.h" + +/* If mode is set to zero, return a null pointer and print characters directly. Otherwise, return a pointer to a buffer containing */ +/* the contents of the file pointed to by fp. */ +char *cat(FILE *fp, const int mode) + { + char c; + + if(!mode) + { + int i, buflen; + buflen = 4096; + char *arr = malloc(4096); + char *newarr = 0; + + for(i = 0; (c = fgetc(fp)) != EOF; i++) + { + if(i >= buflen) + { + buflen *= 2; + newarr = malloc(buflen); + strcpy(newarr, arr); + free(arr); + arr = newarr; + } + + arr[i] = c; + } + arr[i] = '\0'; + + return arr; + } + else + { + for(;(c = fgetc(fp)) != EOF;) + { + putchar(c); + } + } + + return NULL; + } + +int main(int argc, char **argv) + { + char *buf, *newbuf, *tmpbuf; + int i, upresence, fppresence; + i = upresence = fppresence = 0; + long unsigned int buflen; + buflen = 4096; + FILE *fp; + fp = 0; + buf = malloc(buflen); + + if((upresence = (getopt(argc, argv, ":u") == 'u'))) + { + i++; + } + + for(i++;argv[i] != NULL; i++) + { + if(!strcmp(argv[i], "-")) + { + fppresence++; + newbuf = cat(fopen("/dev/stdin", "r"), upresence); + } + else if((fp = fopen(argv[i], "r")) != NULL) + { + fppresence++; + newbuf = cat(fp, upresence); + } + else + { + fprintf(stderr, "%s: failed to open %s. Aborting.\n", argv[0], argv[i]); + exit(1); + } + + if(!upresence and (2 * (strlen(buf) + strlen(newbuf)) >= buflen)) + { + tmpbuf = malloc(2 * (strlen(buf) + strlen(newbuf))); + strcpy(tmpbuf, buf); + free(buf); + buf = tmpbuf; + } + + if(!upresence) + { + strcat(buf, newbuf); + } + } + + if(!fppresence) + { + buf = cat(fopen("/dev/stdin", "r"), upresence); + } + + if(!upresence) + { + printf("%s", buf); + } + + exit(0); + } diff --git a/src/common.h b/src/common.h index 8747a98..69d1e20 100644 --- a/src/common.h +++ b/src/common.h @@ -1,4 +1,7 @@ -#include +#include #include -#include +#include #include +#include +#include + diff --git a/src/truefalse.c b/src/true.c similarity index 100% rename from src/truefalse.c rename to src/true.c