FreeCalypso > hg > freecalypso-tools
view ffstools/tiffs-rd/inode.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 |
line wrap: on
line source
/* * This C module implements the reading and decoding of inode information. */ #include <sys/types.h> #include <endian.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include "types.h" #include "struct.h" #include "globals.h" u8 blank_flash_line[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; alloc_inode_table() { inode_info = malloc(sizeof(struct inode_info *) * inode_limit); if (!inode_info) { perror("malloc of inode table"); exit(1); } bzero(inode_info, sizeof(struct inode_info *) * inode_limit); } static int convert_ptr(in, infp) int in; int *infp; { if (in == 0xFFFF) { *infp = 0; return(0); } if (in < 1 || in >= inode_limit) return(-1); *infp = in; return(1); } validate_inode(ino) { struct inode_flash *fl; struct inode_info *inf; if (ino < 1 || ino >= inode_limit) return(0); if (inode_info[ino]) return(1); fl = (struct inode_flash *)inode_block + ino; if (!bcmp(fl, blank_flash_line, sizeof blank_flash_line)) return(0); inf = malloc(sizeof(struct inode_info)); if (!inf) { perror("malloc of struct inode_info"); exit(1); } bzero(inf, sizeof(struct inode_info)); inf->ino = ino; inf->len = le16toh(fl->len); if (inf->len & 0xF) { fprintf(stderr, "warning: inode #%x: invalid length, skipping\n", ino); free(inf); return(0); } inf->type = fl->type; switch (inf->type) { case 0x00: break; case 0xE1: case 0xF1: case 0xF2: case 0xF3: case 0xF4: if (!inf->len) { fprintf(stderr, "warning: inode #%x: non-deleted object has zero length, skipping\n", ino); free(inf); return(0); } break; default: fprintf(stderr, "warning: inode #%x: unexpected object type %02X, skipping\n", ino, inf->type); free(inf); return(0); } if (convert_ptr(le16toh(fl->descend), &inf->descend) < 0) { fprintf(stderr, "warning: inode #%x: invalid descend pointer, skipping\n", ino); free(inf); return(0); } if (convert_ptr(le16toh(fl->sibling), &inf->sibling) < 0) { fprintf(stderr, "warning: inode #%x: invalid sibling pointer, skipping\n", ino); free(inf); return(0); } if (inf->len) { inf->rawloc = le32toh(fl->dataptr); if (old_16bit_location) inf->rawloc >>= 16; if (inf->rawloc > 0x0FFFFFFF) { invdptr: fprintf(stderr, "warning: inode #%x: invalid data pointer, skipping\n", ino); free(inf); return(0); } inf->offset = inf->rawloc << 4; if (inf->offset >= total_ffs_size) goto invdptr; if (inf->offset + inf->len > total_ffs_size) { fprintf(stderr, "warning: inode #%x: data pointer + length > FFS total size, skipping\n", ino); free(inf); return(0); } inf->dataptr = image + inf->offset; } inode_info[ino] = inf; return(1); } find_root_inode() { int ino; if (root_inode) { if (!validate_inode(root_inode)) { fprintf(stderr, "error: root inode specified with -r is invalid\n"); exit(1); } return(1); } for (ino = 1; ino < inode_limit; ino++) { if (!validate_inode(ino)) continue; if (inode_info[ino]->type != 0xF2) continue; if (*inode_info[ino]->dataptr != '/') continue; root_inode = ino; if (verbose) fprintf(stderr, "Found root inode at #%x\n", ino); if (inode_info[ino]->sibling) fprintf(stderr, "warning: root inode #%x has a non-null sibling pointer\n", ino); return(0); } fprintf(stderr, "error: no root inode found; try -r\n"); exit(1); }