Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pamo.c and scanf

0 views
Skip to first unread message

src153

unread,
Mar 3, 2019, 1:38:57 AM3/3/19
to
On Sat, 2 Mar 2019 17:05:59 -0500, Rod Pemberton <inv...@lkntrgzxc.com>
wrote:

> It may be harder to be a programmer when religious

That's atheist dogma Rod.

> as truth and logic are so important to the task.

God is truth.

Here's a new pamo.c with the fix. It required a longer format string,
but I found a way. Maybe Weiser should put me in his killfile for his
own peace of mind. He seems to have trouble maintaining it.


# include <stdio.h>
# include <stdlib.h>
# include <fcntl.h>
# include <io.h>

typedef unsigned char UCHAR;

# define MOS51 128880

long fz, loc, rvl;
int fd, rv;
char *fn = "$$mos.sys";
char fmt[] = "%lx:%*[ \t]%2[0-9A-Fa-f]%*[ \t]%2[0-9A-Fa-f]%*[ \t]%1[^\n]";
UCHAR data[2], old[4], new[4], *bold = &old[3], *bnew = &new[3];

void
main ()
{
if ((fd = open (fn, O_RDWR | O_BINARY | O_DENYALL)) == -1) {
fprintf (stderr, "failure opening %s: %s", fn, strerror (errno));
exit (1);
}

if ((fz = filelength (fd)) == -1) {
fprintf (stderr, "failure getting size of %s: %s", fn, strerror (errno));
exit (1);
}

if (fz != MOS51) {
fprintf (stderr, "wrong %s file, size must be %ld\n", fn, MOS51);
exit (1);
}

while ((rv = scanf (fmt, &loc, old, new, data)) != EOF) {

if (rv != 3) {
scanf ("%*[^\n]");
continue;
}

sscanf (old, "%x", bold);
sscanf (new, "%x", bnew);

if ((unsigned long) loc > MOS51 - 1) {
fprintf (stderr, "offset %lx exceeds file size\n", loc);
exit (1);
}
if ((rvl = lseek (fd, loc, SEEK_SET)) == -1L) {
fprintf (stderr, "failure seeking %s offset %lx: %s", fn, loc, strerror (errno));
exit (1);
}
if ((rv = read (fd, data, 1)) == -1) {
fprintf (stderr, "failure reading %s offset %lx: %s", fn, loc, strerror (errno));
exit (1);
}
if (*bold != data[0]) {
fprintf (stderr, "patch mismatch at %lx\n", loc);
exit (1);
}
printf ("%lx %x %x\n", loc, *bold, *bnew);

}

rewind (stdin);

while ((rv = scanf (fmt, &loc, old, new, data)) != EOF) {

if (rv != 3) {
scanf ("%*[^\n]");
continue;
}

sscanf (new, "%x", bnew);

if ((rvl = lseek (fd, loc, SEEK_SET)) == -1L) {
fprintf (stderr, "failure seeking %s offset %lx: %s", fn, loc, strerror (errno));
exit (1);
}
if ((rv = write (fd, bnew, 1)) == -1) {
fprintf (stderr, "failure writing %s offset %lx: %s", fn, loc, strerror (errno));
exit (1);
}

}

exit (0);
}

0 new messages