/* This function allows input to be read from a file or the keyboard. On the SUN, an extra getch() must be done to capture the return - otherwise all options are executed twice. */ char get_option(void) { int c; static int save_c; if (input) return(*(input++)); c = os_getch(); #if FF_OS == FF_OS_UNIX /* If c is return, return it If not, then the next character is either a return or another character If it is a return, get it and ignore it If it is a character, put it back so that it can be gotten */ if (c != EOL_MARKER) { save_c = c; if ((c = os_getch()) != EOL_MARKER) ungetc(c, stdin); c = save_c; } #endif return((char)c); }
/* os_strings: List the strings of a binary and * check if the regex given is there. */ int os_string(char *file, char *regex) { int ch, cnt; unsigned char *C; unsigned char *bfr; char line[OS_SIZE_1024 +1]; char *buf; EXEC *head; os_strings oss; /* Return didn't match */ if(!file || !regex) { return(0); } /* Allocating for the buffer */ bfr = calloc(STR_MINLEN + 2, sizeof(char *)); if (!bfr) { merror(MEM_ERROR, ARGV0); return(0); } /* Opening the file */ oss.fp = fopen(file, "r"); if(!oss.fp) { free(bfr); return(0); } /* cleaning the line */ memset(line, '\0', OS_SIZE_1024 +1); /* starting .. (from old strings.c) */ oss.foff = 0; oss.head_len = 0; oss.read_len = -1; head = (EXEC *)oss.hbfr; if ((oss.head_len = read(fileno(oss.fp), head, sizeof(EXEC))) == -1) { oss.head_len = 0; oss.read_len = -1; } else if (oss.head_len == sizeof(EXEC) && !N_BADMAG(*head)) { oss.foff = N_TXTOFF(*head); if (fseek(stdin, oss.foff, SEEK_SET) == -1) { oss.read_len = -1; } else { #ifdef AIX oss.read_len = head->tsize + head->dsize; #else oss.read_len = head->a_text + head->a_data; #endif } oss.head_len = 0; } else { oss.hcnt = 0; } /* Read the file and perform the regex comparison */ for (cnt = 0; (ch = os_getch(&oss)) != EOF;) { if (ISSTR(ch)) { if (!cnt) C = bfr; *C++ = ch; if (++cnt < STR_MINLEN) continue; strncpy(line, (char *)bfr, STR_MINLEN +1); buf = line; buf+=strlen(line); while ((ch = os_getch(&oss)) != EOF && ISSTR(ch)) { if(cnt < OS_SIZE_1024) { *buf = (char)ch; buf++; } else { *buf = '\0'; break; } cnt++; } *buf = '\0'; if(OS_PRegex(line, regex)) { if(oss.fp) fclose(oss.fp); free(bfr); return(1); } } cnt = 0; } if(oss.fp) fclose(oss.fp); free(bfr); return(0); }
int main(int argc, char **argv) { double double_var; char *input_buffer = NULL; #if 0 unsigned char input_tty = 1; #endif int data_file; int option; int last_option; int last_u_option; int flip_bytes = 0; /* long offset; */ /* long record_number; */ long file_position; long data_file_length; long ops_file_length = 0; long new_position; /* long long_var; */ fprintf(stderr, "%s", #ifdef FF_ALPHA "\nWelcome to readfile alpha 1.1 "__DATE__" -- an NGDC binary file reader\n" #elif defined(FF_BETA) "\nWelcome to readfile beta 1.1 "__DATE__" -- an NGDC binary file reader\n" #else "\nWelcome to readfile release 1.1 -- an NGDC binary file reader\n" #endif ); if (argc == 1) { fprintf(stderr, "%s", "\nUSAGE: readfile binary_file\n"); exit(1); } show_opt(); #if FF_OS == FF_OS_DOS data_file = open(argv[1], O_RDONLY | O_BINARY); #endif #if FF_OS == FF_OS_UNIX data_file = open(argv[1], O_RDONLY); #endif if(data_file == -1) { fprintf(stderr,"Could Not Open Input File %s\n", argv[1]); exit(0); } { FILE *fp = fopen(argv[1], "r"); data_file_length = -1; if (fp) { if (!fseek(fp, 0, SEEK_END)) data_file_length = ftell(fp); fclose(fp); } } /* Determine if the input is coming from a file, if so read the file in */ if (!isatty(fileno(stdin))) { ops_file_length = lseek(fileno(stdin), 0L, SEEK_END); input_buffer = (char *)malloc((size_t)ops_file_length + 1); if (!input_buffer) { fprintf(stderr, "Insufficient memory -- file is too large"); exit(1); } lseek(fileno(stdin), 0L, SEEK_SET); read(fileno(stdin), input_buffer, (size_t)ops_file_length); *(input_buffer + ops_file_length) = '\0'; input = input_buffer; } last_u_option = ' '; last_option = ' '; while ((option = get_option()) != 'q') { if (option == UINT_PREFIX) last_u_option = ' '; if (option == EOL_MARKER) option = last_option; switch(option) { case TEXT_CODE: if (check_error(read(data_file, (char *)&double_var, sizeof(char)), sizeof(char))) break; fprintf(stdout, "text: \'%c\', dec: %3d, hex: %2x, oct: %3o\n", *(char *)&double_var, *(unsigned char *)&double_var, *(unsigned char *)&double_var, *(unsigned char *)&double_var); break; case INT8_CODE: if (check_error(read(data_file, (char *)&double_var, SIZE_INT8), SIZE_INT8)) break; fprintf(stdout, "int8: "); fprintf(stdout, fft_cnv_flags_width[FFNT_INT8], INT8_WIDTH, *((int8 *)&double_var)); fprintf(stdout, "\n"); break; case INT16_CODE: if (check_error(read(data_file, (char *)&double_var, SIZE_INT16), SIZE_INT16)) break; if (flip_bytes) { flip_2_bytes(&double_var); fprintf(stdout,"int16, byte swapped: "); fprintf(stdout, fft_cnv_flags_width[FFNT_INT16], INT16_WIDTH, *((int16 *)&double_var)); flip_2_bytes(&double_var); fprintf(stdout,", (\"%s-endian\": ", endian() == LITTLE_ENDIAN ? "little" : "big"); fprintf(stdout, fft_cnv_flags_width[FFNT_INT16], INT16_WIDTH, *((int16 *)&double_var)); fprintf(stdout, ")\n"); } else { fprintf(stdout, "int16: "); fprintf(stdout, fft_cnv_flags_width[FFNT_INT16], INT16_WIDTH, *((int16 *)&double_var)); fprintf(stdout, "\n"); } break; case INT32_CODE: if (check_error(read(data_file, (char *)&double_var, SIZE_INT32), SIZE_INT32)) break; if (flip_bytes) { flip_4_bytes(&double_var); fprintf(stdout,"int32, byte swapped: "); fprintf(stdout, fft_cnv_flags_width[FFNT_INT32], INT32_WIDTH, *((int32 *)&double_var)); flip_4_bytes(&double_var); fprintf(stdout,", (\"%s-endian\": ", endian() == LITTLE_ENDIAN ? "little" : "big"); fprintf(stdout, fft_cnv_flags_width[FFNT_INT32], INT32_WIDTH, *((int32 *)&double_var)); fprintf(stdout, ")\n"); } else { fprintf(stdout, "int32: "); fprintf(stdout, fft_cnv_flags_width[FFNT_INT32], INT32_WIDTH, *((int32 *)&double_var)); fprintf(stdout, "\n"); } break; #ifdef LONGS_ARE_64 case INT64_CODE: if (check_error(read(data_file, (char *)&double_var, SIZE_INT64), SIZE_INT64)) break; if (flip_bytes) { flip_8_bytes(&double_var); fprintf(stdout,"int64, byte swapped: "); fprintf(stdout, fft_cnv_flags_width[FFNT_INT64], INT64_WIDTH, *((int64 *)&double_var)); flip_8_bytes(&double_var); fprintf(stdout,", (\"%s-endian\": ", endian() == LITTLE_ENDIAN ? "little" : "big"); fprintf(stdout, fft_cnv_flags_width[FFNT_INT64], INT64_WIDTH, *((int64 *)&double_var)); fprintf(stdout, ")\n"); } else { fprintf(stdout, "int64: "); fprintf(stdout, fft_cnv_flags_width[FFNT_INT64], INT64_WIDTH, *((int64 *)&double_var)); fprintf(stdout, "\n"); } break; #endif case FLOAT32_CODE: if (check_error(read(data_file,(char *)&double_var, SIZE_FLOAT32), SIZE_FLOAT32)) break; if (flip_bytes) { flip_4_bytes(&double_var); fprintf(stdout, "float32, byte swapped: "); fprintf(stdout, fft_cnv_flags_width_prec[FFNT_FLOAT32], FLOAT32_WIDTH, FLOAT32_PREC, *((float32 *)&double_var)); flip_4_bytes(&double_var); fprintf(stdout,", (\"%s-endian\": ", endian() == LITTLE_ENDIAN ? "little" : "big"); fprintf(stdout, fft_cnv_flags_width_prec[FFNT_FLOAT32], FLOAT32_WIDTH, FLOAT32_PREC, *((float32 *)&double_var)); fprintf(stdout, ")\n"); } else { fprintf(stdout, "float32: "); fprintf(stdout, fft_cnv_flags_width_prec[FFNT_FLOAT32], FLOAT32_WIDTH, FLOAT32_PREC, *((float32 *)&double_var)); fprintf(stdout, "\n"); } break; case FLOAT64_CODE: if (check_error(read(data_file,(char *)&double_var, SIZE_FLOAT64), SIZE_FLOAT64)) break; if (flip_bytes) { flip_8_bytes(&double_var); fprintf(stdout, "float64, byte swapped: "); fprintf(stdout, fft_cnv_flags_width_prec[FFNT_FLOAT64], FLOAT64_WIDTH, FLOAT64_PREC, *((float64 *)&double_var)); flip_8_bytes(&double_var); fprintf(stdout,", (\"%s-endian\": ", endian() == LITTLE_ENDIAN ? "little" : "big"); fprintf(stdout, fft_cnv_flags_width_prec[FFNT_FLOAT64], FLOAT64_WIDTH, FLOAT64_PREC, *((float64 *)&double_var)); fprintf(stdout, ")\n"); } else { fprintf(stdout, "float64: "); fprintf(stdout, fft_cnv_flags_width_prec[FFNT_FLOAT64], FLOAT64_WIDTH, FLOAT64_PREC, *((float64 *)&double_var)); fprintf(stdout, "\n"); } break; case UINT_PREFIX: /* unsigned ... */ /* If last option is not yet defined, prompt the user */ if (last_u_option == ' ') { last_u_option = get_option(); } switch (last_u_option) { case INT8_CODE: /* unsigned char */ if (check_error(read(data_file, (char *)&double_var, SIZE_UINT8), SIZE_UINT8)) break; fprintf(stdout, "uint8: "); fprintf(stdout, fft_cnv_flags_width[FFNT_UINT8], UINT8_WIDTH, *((uint8 *)&double_var)); fprintf(stdout, "\n"); break; case INT16_CODE: if (check_error(read(data_file, (char *)&double_var, SIZE_UINT16), SIZE_UINT16)) break; if (flip_bytes) { flip_2_bytes(&double_var); fprintf(stdout,"uint16, byte swapped: "); fprintf(stdout, fft_cnv_flags_width[FFNT_UINT16], UINT16_WIDTH, *((uint16 *)&double_var)); flip_2_bytes(&double_var); fprintf(stdout,", (\"%s-endian\": ", endian() == LITTLE_ENDIAN ? "little" : "big"); fprintf(stdout, fft_cnv_flags_width[FFNT_UINT16], UINT16_WIDTH, *((uint16 *)&double_var)); fprintf(stdout, ")\n"); } else { fprintf(stdout, "uint16: "); fprintf(stdout, fft_cnv_flags_width[FFNT_UINT16], UINT16_WIDTH, *((uint16 *)&double_var)); fprintf(stdout, "\n"); } break; case INT32_CODE: if (check_error(read(data_file, (char *)&double_var, SIZE_UINT32), SIZE_UINT32)) break; if (flip_bytes) { flip_4_bytes(&double_var); fprintf(stdout,"uint32, byte swapped: "); fprintf(stdout, fft_cnv_flags_width[FFNT_UINT32], UINT32_WIDTH, *((uint32 *)&double_var)); flip_4_bytes(&double_var); fprintf(stdout,", (\"%s-endian\": ", endian() == LITTLE_ENDIAN ? "little" : "big"); fprintf(stdout, fft_cnv_flags_width[FFNT_UINT32], UINT32_WIDTH, *((uint32 *)&double_var)); fprintf(stdout, ")\n"); } else { fprintf(stdout, "uint32: "); fprintf(stdout, fft_cnv_flags_width[FFNT_UINT32], UINT32_WIDTH, *((uint32 *)&double_var)); fprintf(stdout, "\n"); } break; #ifdef LONGS_ARE_64 case INT64_CODE: if (check_error(read(data_file, (char *)&double_var, SIZE_UINT64), SIZE_UINT64)) break; if (flip_bytes) { flip_8_bytes(&double_var); fprintf(stdout,"uint64, byte swapped: "); fprintf(stdout, fft_cnv_flags_width[FFNT_UINT64], UINT64_WIDTH, *((uint64 *)&double_var)); flip_8_bytes(&double_var); fprintf(stdout,", (\"%s-endian\": ", endian() == LITTLE_ENDIAN ? "little" : "big"); fprintf(stdout, fft_cnv_flags_width[FFNT_UINT64], UINT64_WIDTH, *((uint64 *)&double_var)); fprintf(stdout, ")\n"); } else { fprintf(stdout, "uint64: "); fprintf(stdout, fft_cnv_flags_width[FFNT_UINT64], UINT64_WIDTH, *((uint64 *)&double_var)); fprintf(stdout, "\n"); } break; #endif default: fprintf(stdout,"Type 'h' or '?' to see options menu.\n"); last_u_option = ' '; break; }/* end switch on unsigned types */ break; case 'b': /* toggle between little-endian and big-endian */ flip_bytes = (flip_bytes) ? 0 : 1; if (flip_bytes) { fprintf(stderr, "Displaying numbers with byte swapping\n"); } else { /* Say what the native byte order IS */ fprintf(stderr, "Displaying numbers using your machine's native byte order\n"); } break; case 'p': /* position file, don't show file size */ if (input) { input = strtok(input, " "); /* Check for errors */ if (!input) { fprintf(stderr, "Missing blank space after number."); exit(1); } if ((input - input_buffer) >= ops_file_length) { fprintf(stderr, "Reading past end of input file."); exit(1); } sscanf(input, "%ld", &new_position); input += strlen(input) + 1; } else { fprintf(stdout,"Input New File Position in Bytes "); scanf("%ld", &new_position); #if FF_OS == FF_OS_UNIX os_getch(); #endif } if (new_position > data_file_length) { fprintf(stderr,"ERROR: Position not changed.\n"); fprintf(stderr," New position cannot be beyond %ld, the size of %s.\n", data_file_length, argv[1]); break; } file_position = lseek(data_file, new_position, SEEK_SET); fprintf(stdout,"New File Position = %ld\n", file_position); new_position = 0; break; case 'P': /* show file position and size */ file_position = lseek(data_file, 0L, SEEK_CUR); fprintf(stdout,"File Position: %ld\tFile Length: %ld\n", file_position, data_file_length); break; case 'h': case '?': show_opt(); break; default: fprintf(stdout,"Type 'h' or '?' to see options menu.\n"); } /* end switch on all types */ last_option = option; if (last_option != 'u') last_u_option = ' '; }/* end while loop */ return 0; }/* end readfile */