FreeCalypso > hg > freecalypso-tools
view ffstools/tiffs-rd/cat.c @ 465:003e48f8ebe1
rvinterf/etmsync/fsnew.c: cast 0 to (char *) for execl sentinel
I generally don't use NULL and use plain 0 instead, based on a "NULL
considered harmful" discussion on the classiccmp mailing list many aeons
ago (I couldn't find it, and I reason that it must have been 2005 or
earlier), but a recent complaint by a packager sent me searching, and I
found this:
https://ewontfix.com/11/
While I don't give a @#$% about "modern" systems and code-nazi tools,
I realized that passing a plain 0 as a pointer sentinel in execl is wrong
because it will break on systems where pointers are longer than the plain
int type. Again, I don't give a @#$% about the abomination of x86_64 and
the like, but if anyone ever manages to port my code to something like a
PDP-11 (16-bit int, 32-bit long and pointers), then passing a plain 0
as a function argument where a pointer is expected most definitely won't
work: if the most natural stack slot and SP alignment unit is 16 bits,
fitting an int, with longs and pointers taking up two such slots, then
the call stack will be totally wrong with a plain 0 passed for a pointer.
Casting the 0 to (char *) ought to be the most kosher solution for the
most retro systems possible.
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 11 Feb 2019 00:00:19 +0000 |
parents | e7502631a0f9 |
children | 1f27fc13eab7 |
line wrap: on
line source
/* * This C module implements the cat and catino commands. */ #include <sys/types.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include <unistd.h> #include "types.h" #include "struct.h" #include "globals.h" #include "pathname.h" int cat_mode; static u8 hex_buf[16]; static int hex_fill; static u32 hex_offset; cat_hex_flush() { int i, c; printf("%08X: ", hex_offset); for (i = 0; i < 16; i++) { if (i < hex_fill) printf("%02X ", hex_buf[i]); else fputs(" ", stdout); if (i == 7 || i == 15) putchar(' '); } for (i = 0; i < hex_fill; i++) { c = hex_buf[i]; if (c < ' ' || c > '~') c = '.'; putchar(c); } putchar('\n'); } cat_hex_byte(inb) { hex_buf[hex_fill++] = inb; if (hex_fill >= 16) { cat_hex_flush(); hex_offset += hex_fill; hex_fill = 0; } } void cat_chunk(ch) struct chunkinfo *ch; { u8 *p; int c; if (!ch->len) return; switch (cat_mode) { case 0: write(1, ch->start, ch->len); return; case 1: for (p = ch->start; p < ch->end; p++) { c = *p; if (c >= ' ' && c <= '~' || c == '\n') putchar(c); else { if (c & 0x80) { putchar('M'); putchar('-'); c &= 0x7F; } putchar('^'); if (c == 0x7F) putchar('?'); else putchar(c + '@'); } } return; case 2: for (p = ch->start; p < ch->end; p++) cat_hex_byte(*p); return; } } void cat_finish() { switch (cat_mode) { case 1: putchar('\n'); return; case 2: if (hex_fill) cat_hex_flush(); return; } } static void segment_cat_callback(inf, opaque) struct inode_info *inf; u_long opaque; { struct chunkinfo chi; size_extra_chunk(inf, &chi); cat_chunk(&chi); } cmd_cat(argc, argv) char **argv; { extern int optind; int c, headino; struct inode_info *inf; struct chunkinfo chi; optind = 0; while ((c = getopt(argc, argv, "hv")) != EOF) switch (c) { case 'h': cat_mode = 2; continue; case 'v': cat_mode = 1; continue; default: usage: fprintf(stderr, "usage: cat [-v|-h] pathname\n"); exit(1); } if (argc != optind + 1) goto usage; read_ffs_image(); find_inode_block(); alloc_inode_table(); find_root_inode(); headino = find_pathname(argv[optind]); inf = inode_info[headino]; switch (inf->type) { case 0xE1: case 0xF1: break; case 0xF2: fprintf(stderr, "error: the requested object is a directory\n"); exit(1); case 0xF3: fprintf(stderr, "error: the requested object is a symlink; use readlink instead of cat\n"); exit(1); default: fprintf(stderr, "error: unexpected object type %02X\n", inf->type); exit(1); } size_head_chunk(inf, &chi); cat_chunk(&chi); iterate_seg_file(headino, segment_cat_callback, 0L, 0, 0); cat_finish(); exit(0); } cmd_catino(argc, argv) char **argv; { extern int optind; int c, headino; struct inode_info *inf; struct chunkinfo chi; optind = 0; while ((c = getopt(argc, argv, "hv")) != EOF) switch (c) { case 'h': cat_mode = 2; continue; case 'v': cat_mode = 1; continue; default: usage: fprintf(stderr, "usage: catino [-v|-h] ino\n"); exit(1); } if (argc != optind + 1) goto usage; headino = strtoul(argv[optind], 0, 16); read_ffs_image(); find_inode_block(); alloc_inode_table(); if (!validate_inode(headino)) { fprintf(stderr, "catino: specified inode number is invalid\n"); exit(1); } inf = inode_info[headino]; switch (inf->type) { case 0x00: case 0xE1: case 0xF1: case 0xF3: break; case 0xF2: fprintf(stderr, "error: the requested object is a directory\n"); exit(1); default: fprintf(stderr, "error: unexpected object type %02X\n", inf->type); exit(1); } if (!inf->len) { fprintf(stderr, "error: requested inode has been reclaimed\n"); exit(1); } if (!validate_obj_name(headino, 0)) { fprintf(stderr, "error: no valid name at the beginning of the requested seghead chunk\n"); exit(1); } size_head_chunk(inf, &chi); cat_chunk(&chi); iterate_seg_file(headino, segment_cat_callback, 0L, !inf->type, 0); cat_finish(); exit(0); }