home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume05 / xdump < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  7.0 KB

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