jcs
/cat
/amendments
/3
*: Do c header addition based on output, not input filenames
Also include a header at the top with a timestamp.
jcs made amendment 3 about 1 month ago
--- cat.c Fri Oct 16 15:19:41 2026
+++ cat.c Fri Oct 16 16:04:19 2026
@@ -16,6 +16,7 @@
#include <stdio.h>
#include <string.h>
+#include <time.h>
#include "cat.h"
#include "util.h"
@@ -199,10 +200,13 @@ void
select_files(void)
{
char buf[1024];
+ Str255 tbuf;
SFReply reply;
+ struct tm *ttm = NULL;
struct selected_file *file;
long fsize, pos, tsize, wsize;
short error, n, orefnum, ifrefnum;
+ bool c_header = false;
nselected_files = 0;
@@ -250,9 +254,36 @@ select_files(void)
if (error)
panic("Failed to truncate file %s: %d", PtoCstr(reply.fName), error);
+ if (reply.fName[0] > 2 &&
+ reply.fName[reply.fName[0] - 1] == '.' &&
+ (reply.fName[reply.fName[0]] == 'c' ||
+ reply.fName[reply.fName[0]] == 'h')) {
+ /* writing a .c or .h file, include comment headers */
+ c_header = true;
+
+ memcpy(tbuf, reply.fName, reply.fName[0] + 1);
+ PtoCstr(tbuf);
+
+ ttm = localtime((time_t *)&Time);
+
+ wsize = snprintf(buf, sizeof(buf), "/* %s concatenated on "
+ "%04d-%02d-%02d %02d:%02d:%02d */\r",
+ tbuf, ttm->tm_year + 1900, ttm->tm_mon + 1, ttm->tm_mday,
+ ttm->tm_hour, ttm->tm_min, ttm->tm_sec);
+
+ error = FSWrite(orefnum, &wsize, &buf);
+ if (error)
+ panic("Failed to write file header to %s: %d",
+ PtoCstr(reply.fName), error);
+ }
+
for (n = 0; n < nselected_files; n++) {
file = &selected_files[n];
+ memcpy(tbuf, file->name, file->name[0] + 1);
+ PtoCstr(tbuf);
+ progress("Appending %s...", tbuf);
+
error = FSOpen(file->name, file->vrefnum, &ifrefnum);
if (error)
panic("Failed to open source file %s: %d", PtoCstr(file->name),
@@ -268,10 +299,7 @@ select_files(void)
panic("Failed to get pos of file %s: %d",
PtoCstr(file->name), error);
- if (file->name[0] > 2 && file->name[file->name[0] - 1] == '.' &&
- (file->name[file->name[0]] == 'c' ||
- file->name[file->name[0]] == 'h')) {
- /* .c or .h, include filename */
+ if (c_header) {
wsize = 0;
if (n > 0) {
buf[wsize++] = '\r';
@@ -292,7 +320,7 @@ select_files(void)
panic("Failed to write filename header to %s: %d",
PtoCstr(reply.fName), error);
}
-
+
for (pos = 0; pos < fsize; ) {
tsize = sizeof(buf);
if (pos + tsize > fsize)
@@ -310,10 +338,24 @@ select_files(void)
PtoCstr(reply.fName), error);
pos += wsize;
+
+ if (pos == fsize && c_header && tsize > 0 &&
+ buf[tsize - 1] != '\r') {
+ /* c'ish file with no trailing newline, add one */
+ wsize = 1;
+ buf[0] = '\r';
+ error = FSWrite(orefnum, &wsize, &buf);
+ if (error || wsize != 1)
+ panic("Failed to write newline to file %s: %d", wsize,
+ PtoCstr(reply.fName), error);
+ break;
+ }
}
FSClose(ifrefnum);
}
+
+ progress(NULL);
FSClose(orefnum);
}
\ No newline at end of file