8a6667d848eccef1649b746bbfe80472357bb149
[FAPG] / fapg.c
1 /*
2  * FAPG version 0.31
3  *
4  * FAPG means Fast Audio Playlist Generator.
5  * It is a tool to generate list of audio files (Wav, MP3, Ogg, etc)
6  * in various formats (M3U, PLS, HTML, etc).
7  * It is very usefull if you have a large amount of audio files
8  * and you want to quickly and frequently build a playlist.
9  *
10  * Copyright (C) 2003-2004  Antoine Jacquet <royale@zerezo.com>
11  * http://royale.zerezo.com/fapg/
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <getopt.h>
31 #include <dirent.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <limits.h>
37 #include <unistd.h>
38 #include <ctype.h>
39 #include <assert.h>
40 #include "genres.h"
41
42 #define MP3_BASE 1024
43 #define OGG_BASE 1024*10
44 #define MAX 1024*200 /* 200ko for ID3 with JPEG images in it */
45
46 int debug=0;
47 int format=0; /* 0 = m3u ; 1 = pls ; 2 = html */
48 char *genrelist=NULL;
49 unsigned char *prefix="";
50 int recursive=0;
51 int avoidhlinked=0;
52 int separator='/';
53 int skip=0;
54 int windows=0;
55 unsigned char *eol="\n";
56 unsigned char buffer[MAX];
57
58 int counter=0;
59
60 unsigned char artist[1024];
61 unsigned char title[1024];
62 unsigned char genrebuf[1024];
63 unsigned char genre=0;
64 int duration;
65
66 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};
67
68 void usage()
69 {
70   fprintf(stderr,"Usage >> fapg [-b|--backslash] [-d|--debug] [-f|--format=m3u|pls|html] [-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");
71   exit(1);
72 }
73
74 void parse_options(int argc,char **argv)
75 {
76   static char const short_options[]="bdf:g:lo:np:rwx:";
77   static struct option long_options[]=
78   {
79     {"backslash",no_argument,NULL,'b'},
80     {"debug",no_argument,NULL,'d'},
81     {"format",required_argument,NULL,'f'},
82     {"genre",required_argument,NULL,'g'},
83     {"nohardlink",no_argument,NULL,'n'},
84     {"output",required_argument,NULL,'o'},
85     {"prefix",required_argument,NULL,'p'},
86     {"recursive",no_argument,NULL,'r'},
87     {"windows",no_argument,NULL,'w'},
88     {"exclude",required_argument,NULL,'x'}
89   };
90   int c;
91   int option_index=0;
92   while ((c=getopt_long(argc,argv,short_options,long_options,&option_index))!=-1)
93   {
94     switch(c)
95     {
96       case 'b':
97         separator='\\';
98         break;
99       case 'd':
100         debug=1;
101         break;
102       case 'f':
103         if (strcmp(optarg,"m3u")==0)  format=0; else
104         if (strcmp(optarg,"pls")==0)  format=1; else
105         if (strcmp(optarg,"html")==0) format=2; else
106         usage();
107         break;
108       case 'g':
109         if (genrelist==NULL) genrelist=calloc(257,sizeof(char)); /* allow multiple includes/excludes */
110         if (genrelist==NULL) { fprintf(stderr,"Error >> unable to allocate cleared memory\n"); exit(2); }
111         else
112         {
113           int n=0; 
114           while (n<strlen(optarg))
115           {
116             if (debug) fprintf(stderr,"Debug >> genrelist entry activting : %d\n",atoi(&optarg[n]));
117             genrelist[atoi(&optarg[n])]=1;
118             while (isdigit(optarg[n++]));
119           }
120         }
121         break;
122       case 'n':
123         avoidhlinked=1;
124         break;
125       case 'o':
126         close(1);
127         if (fopen(optarg,"w")==NULL) { fprintf(stderr,"Error >> unable to open output file : %s\n",optarg); exit(2); }
128         break;
129       case 'p':
130         prefix=malloc(strlen(optarg)+1);
131         strcpy(prefix,optarg);
132         break;
133       case 'r':
134         recursive=1;
135         break;
136       case 'w':
137         windows=1;
138         eol="\r\n";
139         break;
140       case 'x':
141         if (genrelist==NULL) /* allow multiple includes/excludes - not recommended (confusing) but possible */
142         {
143           int n=0; 
144           genrelist=calloc(257,sizeof(char));
145           while (n<256) genrelist[n++]=1;
146         }
147         if (genrelist==NULL) { fprintf(stderr,"Error >> unable to allocate cleared memory\n"); exit(2); }
148         else
149         {
150           int n=0; 
151           while (n<strlen(optarg))
152           {
153             if (debug) fprintf(stderr,"Debug >> genrelist entry activting : %d\n",atoi(&optarg[n]));
154             genrelist[atoi(&optarg[n])]=0;
155             while (isdigit(optarg[n++]));
156           }
157         }
158         break;
159       default:
160         usage();
161     }
162   }
163   if (genrelist==NULL)
164   {
165     genrelist=calloc(257,sizeof(char));
166     if (genrelist==NULL) { fprintf(stderr,"Error >> unable to allocate cleared memory\n"); exit(2); }
167     else
168     { 
169       int n=0; 
170       while (n<256) genrelist[n++]=1;
171     }
172   }
173 }
174
175 void parse_mp3(unsigned char *file)
176 {
177   int bitrates[2][3][15]=
178     {{{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448},
179       {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
180       {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}},
181      {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},
182       {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
183       {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}}};
184   FILE *fic;
185   unsigned char *c;
186   int lus;
187
188   genre=0;
189   genrebuf[0]=0;
190   if (debug) fprintf(stderr,"Debug >> parsing mp3 : %s\n",file);
191
192   /* read header */
193   if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }
194   lus=fread(buffer,1,MP3_BASE,fic);
195   c=buffer;  
196
197   /* try ID3v2 */
198   if (buffer[0]=='I' && buffer[1]=='D' && buffer[2]=='3')
199   {
200     int size;
201     int version;
202     version=*(buffer+3);
203     if (version<2 || version>4)
204       fprintf(stderr,"Warning >> ID3 v2.%d not implemented ! trying anyway : %s\n",version,file);
205     if (*(buffer+5)!=0)
206       fprintf(stderr,"Warning >> specials headers not implemented (%d) ! trying anyway : %s\n",*(buffer+5),file);
207     c=buffer+6;
208     size=(*c<<21)+(*(c+1)<<14)+(*(c+2)<<7)+(*(c+3));
209     /* read more header */
210     if (size+lus>MAX)
211     {
212       lus+=fread(buffer+lus,1,MAX-lus,fic);
213       fprintf(stderr,"Warning >> ID3 header is huge (%d bytes) ! trying anyway : %s\n",size,file);
214     }
215     else
216       lus+=fread(buffer+lus,1,size,fic);
217     if (size>lus) size=lus;
218     c+=4;
219     if (version==2) while (c<buffer+size)
220     {
221       int size=(*(c+3)<<16)+(*(c+4)<<8)+(*(c+5));
222       if (*c==0) break;
223       if (strncmp(c,"TT2",3)==0)
224       {
225         strncpy(title,c+7,size-1);
226         title[size-1]='\0';
227       }
228       if (strncmp(c,"TP1",3)==0)
229       {
230         strncpy(artist,c+7,size-1);
231         artist[size-1]='\0';
232       }
233       if (strncmp(c,"TCO",3)==0)
234       {
235         /* strncpy(genrebuf,c+7,size-1);*/
236         /* genrebuf[size-1]='\0'; */
237         /* genre=atoi(&genrebuf[1]); */
238         genre=atoi(c+8);
239       }
240       c+=size+6;
241     }
242     if (version==3 || version==4) while (c<buffer+size)
243     {
244       int size=(*(c+4)<<24)+(*(c+5)<<16)+(*(c+6)<<8)+(*(c+7));
245       if (*c==0) break;
246       if (strncmp(c,"TIT2",4)==0)
247       {
248         strncpy(title,c+11,size-1);
249         title[size-1]='\0';
250       }
251       if (strncmp(c,"TPE1",4)==0)
252       {
253         strncpy(artist,c+11,size-1);
254         artist[size-1]='\0';
255       }
256       if (strncmp(c,"TCON",4)==0)
257       {
258         /* strncpy(genrebuf,c+11,size-1); */
259         /* genrebuf[size-1]='\0'; */
260         /* genre=atoi(&genrebuf[1]); */
261         genre=atoi(c+12);
262       }
263       c+=size+10;
264     }
265   }
266   
267   while (c<buffer+lus-10)
268   {
269     if (*c==0xFF && (*(c+1)&0xF0)==0xF0)
270     {
271       int version;
272       int lay;
273       int bitrate_index;
274       int bitrate;
275       version=2-(*(c+1)>>3&1);
276       lay=4-(*(c+1)>>1&3);
277       bitrate_index=*(c+2)>>4&0xF;
278       if (version>=1 && version<=2 && lay-1>=0 && lay-1<=2 && bitrate_index>=0 && bitrate_index<=14)
279         bitrate=bitrates[version-1][lay-1][bitrate_index];
280       else
281         bitrate=0;
282       if (bitrate!=0)
283       {
284         fseek(fic,0,SEEK_END);
285         duration=(ftell(fic)+buffer-c)/125/bitrate;
286       }
287       else
288         duration=0;
289       break;
290     }
291     c++;
292   }
293
294   /* try ID3v1 */
295   if (strlen(artist)==0 && strlen(title)==0)
296   {
297     fseek(fic,-128,SEEK_END);
298     lus=fread(buffer,1,128,fic);
299     if (lus==128 && buffer[0]=='T' && buffer[1]=='A' && buffer[2]=='G')
300     {
301       strncpy(title,buffer+3,30);
302       title[30]='\0';
303       c=title+29;
304       while (c>title && *c==' ') *(c--)='\0';
305       strncpy(artist,buffer+33,30);
306       artist[30]='\0';
307       c=artist+29;
308       while (c>artist && *c==' ') *(c--)='\0';
309       /* strncpy(album,buffer+65,30); */
310       /* strncpy(year,buffer+97,4); */
311       /* strncpy(comment,buffer+101,30); */
312       /* strncpy(genrebuf,buffer+127,1); genre[1]=0; */
313       genre=buffer[127];
314     }
315   }
316
317   fclose(fic);
318 }  
319
320 void parse_ogg(unsigned char *file)
321 {
322   FILE *fic;
323   unsigned char *c;
324   int lus;
325   int sample_rate;
326   int samples;
327
328   if (debug) fprintf(stderr,"Debug >> parsing ogg : %s\n",file);
329
330   /* read header */
331   if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }
332   lus=fread(buffer,1,OGG_BASE,fic);
333
334   /* try Ogg */
335   if (buffer[0]!='O' && buffer[1]!='g' && buffer[2]!='g')
336   {
337     fprintf(stderr,"Warning >> not a Ogg header : %s\n",file);
338     return;
339   }
340   
341   c=buffer+0x28;
342   sample_rate=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
343
344   while (c<buffer+lus-10)
345   {
346     int size;
347     if (strncasecmp(c,"TITLE=",6)==0)
348     {
349       size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);
350       strncpy(title,c+6,size-6);
351       title[size-6]='\0';
352       c+=size;
353     }
354     if (strncasecmp(c,"ARTIST=",7)==0)
355     {
356       size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);
357       strncpy(artist,c+7,size-7);
358       artist[size-7]='\0';
359       c+=size;
360     }
361     if (strncasecmp(c,"GENRE=",6)==0)
362     {
363       static int i=0;
364       size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);
365       strncpy(genrebuf,c+6,size-6);
366       genrebuf[size-6]='\0';
367       c+=size;
368       for(i=0;i<ID3_NR_OF_V1_GENRES;i++)
369       {
370         if (strcasecmp(ID3_v1_genre_description[i],genrebuf)==0)
371         {
372           genre=i;
373           break;
374         }
375         if (i==ID3_NR_OF_V1_GENRES) genre=0;
376       }
377     }
378     c++;
379   }
380   
381   fseek(fic,-OGG_BASE,SEEK_END);
382   lus=fread(buffer,1,OGG_BASE,fic);
383   c=buffer+lus-1;
384   while (strncmp(c,"OggS",4)!=0 && c>buffer) c--;
385   if (c!=buffer)
386   {
387     c+=6;
388     samples=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
389     duration=samples/sample_rate;
390   }
391   
392   fclose(fic);
393 }
394
395 void parse_mpc(unsigned char *file)
396 {
397   FILE *fic;
398   unsigned char *c;
399   int lus;
400   int sample_rates[4]={44100,48000,37800,32000};
401   int frame_count;
402   int size,items;
403   int i;
404
405   if (debug) fprintf(stderr,"Debug >> parsing mpc : %s\n",file);
406
407   /* read header */
408   if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }
409   lus=fread(buffer,1,12,fic);
410
411   /* try Musepack */
412   if (buffer[0]!='M' && buffer[1]!='P' && buffer[2]!='+')
413   {
414     fprintf(stderr,"Warning >> not a Musepack header : %s\n",file);
415     return;
416   }
417   
418   /* only version 7 */
419   if (buffer[3]!=7)
420   {
421     fprintf(stderr,"Warning >> only Musepack SV7 supported : %s\n",file);
422     return;
423   }
424   
425   /* duration */
426   c=buffer+4;
427   frame_count=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
428   c+=5;
429   duration=frame_count*1152/sample_rates[*c&3];
430   
431   /* try APETAGEX footer */
432   fseek(fic,-32,SEEK_END);
433   lus=fread(buffer,1,32,fic);
434   if (lus==32 && strncmp(buffer,"APETAGEX",8)==0)
435   {
436     c=buffer+12;
437     size=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
438     size+=32;
439     c+=4;
440     items=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
441     fseek(fic,-size,SEEK_END);
442     lus=fread(buffer,1,size,fic);
443     if (lus==size && strncmp(buffer,"APETAGEX",8)==0)
444     {
445       c=buffer+32;
446       while (items--)
447       {
448         size=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
449         c+=8;
450         if (strcasecmp(c,"TITLE")==0)
451         {
452           strncpy(title,c+6,size);
453           title[size]='\0';
454         }
455         if (strcasecmp(c,"ARTIST")==0)
456         {
457           strncpy(artist,c+7,size);
458           artist[size]='\0';
459         }
460         if (strcasecmp(c,"GENRE")==0)
461         {
462           for(i=0;i<ID3_NR_OF_V1_GENRES;i++)
463           {
464             strncpy(genrebuf,c+6,size);
465             genrebuf[size]='\0';
466             if (strcasecmp(ID3_v1_genre_description[i],genrebuf)==0)
467             {
468               genre=i;
469               break;
470             }
471             if (i==ID3_NR_OF_V1_GENRES) genre=0;
472           }
473         }
474         c+=strlen(c)+1+size;
475       }
476     }
477   }
478
479   fclose(fic);
480 }
481
482 #define FSN  8
483 #define MAXINO  (1<<22)
484 #define INOTYP  unsigned long
485 #define regbit_qry(x,y)  ( x[( (y) / sizeof(INOTYP) )]  &  1<<( (y) % sizeof(INOTYP) ) )
486 #define regbit_set(x,y)  ( x[( (y) / sizeof(INOTYP) )]  |=  1<<( (y) % sizeof(INOTYP) ) )
487
488 int hlink_check(struct stat*info)
489 {
490   /*
491    * for speed this subroutine should only be called
492    * - if the file has more than one hardlink
493    * - if the file is a resolved softlink
494    */
495   /* the persistent variables */
496   static INOTYP *list[FSN];
497   static dev_t name[FSN];
498   /* some temporary variables */
499   int fsn, is_registered=0;
500
501   /* assertions - in case parameters are lowered for less memory usage */
502   assert(fsn<FSN);
503   assert((info->st_ino)/sizeof(INOTYP)<MAXINO);
504   
505   /* search which internal registration number is used for this filesystem */
506   for( fsn=0; (name[fsn]!=(info->st_dev)) && (name[fsn]!=0) ; fsn++);
507
508   /* if file system is not registered yet, do it and leave */
509   if( name[fsn] == 0 ) 
510   {
511     name[fsn] = (info->st_dev);  
512     /* provide space for the bitmap that maps the inodes of this file system */
513     list[fsn] = (INOTYP*)calloc(MAXINO,sizeof(INOTYP));
514     /* no comparison is needed in empty lists ... return */
515     if(debug) fprintf(stderr, "Debug >> Linked >> Init List %04x @mem %04lx\n", (int)name[fsn], (long)&list[fsn] );
516   } else
517   {
518     /* this looks more complicated than it really is */
519     /* the idea is very simple: 
520      *  provide a bitmap that maps all inodes of a file system
521      *  to mark all files that have already been visited.
522      *  If it is already visited, do not add it to the playlist
523      */
524     /*
525      * The difficulty is as follows:
526      *   struct inode_bitmap { char registered:1; } bitmap[1<<MAXINO];
527      * would be byte-aligned and would allocate at least eight times the needed space.
528      * Feel free to change the definitions that are involved here, if you know better.
529      */
530     if(  regbit_qry( list[fsn], (info->st_ino) ) ) is_registered=1;
531     else regbit_set( list[fsn], (info->st_ino) );
532     /*
533      * the debug expression is more complicated then the working stuff
534      */
535     if(debug)
536       fprintf(stderr, "Debug >> Linked >> DEV %04x INO %06x => "
537     "list[%02x][%04x] = %04x & %04x --> %s registered\n",
538     (int)info->st_dev, (int)info->st_ino, fsn, (int)((info->st_ino)/sizeof(INOTYP)),
539           (int)list[fsn][(info->st_ino)/sizeof(INOTYP)],
540     1<<((info->st_ino)%sizeof(INOTYP)), is_registered?"Already":"Not" );
541   }
542   return is_registered;
543 }
544
545 void parse_directory(unsigned char *path)
546 {
547   int i,n;
548   struct dirent **namelist;
549   unsigned char newpath[PATH_MAX];
550   unsigned char *c;
551   struct stat infos;
552
553   void print_faketitle()
554   {
555     c=newpath+strlen(newpath);
556     while (c>newpath && *c!='/') c--;
557     while (c<newpath+strlen(newpath)-5)
558     {
559       c++;
560       if (*c=='_') putchar(' '); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
561     }
562   }
563   
564   void print_path()
565   {
566     printf(prefix);
567     for (c=newpath+skip;*c!='\0';c++) if (*c=='/') putchar(separator); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
568   }
569
570   if (debug) fprintf(stderr,"Debug >> parsing directory : %s\n",path);
571   if ((n=scandir(path,&namelist,0,alphasort))<0) { fprintf(stderr,"Warning >> can't open directory : %s\n",path); return; }
572   for (i=0;i<n;i++)
573   {
574     sprintf(newpath,"%s/%s",path,namelist[i]->d_name);
575     if (stat(newpath,&infos)!=0) { fprintf(stderr,"Warning >> can't stat file : %s\n",newpath); continue; }
576     if (recursive && S_ISDIR(infos.st_mode) && strcmp(namelist[i]->d_name,".")!=0 && strcmp(namelist[i]->d_name,"..")!=0) parse_directory(newpath);
577     /* hlink_check() might be applied more selective ... avoidhlink is only a simple prereq */
578     if (S_ISREG(infos.st_mode) && ! ( avoidhlinked && hlink_check(&infos) ) )
579     {
580       unsigned char ext[5];
581       int j;
582       for (j=0;j<5;j++) ext[j]=tolower(namelist[i]->d_name[strlen(namelist[i]->d_name)-4+j]);
583       artist[0]='\0';
584       title[0]='\0';
585       duration=-2;
586       if (strcmp(".mp2",ext)==0) { duration=-1; parse_mp3(newpath); }
587       if (strcmp(".mp3",ext)==0) { duration=-1; parse_mp3(newpath); }
588       if (strcmp(".mpc",ext)==0) { duration=-1; parse_mpc(newpath); }
589       if (strcmp(".mp+",ext)==0) { duration=-1; parse_mpc(newpath); }
590       if (strcmp(".ogg",ext)==0) { duration=-1; parse_ogg(newpath); }
591       if (strcmp(".wav",ext)==0) { duration=-1; /* parse_wav(newpath); */ }
592       
593       if (duration!=-2 && genrelist[genre]) /* is it an audio file ? */
594       {
595         counter++;
596         switch (format)
597         {
598           case 0:
599             if (duration!=-1)
600             {
601               printf("#EXTINF:%d,",duration);
602               if (strlen(artist)==0 && strlen(title)==0) print_faketitle();
603               else printf("%s - %s",artist,title);
604               printf("%s",eol);
605             }
606             print_path();
607             printf("%s",eol);
608             break;
609           case 1:
610             printf("File%d=",counter);
611             print_path();
612             printf("%s",eol);
613             printf("Title%d=",counter);
614             if (strlen(artist)==0 && strlen(title)==0) print_faketitle();
615             else printf("%s - %s",artist,title);
616             printf("%s",eol);
617             if (duration!=-1) printf("Length%d=%d%s",counter,duration,eol);
618             break;
619           case 2:
620             printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>",counter,artist,title);
621             if (duration==-1) printf("?"); else printf("%d:%s%d",duration/60,duration%60<10?"0":"",duration%60);
622             printf("</td></tr>%s",eol);
623             break;
624         }
625       }
626     }
627     free(namelist[i]);
628   }
629   free(namelist);
630 }
631
632 int main(int argc,char **argv)
633 {
634   parse_options(argc,argv);
635   if (optind==argc) usage();
636   switch (format)
637   {
638     case 0:
639       printf("#EXTM3U%s",eol);
640       break;
641     case 1:
642       printf("[playlist]%s",eol);
643       break;
644     case 2:
645       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 0.31</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);
646       break;
647   }
648   for (;optind<argc;optind++)
649   {
650     if (strlen(prefix)!=0) skip=strlen(argv[optind]);
651     parse_directory(argv[optind]);
652   }
653   switch(format)
654   {
655     case 1:
656       printf("NumberOfEntries=%d%sVersion=2%s",counter,eol,eol);
657       break;
658     case 2:
659       printf("</table>%s%s<p>Playlist generated by <a href=\"http://royale.zerezo.com/fapg/\">FAPG 0.31</a></p>%s%s</body>%s%s</html>",eol,eol,eol,eol,eol,eol);
660       break;
661   }
662   if (genrelist) free(genrelist);
663   exit(0);
664 }