/
renamejpg.c
75 lines (60 loc) · 1.7 KB
/
renamejpg.c
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
/* RenameJPG Copyright © 1999 J.A. Bezemer GPL'd */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
void main (int argc, char **argv)
{
FILE *jpgfile;
time_t time;
struct tm *temptm;
unsigned char buffer[4];
char timestring[200];
char tempstring[300];
char newname[300];
int i;
struct stat statbuf;
struct stat statbuf2;
int newnamecounter;
if (argc < 2)
{
fprintf (stderr, "Usage: renamejpg [filename(s)]\n\n");
exit(1);
}
for (i=1; i<argc; i++)
{
if ((jpgfile = fopen (argv[i], "rb")) == NULL)
{
fprintf (stderr, "File could not be opened. Aborted.\n\n");
exit(2);
}
fseek(jpgfile, 0x32, SEEK_SET);
fread(buffer, 1, 4, jpgfile);
time= (((time_t) buffer[0]) )
| (((time_t) buffer[1]) << 8 )
| (((time_t) buffer[2]) << 16 )
| (((time_t) buffer[3]) << 24 );
temptm=gmtime(&time);
strftime(timestring, 200, "%Y%m%d-%H%M%S", temptm);
fstat (fileno (jpgfile), &statbuf);
if (statbuf.st_size < 4000)
/* thumbnail */
snprintf(tempstring, 200, "thm_%s", timestring);
else
/* other jpg */
snprintf(tempstring, 200, "aut_%s", timestring);
snprintf(newname, 200, "%s.jpg", tempstring);
newnamecounter=1;
/* check if file exists. Will hang if all names are used... */
while (stat(newname, &statbuf2)==0)
{
newnamecounter++;
snprintf(newname, 200, "%s-%d.jpg", tempstring, newnamecounter);
}
printf("File %s, size %ld, %s -> %s\n", argv[i], statbuf.st_size,
timestring, newname);
fclose(jpgfile);
rename(argv[i], newname);
}
}