home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume5 / xdump < prev    next >
Encoding:
Text File  |  1989-02-03  |  6.8 KB  |  290 lines

  1. Path: xanth!mcnc!gatech!cwjcc!hal!ncoast!allbery
  2. From: bengsig@orcenl.UUCP (Bjorn Engsig)
  3. Newsgroups: comp.sources.misc
  4. Subject: v05i057: xdump, hex/char dump of file or shared memory
  5. Message-ID: <8811211802.AA00294@mcvax.cwi.nl>
  6. Date: 26 Nov 88 04:40:11 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: bengsig@orcenl.UUCP (Bjorn Engsig)
  9. Lines: 278
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. Posting-number: Volume 5, Issue 57
  13. Submitted-by: "Bjorn Engsig" <bengsig@orcenl.UUCP>
  14. Archive-name: xdump
  15.  
  16. # This is a shell archive
  17. #
  18. # file: README
  19. #
  20. if test -f README; then echo shar: README exists; else
  21. sed 's/^X//' << EOF_EOF > README
  22. Xxdump is a utility to give hexadecimal dumps of a file or a
  23. Xshared memory segment.
  24. X
  25. Xxdump may also be used to copy a shared memory segment to a file.
  26. X
  27. XThere is no man-page, but a describing comment in the code.
  28. X
  29. XIt will give an output like:
  30. X
  31. X     0  78 64 75 6d 70 20 69 73 20 61 20 75 74 69 6c 69   xdump is a utili
  32. X    10  74 79 20 74 6f 20 67 69 76 65 20 68 65 78 61 64   ty to give hexad
  33. X    20  65 63 69 6d 61 6c 20 64 75 6d 70 73 20 6f 66 20   ecimal dumps of 
  34. X    30  61 20 66 69 6c 65 20 6f 72 20 61 0a 73 68 61 72   a file or a.shar
  35. X    40  65 64 20 6d 65 6d 6f 72 79 20 73 65 67 6d 65 6e   ed memory segmen
  36. X
  37. Xwith the dump shown as a hexpart and a charpart.  The charpart shows all
  38. Xnon-printable characters as '.'
  39. X
  40. XIt compiles and runs on System V, without any Makefile, just using the
  41. Xdefault make rules.
  42. X
  43. XYou need to remove the define ONLY7BITS, if you want the charpart to include
  44. Xprintable 8-bit characters
  45. EOF_EOF
  46. if test `wc -c <README` -ne     920; then echo shar: README has bad length
  47. else echo shar: README extracted; fi
  48. fi
  49. # file: xdump.c
  50. #
  51. if test -f xdump.c; then echo shar: xdump.c exists; else
  52. sed 's/^X//' << EOF_EOF > xdump.c
  53. X/* xdump.c - make hex dumps */            char usage[]=
  54. X
  55. X"Usage: %s [-f file] [-m shmid] [-M shmkey] [-b list] [-cxo]\n";  /*
  56. X
  57. X
  58. X-f file        : Dump file
  59. X-m shmid    : Dump shared memory segment, given id
  60. X-M shmkey    : Dump shared memory segment, given key
  61. X-b list        : Dump only bytes within list
  62. X-c        : Don't show char part
  63. X-x        : Don't show hex part
  64. X-o        : Copy input (file or sh. mem. segment)
  65. X          to stdout. Dump goes to stderr.
  66. X<stdin>          is taken as input if no -f, -m or -M option is present.
  67. X
  68. Xxdump -oxcM shmkey > file    : These copies a sh. mem. segment
  69. Xxdump -oxcm shmid  > file    : to a file without any printed output
  70. X
  71. Xlist could be -b0x10-0x30,0x50
  72. X       or -b0-256
  73. X       or -b0100-0200
  74. Xand is used to constraint the dump to a range of the file or shmem.
  75. X
  76. Xxdump [options] file is equivalent to xdump [options] -f file.
  77. X
  78. XThe algorith used to dump ranges of the input is not very smart.
  79. X
  80. XThis program is copyrighted by Bjorn Engsig.  Permission is hereby granted
  81. Xto copy, redistribute and use the program, provided only handling fees
  82. Xare charged, and provided this copyright notice is included.
  83. X
  84. XBjorn Engsig, 15.11.88
  85. X*/
  86. X#define ONLY7BIT /* remove this for (simple) 8 bit support,
  87. X            see also Isprint below */
  88. X
  89. X#include <stdio.h>
  90. X#include <sys/types.h>
  91. X#include <sys/ipc.h>
  92. X#include <sys/shm.h>
  93. X#include <signal.h>
  94. X#include <ctype.h>
  95. X#include <fcntl.h>
  96. X
  97. X#ifdef ONLY7BIT
  98. X#define Isprint(c) isprint(c)
  99. X#else
  100. X#define Isprint(c) (isprint(c&0x7f))
  101. X#endif ONLY7BIT
  102. X
  103. Xchar optstring[] = "b:f:M:m:cxo";
  104. Xchar input, *name, *list;
  105. Xunsigned char *line;
  106. Xint nochar, nohex, swchar, swhex, pipeit;
  107. Xint infile,byteno,bflag;
  108. Xunsigned char  *address;
  109. Xextern char *optarg;
  110. Xextern int optind, opterr;
  111. X
  112. Xexithandle()
  113. X{ 
  114. X  exit(0);
  115. X}
  116. X
  117. Xopeninput()
  118. X{
  119. Xint shmid;
  120. X  switch (input) {
  121. X    case 'f':
  122. X      if ((infile=open(name,O_RDONLY))== -1) {
  123. X    perror("xdump: Cannot open file");
  124. X    exit(2);
  125. X      }
  126. X      break;
  127. X    case 'M':
  128. X      if ((shmid = shmget( strtol(name, (char **)0, 0), 0)) == -1) {
  129. X    perror("xdump: Cannot get shmid");
  130. X    exit(2);
  131. X      }
  132. X      goto Doshm;
  133. X      break;
  134. X    case 'm':
  135. X      shmid = strtol(name, (char **)0, 0);
  136. X    Doshm:
  137. X      if ((address = (unsigned char *)shmat(shmid,0,SHM_RDONLY))== (unsigned char *) -1) {
  138. X    perror("xdump: Cannot attach to shared memory");
  139. X    exit(2);
  140. X      }
  141. X      signal(SIGBUS,exithandle);
  142. X      signal(SIGSEGV,exithandle);
  143. X      break;
  144. X    case 0: /* stdin */
  145. X      input='f';
  146. X      infile=0;
  147. X      break;
  148. X    }
  149. X}
  150. X
  151. Xgetline()
  152. X{
  153. X  static unsigned char buf[18];
  154. X  switch(input) {
  155. X    case 'f':
  156. X      line=buf;
  157. X      return read(infile,buf,16);
  158. X    break;
  159. X    case 'm':
  160. X    case 'M':
  161. X      line=(address+=16);
  162. X      return 16;
  163. X    break;
  164. X  }
  165. X}
  166. X
  167. Xinrange(bn)
  168. X  int bn;
  169. X{
  170. X  char *end;
  171. X  static int lower= -1,upper= -1;
  172. X  if (bn<lower)
  173. X    return 0;
  174. X  if (bn>=lower && bn<=upper)
  175. X    return 1;
  176. X  if (*list) {
  177. X    lower=strtol(list,&end,0);
  178. X    list=end+1;
  179. X    if (*end==',' || *end==(char) 0) {
  180. X      upper=lower;
  181. X    } else
  182. X    if (*end=='-') {
  183. X      upper=strtol(list,&end,0);
  184. X      list=end+(!! *end);
  185. X    }
  186. X    lower &= 0xfffffff0;
  187. X    upper &= 0xfffffff0;
  188. X    if (bn>=lower && bn<=upper)
  189. X      return 1;
  190. X  } else
  191. X    if (bflag) exit(0); /* ugly exit on end of list */
  192. X  return 0;
  193. X}
  194. X
  195. Xshowline(out,count)
  196. X  FILE *out;
  197. X{
  198. X  unsigned char outline[74];
  199. X  int i,pc;
  200. X  if (nochar && nohex) {
  201. X    pc = line[0]; /* to provoke buserror */
  202. X    return;
  203. X  }
  204. X  if (list && ! inrange(byteno)) {
  205. X    byteno+=count;
  206. X    return;
  207. X  }
  208. X  sprintf(outline,"%6x  ",byteno);
  209. X  if (!nohex) {
  210. X    for (i=0; i<count; i++)
  211. X      sprintf(strchr(outline,0),"%.2x ",line[i]);
  212. X    for (   ; i<16   ; i++)
  213. X      strcat(outline,"   ");
  214. X    }
  215. X  strcat(outline,"  ");
  216. X  if (!nochar) {
  217. X    for (i=0; i<count; i++) {
  218. X      pc = line[i];
  219. X      if (!Isprint(pc))
  220. X    pc = '.';
  221. X      sprintf(strchr(outline,0),"%c",pc);
  222. X    }
  223. X    for (   ; i<16   ; i++)
  224. X      strcat(outline," ");
  225. X    }
  226. X  fprintf(out,"%s\n",outline);
  227. X  byteno+=count;
  228. X}
  229. X
  230. Xmain(ac,av)
  231. X  int ac;
  232. X  char *av[];
  233. X{
  234. X  int opt,error,count;
  235. X  error=0;
  236. X  while ((opt=getopt(ac,av,optstring))!=EOF)
  237. X    switch (opt) {
  238. X    case 'f':
  239. X    case 'm':
  240. X    case 'M':
  241. X      if (input) error=2;
  242. X      input=opt;
  243. X      name=optarg;
  244. X    break;
  245. X    case 'b':
  246. X      if (list) error=2;
  247. X      list=optarg;
  248. X      bflag++;
  249. X    break;
  250. X    case 'o':
  251. X      pipeit=1;
  252. X    break;
  253. X    case 'c':
  254. X      nochar=1;
  255. X    break;
  256. X    case 'x':
  257. X      nohex=1;
  258. X    break;
  259. X    default:
  260. X      error=1;
  261. X  }
  262. X  if (optind<ac) {
  263. X    if (input) error=2;
  264. X    input='f';
  265. X    name=av[optind];
  266. X  }
  267. X  switch (error) {
  268. X    case 2:
  269. X      fprintf(stderr,"%s: illegal option use\n",av[0]);
  270. X    case 1:
  271. X      fprintf(stderr,usage,av[0]);
  272. X      exit(1);
  273. X  }
  274. X  openinput();
  275. X  signal(SIGQUIT,exithandle);
  276. X  while (count=getline()) {
  277. X    if (pipeit) {
  278. X      showline(stderr,count);
  279. X      write(1,line,count);
  280. X    } else
  281. X      showline(stdout,count);
  282. X  }
  283. X}
  284. X
  285. X  
  286. EOF_EOF
  287. if test `wc -c <xdump.c` -ne    4820; then echo shar: xdump.c has bad length
  288. else echo shar: xdump.c extracted; fi
  289. fi
  290.