2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #include <ctype.h> /* for isprint */
5 #include <unistd.h> /* for fork/exec */
8 #include <string.h> /* for making strcasestr happy */
10 #include "pnd_container.h"
13 #include "pnd_pndfiles.h"
15 unsigned char pnd_pnd_seek_pxml ( FILE *f ) {
17 char *match; // match within buffer
18 unsigned int len; // length of file (not suporting 'big files' 2GB..)
19 unsigned int pos; // current tape head
20 unsigned int readable; // amount to read (for small files/chunks)
22 b = malloc ( PND_PXML_WINDOW_SIZE + 1 );
28 memset ( b, '\0', PND_PXML_WINDOW_SIZE + 1 );
30 // determine length of file
31 fseek ( f, 0, SEEK_END );
33 fseek ( f, 0, SEEK_SET );
35 /* ready to scan through the file, backwards
39 if ( len <= PND_PXML_WINDOW_SIZE ) {
43 pos = len - PND_PXML_WINDOW_SIZE;
44 readable = PND_PXML_WINDOW_SIZE;
49 //printf ( "find pxml; pos %u readable %u\n", pos, readable );
51 // seek into this blocks position
52 fseek ( f, pos, SEEK_SET );
55 fread ( b, 1, readable, f );
57 // find the head tag here? now, there are serious heavy duty algorithyms for locating
58 // strings within an arbitrary buffer. Thats needs to be done. This is the worst
59 // performing brute force strategy here.
60 if ( ( match = pnd_match_binbuf ( b, readable, PXML_TAGHEAD ) ) ) {
61 fseek ( f, pos + ( match - b ), SEEK_SET );
62 //printf ( " match found at %u\n", pos + ( match - b ) );
67 // no match, so we need to back up another chunk; if we can't
68 // back up that much, we're on our last check. if we can't back
69 // up at all, we've already been here and time to fail
72 } else if ( pos > PND_PXML_WINDOW_FRACTIONAL ) {
73 pos -= PND_PXML_WINDOW_FRACTIONAL;
74 readable = PND_PXML_WINDOW_SIZE;
76 readable = PND_PXML_WINDOW_SIZE - pos;
77 memset ( b + pos, '\0', PND_PXML_WINDOW_SIZE - pos );
83 // exeunt, with alarums
88 unsigned char pnd_pnd_accrue_pxml ( FILE *f, char *target, unsigned int maxlen ) {
89 char inbuf [ 1024 ]; // TBD: love all these assumptions? noob!
90 char *insert = target;
93 while ( fgets ( inbuf, 1023, f ) ) {
95 // copy inbuf onto end of target
97 strncpy ( insert, inbuf, maxlen );
98 // reduce counter for max size so we can avoid buffer overruns
100 maxlen -= l; // reduce
101 insert += l; // increment
103 strncat ( target, inbuf, maxlen );
106 if ( strcasestr ( inbuf, PXML_TAGFOOT ) ) {
115 char *pnd_match_binbuf ( char *haystack, unsigned int maxlen, char *needle ) {
116 char *end = haystack + maxlen - strlen ( needle );
117 unsigned int needlelen = strlen ( needle );
119 while ( haystack < end ) {
121 if ( isprint(*haystack) ) {
123 if ( strncasecmp ( haystack, needle, needlelen ) == 0 ) {
124 //printf ( "haystack %s\n", haystack );
136 static unsigned char pnd_pnd_mountie ( char *pndrun, char *fullpath, char *unique_id, unsigned char do_mount ) {
153 printf ( " runscript: %s\n", pndrun );
154 printf ( " path: %s\n", fullpath );
155 printf ( " id: %s\n", unique_id );
158 memset ( argv, '\0', sizeof(char*) * 20 );
162 argv [ f++ ] = pndrun;
164 argv [ f++ ] = fullpath;
166 argv [ f++ ] = unique_id;
176 argv [ f++ ] = NULL; // for execv
181 for ( i = 0; i < f; i++ ) {
182 printf ( "exec's argv %u [ %s ]\n", i, argv [ i ] );
188 if ( ( f = fork() ) < 0 ) {
190 } else if ( f > 0 ) {
194 execv ( pndrun, argv );
200 unsigned char pnd_pnd_mount ( char *pndrun, char *fullpath, char *unique_id ) {
201 return ( pnd_pnd_mountie ( pndrun, fullpath, unique_id, 1 ) );
204 unsigned char pnd_pnd_unmount ( char *pndrun, char *fullpath, char *unique_id ) {
205 return ( pnd_pnd_mountie ( pndrun, fullpath, unique_id, 0 ) );