Update LICENSE, LOG, TODO and Makefile. Add iso646.h to common.h.

Add POSIX implementation of cat(1) (it's really bad.).
This commit is contained in:
Mid Favila 2022-08-13 19:43:46 -04:00
parent 1ac44c03b7
commit 209c0401de
7 changed files with 146 additions and 7 deletions

View File

@ -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.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
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:
<program> Copyright (C) <year> <name of author>
<program> 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.

2
LOG
View File

@ -1 +1 @@
2021/11/23 - Added true and false, as well as a skeleton for wc. Created LOG.
2021/11/23 - Added true and false, as well as a skeleton for wc. Created LOG.

4
TODO
View File

@ -9,10 +9,10 @@ Write the following utilities:
batch(?)
bc
cal
cat
cat (in progress)
chgrp
chmod
chownn
chown
cksum
cmp
comm

24
src/Makefile Normal file
View File

@ -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 $<

112
src/cat.c Normal file
View File

@ -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);
}

View File

@ -1,4 +1,7 @@
#include <stdlib.h>
#include <iso646.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>