1 /*--------------------------------------------------------------------
2 safty profile I/O module.
3
4 Copyright (c) 1998,1999,2000 SASAKI Shunsuke.
5 All rights reserved.
6 --------------------------------------------------------------------*/
7 #include "ed.h"
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11
12
safty_write(char * fn)13 bool safty_write(char *fn)
14 {
15 FILE *fp;
16 EditLine *ed;
17 char buf[MAXEDITLINE+1];
18
19
20 if (access(fn,F_OK)==0&& access(fn,W_OK)==-1)
21 return FALSE;
22
23 fp=fopen(fn,"w");
24 if (fp==NULL)
25 return FALSE;
26
27 ed=GetTop()->next;
28 while (ed!=NULL && ed->buffer!=NULL)
29 {
30 if (ed->next==NULL)
31 {
32 if (*ed->buffer!='\0')
33 fputs(ed->buffer, fp);
34 break;
35 }
36 fprintf(fp, "%s\n", ed->buffer);
37 ed = ed->next;
38 }
39
40 fclose(fp);
41
42 return TRUE;
43 }
44
profile_write()45 void profile_write()
46 {
47 int i;
48 char buf[LN_path+1], savedir[LN_path+1];
49 FILE *fp;
50 struct stat st;
51
52 char sp[MAX_edfiles][LN_path+1];
53 bool mf;
54 int n;
55
56 mf=FALSE;
57 for (i=0; i<MAX_edfiles; i++)
58 {
59 strcpy(sp[i], edbuf[i].path);
60 if (*edbuf[i].path!='\0' && edbuf[i].cf)
61 mf=TRUE;
62 }
63
64 if (mf)
65 {
66 sysinfo_path(savedir, ".neTmp"); // !!
67 if (stat(savedir, &st) < 0)
68 {
69 if (mkdir(savedir, 0700)<0)
70 {
71 fprintf(stderr, "Unable to create directory \"%s\"\n", savedir);
72 perror("");
73 return;
74 }
75 }
76
77 for (i=0; i<MAX_edfiles; i++)
78 {
79 if (*sp[i]=='\0' || !edbuf[i].cf)
80 continue;
81
82 CurrentFileNo=i;
83 for (n=0;;++n)
84 {
85 if (n>=10) // retry
86 {
87 perror("");
88 break;
89 }
90 sprintf(buf, "%s/XXXXXX",savedir);
91 if (mktemp(buf)==NULL)
92 continue;
93
94 if (!safty_write(buf))
95 continue;
96
97 strcpy(sp[i], buf);
98 chmod(buf, 0600);
99 break;
100 }
101 }
102 }
103
104 sysinfo_path(buf, ".neprof");
105 fp=fopen(buf,"w");
106 if (fp==NULL)
107 {
108 fprintf(stderr, "Unable to create .neprof \"%s\"\n", buf);
109 perror("");
110 return;
111 }
112
113 fprintf(fp,"%s\n", PROFILE_VERSION);
114 for (i=0; i<MAX_edfiles; i++)
115 {
116 if (*sp[i]!='\0')
117 {
118 fprintf(fp,"%s:%ld:\t%s:%d:%d\n" ,sp[i] ,edbuf[i].se.ly
119 ,edbuf[i].path ,edbuf[i].kc ,edbuf[i].rm);
120 }
121 }
122 fclose(fp);
123 }
124
profile_read()125 int profile_read()
126 {
127 FILE *fp;
128 char buf[LN_path+1],fn[LN_path+1];
129 char tmp[MAXEDITLINE+1];
130 char *p,*sp,*q;
131 int i;
132 long jl;
133 struct stat sbuf;
134
135 sysinfo_path(fn, ".neprof");
136 fp=fopen(fn,"r");
137 if (fp==NULL)
138 {
139 failed:
140 /*fprintf(stderr,"failed\n");*/
141 return FALSE;
142 }
143 fgets(buf, LN_path, fp);
144 i=strlen(buf);
145 if (i>0 && buf[i-1]=='\n')
146 buf[i-1] = '\0';
147 if (strcmp(buf,PROFILE_VERSION)!=0)
148 goto failed;
149
150 system_msg(WAITING_MSG);
151 while (fgets(buf,LN_path,fp)!=NULL)
152 {
153 sp=buf;
154 q=p=strsep(&sp,":");
155 if (!FileOpenOp(p))
156 continue;
157
158 p=strsep(&sp,":");
159 if (p==NULL)
160 continue;
161 jl=atol(p);
162 csr_setly(jl);
163
164 p=strsep(&sp,":");
165 if (p==NULL)
166 continue;
167 while(isspace(*p))
168 ++p;
169 if (strcmp(edbuf[CurrentFileNo].path, p)!=0)
170 {
171 if (edbuf[CurrentFileNo].ct==-1&&GetLastNumber()==1)
172 {
173 sprintf(tmp,"profile: �ե�����`%s'�������ޤ�����",p);
174 inkey_wait(tmp);
175
176 edbuf_rm(CurrentFileNo);
177 lists_clear();
178
179 FileOpenOp(p);
180 csr_setly(jl);
181 } else
182 {
183 //!! RenameFile(CurrentFileNo, p);
184 strcpy(fn,p);
185 reg_pf(NULL, fn, FALSE);
186
187 strcpy(edbuf[CurrentFileNo].path,fn);
188 SetFileChangeFlag();
189 edbuf[CurrentFileNo].ct= stat(p,&sbuf)==-1?
190 -1: sbuf.st_ctime;
191
192 edbuf_rm(CurrentFileNo);
193 edbuf_add(fn);
194
195 unlink(q);
196 }
197 }
198
199 p=strsep(&sp,":");
200 if (p==NULL)
201 continue;
202 edbuf[CurrentFileNo].kc= atoi(p)%3;
203
204 p=strsep(&sp,":");
205 if (p==NULL)
206 continue;
207 edbuf[CurrentFileNo].rm= atoi(p)%3;
208 }
209 fclose(fp);
210
211 sysinfo_path(fn, ".neprof");
212 unlink(fn);
213 sysinfo_path(fn, ".neTmp");
214 rmdir(fn);
215
216 return TRUE;
217 }
218
219