dump.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 88 行
C
88 行
#include <stdlib.h>#include <stdio.h>#include <string.h>int main(int argc, char* argv[]){ FILE *fi; /* Stream pointer of file to dump.*/ int aux; /* Integer representation of the last character read.*/ long start_pos; /* Position of the first byte to write to stdout.*/ long end_pos; /* Position of last byte to write to stdout.*/ long file_size; /* Size in bytes of the input file.*/ int change_endian = 0; /* 0 leaves original byte order intact, 1 changes endian.*/ /* Determine if the byte order needs to be changed.*/ if (argc > 2 && strcmp(argv[2], "-ch32") == 0) change_endian = 1; switch (argc-change_endian) { case 2: /* Dump whole file */ start_pos = 0; end_pos = -1; break; case 3: /* Dump from postion argv[2] till end of file*/ start_pos = atol(argv[2+change_endian]); end_pos = -1; break; case 4: /* Dump from argv[2] till argv[3] */ case 5: /* Dump from argv[2] till argv[3] */ start_pos = atol(argv[2+change_endian]); end_pos = atol(argv[3+change_endian]); break; default: if (argc != 1) fprintf(stderr, "ERROR! Invalid usage!\n\n"); fprintf(stderr, "Description: dump echoes a portion of a file to stdout.\n"); fprintf(stderr, "Syntax: dump filename [options] [first_byte_pos [last_byte_pos [fill_char]]]\n"); fprintf(stderr, " options : -ch32 changes byte order little<->big endian\n"); fprintf(stderr, " options : for 4-byte blocks.\n"); fprintf(stderr, " first_byte_pos : file_size_in_bytes-1 is allowed.\n"); fprintf(stderr, " last_byte_pos : file_size_in_bytes-1 if absent\n"); fprintf(stderr, " fill_char : character that should fill the gap between\n"); fprintf(stderr, " last_byte_pos and file_size_in_bytes-1\n"); fprintf(stderr, " Escape character (\\) + number allowed\n"); fprintf(stderr, " e.g.: For '\\n' use '\\10'\n"); exit(0); }; /* Verify that the file exists.*/ fi = fopen(argv[1],"rb"); if (fi == NULL) { fprintf(stderr, "ERROR! Could not open input file %s.\n", argv[1]); exit(0); }; /* Set end_pos and file_size.*/ fseek(fi, 0L, SEEK_END); file_size = ftell(fi); if (end_pos == -1) end_pos = file_size - 1; /* Verify that begin_pos and end_pos are valid and correct if possible.*/ if (start_pos>end_pos) return 0; /* Nothing to do.*/ if (start_pos<0 || end_pos<0) return 0; /* Not allowed.*/ /* Dump part of the file if necessary.*/ if (start_pos<file_size) { /* Part of the file needs to be dumped. So do it.*/ fseek(fi, start_pos, SEEK_SET); while (ftell(fi) <= end_pos) { aux = fgetc(fi); if (aux == EOF) break; fputc(aux, stdout); }; }; /* Check if we need to dump filler bytes.*/ if (argc-change_endian == 5 && end_pos>file_size-1) { /* The user has defined a filler byte and there are bytes*/ /* to be filled. Determine how many.*/ long fill_count; /* Number of bytes to fill.*/ int filler; /* Character to use as a filler.*/ fill_count = (start_pos<file_size? end_pos-file_size+1: end_pos-start_pos+1); if (*argv[4+change_endian] == '\\') filler = atoi(argv[4]+1); else filler = (int)*argv[4+change_endian]; while (fill_count-- > 0) fprintf(stdout, "%c", filler); }; return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?