]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/mothra/libpanel/snarf.c
exec(2): fix prototypes
[plan9front.git] / sys / src / cmd / mothra / libpanel / snarf.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <event.h>
5 #include <panel.h>
6 #include "pldefs.h"
7
8 void plputsnarf(char *s){
9         int fd;
10
11         if(s==0 || *s=='\0')
12                 return;
13         if((fd=open("/dev/snarf", OWRITE|OTRUNC))>=0){
14                 write(fd, s, strlen(s));
15                 close(fd);
16         }
17 }
18 char *plgetsnarf(void){
19         int fd, n, r;
20         char *s;
21
22         if((fd=open("/dev/snarf", OREAD))<0)
23                 return nil;
24         n=0;
25         s=nil;
26         for(;;){
27                 s=pl_erealloc(s, n+1024);
28                 if((r = read(fd, s+n, 1024)) <= 0)
29                         break;
30                 n += r;
31         }
32         close(fd);
33         if(n <= 0){
34                 free(s);
35                 return nil;
36         }
37         s[n] = '\0';
38         return s;
39 }
40 void plsnarf(Panel *p){
41         char *s;
42
43         if(p==0 || p->snarf==0)
44                 return;
45         s=p->snarf(p);
46         plputsnarf(s);
47         free(s);
48 }
49 void plpaste(Panel *p){
50         char *s;
51
52         if(p==0 || p->paste==0)
53                 return;
54         if(s=plgetsnarf()){
55                 p->paste(p, s);
56                 free(s);
57         }
58 }