/**
 * Parkprevent, Created in 2006 by Benjamin Lutz and released into
 * the public domain.
 *
 * This is a very simple program that writes a few bytes to the disk
 * every two seconds. The purpose of this is to prevent the head on
 * the Seagate harddisks in the first-generation Mac Minis from going
 * into its parking position; it does that excessively often, about
 * every 2.5 seconds. While apparently not harmful to the drive, it's
 * somewhat annoying because of the noise it produces.
**/


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define INTERVAL 2

int main() {
	char filename[] = "/tmp/parkprevent.tmp";
	char tmpstring[] = "parkprevent";
	int tmpstringlen;
	int fileno;
	int rv;
	char* crv;
	
	tmpstringlen = strlen(tmpstring);
	umask(0177);
	
	while(1) {
		fileno = open(filename, O_WRONLY | O_CREAT | O_SYNC | S_IRUSR | S_IWUSR);
		if (fileno == -1) { perror("open"); exit(1); }
		
		rv = write(fileno, tmpstring, tmpstringlen);
		if (rv == -1) { perror("write"); exit(1); }
		
		rv = fsync(fileno);
		if (rv == -1) { perror("fsync"); exit(1); }
			
		rv = close(fileno);
		if (rv == -1) { perror("close"); exit(1); }
		
		rv = unlink(filename);
		if (rv == -1) { perror("unlink"); exit(1); }
		
		sleep(INTERVAL);
	}
}
