unstr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* $NetBSD: unstr.c,v 1.11 2004/02/08 22:23:50 jsm Exp $ */
  2. /*-
  3. * Copyright (c) 1991, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Ken Arnold.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <sys/cdefs.h>
  34. #ifndef lint
  35. __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
  36. The Regents of the University of California. All rights reserved.\n");
  37. #endif /* not lint */
  38. #ifndef lint
  39. #if 0
  40. static char sccsid[] = "@(#)unstr.c 8.1 (Berkeley) 5/31/93";
  41. #else
  42. __RCSID("$NetBSD: unstr.c,v 1.11 2004/02/08 22:23:50 jsm Exp $");
  43. #endif
  44. #endif /* not lint */
  45. /*
  46. * This program un-does what "strfile" makes, thereby obtaining the
  47. * original file again. This can be invoked with the name of the output
  48. * file, the input file, or both. If invoked with only a single argument
  49. * ending in ".dat", it is pressumed to be the input file and the output
  50. * file will be the same stripped of the ".dat". If the single argument
  51. * doesn't end in ".dat", then it is presumed to be the output file, and
  52. * the input file is that name prepended by a ".dat". If both are given
  53. * they are treated literally as the input and output files.
  54. *
  55. * Ken Arnold Aug 13, 1978
  56. */
  57. # include <sys/types.h>
  58. # include <sys/param.h>
  59. # include <sys/endian.h>
  60. # include <ctype.h>
  61. # include <err.h>
  62. # include <stdio.h>
  63. # include <stdlib.h>
  64. # include <string.h>
  65. # include "strfile.h"
  66. # ifndef MAXPATHLEN
  67. # define MAXPATHLEN 1024
  68. # endif /* MAXPATHLEN */
  69. char *Infile, /* name of input file */
  70. Datafile[MAXPATHLEN], /* name of data file */
  71. Delimch; /* delimiter character */
  72. FILE *Inf, *Dataf;
  73. void getargs(char *[]);
  74. int main(int, char *[]);
  75. void order_unstr(STRFILE *);
  76. /* ARGSUSED */
  77. int
  78. main(ac, av)
  79. int ac __attribute__((__unused__));
  80. char **av;
  81. {
  82. static STRFILE tbl; /* description table */
  83. getargs(av);
  84. if ((Inf = fopen(Infile, "r")) == NULL)
  85. err(1, "fopen %s", Infile);
  86. if ((Dataf = fopen(Datafile, "r")) == NULL)
  87. err(1, "fopen %s", Datafile);
  88. (void) fread((char *) &tbl, sizeof tbl, 1, Dataf);
  89. BE32TOH(tbl.str_version);
  90. BE32TOH(tbl.str_numstr);
  91. BE32TOH(tbl.str_longlen);
  92. BE32TOH(tbl.str_shortlen);
  93. BE32TOH(tbl.str_flags);
  94. if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) {
  95. fprintf(stderr, "nothing to do -- table in file order\n");
  96. exit(1);
  97. }
  98. Delimch = tbl.str_delim;
  99. order_unstr(&tbl);
  100. (void) fclose(Inf);
  101. (void) fclose(Dataf);
  102. exit(0);
  103. }
  104. void
  105. getargs(av)
  106. char *av[];
  107. {
  108. if (!*++av) {
  109. (void) fprintf(stderr, "usage: unstr datafile\n");
  110. exit(1);
  111. }
  112. Infile = *av;
  113. (void) strcpy(Datafile, Infile);
  114. (void) strcat(Datafile, ".dat");
  115. }
  116. void
  117. order_unstr(tbl)
  118. STRFILE *tbl;
  119. {
  120. unsigned int i;
  121. char *sp;
  122. u_int64_t pos;
  123. char buf[BUFSIZ];
  124. for (i = 0; i < tbl->str_numstr; i++) {
  125. (void) fread((char *) &pos, 1, sizeof pos, Dataf);
  126. (void) fseek(Inf, be64toh(pos), SEEK_SET);
  127. if (i != 0)
  128. (void) printf("%c\n", Delimch);
  129. for (;;) {
  130. sp = fgets(buf, sizeof buf, Inf);
  131. if (sp == NULL || STR_ENDSTRING(sp, *tbl))
  132. break;
  133. else
  134. fputs(sp, stdout);
  135. }
  136. }
  137. }