On Sat, 05 Aug 2006 20:14:24 -0700
Carl William Spitzer IV
/*struct date */ /* int da_year; current year */ /* char da_day; day of the month */ /* char da_mon; month (1 = Jan) */ /* */ /*struct time */ /* unsigned char ti_min; minutes */ /* unsigned char ti_hour; hours */ /* unsigned char ti_hund; hundredths of seconds */ /* unsigned char ti_sec; seconds */ /* ----------------------- function prototype section ---------------------- */ void main( int argc, char *argv[] ); void UniqueName( char *); /* ------------------------ preprocessor statements ------------------------ */ #include
#include #include #include #include #include #include /* -------------------- global variables and structures -------------------- */ /* ----------------------- main and other functions ------------------------ */ void main (int argc, char *argv[]) { char *OldName; char *NewName; char drive[MAXDRIVE]; char dir[MAXDIR]; char file[MAXFILE]; char ext[MAXEXT]; int flags;
if (argc<1) { printf("Usage: %s filespec\n",argv[0]); exit(1); }
flags = 0; OldName = argv[1];
do { UniqueName(NewName); flags=fnsplit(OldName,drive,dir,file,ext); /* split the string to separate elems */ fnmerge(NewName,drive,dir,NewName,ext); /* merge everything into one string */ flags = ( (rename(OldName, NewName) == 0) ? 0 : 1); } while (flags == 1); }
void UniqueName( char * NN) { struct date Today; struct time Now; div_t x,y; char Buffer[20];
gettime (&Now); getdate (&Today);
x = div( (Today.da_year-1980) * Today.da_day * Today.da_mon,2); do y = div(Now.ti_hour * Now.ti_min * Now.ti_sec + Now.ti_hund,2); while (y.quot == 0); sprintf (Buffer,"%04X%04X",x.quot,y.quot); strcpy(NN, Buffer); } The date/time structure can be replaced by 'struct tm' in time.h (as Per mentioned). Getttime and getdate can be replaced by a call to time(3) which returns a time_t, then localtime(3) fills struct tm: time_t now; struct tm today; now = time(); localtime(&today);
Also look at the strftime(3) function to format the time/date.
The DOS filespec contains the drive letter (eg c:), the path, and the
filename.
The Linux functions, dirname(3) and basename(3) should suffice in
splitting the filename from the path. That leaves the extension. You
can use the strrchr(3) funtion to locate the period. Remember, Unix
names do not require an extension.
But, a much simpler way to do this is to use the mkstemp(3) function.
This function will also create a new file. The above code can
effectively be:
#include