File: id3.c

package info (click to toggle)
id3 0.15-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 152 kB
  • ctags: 74
  • sloc: ansic: 701; makefile: 90
file content (328 lines) | stat: -rw-r--r-- 9,546 bytes parent folder | download | duplicates (2)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
92
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* id3 - an ID3 tag editor
 * Copyright (c) 1998,1999,2000 Robert Woodcock <rcw@debian.org>
 * This code is hereby licensed for public consumption under either the
 * GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *                                 
 * genre.h is (c) 1998 Robert Alto <badcrc@tscnet.com> and licensed only
 * under the GPL.
 */

const char version[]="0.15";

const char usage[]= "usage: id3 -[tTaAycg] \"value\" file1 [file2...]\n\
       id3 -d file1 [file2...]\n\
       id3 -l file1 [file2...]\n\
       id3 -L\n\
       id3 -v\n\
 -t   Modifies a Title tag\n\
 -T   Modifies a Track tag\n\
 -a   Modifies an Artist tag\n\
 -A   Modifies an Album tag\n\
 -y   Modifies a Year tag\n\
 -c   Modifies a Comment tag\n\
 -g   Modifies a Genre tag\n\
 -l   Lists an ID3 tag\n\
 -L   Lists all genres\n\
 -R   Uses an rfc822-style format for output\n\
 -d   Deletes an ID3 tag\n\
 -h   Displays this help info\n\
 -v   Prints version info\n";

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "genre.h"


struct id3 {
	char tag[3];
	char title[30];
	char artist[30];
	char album[30];
	char year[4];
	/* With ID3 v1.0, the comment is 30 chars long */
	/* With ID3 v1.1, if comment[28] == 0 then comment[29] == tracknum */
	char comment[30];
	unsigned char genre;
};

