]> git.lizzy.rs Git - plan9front.git/blob - sys/src/ape/lib/bsd/_sock_srv.c
ape: Add mkstemp to /sys/src/ape/lib/ap/gen/mkfile
[plan9front.git] / sys / src / ape / lib / bsd / _sock_srv.c
1 /* posix */
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <errno.h>
9
10 /* socket extensions */
11 #include <sys/uio.h>
12 #include <sys/socket.h>
13
14 #include "priv.h"
15
16 /* we can't avoid overrunning npath because we don't know how big it is. */
17 void
18 _sock_srvname(char *npath, char *path)
19 {
20         char *p;
21
22         strcpy(npath, "/srv/UD.");
23         p = strrchr(path, '/');
24         if(p == 0)
25                 p = path;
26         else
27                 p++;
28         strcat(npath, p);
29 }
30
31 int
32 _sock_srv(char *path, int fd)
33 {
34         int sfd;
35         char msg[8+256+1];
36
37         /* change the path to something in srv */
38         _sock_srvname(msg, path);
39
40         /* remove any previous instance */
41         unlink(msg);
42
43         /* put the fd in /srv and then close it */
44         sfd = creat(msg, 0666);
45         if(sfd < 0){
46                 close(fd);
47                 return -1;
48         }
49         snprintf(msg, sizeof msg, "%d", fd);
50         if(write(sfd, msg, strlen(msg)) < 0){
51                 close(sfd);
52                 close(fd);
53                 return -1;
54         }
55         close(sfd);
56         close(fd);
57         return 0;
58 }