|
|
1.1 root 1: #include <stdio.h>
2: #include <sys/types.h>
3: #include <sys/timeb.h>
4:
5: /*
6: * convert a pt number into a pt name
7: */
8: #define PTSIZE sizeof("/dev/pt/ptxxx")
9: static char *devname = "/dev/pt/ptxxx";
10:
11: char *
12: ptname(pt)
13: int pt;
14: {
15: register char *pp;
16:
17: pp = devname + sizeof("/dev/pt/pt") - 1;
18: if (pt/100 > 0)
19: *pp++ = '0' + ((pt/100)%10);
20: *pp++ = '0' + ((pt/10)%10);
21: *pp++ = '0' + (pt%10);
22: *pp++ = '\0';
23:
24: return devname;
25: }
26:
27: /*
28: * Returns an fd for an open odd numbered pt.
29: */
30: int
31: ptopen(ptp)
32: int *ptp; /* the opened pt */
33: {
34: int pt, hpt, hash, fd;
35: char *name;
36: struct timeb tb;
37: extern char *ptname();
38:
39: /* find highest pt number */
40: if ((hpt = highpair()) < 0)
41: return -1;
42:
43: /* search for an unused pt */
44: ftime(&tb);
45: pt = hash = (tb.millitm/10) % hpt & ~1;
46: do {
47: if ((fd = open(ptname(pt+1), 2)) >= 0)
48: break;
49: pt = (pt + 2) % hpt;
50: } while (pt != hash);
51: *ptp = pt;
52: return fd;
53: }
54:
55: /*
56: * read the directory and return the highest numbered pt
57: */
58:
59: #define PTDIR "/dev/pt"
60: static int highest = -1;
61:
62: static
63: highpair()
64: {
65: # include <sys/types.h>
66: # include <sys/dir.h>
67: FILE *dir;
68: struct direct dbuf;
69: int pt;
70:
71: if (highest > -1)
72: return highest;
73:
74: if ((dir = fopen(PTDIR, "r")) == NULL)
75: return -1;
76: while (fread(&dbuf, sizeof(dbuf), 1, dir) != 0) {
77: if (dbuf.d_ino != 0 && strncmp(dbuf.d_name, "pt", 2) == 0) {
78: pt = atoi(&dbuf.d_name[2]);
79: if (pt > highest)
80: highest = pt;
81: }
82: }
83:
84: /* point to the first of an even odd pair */
85: if (highest & 1)
86: highest--;
87: else
88: highest -= 2;
89: return highest;
90: }
91:
92: /*
93: * Like pipe(2) except that a pt is opened. Returns the number of
94: * the slave pt or -1.
95: */
96: int
97: ptpipe(pfd)
98: int *pfd;
99: {
100: int slave;
101:
102: pfd[0] = ptopen(&slave);
103: if (pfd[0] < 0)
104: return -1;
105: pfd[1] = open(ptname(slave), 2);
106: if (pfd[1] < 0) {
107: close(pfd[0]);
108: return -1;
109: }
110: return slave;
111: }
112: char *
113: whoami()
114: {
115: static char name[128];
116: int fd, n;
117: char *cp;
118:
119: fd = open("/etc/whoami", 0);
120: if (fd < 0)
121: return ("Kremvax");
122: n = read(fd, name, sizeof(name)-1);
123: if (n <= 0)
124: return ("Kremvax");
125: name[n] = '\0';
126: for (cp=name; *cp; cp++)
127: if (*cp == '\n') {
128: *cp = '\0';
129: break;
130: }
131: return name;
132: }
133:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.