3abf62d294aa8bd2f10760b05d0877b73efb3f1b
[pandora-libraries.git] / lib / pnd_desktop.c
1
2 #include <stdio.h> /* for FILE etc */
3 #include <string.h>
4 #include <unistd.h> /* for unlink */
5 #include <stdlib.h> /* for free */
6
7 #include "pnd_apps.h"
8 #include "pnd_container.h"
9 #include "pnd_pxml.h"
10 #include "pnd_discovery.h"
11 #include "pnd_pndfiles.h"
12 #include "pnd_conf.h"
13 #include "pnd_desktop.h"
14 #include "pnd_logger.h"
15
16 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
17   char filename [ FILENAME_MAX ];
18   char buffer [ 1024 ];
19   FILE *f;
20
21   // specification
22   // http://standards.freedesktop.org/desktop-entry-spec
23
24   // validation
25
26   if ( ! p -> unique_id ) {
27     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
28     return ( 0 );
29   }
30
31   if ( ! p -> exec ) {
32     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing exec\n", targetpath );
33     return ( 0 );
34   }
35
36   if ( ! targetpath ) {
37     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
38     return ( 0 );
39   }
40
41   if ( ! pndrun ) {
42     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
43     return ( 0 );
44   }
45
46   // set up
47
48   sprintf ( filename, "%s/%s#%u.desktop", targetpath, p -> unique_id, p -> subapp_number );
49
50   // emit
51
52   //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
53
54   f = fopen ( filename, "w" );
55
56   if ( ! f ) {
57     return ( 0 );
58   }
59
60   fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
61
62   if ( p -> title_en ) {
63     snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
64     fprintf ( f, "%s", buffer );
65   }
66
67   fprintf ( f, "Type=Application\n" );
68   fprintf ( f, "Version=1.0\n" );
69
70   if ( p -> icon ) {
71     snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
72     fprintf ( f, "%s", buffer );
73   }
74
75   if ( p -> unique_id ) {
76     fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
77   }
78
79 #if 1
80   if ( p -> desc_en && p -> desc_en [ 0 ] ) {
81     snprintf ( buffer, 1020, "Comment=%s\n", p -> desc_en ); // no [en] needed I suppose, yet
82     fprintf ( f, "%s", buffer );
83   }
84 #endif
85
86 #if 0 // we let pnd_run.sh command line handle this instead of in .desktop
87   if ( p -> startdir ) {
88     snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
89     fprintf ( f, "%s", buffer );
90   } else {
91     fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
92   }
93 #endif
94
95   if ( p -> exec ) {
96     char *nohup;
97
98     if ( p -> option_no_x11 ) {
99       nohup = "/usr/bin/nohup ";
100     } else {
101       nohup = "";
102     }
103
104     // basics
105     if ( p -> object_type == pnd_object_type_directory ) {
106       snprintf ( buffer, 1020, "Exec=%s%s -p \"%s\" -e \"%s\" -b \"%s\"",
107                  nohup, pndrun, p -> object_path, p -> exec,
108                  p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
109     } else if ( p -> object_type == pnd_object_type_pnd ) {
110       snprintf ( buffer, 1020, "Exec=%s%s -p \"%s/%s\" -e \"%s\" -b \"%s\"",
111                  nohup, pndrun, p -> object_path, p -> object_filename, p -> exec,
112                  p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
113     }
114
115     // start dir
116     if ( p -> startdir ) {
117       strncat ( buffer, " -s ", 1020 );
118       strncat ( buffer, p -> startdir, 1020 );
119     }
120
121     // args
122     if ( p -> execargs ) {
123       char argbuf [ 1024 ];
124       snprintf ( argbuf, 1000, " -a \"%s\"", p -> execargs );
125       strncat ( buffer, argbuf, 1020 );
126     }
127
128     // clockspeed
129     if ( p -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
130       strncat ( buffer, " -c ", 1020 );
131       strncat ( buffer, p -> clockspeed, 1020 );
132     }
133
134     // exec options
135     if ( pnd_pxml_get_x11 ( p -> option_no_x11 ) == pnd_pxml_x11_stop ) {
136       strncat ( buffer, " -x ", 1020 );
137     }
138
139     // newline
140     strncat ( buffer, "\n", 1020 );
141
142     // emit
143     fprintf ( f, "%s", buffer );
144   }
145
146   // categories
147   {
148     char cats [ 512 ] = "";
149     int n;
150     pnd_conf_handle c;
151     char *confpath;
152
153     // uuuuh, defaults?
154     // "Application" used to be in the standard and is commonly supported still
155     // Utility and Network should ensure the app is visible 'somewhere' :/
156     char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
157
158     // determine searchpath (for conf, not for apps)
159     confpath = pnd_conf_query_searchpath();
160
161     // inhale the conf file
162     c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
163
164     // if we can find a default category set, pull it in; otherwise assume
165     // the hardcoded one
166     if ( pnd_conf_get_as_char ( c, "default" ) ) {
167       defaults = pnd_conf_get_as_char ( c, "default" );
168     }
169
170     // ditch the confpath
171     free ( confpath );
172
173     // attempt mapping
174     if ( c ) {
175
176       n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
177
178       if ( n ) {
179         fprintf ( f, "Categories=%s\n", cats );
180       } else {
181         fprintf ( f, "Categories=%s\n", defaults );
182       }
183
184     } else {
185       fprintf ( f, "Categories=%s\n", defaults );
186     }
187
188   }
189
190   fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
191
192   fclose ( f );
193
194   return ( 1 );
195 }
196
197 unsigned char pnd_emit_dotinfo ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
198   char filename [ FILENAME_MAX ];
199   char buffer [ 1024 ];
200   FILE *f;
201   char *viewer, *searchpath;
202   pnd_conf_handle desktoph;
203
204   // specification
205   // http://standards.freedesktop.org/desktop-entry-spec
206
207   // validation
208   //
209
210   // viewer
211   searchpath = pnd_conf_query_searchpath();
212
213   desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );
214
215   if ( ! desktoph ) {
216     return ( 0 );
217   }
218
219   viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );
220
221   if ( ! viewer ) {
222     return ( 0 ); // no way to view the file
223   }
224
225   // etc
226   if ( ! p -> unique_id ) {
227     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
228     return ( 0 );
229   }
230
231   if ( ! p -> info_filename ) {
232     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_filename\n", targetpath );
233     return ( 0 );
234   }
235
236   if ( ! p -> info_name ) {
237     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_name\n", targetpath );
238     return ( 0 );
239   }
240
241   if ( ! targetpath ) {
242     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
243     return ( 0 );
244   }
245
246   if ( ! pndrun ) {
247     pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
248     return ( 0 );
249   }
250
251   // set up
252
253   sprintf ( filename, "%s/%s#%uinfo.desktop", targetpath, p -> unique_id, p -> subapp_number );
254
255   // emit
256
257   f = fopen ( filename, "w" );
258
259   if ( ! f ) {
260     return ( 0 );
261   }
262
263   fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
264
265   if ( p -> info_name ) {
266     snprintf ( buffer, 1020, "Name=%s\n", p -> info_name );
267     fprintf ( f, "%s", buffer );
268   }
269
270   fprintf ( f, "Type=Application\n" );
271   fprintf ( f, "Version=1.0\n" );
272
273 #if 0
274   if ( p -> icon ) {
275     snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
276     fprintf ( f, "%s", buffer );
277   }
278 #endif
279
280   if ( p -> unique_id ) {
281     fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
282   }
283
284   if ( p -> title_en && p -> title_en [ 0 ] ) {
285     snprintf ( buffer, 1020, "Comment=Automatic menu info entry for %s\n", p -> title_en );
286     fprintf ( f, "%s", buffer );
287   }
288
289 #if 0 // we let pnd_run.sh command line handle this instead of in .desktop
290   if ( p -> startdir ) {
291     snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
292     fprintf ( f, "%s", buffer );
293   } else {
294     fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
295   }
296 #endif
297
298   // exec line
299   char args [ 1001 ];
300   char *pargs = args;
301   if ( pnd_conf_get_as_char ( desktoph, "info.viewer_args" ) ) {
302     snprintf ( pargs, 1001, "%s %s",
303                pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
304   } else {
305     pargs = NULL;
306     // WARNING: This might not be quite right; if no viewer-args, shouldn't we still append the info-filename? likewise,
307     //          even if we do have view-args, shouldn't we check if filename is present?
308   }
309
310   char pndfile [ 1024 ];
311   if ( p -> object_type == pnd_object_type_directory ) {
312     // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
313     strncpy ( pndfile, p -> object_path, 1000 );
314   } else if ( p -> object_type == pnd_object_type_pnd ) {
315     // pnd_run.sh wants the full path and filename for the .pnd file
316     snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
317   }
318
319   pnd_apps_exec_info_t info;
320   info.viewer = viewer;
321   info.args = pargs;
322
323   if ( ! pnd_apps_exec_disco ( pndrun, p, PND_EXEC_OPTION_NORUN | PND_EXEC_OPTION_INFO, &info ) ) {
324     return ( 0 );
325   }
326
327   fprintf ( f, "Exec=%s\n", pnd_apps_exec_runline() );
328
329   if ( pnd_conf_get_as_char ( desktoph, "info.category" ) ) {
330     fprintf ( f, "Categories=%s\n", pnd_conf_get_as_char ( desktoph, "info.category" ) );
331   } else {
332     fprintf ( f, "Categories=Documentation\n" );
333   }
334
335   fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
336
337   fclose ( f );
338
339   return ( 1 );
340 }
341
342 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
343   //#define BITLEN (8*1024)
344 #define BITLEN (64*1024)
345   char buffer [ FILENAME_MAX ]; // target filename
346   char from [ FILENAME_MAX ];   // source filename
347   unsigned char bits [ BITLEN ];
348   unsigned int bitlen;
349   FILE *pnd, *target;
350
351   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
352   if ( ( p -> object_type == pnd_object_type_pnd ) &&
353        ( ! p -> pnd_icon_pos ) )
354   {
355     return ( 0 ); // discover code didn't find it, so FAIL
356   }
357
358   // determine filename for target
359   sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id /*, p -> subapp_number*/ ); // target
360
361   /* first.. open the source file, by type of application:
362    * are we looking through a pnd file or a dir?
363    */
364   if ( p -> object_type == pnd_object_type_directory ) {
365     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
366   } else if ( p -> object_type == pnd_object_type_pnd ) {
367     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
368   }
369
370   pnd = fopen ( from, "rb" );
371
372   if ( ! pnd ) {
373     pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open source\n" );
374     return ( 0 );
375   }
376
377   unsigned int len;
378
379   target = fopen ( buffer, "wb" );
380
381   if ( ! target ) {
382     fclose ( pnd );
383     pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open target\n" );
384     return ( 0 );
385   }
386
387   fseek ( pnd, 0, SEEK_END );
388   len = ftell ( pnd );
389   //fseek ( pnd, 0, SEEK_SET );
390
391   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
392
393   len -= p -> pnd_icon_pos;
394
395   pnd_log ( PND_LOG_DEFAULT, "    Emit icon, length: %u\n", len );
396
397   while ( len ) {
398
399     if ( len > (BITLEN) ) {
400       bitlen = (BITLEN);
401     } else {
402       bitlen = len;
403     }
404
405     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
406       fclose ( pnd );
407       fclose ( target );
408       unlink ( buffer );
409       pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad read\n" );
410       return ( 0 );
411     }
412
413 #if 0
414     {
415       unsigned int i = 0;
416       char bigbuffer [ 200 * 1024 ] = "\0";
417       char b [ 10 ];
418       pnd_log ( PND_LOG_DEFAULT, "    Read hexdump\n" );
419       while ( i < bitlen ) {
420         sprintf ( b, "%x,", bits [ i ] );
421         strcat ( bigbuffer, b );
422         i++;
423       }
424       pnd_log ( PND_LOG_DEFAULT, bigbuffer );
425     }
426 #endif
427
428     if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
429       fclose ( pnd );
430       fclose ( target );
431       unlink ( buffer );
432       pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad write\n" );
433       return ( 0 );
434     }
435
436     len -= bitlen;
437     //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, next block, length: %u\n", len );
438   } // while
439
440   fclose ( pnd );
441   fclose ( target );
442
443   //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, done.\n" );
444
445   return ( 1 );
446 }
447
448 #if 1 // we switched direction to freedesktop standard categories
449 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
450 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
451   char *t;
452   char *match;
453
454   // clear target so we can easily append
455   memset ( target_buffer, '\0', len );
456
457   // for each main-cat and sub-cat, including alternates, just append them all together
458   // we'll try mapping them, since the categories file is there, but we'll default to
459   // copying over; this lets the conf file do merging or renaming of cagtegories, which
460   // could still be useful, but we can leave the conf file empty to effect a pure
461   // trusted-PXML-copying
462
463   // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
464   // there are hundreds), and would also mean new ones and peoples custom ones would
465   // flop
466
467   /* attempt primary category chain
468    */
469   #define MAPCAT(field)                                     \
470     if ( ( t = d -> field ) ) {                             \
471       match = pnd_map_dotdesktop_category ( c, t );         \
472       strncat ( target_buffer, match ? match : t, len );    \
473       strncat ( target_buffer, ";", len );                  \
474     }
475
476   MAPCAT(main_category);
477   MAPCAT(main_category1);
478   MAPCAT(main_category2);
479   MAPCAT(alt_category);
480   MAPCAT(alt_category1);
481   MAPCAT(alt_category2);
482
483   if ( target_buffer [ 0 ] ) {
484     return ( 1 ); // I guess its 'good'?
485   }
486
487 #if 0
488   if ( ( t = d -> main_category ) ) {
489     match = pnd_map_dotdesktop_category ( c, t );
490     strncat ( target_buffer, match ? match : t, len );
491     strncat ( target_buffer, ";", len );
492   }
493 #endif
494
495   return ( 0 );
496 }
497 #endif
498
499 #if 0 // we switched direction
500 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
501 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
502   unsigned short int n = 0; // no. matches
503   char *t;
504   char *match;
505
506   // clear target so we can easily append
507   memset ( target_buffer, '\0', len );
508
509   /* attempt primary category chain
510    */
511   match = NULL;
512
513   if ( ( t = d -> main_category ) ) {
514     match = pnd_map_dotdesktop_category ( c, t );
515   }
516
517   if ( ( ! match ) &&
518        ( t = d -> main_category1 ) )
519   {
520     match = pnd_map_dotdesktop_category ( c, t );
521   }
522
523   if ( ( ! match ) &&
524        ( t = d -> main_category2 ) )
525   {
526     match = pnd_map_dotdesktop_category ( c, t );
527   }
528   
529   if ( match ) {
530     strncat ( target_buffer, match, len );
531     len -= strlen ( target_buffer );
532     n += 1;
533   }
534
535   /* attempt secondary category chain
536    */
537   match = NULL;
538
539   if ( ( t = d -> alt_category ) ) {
540     match = pnd_map_dotdesktop_category ( c, t );
541   }
542
543   if ( ( ! match ) &&
544        ( t = d -> alt_category1 ) )
545   {
546     match = pnd_map_dotdesktop_category ( c, t );
547   }
548
549   if ( ( ! match ) &&
550        ( t = d -> alt_category2 ) )
551   {
552     match = pnd_map_dotdesktop_category ( c, t );
553   }
554   
555   if ( match ) {
556     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
557       strcat ( target_buffer, ";" );
558       len -= 1;
559     }
560     strncat ( target_buffer, match, len );
561     len -= strlen ( target_buffer );
562     n += 1;
563   }
564
565 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
566   match = NULL;
567
568   if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
569     match = pnd_map_dotdesktop_category ( c, t );
570   }
571
572   if ( ( ! match ) &&
573        ( t = pnd_pxml_get_subcategory1 ( h ) ) )
574   {
575     match = pnd_map_dotdesktop_category ( c, t );
576   }
577
578   if ( ( ! match ) &&
579        ( t = pnd_pxml_get_subcategory2 ( h ) ) )
580   {
581     match = pnd_map_dotdesktop_category ( c, t );
582   }
583   
584   if ( match ) {
585     strncat ( target_buffer, match, len );
586     len -= strlen ( target_buffer );
587     n += 1;
588   }
589
590   /* attempt secondary category chain
591    */
592   match = NULL;
593
594   if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
595     match = pnd_map_dotdesktop_category ( c, t );
596   }
597
598   if ( ( ! match ) &&
599        ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
600   {
601     match = pnd_map_dotdesktop_category ( c, t );
602   }
603
604   if ( ( ! match ) &&
605        ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
606   {
607     match = pnd_map_dotdesktop_category ( c, t );
608   }
609   
610   if ( match ) {
611     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
612       strcat ( target_buffer, ";" );
613       len -= 1;
614     }
615     strncat ( target_buffer, match, len );
616     len -= strlen ( target_buffer );
617     n += 1;
618   }
619 #endif
620
621   if ( n && len ) {
622     strcat ( target_buffer, ";" );
623   }
624
625   return ( n );
626 }
627 #endif
628
629 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
630 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
631   char *key;
632   char *ret;
633
634   key = malloc ( strlen ( single_category ) + 4 + 1 );
635
636   sprintf ( key, "map.%s", single_category );
637
638   ret = pnd_conf_get_as_char ( c, key );
639
640   free ( key );
641
642   return ( ret );
643 }
644
645 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
646   // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
647   // with a fwrite at the end...
648   char from [ FILENAME_MAX ];   // source filename
649   char bits [ 8 * 1024 ];
650   unsigned int bitlen;
651   FILE *pnd = NULL;
652   unsigned char *target = NULL, *targiter = NULL;
653
654   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
655   if ( ( p -> object_type == pnd_object_type_pnd ) &&
656        ( ! p -> pnd_icon_pos ) )
657   {
658     return ( NULL ); // discover code didn't find it, so FAIL
659   }
660
661   /* first.. open the source file, by type of application:
662    * are we looking through a pnd file or a dir?
663    */
664   if ( p -> object_type == pnd_object_type_directory ) {
665     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
666   } else if ( p -> object_type == pnd_object_type_pnd ) {
667     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
668   }
669
670   pnd = fopen ( from, "r" );
671
672   if ( ! pnd ) {
673     return ( NULL );
674   }
675
676   // determine length of file, then adjust by icon position to find begin of icon
677   unsigned int len;
678
679   fseek ( pnd, 0, SEEK_END );
680   len = ftell ( pnd );
681   //fseek ( pnd, 0, SEEK_SET );
682
683   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
684
685   len -= p -> pnd_icon_pos;
686
687   // create target buffer
688   target = malloc ( len );
689
690   if ( ! target ) {
691     fclose ( pnd );
692     return ( 0 );
693   }
694
695   targiter = target;
696
697   if ( r_buflen ) {
698     *r_buflen = len;
699   }
700
701   // copy over icon to target
702   while ( len ) {
703
704     if ( len > (8*1024) ) {
705       bitlen = (8*1024);
706     } else {
707       bitlen = len;
708     }
709
710     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
711       fclose ( pnd );
712       free ( target );
713       return ( NULL );
714     }
715
716     memmove ( targiter, bits, bitlen );
717     targiter += bitlen;
718
719     len -= bitlen;
720   } // while
721
722   fclose ( pnd );
723
724   return ( target );
725 }