int main (int argc, char *argv[])
{
	int deletetag=0, listtag=0, rfc822style=0, hastag=0, notdir=0;
	int v11tag=0, i, r, exitcode=0;
	int matches, lettersmatched; /* genre lazy matching */
	int newtitle=0, newartist=0, newalbum=0;
	int newyear=0, newcomment=0, newgenre=0;
	int newtrack=0;
	struct id3 oldid3, newid3;
	struct stat finfo;
	FILE *fp;
	
	memset(&newid3, 0, 127);
	newid3.genre=255;
	
	if (argc < 2) {
		fprintf(stderr, "%s", usage);
		exit(1);
	}
	
	while (1) {
		r = getopt(argc, argv, "dhlLRvt:T:a:A:y:c:g:");
		if (r == -1) break;
		switch (r) {
		case 'd': /* delete a tag */
			deletetag=1;
			break;
		case 'l': /* list a tag */
			listtag=1;
			break;
		case 'L': /* list all genres */
		        for (i=0; i<genre_count; i++)
		        	printf("%3d: %s\n", i, genre_table[i]);
		        exit(0);
		        break;
		case 'R': /* list tags in rfc822-style format */
		        rfc822style=1;
		        break;
		case 't': /* Title */
			memcpy(newid3.title, optarg, 30);
			for (i=strlen(optarg);i<30;i++) newid3.title[i]=0;
			newtitle=1;
			break;
		case 'T': /* Track */
			if (isdigit(optarg[0])) {
				newid3.comment[28] = 0;
				newid3.comment[29] = (unsigned char)atoi(optarg);
				newtrack=1;
			} else {
				fprintf(stderr, "%s: Track: %s: Expected a number.\n", argv[0], optarg);
				exit(1);
			}
			break;
		case 'a': /* Artist */
			memcpy(newid3.artist, optarg, 30);
			for (i=strlen(optarg);i<30;i++) newid3.artist[i]=0;
			newartist=1;
			break;
		case 'A': /* Album */
			memcpy(newid3.album, optarg, 30);
			for (i=strlen(optarg);i<30;i++) newid3.album[i]=0;
			newalbum=1;
			break;
		case 'y': /* Year */
			memcpy(newid3.year, optarg, 4);
			for (i=strlen(optarg);i<4;i++) newid3.year[i]=0;
			newyear=1;
			break;
		case 'c': /* Comment */
			memcpy(newid3.comment, optarg, 28);
			for (i=strlen(optarg);i<28;i++) newid3.comment[i]=0;
			newcomment=1;
			break;
		case 'g': /* Genre */
			if (isdigit(optarg[0])) { /* genre by number */
				newid3.genre = (unsigned char)atoi(optarg);
			} else { /* genre by name */
				matches=3; /* don't trip the first time */
				lettersmatched=0;
				/* lazy match - keep comparing down the list until we only get one hit */
				for (lettersmatched=0; matches>1; lettersmatched++) {
					for (i=matches=0; i<genre_count; i++) {
						/* Kludge to avoid infinite loop with "-g Fusion" */
						if (i == 84) i++;
						if (strncasecmp(genre_table[i], optarg, lettersmatched) == 0) {
							matches++;
							newid3.genre = (unsigned char)i;
						}
					}
				}
				if (matches == 0) { /* no hits - complain to user */
					fprintf(stderr, "%s: No such genre '%s'.\n", argv[0], optarg);
					exit(1);
				}	
			}	
			newgenre=1;
			break;
		case 'v': /* Version info */
			printf("This is id3 v%s.\n", version);
			exit(0);
			break;
		case 'h': /* Help info */
			printf("%s", usage);
			exit(0);
			break;
		case ':': /* need a value for an option */
		case '?': /* unknown option */
			fprintf(stderr, "%s", usage);
			exit(1);
		}
	}
	
	if (optind >= argc) {
		fprintf(stderr, "%s: Need a filename to work on.\n%s", argv[0], usage);
		exit(1);
	}
	
	for (i=optind; i<argc; i++) {
		notdir=hastag=1; /* innocent until proven guilty */
		if (newtitle || newartist || newalbum || newyear ||
			newcomment || newtrack || newgenre || deletetag) {
			fp = fopen(argv[i], "r+"); /* read/write */
		} else {
			fp = fopen(argv[i], "r"); /* read only */
		}
		if (fp == NULL) { /* file didn't open */
			fprintf(stderr, "%s: fopen: %s: ", argv[0], argv[i]);
			perror("");
			exitcode=1;
			continue;
		}
		fstat(fileno(fp), &finfo);
		if (S_ISDIR(finfo.st_mode)) notdir=0;
		
		if (fseek(fp, -128, SEEK_END) < 0) {
			/* problem rewinding */
			hastag=0;
		} else { /* we rewound successfully */ 
			if (fread(&oldid3, 128, 1, fp) < 0) {
				/* read error */
				fprintf(stderr, "%s: fread: %s: ", argv[0], argv[i]);
				perror("");
				exitcode=1;
				hastag=0;
			}
		}
		
		/* This simple detection code has a 1 in 16777216
		 * chance of misrecognizing or deleting the last 128
		 * bytes of your mp3 if it isn't tagged. ID3 ain't
		 * world peace, live with it.
		 */
		
		if (strncmp(oldid3.tag, "TAG", 3)) hastag=0;
		
		/* v1.1 tag == true if comment[28] == 0 */
		v11tag = !oldid3.comment[28];
		
		if (!hastag) {
			memset(&oldid3, 0, 127);
			oldid3.genre=255;
		}

		if (listtag) {
			if (rfc822style) {
				printf("\nFilename: %s\n", argv[i]);
				printf("Title: %-30.30s\n", oldid3.title);
				printf("Artist: %-30.30s\n", oldid3.artist);
				printf("Album: %-30.30s\n", oldid3.album);
				printf("Year: %-4.4s\n", oldid3.year);
				printf("Genre: %s (%d)\n",
					(oldid3.genre < genre_count) ?
					genre_table[oldid3.genre] : "Unknown", oldid3.genre);
				if (v11tag)
					printf("Track: %d\nComment: %-28.28s\n",
						oldid3.comment[29], oldid3.comment);
				else
					printf("Comment: %-30.30s\n", oldid3.comment);	
			} else {
				printf("%s:", argv[i]);
				if (hastag) {
					printf("\nTitle  : %-30.30s  Artist: %-30.30s\n",
						oldid3.title, oldid3.artist);
					printf("Album  : %-30.30s  Year: %-4.4s, Genre: %s (%d)\n",
						oldid3.album, oldid3.year, 
						(oldid3.genre < genre_count)
						? genre_table[oldid3.genre] : 
						"Unknown", oldid3.genre);
        				if (v11tag)
        					printf("Comment: %-28.28s    Track: %d\n", oldid3.comment, oldid3.comment[29]);
        				else
        					printf("Comment: %-30.30s\n", oldid3.comment);
				} else {
					if (notdir) {
						printf(" No ID3 tag.\n");
					} else {
						printf(" Directory.\n");
					}
				}
			}
		}
		
		if (hastag && deletetag) {
			if (ftruncate(fileno(fp), ftell(fp)-128) < 0) {
				fprintf(stderr, "%s: ftruncate: %s: ", argv[0], argv[i]);
				perror("");
				exitcode=1;
			}
			fclose(fp);
			continue;
		}
		
		strncpy(newid3.tag, "TAG", 3);
		if (!newtitle) memcpy(&newid3.title, &oldid3.title, 30);		
		if (!newartist) memcpy(&newid3.artist, &oldid3.artist, 30);
		if (!newalbum) memcpy(&newid3.album, &oldid3.album, 30);
		if (!newyear) memcpy(&newid3.year, &oldid3.year, 4);
		if (!newcomment) memcpy(&newid3.comment, &oldid3.comment, 28);
		if (!newtrack) { memcpy(&newid3.comment[28], &oldid3.comment[28], 2); }
		if (!newgenre) memcpy(&newid3.genre, &oldid3.genre, 1);
		v11tag = !newid3.comment[28];


		if (newtitle || newartist || newalbum || newyear || newcomment || newgenre || newtrack) {
			/* something did change */
			if (rfc822style) {
				printf("\nFilename: %s\n", argv[i]);
				printf("Title: %-30.30s\n", newid3.title);
				printf("Artist: %-30.30s\n", newid3.artist);
				printf("Album: %-30.30s\n", newid3.album);
				printf("Year: %-4.4s\n", newid3.year);
				printf("Genre: %s (%d)\n",
					(newid3.genre < genre_count) ?
					genre_table[newid3.genre] : "Unknown", newid3.genre);
				if (v11tag)
					printf("Track: %d\nComment: %-28.28s\n",
						newid3.comment[29], newid3.comment);
				else
					printf("Comment: %-30.30s\n", newid3.comment);	
			} else {
				printf("Title  : %-30.30s  Artist: %-30.30s\n", newid3.title, newid3.artist);
				printf("Album  : %-30.30s  Year: %-4.4s, Genre: %s (%d)\n",
					newid3.album, newid3.year,
					(newid3.genre < genre_count) ?
					genre_table[newid3.genre] : "Unknown", newid3.genre);
				if (v11tag)
					printf("Comment: %-28.28s    Track: %d\n", newid3.comment, newid3.comment[29]);
				else
					printf("Comment: %-30.30s\n", newid3.comment);
			}
			if (hastag) {
				/* seek back 128 bytes to overwrite it */
				fseek(fp, -128, SEEK_END);
			} else {
				/* new tag */
				fseek(fp, 0, SEEK_END);
			}
			if (fwrite(&newid3, 128, 1, fp) < 0) exitcode=1;
		}
		fclose(fp);
		
		/* Reset tag flags so that we don't mistakenly assume the
		 * next file also has a tag. (No need to reset hastag here,
		 * because it is always set to 1 until a non-tag is found.
		 */
		oldid3.tag[0] = '\0';
		
	}
	return exitcode;
}