File:  [Research Unix] / researchv8dc / cmd / upas / print / semaphore.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 17:21:59 2018 UTC (8 years, 3 months ago) by root
Branches: belllabs, MAIN
CVS tags: researchv8, HEAD
researchv8 Dan Cross

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "mail.h"
#include "string.h"

/* imported */
extern char *getenv();
extern int getpid();
extern char *ttyname();

/* global to the module */
static char semaphore[PATHSIZE];
static char tty[64];

extern void
V()
{
	unlink(semaphore);
}

/* return name of tty if file is already being read, NULL otherwise */
extern char *
P()
{
	char file[PATHSIZE];
	struct stat sbuf;
	FILE *fp;
	int pid;
	char *home = getenv("HOME");

	if (home == NULL)
		return NULL;
	(void)strcpy(file, home);
	(void)strcat(file, "/.Maillock");
	if (stat(file, &sbuf) >= 0) {

		/* lock file exists */
		fp = fopen(file, "r");
		if (fp == NULL)
			return "another tty";
		fscanf(fp, "%d %s", &pid, tty);
		fclose(fp);
		if (kill(pid, 0) == 0)
			return tty;
	}

	/* create a semaphore file */
	strcpy(semaphore, file);
	V();
	fp = fopen(semaphore, "w");
	if (fp == NULL)
		return 0;		/* nothing else we can do */
	fprintf(fp, "%d %s\n", getpid(), ttyname(2));
	fclose(fp);
	return 0;
}


unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.