4
* Sander Janssen <janssen@rendo.dekooi.nl>
6
* This software is covered by the GNU public license,
7
* which should be in a file named LICENSE acompanying
19
void pad(char *string, int length);
21
void buildtag(char *buf, char *title, char *artist, char *album, char *year, char *comment, char *genre);
22
void set_filename(int argc,char *argv[]);
24
int main(int argc, char *argv[])
27
int verbose = 0, forced = 0, burn = 0, info = 0, all = 0;
33
char filenamelook[100];
37
if (argc < 2 ) /* If nothing is given */
39
fprintf(stderr,"Mp3rename\n\nusage: [-vfhsbia] [file ...]\n\n");
43
/* Lets checkout the options */
45
while ((ch = getopt(argc, argv, "vfhsbia")) != -1)
48
case 'v': /* Verbose mode */
51
case 'f': /* Always ask mode */
54
case 'h': /* Display the help */
57
case 's': /* Set the default look */
58
set_filename(argc,argv);
60
case 'b': /* Burn modus cut of at 32 chars */
63
case 'i': /* Just the id3tag */
66
case 'a': /* Ask everything */
69
default: /* If wrong option is given */
70
fprintf(stderr,"Mp3rename\n\nusage: [-vfh] [file ...]\n\n");
75
if ( info == 1 && ( forced == 1 || verbose == 1))
77
printf("Info modus can not be used with other arguments.\n\n");
80
sprintf(str,"%s/.mp3rename",getenv("HOME")); /* Lets get the home dir */
82
if ( !( fp=fopen(str,"r") ) ) /* if we don't find our config file */
84
sprintf(filenamelook,"(&a)-&t");
85
printf("Using default look, please use the -s option to set your own look\n");
88
fgets(filenamelook, 100, fp);
90
if(burn != 1) /* If burn is on we will add the .mp3 later */
91
strcat(filenamelook,".mp3"); /* add .mp3 so that the filename will be complete */
94
char title[31]="", artist[31]="", album[31]="", year[5]="", comment[31]="", fbuf[4], newfilename[160]="",nieuw[150]="",dir[150]="",dirsource[200],fullline[228]="", burnname[29]="";
97
if ( !( fp=fopen(*argv,"rb+") ) ) /* If the file doesn exist */
99
perror("Error opening file");
100
++argv; /* Prepare for the next file */
104
/* Lets check if we have a real mp3 file */
106
fread(sig,sizeof(sig),1,fp);
109
if(!((sig[0] == 0xff) && (sig[1] == 0xf0)))
111
fprintf(stderr,"%s is not an MP3 file!\n",*argv);
117
/* Lets go to the beginning of the tag */
118
if ( fseek(fp, -128, SEEK_END ))
125
/* Lets see if we already have a id3 tag */
126
fread(fbuf,1,3,fp); fbuf[3] = '\0';
127
if (!strcmp("TAG",fbuf) && !forced)
129
fseek(fp, -125, SEEK_END);
130
fread(title,1,30,fp); title[30] = '\0';
131
fread(artist,1,30,fp); artist[30] = '\0';
132
fread(album,1,30,fp); album[30] = '\0';
133
fread(year,1,4,fp); year[4] = '\0';
134
fread(comment,1,30,fp); comment[30] = '\0';
136
fseek(fp, -128, SEEK_END); /* back to the beginning of the tag */
140
if (!strcmp("TAG",fbuf) ) /* go to the position to append one */
144
fseek(fp, -125, SEEK_END);
145
fread(title,1,30,fp); title[30] = '\0';
146
fread(artist,1,30,fp); artist[30] = '\0';
147
fread(album,1,30,fp); album[30] = '\0';
148
fread(year,1,4,fp); year[4] = '\0';
149
fread(comment,1,30,fp); comment[30] = '\0';
152
fseek(fp, -128, SEEK_END);
155
fseek(fp, 0, SEEK_END);
156
if(verbose || forced) /* Manual change of the name */
159
printf("%s hasen't got a id3 tag. \n",*argv);
161
printf("%s:\n",*argv);
164
for( i=0 ; i!=(strlen(filenamelook)) ; i++)
167
if(filenamelook[i] == '&')
169
switch(filenamelook[i+1])
172
printf("Please enter the artist's name.\n");
173
do /* Lets get the artist */
175
input_char = getchar();
176
if (input_char != '\n' && p == 0)
178
if (input_char != '\n' && input_char != EOF )
179
sprintf(artist,"%s%c",artist,input_char);
181
} while (input_char != '\n');
185
printf("Please enter the title.\n");
186
do /* Lets get the song title */
188
input_char = getchar();
189
if (input_char != '\n' && p == 0)
191
if (input_char != '\n' && input_char != EOF)
192
sprintf(title,"%s%c",title,input_char);
194
} while (input_char != '\n');
198
printf("Please enter the album.\n");
199
do /* Lets get the album */
201
input_char = getchar();
202
if (input_char != '\n' && p == 0)
204
if (input_char != '\n' && input_char != EOF)
205
sprintf(album,"%s%c",album,input_char);
207
} while (input_char != '\n');
211
printf("Please enter the year.\n");
212
do /* Lets get the year */
214
input_char = getchar();
215
if (input_char != '\n' && p == 0)
217
if (input_char != '\n' && input_char != EOF)
218
sprintf(year,"%s%c",year,input_char);
220
} while (input_char != '\n');
223
default: /* the user has entered a character behind the & we don't know */
224
printf("Illegal char in config file please use the option '-s help' for more information");
233
printf("Please enter the artist's name.\n");
234
do /* Lets get the artist */
236
input_char = getchar();
237
if (input_char != '\n' && p == 0)
239
if (input_char != '\n' && input_char != EOF )
240
sprintf(artist,"%s%c",artist,input_char);
242
} while (input_char != '\n');
244
printf("Please enter the title.\n");
245
do /* Lets get the song title */
247
input_char = getchar();
248
if (input_char != '\n' && p == 0)
250
if (input_char != '\n' && input_char != EOF)
251
sprintf(title,"%s%c",title,input_char);
253
} while (input_char != '\n');
256
printf("Please enter the album.\n");
257
do /* Lets get the album */
259
input_char = getchar();
260
if (input_char != '\n' && p == 0)
262
if (input_char != '\n' && input_char != EOF)
263
sprintf(album,"%s%c",album,input_char);
265
} while (input_char != '\n');
267
printf("Please enter the year.\n");
268
do /* Lets get the year */
270
input_char = getchar();
271
if (input_char != '\n' && p == 0)
273
if (input_char != '\n' && input_char != EOF)
274
sprintf(year,"%s%c",year,input_char);
276
} while (input_char != '\n');
279
else /* If we aren't in verbose or forced mode */
281
printf("%s hasen't got a id3 tag\n",*argv);
291
printf("Artist : %s\n",artist);
292
printf("Title : %s\n",title);
293
printf("Album : %s\n",album);
294
printf("Year : %s\n\n",year);
299
/* Remove trailing spaces */
301
while (i && artist[i]==' ')
308
while (i && title[i]==' ')
314
while (i && album[i]==' ')
320
while (i && year[i]==' ')
326
/* We go through the filenamelook until we find a &x combination
327
then we replace the &x with album/title/year/artis */
328
for( i=0 ; i!=(strlen(filenamelook)) ; i++)
330
if(filenamelook[i] == '&')
333
switch(filenamelook[i+1])
336
sprintf(tmp,"%s%s",newfilename,artist);
337
strcpy(newfilename,tmp);
341
sprintf(tmp,"%s%s",newfilename,title);
342
strcpy(newfilename,tmp);
346
sprintf(tmp,"%s%s",newfilename,album);
347
strcpy(newfilename,tmp);
351
sprintf(tmp,"%s%s",newfilename,year);
352
strcpy(newfilename,tmp);
356
printf("Illegal char in config file please use the option '-s help' for more information\n");
361
else /* otherwise we just print the character in the filenamelook in the newfilename */
364
sprintf(tmp,"%s%c",newfilename,filenamelook[i]);
365
strcpy(newfilename,tmp);
369
/* Lets find out what directory we are working in */
371
sprintf(dirsource,"%s",*argv);
372
i=strlen(dirsource)+1;
375
if(dirsource[i] == '/')
378
dir[i] = dirsource[i];
384
/* Build the new tag from the new names */
386
buildtag(fullline,title,artist,album,year,comment,genre);
387
fwrite(fullline,1,128,fp);
391
/* Lets catch illegal characters */
392
for (i=0; i < strlen(newfilename); i++)
394
switch (newfilename[i])
396
case '<': newfilename[i] = '[';
398
case '>': newfilename[i] = ']';
400
case '|': newfilename[i] = '_';
402
case '/': newfilename[i] = '-';
404
case '\\': newfilename[i]= '-';
406
case '*': newfilename[i] = '_';
408
case '?': newfilename[i] = '_';
410
case ':': newfilename[i] = ';';
412
case '"': newfilename[i] = '-';
418
/* Lets rename the file */
419
if(burn == 1) /* If burn is on the size */
420
{ /* shouldn't be bigger than 32 chars including the .mp3 */
421
strncpy(burnname,newfilename,sizeof(burnname)-1);
422
sprintf(nieuw,"%s%s.mp3",dir,burnname);
426
sprintf(nieuw,"%s%s",dir,newfilename);
428
if(rename(*argv,nieuw))
429
printf("Error renaming %s\n",*argv);
431
++argv; /* Prepare for the next */
439
void buildtag(char *buf, char *title, char *artist, char *album, char *year, char *comment, char *genre)
444
strncat(buf,title,30);
446
strncat(buf,artist,30);
448
strncat(buf,album,30);
452
strncat(buf,comment,30);
453
strncat(buf,genre,1);
456
void pad(char *string, int length)
472
printf("Mp3rename 0.6\n\n");
473
printf("Options:\n");
474
printf("\t-f\t Force non id3 rename.\n");
475
printf("\t-v\t Verbose mode.\n");
476
printf("\t-h\t Display this help message.\n");
477
printf("\t-b\t Limit the file size to 32 chars.\n");
478
printf("\t-i\t Only show the id3tags.\n");
479
printf("\t-a\t Ask everything for the id3tag.\n\n");
480
printf("\t-s\t Set the default filename look.\n");
481
printf("\t \t for more help on this option: -s help\n\n");
482
printf("Sander Janssen <janssen@rendo.dekooi.nl>\n\n");
485
void set_filename(int argc,char *argv[])
494
printf("No config format given\n\r");
498
if(!strcmp(*argv,"help"))
500
printf("Mp3rename 0.6\n\n");
501
printf(" Use this option to set the default look of the file\n");
502
printf(" The information is saved in ~/.mp3rename\n");
503
printf(" You can use &t title, &b album, &y year and &a artist\n\n");
504
printf(" Example : mp3rename -s '(&a)-&t-&b'\n");
505
printf(" for (artist)-title-album.mp3\n\n");
508
sprintf(str,"%s/.mp3rename",getenv("HOME"));
510
if ( !( fp=fopen(str,"w") ) )
512
printf("Error opening config file\n\r");
516
if(!strcmp(*argv,""))
518
printf("No config format given\n\r");
521
printf("Default is now set\n\n");