#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

void die(const char *s) { perror(s); exit(1); abort(); }

int main(int argc, char **argv)
{
  struct stat st; const char *file; int fd;
  if (argc!=2 || (file=argv[1])==0) die("Usage: unlinkcheck file");
  if (!lstat(file,&st)) die("File must not exist");
  if ((fd = open(file, O_RDWR|O_CREAT|O_EXCL,0700))<0) die("cannot create file");
  if (unlink(file)) die("cannot unlink");
  if (fstat(fd,&st)) die("cannot stat deleted file");
  die("all ok");
  return 0;
}

