FreeBSD/libexec/tftpd/tftpd.c
/*
 * Find the next value for YYYYMMDD.nn when the file to be written should
 * be unique. Due to the limitations of nn, we will fail if nn reaches 100.
 * Besides, that is four updates per hour on a file, which is kind of
 * execessive anyway.
 */
Find the next value for YYYYMMDD.nn when the file to be written should
be unique. Due to the limitations of nn, we will fail if nn reaches 100.
Besides, that is four updates per hour on a file, which is kind of
execessive anyway.
Find the next value for YYYYMMDD . nn when the file to be written should be unique . Due to the limitations of nn we will fail if nn reaches 100 . Besides that is four updates per hour on a file which is kind of execessive anyway .
static int
find_next_name(char *filename, int *fd)
{
	int i;
	time_t tval;
	size_t len;
	struct tm lt;
	char yyyymmdd[MAXPATHLEN];
	char newname[MAXPATHLEN];
	time(&tval);
	lt = *localtime(&tval);
	len = strftime(yyyymmdd, sizeof(yyyymmdd), newfile_format, &lt);
	if (len == 0) {
		syslog(LOG_WARNING,
			"Filename suffix too long (%d characters maximum)",
			MAXPATHLEN);
		return (EACCESS);
	}
	if (strlen(filename) > MAXPATHLEN - len - 5) {
		syslog(LOG_WARNING,
			"Filename too long (%zd characters, %zd maximum)",
			strlen(filename), MAXPATHLEN - len - 5);
		return (EACCESS);
	}
	for (i = 0; i < 100; i++) {
		sprintf(newname, "%s.%s.%02d", filename, yyyymmdd, i);
		*fd = open(newname,
		    O_WRONLY | O_CREAT | O_EXCL, 
		    S_IRUSR | S_IWUSR | S_IRGRP |
		    S_IWGRP | S_IROTH | S_IWOTH);
		if (*fd > 0)
			return 0;
	}
	return (EEXIST);
}
static int find_next_name ( char * filename , int * fd ) { int i ; time_t tval ; size_t len ; struct tm lt ; char yyyymmdd [ MAXPATHLEN ] ; char newname [ MAXPATHLEN ] ; time ( & tval ) ; lt = * localtime ( & tval ) ; len = strftime ( yyyymmdd , sizeof ( yyyymmdd ) , newfile_format , & lt ) ; if ( len == 0 ) { syslog ( LOG_WARNING , " " , MAXPATHLEN ) ; return ( EACCESS ) ; } if ( strlen ( filename ) > MAXPATHLEN - len - 5 ) { syslog ( LOG_WARNING , " " , strlen ( filename ) , MAXPATHLEN - len - 5 ) ; return ( EACCESS ) ; } for ( i = 0 ; i < 100 ; i ++ ) { sprintf ( newname , " " , filename , yyyymmdd , i ) ; * fd = open ( newname , O_WRONLY | O_CREAT | O_EXCL , S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH ) ; if ( * fd > 0 ) return 0 ; } return ( EEXIST ) ; }
Find the next value for YYYYMMDD.nn when the file to be written should
 be unique.
Find the next value for YYYYMMDD . nn when the file to be written should be unique .
/* Create the YYYYMMDD part of the filename */ /* Make sure the new filename is not too long */ /* Find the first file which doesn't exist */