4 Recover text articles from cyclic buffers
5 Articles start with "\0Path:"
6 and end with "\r\n.\r\n"
8 Tested with INND 2.2 under AIX 4.2
13 (1) Pull 16 bytes at a time
14 (2) Last 7 bytes must be \000\000\000Path
15 (3) When found, print "\nPath";
16 (4) print subsequent bytes until \r\n.\r\n found
29 /* String buffer size */
32 #define MAX_ART_SIZE 2200000
35 #define WRITEMSG printf ("File %s line %i\n", __FILE__, __LINE__); \
38 #define WRITEVAR(VAR_NAME,VAR_TYPE) \
40 printf ("FILE %s LINE %i :", __FILE__, __LINE__); \
41 printf ("%s = ", #VAR_NAME); \
42 printf (#VAR_TYPE, (VAR_NAME) ); \
46 #define WRITETXT(TEXT) \
47 printf ("FILE %s LINE %i \"%s\"\n", __FILE__, __LINE__, TEXT); \
56 int WriteArticle (char *, int, char *, char *, char *, int);
59 char ArtHead[7] = {0, 0, 0, 'P', 'a', 't', 'h'};
60 char ArtTail[5] = {'\r', '\n', '.', '\r', '\n'};
63 int main (int argc, char *argv[])
67 bool ReadingArticle = false;
72 int osize = MAX_ART_SIZE;
79 /* Check number of args */
82 printf ("Usage: pullart <cycbuff> <fileprefix> [<header> <string>]\n");
83 printf (" Read cycbuffer <cycbuff> and print all articles whose\n");
84 printf (" article header <header> contains <string>.\n");
85 printf (" Articles are written to files name <fileprefix>.nnnnnn\n");
86 printf (" where nnnnnn is numbered sequentially from 0.\n");
87 printf (" If <header> and <string> not specified, all articles\n");
88 printf (" are written.\n");
89 printf (" Examples:\n");
90 printf (" pullart /news3/cycbuff.3 alt.rec Newsgroup: alt.rec\n");
91 printf (" pullart /news3/cycbuff.3 all\n");
92 printf (" pullart firstbuff article Subject bluejay\n");
96 /* Allocate output buffer */
97 obuffer = (char *) calloc (osize+1, sizeof(char));
100 printf ("Cannot allocate obuffer[]\n");
105 /* Open input file */
106 Infile = fopen (argv[INFILE], "rb");
109 printf ("Cannot open input file.\n");
114 if (argc>=4) header = argv[HEADER];
115 if (argc>=5) string = argv[STRING];
116 if (*header=='\0') header=NULL;
117 if (*string=='\0') string=NULL;
120 printf ("filename <%s>\n", argv[INFILE]);
121 printf ("fileprefix <%s>\n", argv[FILEPREFIX]);
122 printf ("header <%s>\n", header);
123 printf ("string <%s>\n", string);
126 /* Skip first 0x38000 16byte buffers */
127 i = fseek (Infile, 0x38000L, SEEK_SET);
129 /* Read following 16 byte buffers */
130 ReadingArticle = false;
131 NumTailCharFound = 0;
134 while ( 0!=fread(buffer, 16, 1, Infile) )
139 /* Found start of article, start writing to obuffer */
140 if (0==memcmp(buffer+9, ArtHead, 7))
142 ReadingArticle = true;
143 memcpy (obuffer, "Path", 4);
148 /* Currnetly reading article */
154 /* Article too big, drop it and move on */
158 ("article number %i bigger than buffer size %i.\n",
161 ReadingArticle=false;
165 /* Add current character to output buffer, but remove \r */
166 if ('\r' != buffer[i])
167 obuffer[opos++] = buffer[i];
169 /* Check for article ending sequence */
170 if (buffer[i]==ArtTail[NumTailCharFound])
177 /* End found, write article, reset for next */
178 if (NumTailCharFound==LenTail)
180 ReadingArticle = false;
181 NumTailCharFound = 0;
183 /* Add trailing \0 to buffer */
184 obuffer[opos+1] = '\0';
186 fileno += WriteArticle
187 (obuffer, opos, argv[FILEPREFIX],
188 header, string, fileno);
206 Writes article stored in buff[] if it has a
207 "Newsgroups:" header line which contains *newsgroup
208 Write to a file named fileprefix.fileno
212 (char *buff, int n, char *fileprefix, char *headerin, char *string, int fileno)
219 char filename[NBUFF];
223 /* Prevent buffer overflow due to fileprefix too long */
224 if (strlen(fileprefix)>384)
227 ("program error: cannot have file prefix greater then 384 characters\n");
232 Is header here? Search if header string requested, leave if not found
237 strlcpy(header, "\n", sizeof(header));
238 strlcat(header, headerin, sizeof(header));
240 begptr = strstr (buff, header);
242 /* return if Header name not found */
249 Header found. What about string?
250 Search if string requested, leave if not found
254 /* Find end of header line */
256 endptr = strchr (begptr, '\n');
258 /* Something is wrong, end of header not found, do not write
264 /* Temporarily make string end a null char */
267 newsptr = strstr (begptr, string);
269 /* Requested newsgroup not found */
273 /* Restore character at end of header string */
276 /* No string specified */
279 /* No header specified */
281 /* Open file, write buffer, close file */
282 snprintf (filename, sizeof(filename), "%s.%06i", fileprefix, fileno);
284 outfile = fopen (filename, "wt");
286 printf ("Cannot open file name %s\n", filename);
291 fprintf (outfile, "%c", *buff++);
295 /* Return number of files written */