FreeBSD/usr.sbin/config/main.c
/*
 * moveifchanged --
 *	compare two files; rename if changed.
 */
moveifchanged
compare two files; rename if changed.
moveifchanged compare two files ; rename if changed .
void
moveifchanged(const char *from_name, const char *to_name)
{
	char *p, *q;
	int changed;
	size_t tsize;
	struct stat from_sb, to_sb;
	int from_fd, to_fd;
	changed = 0;
	if ((from_fd = open(from_name, O_RDONLY)) < 0)
		err(EX_OSERR, "moveifchanged open(%s)", from_name);
	if ((to_fd = open(to_name, O_RDONLY)) < 0)
		changed++;
	if (!changed && fstat(from_fd, &from_sb) < 0)
		err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
	if (!changed && fstat(to_fd, &to_sb) < 0)
		err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
	if (!changed && from_sb.st_size != to_sb.st_size)
		changed++;
	tsize = (size_t)from_sb.st_size;
	if (!changed) {
		p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
		if (p == MAP_FAILED)
			err(EX_OSERR, "mmap %s", from_name);
		q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
		if (q == MAP_FAILED)
			err(EX_OSERR, "mmap %s", to_name);
		changed = memcmp(p, q, tsize);
		munmap(p, tsize);
		munmap(q, tsize);
	}
	if (changed) {
		if (rename(from_name, to_name) < 0)
			err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
	} else {
		if (unlink(from_name) < 0)
			err(EX_OSERR, "unlink(%s)", from_name);
	}
}
void moveifchanged ( const char * from_name , const char * to_name ) { char * p , * q ; int changed ; size_t tsize ; struct stat from_sb , to_sb ; int from_fd , to_fd ; changed = 0 ; if ( ( from_fd = open ( from_name , O_RDONLY ) ) < 0 ) err ( EX_OSERR , " " , from_name ) ; if ( ( to_fd = open ( to_name , O_RDONLY ) ) < 0 ) changed ++ ; if ( ! changed && fstat ( from_fd , & from_sb ) < 0 ) err ( EX_OSERR , " " , from_name ) ; if ( ! changed && fstat ( to_fd , & to_sb ) < 0 ) err ( EX_OSERR , " " , to_name ) ; if ( ! changed && from_sb . st_size != to_sb . st_size ) changed ++ ; tsize = ( size_t ) from_sb . st_size ; if ( ! changed ) { p = mmap ( NULL , tsize , PROT_READ , MAP_SHARED , from_fd , ( off_t ) 0 ) ; if ( p == MAP_FAILED ) err ( EX_OSERR , " " , from_name ) ; q = mmap ( NULL , tsize , PROT_READ , MAP_SHARED , to_fd , ( off_t ) 0 ) ; if ( q == MAP_FAILED ) err ( EX_OSERR , " " , to_name ) ; changed = memcmp ( p , q , tsize ) ; munmap ( p , tsize ) ; munmap ( q , tsize ) ; } if ( changed ) { if ( rename ( from_name , to_name ) < 0 ) err ( EX_OSERR , " " , from_name , to_name ) ; } else { if ( unlink ( from_name ) < 0 ) err ( EX_OSERR , " " , from_name ) ; } }
moveifchanged
      compare two files; rename if changed.
moveifchanged compare two files ; rename if changed .