ftp.nice.ch/pub/next/developer/languages/lisp/AKCL.1.599.s.tar.gz#/akcl-1-599/c/seekNeXT.c

This is seekNeXT.c in view mode; [Download] [Up]

/*
 *	seekNeXT.c:	SEEK_TO_END_OFILE(fp) for NeXT computers
 *
 *		by Noritake Yonezawa, April 26 1991
 */

#define MACH_O
#undef A_OUT

#include <stdio.h>
#include <ar.h>
#include <nlist.h>
#include <sys/types.h>
#include <sys/file.h>

#if !defined(A_OUT) && !defined(MACH_O)
#define A_OUT
#endif

#ifdef A_OUT
#include <a.out.h>
#endif

#ifdef MACH_O
#include <sys/loader.h>
#endif

#include <strings.h>

char               *malloc();

#ifdef MAIN
void
main(int argc,
     char **argv)
{
    char                command[50];
    FILE               *fp;
    char                c;

    if (argc != 2) {
	fprintf(stderr, "Usage: %s file\n", argv[0]);
	exit(1);
    }
    if ((fp = fopen(argv[1], "r")) == NULL) {
	perror(argv[1]);
	exit(1);
    }
    if (!seek_to_end_ofile(fp)) {
	fclose(fp);
	fprintf(stderr, "something wrong\n");
	exit(1);
    }
    while ((c = fgetc(fp)) != EOF)
	putchar(c);
    close(fp);
    sprintf(command, "/bin/ls -l %s", argv[1]);
    system(command);
}

#endif

int
seek_to_end_ofile(FILE * fp)
{
#ifdef A_OUT
    {
	struct exec         hdr;
	int                 i;

	fseek(fp, 0L, 0);
#ifdef HEADER_SEEK_FD
    /*
     * Skip the headers that encapsulate our data in some other format such
     * as COFF.  
     */
	HEADER_SEEK_FD(desc);
#endif
	if (fread(&hdr, sizeof(struct exec), 1, fp) && !N_BADMAG(hdr)) {
#ifdef MAIN
	    fprintf(stderr, "Hmm... it looks an a.out format\n");
#endif
	    fseek(fp, hdr.a_text + hdr.a_data +
		  hdr.a_syms + hdr.a_trsize + hdr.a_drsize, 1);
	    fread(&i, sizeof(i), 1, fp);
	    fseek(fp, i - sizeof(i), 1);
#ifdef MAIN
	    fprintf(stderr, "ftell     = %d\n", ftell(fp));
#endif
	    return 1;
	}
    }
#endif				/* A_OUT */

#ifdef MACH_O
    {
	struct mach_header  mach_header;
	char               *hdrbuf;
	struct load_command *load_command;
	struct segment_command *segment_command;
	struct section     *section;
	struct symtab_command *symtab_command;
	struct symseg_command *symseg_command;
	int                 len, cmd, seg;
	int                 end_sec, end_ofile;

	end_ofile = 0;
	fseek(fp, 0L, 0);
	len = fread((char *)&mach_header, sizeof(struct mach_header), 1, fp);
	if (len == 1 && mach_header.magic == MH_MAGIC) {
#ifdef MAIN
	    fprintf(stderr, "Hmm... it looks a Mach-O format\n");
#endif
	    hdrbuf = malloc(mach_header.sizeofcmds);
	    len = fread(hdrbuf, mach_header.sizeofcmds, 1, fp);
	    if (len != 1) {
		fprintf(stderr, "failure reading Mach-O load commands\n");
		return 0;
	    }
	    load_command = (struct load_command *) hdrbuf;
	    for (cmd = 0; cmd < mach_header.ncmds; ++cmd) {
		switch (load_command->cmd) {
		case LC_SEGMENT:
		    segment_command = (struct segment_command *) load_command;
		    section = (struct section *) ((char *)(segment_command + 1));
		    for (seg = 0; seg < segment_command->nsects; ++seg, ++section) {
			end_sec = section->offset + section->size;
			if (end_sec > end_ofile)
			    end_ofile = end_sec;
		    }
		    break;
		case LC_SYMTAB:
		    symtab_command = (struct symtab_command *) load_command;
		    end_sec = symtab_command->symoff + symtab_command->nsyms * sizeof(struct nlist);
		    if (end_sec > end_ofile)
			end_ofile = end_sec;
		    end_sec = symtab_command->stroff + symtab_command->strsize;
		    if (end_sec > end_ofile)
			end_ofile = end_sec;
		    break;
		case LC_SYMSEG:
		    symseg_command = (struct symseg_command *) load_command;
		    end_sec = symseg_command->offset + symseg_command->size;
		    if (end_sec > end_ofile)
			end_ofile = end_sec;
		    break;
		}
		load_command = (struct load_command *)
		  ((char *)load_command + load_command->cmdsize);
	    }
	    free(hdrbuf);
	    fseek(fp, end_ofile, 0);
#ifdef MAIN
	    fprintf(stderr, "end_ofile = %u\n", end_ofile);
	    fprintf(stderr, "ftell     = %d\n", ftell(fp));
#endif
	    return 1;
	}
    }
#endif				/* MACH_O */
    return 0;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.