e627ea5294aa648c02264871c77043ce0ee1b07e
[FAPG] / fapg.c
1 /*
2  *               FAPG
3  */
4 #define VERSION "0.34"
5 /*
6  * FAPG means Fast Audio Playlist Generator.
7  * It is a tool to generate list of audio files (Wav, MP3, Ogg, etc)
8  * in various formats (M3U, PLS, HTML, etc).
9  * It is very usefull if you have a large amount of audio files
10  * and you want to quickly and frequently build a playlist.
11  *
12  * Copyright (C) 2003-2004  Antoine Jacquet <royale@zerezo.com>
13  * http://royale.zerezo.com/fapg/
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <getopt.h>
33 #include <dirent.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <limits.h>
39 #include <unistd.h>
40 #include <ctype.h>
41 #include <time.h>
42 #include <assert.h>
43 #include "genres.h"
44
45 #define MP3_BASE 1024
46 #define OGG_BASE 1024*10
47 #define MAX 1024*200 /* 200ko for ID3 with JPEG images in it */
48
49 int debug=0;
50 int format=0; /* 0 = m3u ; 1 = pls ; 2 = html ; 3 = rss */
51 char *genrelist=NULL;
52 unsigned char *prefix="";
53 unsigned char *base="";
54 unsigned char *dir="";
55 unsigned char *hostname="fritzserver.de";
56 unsigned char *referal="http://www.explaining.text.org/select.php?title=";
57 int recursive=0;
58 int avoidhlinked=0;
59 int separator='/';
60 int skip=0;
61 int windows=0;
62 unsigned char *eol="\n";
63 unsigned char buffer[MAX];
64
65 int counter=0;
66
67 unsigned char artist[1024];
68 unsigned char title[1024];
69 unsigned char genrebuf[1024];
70 unsigned char genre=0;
71 int duration;
72
73 unsigned char unix2dos[256]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,70,35,36,37,38,39,40,41,82,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,84,59,36,61,65,71,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,36,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,36,125,126,127,199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,201,230,198,244,246,242,251,249,255,214,220,248,163,216,215,131,225,237,243,250,241,209,170,186,191,174,172,189,188,161,171,187,166,166,166,166,166,193,194,192,169,166,166,43,43,162,165,43,43,45,45,43,45,43,227,195,43,43,45,45,166,45,43,164,240,208,202,203,200,105,205,206,207,43,43,166,220,166,204,175,211,223,212,210,245,213,181,254,222,218,219,217,253,221,175,180,173,177,61,190,182,167,247,184,176,168,183,185,179,178,166,160};
74
75 void usage()
76 {
77   fprintf(stderr,"Usage >> fapg [-b|--backslash] [-d|--debug] [-f|--format=m3u|pls|html|rss] [-g|--genre=#:#:...] [-n|--nohardlink] [-o|--output=/path/to/file.m3u] [-p|--prefix=/the/prefix] [-r|--recursive] [-w|--windows] [-x|--exclude=#:#:...] /path/to/mp3/dir1 [/path/to/mp3/dir2 ...]\n");
78   exit(1);
79 }
80
81 const char*reflink(const char*title)
82 {
83   int i=0;
84   static char *buffer;
85   buffer=malloc(strlen(title)+strlen(referal)+2);
86   strcpy(buffer,referal);
87   strcat(buffer,title);
88   for(i=strlen(referal);i<strlen(buffer);i++)
89   {
90      switch(buffer[i])
91      {
92         case ' ': buffer[i]='+'; break;
93         default: ;
94      }
95   }
96   return(buffer);
97 }
98
99 void parse_options(int argc,char **argv)
100 {
101   static char const short_options[]="bdf:g:lo:np:rwx:";
102   static struct option long_options[]=
103   {
104     {"backslash",no_argument,NULL,'b'},
105     {"debug",no_argument,NULL,'d'},
106     {"format",required_argument,NULL,'f'},
107     {"genre",required_argument,NULL,'g'},
108     {"nohardlink",no_argument,NULL,'n'},
109     {"output",required_argument,NULL,'o'},
110     {"prefix",required_argument,NULL,'p'},
111     {"recursive",no_argument,NULL,'r'},
112     {"windows",no_argument,NULL,'w'},
113     {"exclude",required_argument,NULL,'x'}
114   };
115   int c;
116   int option_index=0;
117   while ((c=getopt_long(argc,argv,short_options,long_options,&option_index))!=-1)
118   {
119     switch(c)
120     {
121       case 'b':
122         separator='\\';
123         break;
124       case 'd':
125         debug=1;
126         break;
127       case 'f':
128         if (strcmp(optarg,"m3u")==0)  format=0; else
129         if (strcmp(optarg,"pls")==0)  format=1; else
130         if (strcmp(optarg,"html")==0) format=2; else
131         if (strcmp(optarg,"rss")==0)  format=3; else
132         usage();
133         break;
134       case 'g':
135         if (genrelist==NULL) genrelist=calloc(257,sizeof(char)); /* allow multiple includes/excludes */
136         if (genrelist==NULL) { fprintf(stderr,"Error >> unable to allocate cleared memory\n"); exit(2); }
137         else
138         {
139           int n=0; 
140           while (n<strlen(optarg))
141           {
142             if (debug) fprintf(stderr,"Debug >> genrelist entry activting : %d\n",atoi(&optarg[n]));
143             genrelist[atoi(&optarg[n])]=1;
144             while (isdigit(optarg[n++]));
145           }
146         }
147         break;
148       case 'n':
149         avoidhlinked=1;
150         break;
151       case 'o':
152         close(1);
153         if (fopen(optarg,"w")==NULL) { fprintf(stderr,"Error >> unable to open output file : %s\n",optarg); exit(2); }
154         break;
155       case 'p':
156         prefix=malloc(strlen(optarg)+1);
157         strcpy(prefix,optarg);
158         base=malloc(strlen(prefix)+1);
159         strcpy(base,prefix);
160         dir=strchr(base,'/'); 
161         if( (dir!=NULL) && (dir[1]=='/') ) dir=strchr(dir+2,'/');
162         if  (dir!=NULL) *dir++=0; else dir="";
163              /* if prefix is a weblink, base is the baselink, dir is the path */
164         break;
165       case 'r':
166         recursive=1;
167         break;
168       case 'w':
169         windows=1;
170         eol="\r\n";
171         break;
172       case 'x':
173         if (genrelist==NULL) /* allow multiple includes/excludes - not recommended (confusing) but possible */
174         {
175           int n=0; 
176           genrelist=calloc(257,sizeof(char));
177           while (n<256) genrelist[n++]=1;
178         }
179         if (genrelist==NULL) { fprintf(stderr,"Error >> unable to allocate cleared memory\n"); exit(2); }
180         else
181         {
182           int n=0; 
183           while (n<strlen(optarg))
184           {
185             if (debug) fprintf(stderr,"Debug >> genrelist entry activting : %d\n",atoi(&optarg[n]));
186             genrelist[atoi(&optarg[n])]=0;
187             while (isdigit(optarg[n++]));
188           }
189         }
190         break;
191       default:
192         usage();
193     }
194   }
195   /* hostname = getenv("HOSTNAME"); */
196   if (genrelist==NULL)
197   {
198     genrelist=calloc(257,sizeof(char));
199     if (genrelist==NULL) { fprintf(stderr,"Error >> unable to allocate cleared memory\n"); exit(2); }
200     else
201     { 
202       int n=0; 
203       while (n<256) genrelist[n++]=1;
204     }
205   }
206 }
207
208 void parse_mp3(unsigned char *file)
209 {
210   int bitrates[2][3][15]=
211     {{{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448},
212       {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
213       {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}},
214      {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},
215       {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
216       {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}}};
217   FILE *fic;
218   unsigned char *c;
219   int lus;
220
221   genre=0;
222   genrebuf[0]=0;
223   if (debug) fprintf(stderr,"Debug >> parsing mp3 : %s\n",file);
224
225   /* read header */
226   if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }
227   lus=fread(buffer,1,MP3_BASE,fic);
228   c=buffer;  
229
230   /* try ID3v2 */
231   if (buffer[0]=='I' && buffer[1]=='D' && buffer[2]=='3')
232   {
233     int size;
234     int version;
235     version=*(buffer+3);
236     if (version<2 || version>4)
237       fprintf(stderr,"Warning >> ID3 v2.%d not implemented ! trying anyway : %s\n",version,file);
238     if (*(buffer+5)!=0)
239       fprintf(stderr,"Warning >> specials headers not implemented (%d) ! trying anyway : %s\n",*(buffer+5),file);
240     c=buffer+6;
241     size=(*c<<21)+(*(c+1)<<14)+(*(c+2)<<7)+(*(c+3));
242     /* read more header */
243     if (size+lus>MAX)
244     {
245       lus+=fread(buffer+lus,1,MAX-lus,fic);
246       fprintf(stderr,"Warning >> ID3 header is huge (%d bytes) ! trying anyway : %s\n",size,file);
247     }
248     else
249       lus+=fread(buffer+lus,1,size,fic);
250     if (size>lus) size=lus;
251     c+=4;
252     if (version==2) while (c<buffer+size)
253     {
254       int size=(*(c+3)<<16)+(*(c+4)<<8)+(*(c+5));
255       if (*c==0) break;
256       if (strncmp(c,"TT2",3)==0)
257       {
258         strncpy(title,c+7,size-1);
259         title[size-1]='\0';
260       }
261       if (strncmp(c,"TP1",3)==0)
262       {
263         strncpy(artist,c+7,size-1);
264         artist[size-1]='\0';
265       }
266       if (strncmp(c,"TCO",3)==0)
267       {
268         /* strncpy(genrebuf,c+7,size-1);*/
269         /* genrebuf[size-1]='\0'; */
270         /* genre=atoi(&genrebuf[1]); */
271         genre=atoi(c+8);
272       }
273       c+=size+6;
274     }
275     if (version==3 || version==4) while (c<buffer+size)
276     {
277       int size=(*(c+4)<<24)+(*(c+5)<<16)+(*(c+6)<<8)+(*(c+7));
278       if (*c==0) break;
279       if (strncmp(c,"TIT2",4)==0)
280       {
281         strncpy(title,c+11,size-1);
282         title[size-1]='\0';
283       }
284       if (strncmp(c,"TPE1",4)==0)
285       {
286         strncpy(artist,c+11,size-1);
287         artist[size-1]='\0';
288       }
289       if (strncmp(c,"TCON",4)==0)
290       {
291         /* strncpy(genrebuf,c+11,size-1); */
292         /* genrebuf[size-1]='\0'; */
293         /* genre=atoi(&genrebuf[1]); */
294         genre=atoi(c+12);
295       }
296       c+=size+10;
297     }
298   }
299   
300   while (c<buffer+lus-10)
301   {
302     if (*c==0xFF && (*(c+1)&0xF0)==0xF0)
303     {
304       int version;
305       int lay;
306       int bitrate_index;
307       int bitrate;
308       version=2-(*(c+1)>>3&1);
309       lay=4-(*(c+1)>>1&3);
310       bitrate_index=*(c+2)>>4&0xF;
311       if (version>=1 && version<=2 && lay-1>=0 && lay-1<=2 && bitrate_index>=0 && bitrate_index<=14)
312         bitrate=bitrates[version-1][lay-1][bitrate_index];
313       else
314         bitrate=0;
315       if (bitrate!=0)
316       {
317         fseek(fic,0,SEEK_END);
318         duration=(ftell(fic)+buffer-c)/125/bitrate;
319       }
320       else
321         duration=0;
322       break;
323     }
324     c++;
325   }
326
327   /* try ID3v1 */
328   if (strlen(artist)==0 && strlen(title)==0)
329   {
330     fseek(fic,-128,SEEK_END);
331     lus=fread(buffer,1,128,fic);
332     if (lus==128 && buffer[0]=='T' && buffer[1]=='A' && buffer[2]=='G')
333     {
334       strncpy(title,buffer+3,30);
335       title[30]='\0';
336       c=title+29;
337       while (c>title && *c==' ') *(c--)='\0';
338       strncpy(artist,buffer+33,30);
339       artist[30]='\0';
340       c=artist+29;
341       while (c>artist && *c==' ') *(c--)='\0';
342       /* strncpy(album,buffer+65,30); */
343       /* strncpy(year,buffer+97,4); */
344       /* strncpy(comment,buffer+101,30); */
345       /* strncpy(genrebuf,buffer+127,1); genre[1]=0; */
346       genre=buffer[127];
347     }
348   }
349
350   fclose(fic);
351 }  
352
353 void parse_ogg(unsigned char *file)
354 {
355   FILE *fic;
356   unsigned char *c;
357   int lus;
358   int sample_rate;
359   int samples;
360
361   if (debug) fprintf(stderr,"Debug >> parsing ogg : %s\n",file);
362
363   /* read header */
364   if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }
365   lus=fread(buffer,1,OGG_BASE,fic);
366
367   /* try Ogg */
368   if (buffer[0]!='O' && buffer[1]!='g' && buffer[2]!='g')
369   {
370     fprintf(stderr,"Warning >> not a Ogg header : %s\n",file);
371     return;
372   }
373   
374   c=buffer+0x28;
375   sample_rate=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
376
377   while (c<buffer+lus-10)
378   {
379     int size;
380     if (strncasecmp(c,"TITLE=",6)==0)
381     {
382       size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);
383       strncpy(title,c+6,size-6);
384       title[size-6]='\0';
385       c+=size;
386     }
387     if (strncasecmp(c,"ARTIST=",7)==0)
388     {
389       size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);
390       strncpy(artist,c+7,size-7);
391       artist[size-7]='\0';
392       c+=size;
393     }
394     if (strncasecmp(c,"GENRE=",6)==0)
395     {
396       static int i=0;
397       size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);
398       strncpy(genrebuf,c+6,size-6);
399       genrebuf[size-6]='\0';
400       c+=size;
401       for(i=0;i<ID3_NR_OF_V1_GENRES;i++)
402       {
403         if (strcasecmp(ID3_v1_genre_description[i],genrebuf)==0)
404         {
405           genre=i;
406           break;
407         }
408         if (i==ID3_NR_OF_V1_GENRES) genre=0;
409       }
410     }
411     c++;
412   }
413   
414   fseek(fic,-OGG_BASE,SEEK_END);
415   lus=fread(buffer,1,OGG_BASE,fic);
416   c=buffer+lus-1;
417   while (strncmp(c,"OggS",4)!=0 && c>buffer) c--;
418   if (c!=buffer)
419   {
420     c+=6;
421     samples=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
422     duration=samples/sample_rate;
423   }
424   
425   fclose(fic);
426 }
427
428 void parse_mpc(unsigned char *file)
429 {
430   FILE *fic;
431   unsigned char *c;
432   int lus;
433   int sample_rates[4]={44100,48000,37800,32000};
434   int frame_count;
435   int size,items;
436   int i;
437
438   if (debug) fprintf(stderr,"Debug >> parsing mpc : %s\n",file);
439
440   /* read header */
441   if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }
442   lus=fread(buffer,1,12,fic);
443
444   /* try Musepack */
445   if (buffer[0]!='M' && buffer[1]!='P' && buffer[2]!='+')
446   {
447     fprintf(stderr,"Warning >> not a Musepack header : %s\n",file);
448     return;
449   }
450   
451   /* only version 7 */
452   if (buffer[3]!=7)
453   {
454     fprintf(stderr,"Warning >> only Musepack SV7 supported : %s\n",file);
455     return;
456   }
457   
458   /* duration */
459   c=buffer+4;
460   frame_count=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
461   c+=5;
462   duration=frame_count*1152/sample_rates[*c&3];
463   
464   /* try APETAGEX footer */
465   fseek(fic,-32,SEEK_END);
466   lus=fread(buffer,1,32,fic);
467   if (lus==32 && strncmp(buffer,"APETAGEX",8)==0)
468   {
469     c=buffer+12;
470     size=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
471     size+=32;
472     c+=4;
473     items=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
474     fseek(fic,-size,SEEK_END);
475     lus=fread(buffer,1,size,fic);
476     if (lus==size && strncmp(buffer,"APETAGEX",8)==0)
477     {
478       c=buffer+32;
479       while (items--)
480       {
481         size=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
482         c+=8;
483         if (strcasecmp(c,"TITLE")==0)
484         {
485           strncpy(title,c+6,size);
486           title[size]='\0';
487         }
488         if (strcasecmp(c,"ARTIST")==0)
489         {
490           strncpy(artist,c+7,size);
491           artist[size]='\0';
492         }
493         if (strcasecmp(c,"GENRE")==0)
494         {
495           for(i=0;i<ID3_NR_OF_V1_GENRES;i++)
496           {
497             strncpy(genrebuf,c+6,size);
498             genrebuf[size]='\0';
499             if (strcasecmp(ID3_v1_genre_description[i],genrebuf)==0)
500             {
501               genre=i;
502               break;
503             }
504             if (i==ID3_NR_OF_V1_GENRES) genre=0;
505           }
506         }
507         c+=strlen(c)+1+size;
508       }
509     }
510   }
511
512   fclose(fic);
513 }
514
515 #define FSN  32
516 #define MAXINO  (1<<24)
517 #define INOTYP  unsigned long
518 #define regbit_qry(x,y)  ( x[( (y) / sizeof(INOTYP) )]  &  1<<( (y) % sizeof(INOTYP) ) )
519 #define regbit_set(x,y)  ( x[( (y) / sizeof(INOTYP) )]  |=  1<<( (y) % sizeof(INOTYP) ) )
520
521 int hlink_check(struct stat*info)
522 {
523   /*
524    * for speed this subroutine should only be called
525    * - if the file has more than one hardlink
526    * - if the file is a resolved softlink
527    */
528   /* the persistent variables */
529   static INOTYP *list[FSN];
530   static dev_t name[FSN];
531   /* some temporary variables */
532   int fsn, is_registered=0;
533
534   /* assertions - in case parameters are lowered for less memory usage */
535   assert(fsn<FSN);
536   assert((info->st_ino)/sizeof(INOTYP)<MAXINO);
537   
538   /* search which internal registration number is used for this filesystem */
539   for( fsn=0; (name[fsn]!=(info->st_dev)) && (name[fsn]!=0) ; fsn++);
540
541   /* if file system is not registered yet, do it and leave */
542   if( name[fsn] == 0 ) 
543   {
544     name[fsn] = (info->st_dev);  
545     /* provide space for the bitmap that maps the inodes of this file system */
546     list[fsn] = (INOTYP*)calloc(MAXINO,sizeof(INOTYP));
547     /* no comparison is needed in empty lists ... return */
548     if(debug) fprintf(stderr, "Debug >> Linked >> Init List %04x @mem %04lx\n", (int)name[fsn], (long)&list[fsn] );
549   } else
550   {
551     /* this looks more complicated than it really is */
552     /* the idea is very simple: 
553      *  provide a bitmap that maps all inodes of a file system
554      *  to mark all files that have already been visited.
555      *  If it is already visited, do not add it to the playlist
556      */
557     /*
558      * The difficulty is as follows:
559      *   struct inode_bitmap { char registered:1; } bitmap[1<<MAXINO];
560      * would be byte-aligned and would allocate at least eight times the needed space.
561      * Feel free to change the definitions that are involved here, if you know better.
562      */
563     if(  regbit_qry( list[fsn], (info->st_ino) ) ) is_registered=1;
564     else regbit_set( list[fsn], (info->st_ino) );
565     /*
566      * the debug expression is more complicated then the working stuff
567      */
568     if(debug)
569       fprintf(stderr, "Debug >> Linked >> DEV %04x INO %06x => "
570     "list[%02x][%04x] = %04x & %04x --> %s registered\n",
571     (int)info->st_dev, (int)info->st_ino, fsn, (int)((info->st_ino)/sizeof(INOTYP)),
572           (int)list[fsn][(info->st_ino)/sizeof(INOTYP)],
573     1<<((info->st_ino)%sizeof(INOTYP)), is_registered?"Already":"Not" );
574   }
575   return is_registered;
576 }
577
578
579 void parse_file(unsigned char *newpath)
580 {
581   unsigned char *c;
582   unsigned char ext[5];
583   int j;
584
585   void print_faketitle()
586   {
587     c=newpath+strlen(newpath);
588     while (c>newpath && *c!='/') c--;
589     while (c<newpath+strlen(newpath)-5)
590     {
591       c++;
592       if (*c=='_') putchar(' '); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
593     }
594   }
595   
596   void print_path()
597   {
598     char last=0;
599     printf(prefix);
600     c=newpath+skip;
601     /* remove leading "./" when parsing current directory */
602     if (*c=='.' && *(c+1)=='/') { c+=2; last='/'; }
603     for (;*c!='\0';c++)
604     {
605       /* remove double "//" when parsing a directory ending with a "/" */
606       if (last=='/' && *c=='/') continue;
607       last=*c;
608       if (*c=='/') putchar(separator); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
609     }
610   }
611
612   for (j=0;j<5;j++) ext[j]=tolower(newpath[strlen(newpath)-4+j]);
613   artist[0]='\0';
614   title[0]='\0';
615   duration=-2;
616   if (strcmp(".mp2",ext)==0) { duration=-1; parse_mp3(newpath); }
617   if (strcmp(".mp3",ext)==0) { duration=-1; parse_mp3(newpath); }
618   if (strcmp(".mpc",ext)==0) { duration=-1; parse_mpc(newpath); }
619   if (strcmp(".mp+",ext)==0) { duration=-1; parse_mpc(newpath); }
620   if (strcmp(".ogg",ext)==0) { duration=-1; parse_ogg(newpath); }
621   if (strcmp(".wav",ext)==0) { duration=-1; /* parse_wav(newpath); */ }
622       
623       if (duration!=-2 && genrelist[genre]) /* is it an audio file ? */
624       {
625         counter++;
626         switch (format)
627         {
628           case 0:
629             if (duration!=-1)
630             {
631               printf("#EXTINF:%d,",duration);
632               if (strlen(artist)==0 && strlen(title)==0) print_faketitle();
633               else printf("%s - %s",artist,title);
634               printf("%s",eol);
635             }
636             print_path();
637             printf("%s",eol);
638             break;
639           case 1:
640             printf("File%d=",counter);
641             print_path();
642             printf("%s",eol);
643             printf("Title%d=",counter);
644             if (strlen(artist)==0 && strlen(title)==0) print_faketitle();
645             else printf("%s - %s",artist,title);
646             printf("%s",eol);
647             if (duration!=-1) printf("Length%d=%d%s",counter,duration,eol);
648             break;
649           case 2:
650             printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>",counter,artist,title);
651             if (duration==-1) printf("?"); else printf("%d:%s%d",duration/60,duration%60<10?"0":"",duration%60);
652             printf("</td></tr>%s",eol);
653             break;
654           case 3:
655             if (duration!=-1)
656             {   time_t zeit;
657                 time(&zeit);
658                 char timebuffer[256];
659                 strftime(timebuffer,255, "%a %d %b %Y %T %Z", localtime(&zeit) ); /* ctime() had a trailing CR */
660                 printf("\t<item>%s",eol);
661                 if (strlen(artist)==0 && strlen(title)==0) 
662                 {
663                   /* find a better solution for this */
664                   printf("\t\t<author>%s</author>%s\t\t<title>%s</title>%s",newpath,eol, newpath,eol );
665                 }
666                 else printf("\t\t<author>%s</author>%s\t\t<title>%s</title>%s",artist,eol, title,eol );
667                 /* you might want to add more into description --> look for a smart, not a fast program */
668                 printf("\t\t<description><![CDATA[<h4>%s</h4><h5>%s</h5><a href=\"",title,artist);
669                 print_path();
670                 printf("\">Direct Link to Audiofile</a><br>]]></description>%s",eol);
671 #if 1
672                 printf("\t\t<link>%s</link>%s",reflink(title),eol);
673 #endif
674                 printf("\t\t<pubDate>%s</pubDate>%s",timebuffer,eol);
675                 printf("\t\t<enclosure url=\"");
676                 print_path();
677                 printf("\" length=\"%d\" type=\"audio/mpeg\"/>%s\t\t<guid>",duration, eol);
678                 print_path();
679                 printf("</guid>%s",eol );
680                 printf("\t\t<itunes:duration>%d:%d:%d</itunes:duration>%s",duration/3600,(duration/60)%60,duration%60,eol);
681                 printf("\t\t<itunes:author>%s</itunes:author>%s", artist, eol);
682                 printf("\t</item>%s",eol );
683             }
684             break;
685         }
686       }
687 }
688
689 void parse_directory(unsigned char *path)
690 {
691   int i,n;
692   struct dirent **namelist;
693   unsigned char newpath[PATH_MAX];
694   struct stat infos;
695
696   if (debug)
697     fprintf(stderr,"Debug >> parsing directory : %s\n",path);
698   if (stat(path,&infos)!=0)
699   {
700     fprintf(stderr,"Warning >> can't stat entry : %s\n",path);
701     return;
702   }
703   /* check if it is a filename */
704   if (S_ISREG(infos.st_mode) || S_ISLNK(infos.st_mode))
705   {
706     parse_file(path);
707     return;
708   }
709   /* must be a directory - or something unusable like pipe, socket, etc */
710   if ((n=scandir(path,&namelist,0,alphasort))<0)
711   {
712     fprintf(stderr,"Warning >> can't open directory : %s\n",path); 
713     return;
714   }
715   for (i=0;i<n;i++)
716   {
717     sprintf(newpath,"%s/%s",path,namelist[i]->d_name);
718
719     if (stat(newpath,&infos)!=0)
720     {
721       fprintf(stderr,"Warning >> can't stat entry : %s\n",newpath);
722       continue;
723     }
724     if (recursive && S_ISDIR(infos.st_mode)
725                   && strcmp(namelist[i]->d_name,".")!=0
726                   && strcmp(namelist[i]->d_name,"..")!=0) parse_directory(newpath);
727     /* hlink_check() might be applied more selective ... avoidhlink is only a simple prereq */
728     if (S_ISREG(infos.st_mode) && ! ( avoidhlinked && hlink_check(&infos) ) )
729     {
730       parse_file(newpath);
731     }
732     free(namelist[i]);
733   }
734   free(namelist);
735 }
736
737 int main(int argc,char **argv)
738 {
739   parse_options(argc,argv);
740   if (optind==argc) usage();
741   switch (format)
742   {
743     case 0:
744       printf("#EXTM3U%s",eol);
745       break;
746     case 1:
747       printf("[playlist]%s",eol);
748       break;
749     case 2:
750       printf("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">%s%s<html>%s%s<head>%s<title>Playlist generated by FAPG " VERSION "</title>%s<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />%s<style type=\"text/css\">%s<!--%s%sbody,td,tr {%s font-family: Verdana, Arial, Helvetica, sans-serif;%s  font-size: 12px;%s  color: #000000;%s}%s%sbody {%s  background: #ffffff;%s}%s%sth {%s  text-align: center;%s  background: #ffcccc;%s  padding-left: 15px;%s  padding-right: 15px;%s  border: 1px #dd8888 solid;%s}%s%std {%s  text-align: center;%s  background: #eeeeee;%s  padding-left: 15px;%s  padding-right: 15px;%s  border: 1px #cccccc solid;%s}%s%sh1 {%s  font-size: 25px;%s}%s%sp {%s  font-size: 10px;%s}%s%sa {%s  color: #993333;%s  text-decoration: none;%s}%s%sa:hover {%s text-decoration: underline;%s}%s%s-->%s</style>%s</head>%s%s<body>%s%s<h1>Playlist</h1>%s%s<table>%s<tr><th>Entry</th><th>Artist</th><th>Title</th><th>Length</th></tr>%s",eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol);
751       break;
752     case 3:
753       { time_t zeit;
754         time(&zeit);
755         char timebuffer[256];
756         strftime(timebuffer,255, "%a %d %b %Y %T %Z", localtime(&zeit) );
757         printf("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>%s<!-- generator=\"FAPG " VERSION " -->%s<rss xmlns:itunes=\"http://www.itunes.com/DTDs/Podcast-1.0.dtd\" version=\"2.0\">%s    <channel>%s\t<title>%s - %s</title>%s\t<description>Directory Tree %s</description>%s\t<link>%s</link>%s\t<itunes:image href=\"%s/rss/podcast.jpg\"/>%s\t<lastBuildDate>%s</lastBuildDate>%s\t<generator>FAPG " VERSION "</generator>%s\t<image>%s\t\t<url>%s/podcast.jpg</url>%s\t\t<title>Server Logo</title>%s\t\t<link>%s</link>%s\t\t<description>Feed provided by FAPG. Click to visit.</description>%s\t</image>%s\t<itunes:owner>%s\t\t<itunes:name>Admin %s</itunes:name>%s\t\t<itunes:email>podcast@%s</itunes:email>%s\t</itunes:owner>%s\t<category>Various</category>%s\t<itunes:subtitle>Directory Tree %s</itunes:subtitle>%s\t<itunes:author>%s</itunes:author>%s\t<copyright>unknown</copyright>%s\t<language>%s</language>%s\t<itunes:explicit>No</itunes:explicit>%s\t<ttl>1800</ttl>%s",eol,eol,eol,eol,hostname,dir,eol,prefix,eol,base,eol,prefix,eol,timebuffer,eol,eol,eol,base,eol,eol,base,eol,eol,eol,eol,base,eol,hostname,eol,eol,eol,dir,eol,getenv("LOGNAME"),eol,eol,getenv("LANG"),eol,eol,eol);
758       }
759       break;
760   }
761   for (;optind<argc;optind++)
762   {
763 #if 0
764     /* activating skip means that all given path components are stripped */
765     /* in consequence a list of subdirs is always mapped to the same prefix */
766     /* a recommended workaround is to move to the desired subdir and avoid absolute directories in the list */
767     if (strlen(prefix)!=0) skip=strlen(argv[optind]);
768 #endif
769     parse_directory(argv[optind]);
770   }
771   switch(format)
772   {
773     case 1:
774       printf("NumberOfEntries=%d%sVersion=2%s",counter,eol,eol);
775       break;
776     case 2:
777       printf("</table>%s%s<p>Playlist generated by <a href=\"http://royale.zerezo.com/fapg/\">FAPG " VERSION "</a></p>%s%s</body>%s%s</html>",eol,eol,eol,eol,eol,eol);
778       break;
779     case 3:
780       printf("    </channel>%s</rss>%s",eol,eol);
781       break;
782   }
783   if (genrelist) free(genrelist);
784   exit(0);
785 }