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
|
#ifndef LINT
static char *rcsid="$Id: fnames.c,v 1.7 2001/01/11 06:49:57 crosser Exp $";
#endif
/*
Copyright (c) 1997,1998 Eugene G. Crosser
Copyright (c) 1998 Bruce D. Lightner (DOS/Windows support)
You may distribute and/or use for any purpose modified or unmodified
copies of this software if you preserve the copyright notice above.
THIS SOFTWARE IS PROVIDED AS IS AND COME WITH NO WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED. IN NO EVENT WILL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY DAMAGES RESULTING FROM THE
USE OF THIS SOFTWARE.
*/
/*
$Log: fnames.c,v $
Revision 1.7 2001/01/11 06:49:57 crosser
minor cleanups on source
make manual page in sync with the source
Revision 1.6 2000/05/09 13:20:54 crosser
configure read() with alarm() better.
Address signed vs. unsigned arguments
other cleanups to make most notorious compilers happy
Revision 1.5 1999/08/01 21:36:54 crosser
Modify source to suit ansi2knr
(I hate the style that ansi2knr requires but you don't expect me
to write another smarter ansi2knr implementation, right?)
Revision 1.4 1999/03/27 23:52:29 crosser
remove old check for valid pictm because it's not needed with gltimetz
Revision 1.3 1999/03/22 05:59:31 lightner
Include <windows.h> for Win32 compile
Use gltimetz() instead of gmtime()/localtime()
Revision 1.2 1999/03/10 22:34:43 crosser
clenup
Revision 1.1 1999/03/09 18:36:34 crosser
Initial revision
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include <errno.h>
#include "fnames.h"
#include "ctimez.h"
#if defined(MSWINDOWS)
#include <windows.h>
#endif
#ifdef MSWINDOWS
#define MS_PROGRAM_NAMES
#define ERRNO GetLastError()
#else
#define ERRNO errno
#endif
static char *shortcut[][4] = {
{
"%m%d_%%03d.jpg",
"%y%m%d%%02d.jpg",
"%Y_%m_%d-%H_%M_%S.jpg",
"%Y/%m/%d/%H_%M_%S.jpg"
},
{
"%m%d-%%03d.jpg",
"%y%m%d%%02d.jpt", /* this is not a typo */
"%Y_%m_%d-%H_%M_%S-t.jpg",
"%Y/%m/%d/%H_%M_%S-t.jpg"
},
{
"%m%d_%%03d.wav",
"%y%m%d%%02d.wav",
"%Y_%m_%d-%H_%M_%S.wav",
"%Y/%m/%d/%H_%M_%S.wav"
}
};
#define MAXSHORTCUT '4'
static int ftype=0;
void
makename(char *fname,int flen,char *filenm,int datatype,
time_t pictime,char *nameformat,int usetimezone)
{
int count;
struct stat st;
struct tm *pictm;
int hascount=0;
int width=0;
int maxcount;
char *ifslash;
char *p;
if ((filenm[0] == '\0') ||
(filenm[strlen(filenm)-1] == '\\') ||
(filenm[strlen(filenm)-1] == '/' )) {
ifslash="";
} else {
ifslash="/";
}
pictm=gltimetz(&pictime,usetimezone);
if (nameformat) {
if (strlen(nameformat) == 1) {
if (*nameformat == 'd') {
ftype=1;
nameformat="%010lu";
} else if (*nameformat == 'x') {
ftype=1;
nameformat="%08lx";
#ifdef HAVE_STRFTIME
} else if ((*nameformat >= '1') &&
(*nameformat <= MAXSHORTCUT)) {
ftype=2;
nameformat=
shortcut[datatype][(*nameformat)-'1'];
#endif
} else {
fprintf(stderr,
"invalid shortcut `-f %c', using default\n",
*nameformat);
nameformat=NULL;
}
} else {
#ifdef HAVE_STRFTIME
ftype=2;
#else
fprintf(stderr,
"format `-f %s' unsupported, using default\n",
nameformat);
nameformat=NULL;
#endif
}
}
if (nameformat && (ftype == 2)) {
/* see if there is a field for count */
enum {deflt,escaped,pc1,pc2,digit} state=deflt;
for (p=nameformat;*p;p++) switch (state) {
case deflt:
switch (*p) {
case '\\': state=escaped; break;
case '%': state=pc1; break;
default: state=deflt; break;
}
break;
case escaped:
state=deflt;
break;
case pc1:
switch (*p) {
case '%': state=pc2; break;
default: state=deflt; break;
}
break;
case pc2:
if ((*p >= '0') && (*p <= '9')) {
state=digit;
width*=10;
width+=(*p-'0');
} else
state=deflt;
break;
case digit:
if ((*p >= '0') && (*p <= '9')) {
state=digit;
width*=10;
width+=(*p-'0');
} else
state=deflt;
if (*p == 'd') hascount++;
break;
}
}
if (hascount > 1) {
fprintf(stderr,"bad `-f %s' option: more than one count fields\n",
nameformat);
hascount=0;
width=0;
}
if (hascount && ((width < 1) || (width > 8))) {
fprintf(stderr,"bad count field width in `-f %s' option\n",
nameformat);
hascount=0;
width=0;
}
switch (width) {
case 1: maxcount=10; break;
case 2: maxcount=100; break;
default: maxcount=1000; break;
}
for (count=1;count<maxcount;count++) {
if (ftype == 0) {
sprintf(fname,"%s%s%02d%02d%s%03d.%s",
filenm,
ifslash,
pictm->tm_mon+1,
pictm->tm_mday,
(datatype==THUMBNAIL)?"-":"_",
count,
(datatype==AUDIO)?"wav":"jpg");
if ((stat(fname,&st) < 0) && (ERRNO == ENOENT)) {
break;
}
} else if (ftype == 1) {
char timestr[MAXFORMAT+1];
char ext[5];
sprintf(timestr,nameformat,pictime);
if (count == 1) ext[0]='\0';
else sprintf(ext,".%03d",count-1);
sprintf(fname,"%s%s%s%s",
filenm,
ifslash,
timestr,
ext);
if ((stat(fname,&st) < 0) && (ERRNO == ENOENT)) {
break;
}
#ifdef HAVE_STRFTIME
} else if (ftype == 2) {
char timestr[MAXFORMAT+1];
char timestr2[MAXFORMAT+1];
char ext[5];
if (hascount) {
(void)strftime(timestr2,(size_t)MAXFORMAT-width,
nameformat,pictm);
sprintf(timestr,timestr2,count);
} else {
(void)strftime(timestr,MAXFORMAT,
nameformat,pictm);
}
if ((hascount) || (count == 1)) ext[0]='\0';
else sprintf(ext,".%03d",count-1);
sprintf(fname,"%s%s%s%s",
filenm,
ifslash,
timestr,
ext);
if ((stat(fname,&st) < 0) && (ERRNO == ENOENT)) {
break;
}
#endif /* HAVE_STRFTIME */
}
}
/* Just for case (we should not have overwritten the buffer) */
fname[flen-1]='\0';
/* OK, now time to create intermediate directories */
#ifdef HAVE_MKDIR
for (p=fname+strlen(filenm);*p;p++) if (*p == '/') {
*p='\0';
(void)mkdir(fname,0777);
*p='/';
}
#endif
}
|