usr.bin/xinstall/xinstall.c
int
create_newfile(const char *path, int target, struct stat *sbp)
{
	char backup[MAXPATHLEN];

	if (target) {
		/*
		 * Unlink now... avoid ETXTBSY errors later.  Try to turn
		 * off the append/immutable bits -- if we fail, go ahead,
		 * it might work.
		 */
		if (sbp->st_flags & NOCHANGEBITS)
			chflags(path, sbp->st_flags & ~NOCHANGEBITS);

		if (dobackup) {
			if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
			    path, suffix) != strlen(path) + strlen(suffix))
				errx(EX_OSERR, "%s: backup filename too long",
				    path);
			snprintf(backup, MAXPATHLEN, "%s%s",
			    path, suffix);
			if (verbose)
				printf("install: %s -> %s\n",
				    path, backup);
			if (rename(path, backup) < 0)
				err(EX_OSERR, "rename: %s to %s", path, backup);
		} else
			unlink(path);
	}

	return (open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
}
/*
 * create_newfile --
 *	create a new file, overwriting an existing one if necessary
 */
create_newfile
create a new file, overwriting an existing one if necessary
create_newfile create a new file overwriting an existing one if necessary
int
create_newfile(const char *path, int target, struct stat *sbp)
{
	char backup[MAXPATHLEN];
	if (target) {
		if (sbp->st_flags & NOCHANGEBITS)
			chflags(path, sbp->st_flags & ~NOCHANGEBITS);
		if (dobackup) {
			if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
			    path, suffix) != strlen(path) + strlen(suffix))
				errx(EX_OSERR, "%s: backup filename too long",
				    path);
			snprintf(backup, MAXPATHLEN, "%s%s",
			    path, suffix);
			if (verbose)
				printf("install: %s -> %s\n",
				    path, backup);
			if (rename(path, backup) < 0)
				err(EX_OSERR, "rename: %s to %s", path, backup);
		} else
			unlink(path);
	}
	return (open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
}
int create_newfile ( const char * path , int target , struct stat * sbp ) { char backup [ MAXPATHLEN ] ; if ( target ) { if ( sbp -> st_flags & NOCHANGEBITS ) chflags ( path , sbp -> st_flags & ~ NOCHANGEBITS ) ; if ( dobackup ) { if ( ( size_t ) snprintf ( backup , MAXPATHLEN , " " , path , suffix ) != strlen ( path ) + strlen ( suffix ) ) errx ( EX_OSERR , " " , path ) ; snprintf ( backup , MAXPATHLEN , " " , path , suffix ) ; if ( verbose ) printf ( " \n " , path , backup ) ; if ( rename ( path , backup ) < 0 ) err ( EX_OSERR , " " , path , backup ) ; } else unlink ( path ) ; } return ( open ( path , O_CREAT | O_RDWR | O_TRUNC , S_IRUSR | S_IWUSR ) ) ; }
create_newfile
      create a new file, overwriting an existing one if necessary
create_newfile create a new file overwriting an existing one if necessary
/*
		 * Unlink now... avoid ETXTBSY errors later.  Try to turn
		 * off the append/immutable bits -- if we fail, go ahead,
		 * it might work.
		 */