|
|
1.1 root 1: #include <stdio.h>
2: #include <sys/types.h>
3: #include <sys/stat.h>
4: #include "mail.h"
5: #include "string.h"
6:
7: /* imported */
8: extern char *getenv();
9: extern int getpid();
10: extern char *ttyname();
11:
12: /* global to the module */
13: static char semaphore[PATHSIZE];
14: static char tty[64];
15:
16: extern void
17: V()
18: {
19: unlink(semaphore);
20: }
21:
22: /* return name of tty if file is already being read, NULL otherwise */
23: extern char *
24: P()
25: {
26: char file[PATHSIZE];
27: struct stat sbuf;
28: FILE *fp;
29: int pid;
30: char *home = getenv("HOME");
31:
32: if (home == NULL)
33: return NULL;
34: (void)strcpy(file, home);
35: (void)strcat(file, "/.Maillock");
36: if (stat(file, &sbuf) >= 0) {
37:
38: /* lock file exists */
39: fp = fopen(file, "r");
40: if (fp == NULL)
41: return "another tty";
42: fscanf(fp, "%d %s", &pid, tty);
43: fclose(fp);
44: if (kill(pid, 0) == 0)
45: return tty;
46: }
47:
48: /* create a semaphore file */
49: strcpy(semaphore, file);
50: V();
51: fp = fopen(semaphore, "w");
52: if (fp == NULL)
53: return 0; /* nothing else we can do */
54: fprintf(fp, "%d %s\n", getpid(), ttyname(2));
55: fclose(fp);
56: return 0;
57: }
58:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.