1 /* we_fl_unix.c                                           */
2 /* Copyright (C) 1993 Fred Kruse                          */
3 /* This is free software; you can redistribute it and/or  */
4 /* modify it under the terms of the                       */
5 /* GNU General Public License, see the file COPYING.      */
6 
7 #include "messages.h"
8 #include "edit.h"
9 
10 #ifdef UNIX
11 
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <errno.h>
15 
16 #if (defined(__unix__) || defined(unix)) && !defined(USG)
17 #include <sys/param.h>
18 #endif
19 
20 struct dirfile *e_make_win_list(FENSTER * f);
21 extern char    *e_tmp_dir;
22 
23 #ifdef NOSYMLINKS
24 #define readlink(x, y, z) -1
25 #define WpeRenameLink(x, y, z, f) 0
26 #define WpeLinkFile(x, y, sw, f) link(x, y)
27 #define lstat(x,y)  stat(x,y)
28 #undef S_ISLNK
29 #define S_ISLNK(x)  0
30 #else
31 #include <unistd.h>
32 #endif
33 
34 #define WPE_PATHMAX 2048
35 
36 /* buffer size for copying */
37 #define E_C_BUFFERSIZE 524288        /*   1/2  Mega Byte   */
38 
39 #ifdef DEBUG
SpecialError(char * text,int sw,FARBE * f,char * file,int line)40 int SpecialError(char *text, int sw, FARBE *f, char *file, int line)
41 {
42   fprintf(stderr, "\nFile \"%s\" line %d\n", file, line);
43   return e_error(text, sw, f);
44 }
45 
46 #define e_error(text, sw, f) SpecialError(text, sw, f, __FILE__, __LINE__)
47 #endif
48 
49 
50 /* setup the file-manager structures
51    'dirct' or the current dir. is used to
52    search for directories and files */
53 /* sw | file manager type
54    ------------------------
55    0  | open/new file
56    1  | read block of text from file
57    2  | write block of text to file
58    3  | execute file
59    4  | save as
60    5  | add file to project
61    6  | wastebasket
62 
63 */
WpeCreateFileManager(int sw,ECNT * cn,char * dirct)64 int WpeCreateFileManager(int sw, ECNT *cn, char *dirct)
65 {
66   extern char    *e_hlp_str[];
67   extern WOPT    *fblst, *rblst, *wblst, *xblst, *sblst, *ablst;
68   FENSTER        *f;
69   int             i, j;
70   FLBFFR         *b;
71   int             allocate_size;  /* inital memory size for allocation */
72   char           *sfile;
73 
74   /* check whether we reached the maximum number of windows */
75   if(cn->mxedt >= MAXEDT)
76   {
77     e_error(e_msg[ERR_MAXWINS], 0, cn->fb);
78     return(-1);
79   }
80 
81   /* search for a not used window ID number (j) */
82   for(j = 1; j <= MAXEDT; j++)
83   {
84     for(i = 1; i <= cn->mxedt && cn->edt[i] != j; i++)
85       ;
86     if(i > cn->mxedt)
87       break;
88   }
89 
90   /* change the shape of the mouse :-) */
91   WpeMouseChangeShape(WpeWorkingShape);
92 
93   /* currently active window,
94      one more window in the system,
95      its number is j */
96   cn->curedt = j;
97   (cn->mxedt)++;
98   cn->edt[cn->mxedt] = j;
99 
100   /* allocate window structure */
101   if((f = (FENSTER *)MALLOC(sizeof(FENSTER))) == NULL)
102     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
103 
104   /* allocate buffer related to the window (NOT proper type, later casted) */
105   if((b = (FLBFFR *) MALLOC(sizeof(FLBFFR))) == NULL)
106     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
107 
108   f->fb = cn->fb;
109   cn->f[cn->mxedt] = f;        /* store the window structure at appropriate place */
110   f->a = e_set_pnt(11, 2);     /* beginning of the box */
111   f->e = e_set_pnt(f->a.x + 55, f->a.y + 20); /* other coord. of the box */
112   f->winnum = cn->curedt;
113   f->dtmd = DTMD_FILEMANAGER;
114   f->ins = 1;
115   f->save = 0;
116   f->zoom = 0;
117   f->ed = cn;
118   f->c_sw = NULL;
119   f->c_st = NULL;
120   f->pic = NULL;
121   if(sw == 6)
122   {
123     sw = 0;
124     f->datnam = "Wastebasket";
125     f->save = 1;
126   }
127   else
128     f->datnam = "File-Manager";  /* window header text */
129 
130   /* status line text for different mode */
131   if(sw == 0)
132   {
133     f->blst = fblst;
134     f->nblst = 8;
135   }
136   else if(sw == 1)
137   {
138     f->blst = rblst;
139     f->nblst = 4;
140   }
141   else if(sw == 2)
142   {
143     f->blst = wblst;
144     f->nblst = 4;
145   }
146   else if(sw == 3)
147   {
148     f->blst = xblst;
149     f->nblst = 4;
150   }
151   else if(sw == 4)
152   {
153     f->blst = sblst;
154     f->nblst = 5;
155   }
156   else if(sw == 5)
157   {
158     f->blst = ablst;
159     f->nblst = 4;
160   }
161 
162   if(sw == 3)
163     f->hlp_str = e_hlp_str[5];
164   else
165     f->hlp_str = e_hlp_str[4];
166 
167   if(!dirct || dirct[0] == '\0')
168   {
169     /* no working directory has been given */
170     f->dirct = WpeGetCurrentDir(cn);
171   }
172   else
173   {
174     /* working directory is given, copy it over */
175     allocate_size = strlen(dirct);
176     if((f->dirct = MALLOC(allocate_size+1)) == NULL)
177       e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
178 
179     strcpy(f->dirct, dirct);
180   }
181 
182   strcpy(f->fd.search, "");
183   strcpy(f->fd.replace, "");
184   strcpy(f->fd.file, SUDIR);
185   f->fd.dirct = WpeStrdup(f->dirct);
186 
187   f->fd.sw = 16;
188   f->fd.sn = 0;
189   f->fd.rn = 0;
190 
191   f->b = (BUFFER *)b;
192   /* the find pattern can only be 79 see FIND structure */
193   if((b->rdfile = MALLOC(80)) == NULL)
194     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
195   strcpy(b->rdfile, f->fd.file);               /* find file pattern */
196 
197   b->sw = sw;
198 
199   /* window for files */
200   if((b->fw = (FLWND *) MALLOC(sizeof(FLWND))) == NULL)
201     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
202 
203   /* window for directory */
204   if((b->dw = (FLWND *) MALLOC(sizeof(FLWND))) == NULL)
205     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
206 
207 
208   if((sfile = MALLOC(strlen(f->dirct)+strlen(b->rdfile)+2)) == NULL)
209     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
210 
211   /* determine current directory */
212   b->cd = WpeCreateWorkingDirTree(f->save, cn);
213   /* it is necessary to do this, because the file manager may not be
214      in the appropriate directory here */
215   sprintf(sfile, "%s/%s", f->dirct, SUDIR);
216   /* find all other directories in the current */
217   b->dd = e_find_dir(sfile, f->ed->flopt & FM_SHOW_HIDDEN_DIRS ? 1 : 0);
218   /* setup the drawing in the dir tree window */
219   b->dw->df = WpeGraphicalDirTree(b->cd, b->dd, cn);
220 
221   i = f->ed->flopt & FM_SHOW_HIDDEN_FILES ? 1 : 0;
222   if(sw == 3)
223     i |= 2;
224 
225   /* finds all files matching the pattern */
226   sprintf(sfile, "%s/%s", f->dirct, b->rdfile);
227   b->df = e_find_files(sfile, i);
228 
229   FREE(sfile);
230 
231   /* setup the drawing in the file list window */
232   b->fw->df = WpeGraphicalFileList(b->df, f->ed->flopt >> 9, cn);
233 
234   /* file box - geometry and vertical slider settings */
235   b->fw->mxa = f->a.x;
236   b->fw->mxe = f->e.x;
237   b->fw->mya = f->a.y;
238   b->fw->mye = f->e.y;
239   b->fw->xa = f->e.x - 33;
240   b->fw->xe = f->e.x - 17;
241   b->fw->ya = f->a.y + 6;
242   b->fw->ye = f->a.y + 17;
243   b->fw->f = f;
244   b->fw->ia = b->fw->nf = b->fw->nxfo = b->fw->nyfo = 0;
245   b->fw->srcha = b->fw->ja = 12;
246 
247   /* directory box - geometry and vertical slider settings */
248   b->dw->mxa = f->a.x;
249   b->dw->mxe = f->e.x;
250   b->dw->mya = f->a.y;
251   b->dw->mye = f->e.y;
252   b->dw->xa = f->a.x + 3;
253   b->dw->xe = f->a.x + 28;
254   b->dw->ya = f->a.y + 6;
255   b->dw->ye = f->a.y + 17;
256   b->dw->f = f;
257   b->dw->ia = b->dw->ja = b->dw->nxfo = 0;
258   b->dw->srcha = -1;
259   b->dw->nf = b->dw->nyfo = b->cd->anz - 1;
260 
261   if(cn->mxedt > 1)
262     e_ed_rahmen(cn->f[cn->mxedt - 1], 0);
263 
264   e_firstl(f, 1);
265 
266   /* basically it draws the window out */
267   WpeDrawFileManager(f);
268 
269   /* restore the shape of the mouse */
270   WpeMouseRestoreShape();
271   return(0);
272 }
273 
274 /* drawing out the file-manager,
275    first buttons, than the dir tree and file list */
WpeDrawFileManager(FENSTER * f)276 int WpeDrawFileManager(FENSTER * f)
277 {
278   FLBFFR         *b = (FLBFFR *)f->b;
279   int             i, j;
280   int             bx1 = 1, bx2 = 1, bx3 = 1, by = 4;
281 
282   for(j = f->a.y + 1; j < f->e.y; j++)
283     for(i = f->a.x + 1; i < f->e.x; i++)
284       e_pr_char(i, j, ' ', f->fb->nt.fb);
285 
286   if(NUM_LINES_ON_SCREEN <= 17)
287     by = -1;
288   else if(b->sw != 0 || NUM_LINES_ON_SCREEN <= 19)
289     by = 2;
290 
291   if(NUM_LINES_ON_SCREEN > 17)
292   {
293     e_pr_str((f->a.x + 4), f->e.y - by, "Cancel", f->fb->nz.fb, -1, -1,
294              f->fb->ns.fb, f->fb->nt.fb);
295     e_pr_str((f->a.x + 14), f->e.y - by, "Change Dir", f->fb->nz.fb, 0, -1,
296              f->fb->ns.fb, f->fb->nt.fb);
297     if(b->sw == 1 && NUM_COLS_ON_SCREEN >= 34)
298       e_pr_str((f->a.x + 28), f->e.y - by, "Read", f->fb->nz.fb, 0, -1,
299                f->fb->ns.fb, f->fb->nt.fb);
300     else if(b->sw == 2 && NUM_COLS_ON_SCREEN >= 35)
301       e_pr_str((f->a.x + 28), f->e.y - by, "Write", f->fb->nz.fb, 0, -1,
302                f->fb->ns.fb, f->fb->nt.fb);
303     else if(b->sw == 4)
304     {
305       if(NUM_COLS_ON_SCREEN >= 34)
306         e_pr_str((f->a.x + 28), f->e.y - by, "Save", f->fb->nz.fb, 0, -1,
307                  f->fb->ns.fb, f->fb->nt.fb);
308     }
309     else if(b->sw == 3 && NUM_COLS_ON_SCREEN >= 37)
310       e_pr_str((f->a.x + 28), f->e.y - by, "Execute", f->fb->nz.fb, 0, -1,
311                f->fb->ns.fb, f->fb->nt.fb);
312     else if(b->sw == 5 && NUM_COLS_ON_SCREEN >= 33)
313       e_pr_str((f->a.x + 28), f->e.y - by, "Add", f->fb->nz.fb, 0, -1,
314                f->fb->ns.fb, f->fb->nt.fb);
315     else if(b->sw == 0)
316     {
317       if(NUM_COLS_ON_SCREEN >= 35)
318         e_pr_str((f->a.x + 28), f->e.y - by, "MKdir", f->fb->nz.fb, 1, -1,
319                  f->fb->ns.fb, f->fb->nt.fb);
320       if(NUM_COLS_ON_SCREEN >= 49)
321         e_pr_str((f->a.x + 37), f->e.y - by, "Attributes", f->fb->nz.fb, 0, -1,
322                  f->fb->ns.fb, f->fb->nt.fb);
323     }
324   }
325   if(b->sw == 0 && NUM_LINES_ON_SCREEN > 19)
326   {
327     e_pr_str((f->a.x + 4), f->e.y - 2, "Move", f->fb->nz.fb, 0, -1,
328              f->fb->ns.fb, f->fb->nt.fb);
329 
330     if(NUM_COLS_ON_SCREEN >= 21)
331       e_pr_str((f->a.x + 13), f->e.y - 2, "Remove", f->fb->nz.fb, 0, -1,
332                f->fb->ns.fb, f->fb->nt.fb);
333 
334     if(NUM_COLS_ON_SCREEN >= 30)
335       e_pr_str((f->a.x + 24), f->e.y - 2, "Link", f->fb->nz.fb, 0, -1,
336                f->fb->ns.fb, f->fb->nt.fb);
337 
338     if(NUM_COLS_ON_SCREEN >= 39)
339       e_pr_str((f->a.x + 33), f->e.y - 2, "COpy", f->fb->nz.fb, 1, -1,
340                f->fb->ns.fb, f->fb->nt.fb);
341 
342     if(NUM_COLS_ON_SCREEN >= 48)
343       e_pr_str((f->a.x + 42), f->e.y - 2, "Edit", f->fb->nz.fb, 0, -1,
344                f->fb->ns.fb, f->fb->nt.fb);
345 
346   }
347   if(NUM_COLS_ON_SCREEN < 45)
348     bx3 = 0;
349   if(NUM_COLS_ON_SCREEN < 44)
350     bx2 = 0;
351   if(NUM_COLS_ON_SCREEN < 43)
352     bx1 = 0;
353   b->xfd = (NUM_COLS_ON_SCREEN - bx1 - bx2 - bx3 - 6) / 2;
354   b->xdd = NUM_COLS_ON_SCREEN - bx1 - bx2 - bx3 - b->xfd - 6;
355   b->xda = 2 + bx1;
356   b->xfa = 4 + bx1 + bx2 + b->xdd;
357 
358   e_pr_str((f->a.x + b->xfa), f->a.y + 2, "Name:", f->fb->nt.fb, 0, 1,
359            f->fb->nsnt.fb, f->fb->nt.fb);
360 /*    e_schr_nchar(b->rdfile, f->a.x+b->xfa, f->a.y+3, 0, b->xfd+1, f->fb->fr.fb);   */
361   e_schr_nchar_wsv(b->rdfile, f->a.x + b->xfa, f->a.y + 3, 0, b->xfd + 1,
362                    f->fb->fr.fb, f->fb->fz.fb);
363   e_pr_str((f->a.x + b->xfa), f->a.y + 5, "Files:", f->fb->nt.fb, 0, 1,
364            f->fb->nsnt.fb, f->fb->nt.fb);
365 
366   e_pr_str((f->a.x + b->xda), f->a.y + 2, "Directory:", f->fb->nt.fb, 0, 1,
367            f->fb->nsnt.fb, f->fb->nt.fb);
368 /*    e_schr_nchar(f->dirct, f->a.x+b->xda, f->a.y+3, 0, b->xdd+1, f->fb->fr.fb);   */
369   e_schr_nchar_wsv(f->dirct, f->a.x + b->xda, f->a.y + 3, 0, b->xdd + 1,
370                    f->fb->fr.fb, f->fb->fz.fb);
371   e_pr_str((f->a.x + b->xda), f->a.y + 5, "DirTree:", f->fb->nt.fb, 3, 1,
372            f->fb->nsnt.fb, f->fb->nt.fb);
373 
374   b->fw->mxa = f->a.x;
375   b->fw->mxe = f->e.x;
376   b->fw->mya = f->a.y;
377   b->fw->mye = f->e.y;
378   b->fw->xa = f->a.x + b->xfa;
379   b->fw->xe = b->fw->xa + b->xfd;
380   b->fw->ya = f->a.y + 6;
381   b->fw->ye = f->e.y - 2 - by;
382   b->dw->mxa = f->a.x;
383   b->dw->mxe = f->e.x;
384   b->dw->mya = f->a.y;
385   b->dw->mye = f->e.y;
386   b->dw->xa = f->a.x + b->xda;
387   b->dw->xe = b->dw->xa + b->xdd;
388   b->dw->ya = f->a.y + 6;
389   b->dw->ye = f->e.y - 2 - by;
390 
391   /* slider bars for file list */
392   e_mouse_bar(b->fw->xe, b->fw->ya, b->fw->ye - b->fw->ya, 0, b->fw->f->fb->em.fb);
393   e_mouse_bar(b->fw->xa, b->fw->ye, b->fw->xe - b->fw->xa, 1, b->fw->f->fb->em.fb);
394   /* file list window */
395   e_pr_file_window(b->fw, 0, 1, f->fb->ft.fb, f->fb->fz.fb, f->fb->frft.fb);
396 
397   /* slide bars for directory window */
398   e_mouse_bar(b->dw->xe, b->dw->ya, b->dw->ye - b->dw->ya, 0, b->dw->f->fb->em.fb);
399   e_mouse_bar(b->dw->xa, b->dw->ye, b->dw->xe - b->dw->xa, 1, b->dw->f->fb->em.fb);
400   /* directory window */
401   e_pr_file_window(b->dw, 0, 1, f->fb->ft.fb, f->fb->fz.fb, f->fb->frft.fb);
402   return(0);
403 }
404 
405 /* tries to find the required style file manager */
WpeCallFileManager(int sw,FENSTER * f)406 int WpeCallFileManager(int sw, FENSTER * f)
407 {
408   int             i, ret;
409   FLBFFR         *b;
410 
411   for(i = f->ed->mxedt; i > 0; i--)
412     if(f->ed->f[i]->dtmd == DTMD_FILEMANAGER)     /* check only file manager windows */
413     {
414       b = (FLBFFR *)f->ed->f[i]->b;
415       /* open/new file manager and it is not in save mode */
416       if(sw == 0 && b->sw == sw && f->ed->f[i]->save != 1)
417         break;
418       /* wastebasket mode required,
419          the window is "open/new" file manager and save mode turned on */
420       else if(sw == 6 && b->sw == 0 && f->ed->f[i]->save == 1)
421         break;
422       /* not open/new or wastebasket filemanager and it is the required style */
423       else if(sw != 0 && sw != 6 && b->sw == sw)
424         break;
425     }
426 
427   if(i <= 0) /* we did not find the required style file-manager */
428   {
429     if(sw == 6) /* wastebasket mode */
430     {
431       char *tmp;
432 
433       if ((tmp = WpeGetWastefile("")))
434       {
435         ret = WpeCreateFileManager(sw, f->ed, tmp);
436         FREE(tmp);
437         return(ret);
438       }
439       else
440       {
441         e_error(e_msg[ERR_NOWASTE], 0, f->ed->fb);
442         return 0;                /* Error -- no wastebasket */
443       }
444     }
445     /* create the required style file manager */
446     return(WpeCreateFileManager(sw, f->ed, ""));
447   }
448   /* switch to the found file manager */
449   e_switch_window(f->ed->edt[i], f);
450   return(0);
451 }
452 
453 /* It will always create a new file manager */
WpeManagerFirst(FENSTER * f)454 int WpeManagerFirst(FENSTER * f)
455 {
456   return(WpeCreateFileManager(0, f->ed, ""));
457 }
458 
459 /* try to find an "open/new" style file manager or create one */
WpeManager(FENSTER * f)460 int WpeManager(FENSTER * f)
461 {
462   return(WpeCallFileManager(0, f));
463 }
464 
465 /* try to find an "execute" style file manager or create one */
WpeExecuteManager(FENSTER * f)466 int WpeExecuteManager(FENSTER * f)
467 {
468   return(WpeCallFileManager(3, f));
469 }
470 
471 /* try to find a "save as" style file manager or create one */
WpeSaveAsManager(FENSTER * f)472 int WpeSaveAsManager(FENSTER * f)
473 {
474   return(WpeCreateFileManager(4, f->ed, ""));
475 }
476 
477 
478 /* File Manager Handler */
WpeHandleFileManager(ECNT * cn)479 int WpeHandleFileManager(ECNT * cn)
480 {
481   FENSTER        *f = cn->f[cn->mxedt], *fe = NULL;
482   FLBFFR         *b = (FLBFFR *) f->b;
483   BUFFER         *be = NULL;
484   SCHIRM         *se = NULL;
485   int             c = AltC, i, j, t;
486   int             winnum = 0, nco, svmode = -1, fmode, len, start;
487   int             g[4], cold = AltN;
488   char            filen[128], *ftmp, *dtp = NULL, *ftp = NULL, *svdir = NULL;
489   char           *dirtmp = NULL;
490   PIC            *outp = NULL;
491   FILE           *fp;
492   struct stat     buf;
493   char            dtmd;
494 
495   /* check whether we really get a file-manager window here */
496   if(f->dtmd != DTMD_FILEMANAGER)
497     return(0);
498 
499   if(f->save == 1)
500   {
501     svmode = f->ed->flopt;
502     f->ed->flopt = FM_SHOW_HIDDEN_FILES | FM_SHOW_HIDDEN_DIRS |
503       FM_MOVE_OVERWRITE | FM_REKURSIVE_ACTIONS;
504   }
505 
506   /* if it is project management or saving mode
507      save the current directory to return to it */
508   if(f->save == 1 || b->sw == 5)
509   {
510     if((svdir = MALLOC(strlen(f->ed->dirct) + 1)) == NULL)
511       e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
512     strcpy(svdir, f->ed->dirct);
513   }
514 
515   nco = b->cd->anz - 1;
516   /* when searching among files, search hidden ones as well */
517   fmode = f->ed->flopt & FM_SHOW_HIDDEN_FILES ? 1 : 0;
518   /* in execution mode show hidden dirs as well */
519   if(b->sw == 3)
520     fmode |= 2;
521 
522   /* searching for the last edited/touched file on the desktop */
523   for(i = cn->mxedt; i > 0; i--)
524   {
525     if (DTMD_ISTEXT(cn->f[i]->dtmd))
526     {
527       fe = cn->f[i];
528       be = fe->b;
529       se = fe->s;
530       winnum = cn->edt[i];
531       break;
532     }
533   }
534   strcpy(f->fd.file, b->rdfile);
535 
536   /* go until quit */
537   while(c != WPE_ESC)
538   {
539     /* draw out dir tree and file list windows */
540     e_pr_file_window(b->fw, 0, 1, f->fb->ft.fb, f->fb->fz.fb, f->fb->frft.fb);
541     e_pr_file_window(b->dw, 0, 1, f->fb->ft.fb, f->fb->fz.fb, f->fb->frft.fb);
542 
543     switch(c)
544     {
545       /* filename entry box activation */
546       case AltN:
547         cold = c;
548         fk_cursor(1);
549         /* get some answer from the name entry box,
550            result file copied into b->rdfile, max 79 char + '\0' */
551         c = e_schr_lst_wsv(b->rdfile, f->a.x + b->xfa, f->a.y + 3, b->xfd + 1, 79,
552                            f->fb->fr.fb, f->fb->fz.fb, &f->ed->fdf, f);
553 
554         /* determine the entered filename, going backward */
555         for(i = strlen(b->rdfile); i >= 0 && b->rdfile[i] != DIRC; i--)
556           ;
557         strcpy(f->fd.file, b->rdfile + 1 + i);
558 
559         /* there is some directory structure in the filename */
560         if(i >= 0)
561         {
562           if(i == 0)
563             i++;
564           b->rdfile[i] = '\0';
565           /* change the working directory */
566           FREE(f->dirct);
567           if((f->dirct = MALLOC(strlen(b->rdfile) + 1)) == NULL)
568             e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
569           strcpy(f->dirct, b->rdfile);
570 
571           /* restore original filename */
572           strcpy(b->rdfile, f->fd.file);
573           c = AltC;
574         }
575 #if  MOUSE
576         /* if mouse was used (the reason it returned) get appropriate key interpretation */
577         if(c == -1)
578           c = WpeMngMouseInFileManager(f);
579 #endif
580         if((c >= Alt1 && c <= Alt9) || (c >= 1024 && c <= 1049))
581         {
582           /* window changing, make the entry unhighlighted */
583           e_schr_nchar_wsv(b->rdfile, f->a.x + b->xfa, f->a.y + 3, 0, b->xfd + 1,
584                            f->fb->fr.fb, f->fb->fz.fb);
585           break;
586         }
587         if(c == CLE || c == CCLE) /* goto dir name window */
588           c = AltD;
589         else if(c == CDO || c == BDO || c == WPE_TAB) /* goto file list window */
590           c = AltF;
591         else if(c == WPE_BTAB) /* goto dir tree window */
592           c = AltT;
593         else if(    (   c == WPE_CR
594                      || (b->sw == 0 && c == AltE)
595                      || (b->sw == 1 && c == AltR)
596                      || (b->sw == 2 && c == AltW)
597                      || (b->sw == 3 && c == AltE)
598                      || (b->sw == 5 && c == AltA)
599                      || (b->sw == 4 && (c == AltS || c == AltY)) )
600                  && (   strstr(b->rdfile, "*")
601                      || strstr(b->rdfile, "?")
602                      || strstr(b->rdfile, "[") ) )
603         {
604           WpeMouseChangeShape(WpeWorkingShape);
605           /* free up existing structures */
606           freedf(b->df);
607           freedf(b->fw->df);
608           /* find files according to the new pattern */
609           b->df = e_find_files(b->rdfile, fmode);
610           /* setup the drawing in the dir tree window */
611           b->fw->df = WpeGraphicalFileList(b->df, f->ed->flopt >> 9, cn);
612           b->fw->ia = b->fw->nf = 0;
613           b->fw->ja = b->fw->srcha;
614           /* jump to file list window */
615           c = AltF;
616           WpeMouseRestoreShape();
617         }
618         else
619         {
620           strcpy(filen, b->rdfile);   /* !!! alloc for filen ??? */
621           if(c == WPE_CR)
622           {
623             if(b->sw == 1)
624               c = AltR;
625             else if(b->sw == 2)
626               c = AltW;
627             else if(b->sw == 4)
628               c = AltS;
629             else if(b->sw == 5)
630               c = AltA;
631             else
632               c = AltE;
633           }
634         }
635         /* entry window is left, make the entry unhighlighted */
636         if(c != AltN)
637           e_schr_nchar_wsv(b->rdfile, f->a.x + b->xfa, f->a.y + 3, 0, b->xfd + 1,
638                            f->fb->fr.fb, f->fb->fz.fb);
639         fk_cursor(0);
640         break;
641 
642       /* directory name entry box activation */
643       case AltD:
644         cold = c;
645         fk_cursor(1);
646 
647         /* get the directory name */
648         if ((dirtmp = WpeMalloc(WPE_PATHMAX)) == NULL)        /* dirct mat not have enough memory */
649           e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
650 
651         if (strlen(f->dirct) >= WPE_PATHMAX)
652         {
653           strncpy(dirtmp, f->dirct, WPE_PATHMAX - 1);
654           dirtmp[WPE_PATHMAX - 1] = '\0';
655         }
656         else
657           strcpy(dirtmp, f->dirct);
658 
659         c = e_schr_lst_wsv(dirtmp, f->a.x + b->xda, f->a.y + 3, b->xdd + 1, WPE_PATHMAX,
660                            f->fb->fr.fb, f->fb->fz.fb, &f->ed->ddf, f);
661         FREE(f->dirct);
662         f->dirct = WpeStrdup(dirtmp);
663         WpeFree(dirtmp);
664 
665 #if  MOUSE
666         if(c == -1)
667           c = WpeMngMouseInFileManager(f);
668 #endif
669         if(c == CRI || c == CCRI) /* goto name entry windwow */
670           c = AltN;
671         else if(c == CDO || c == BDO || c == WPE_TAB) /* goto dir tree window */
672           c = AltT;
673         else if(c == WPE_BTAB) /* goto file list window */
674           c = AltF;
675         else if(c == WPE_CR) /* change dir */
676           c = AltC;
677         /* window left, make the entry unhighlighted */
678         if(c != AltD)
679           e_schr_nchar_wsv(f->dirct, f->a.x + b->xda, f->a.y + 3, 0, b->xdd + 1,
680                            f->fb->fr.fb, f->fb->fz.fb);
681         fk_cursor(0);
682         break;
683 
684       /* directory tree list window activation */
685       case AltT:
686         cold = c;
687         c = e_file_window(1, b->dw, f->fb->ft.fb, f->fb->fz.fb);
688 #if  MOUSE
689         if(c == MBKEY)         /* handle mouse actions in the window */
690           c = WpeMngMouseInFileManager(f);
691         else if(c < 0)
692           c = WpeMouseInFileDirList(c, 1, f);
693 #endif
694         if(c == CCRI)
695           c = AltF;
696         else if(c == BUP)
697           c = AltD;
698         else if(c == WPE_TAB)
699           c = AltN;
700         else if(c == WPE_BTAB)
701           c = AltD;
702         else if(c == AltC || (c == WPE_CR && b->dw->nf != nco))
703         {
704           if ((dirtmp = WpeAssemblePath(f->dirct, b->cd, b->dd, b->dw->nf, f)))
705           {
706             FREE(f->dirct);
707             f->dirct = dirtmp;
708             e_schr_nchar_wsv(f->dirct, f->a.x + b->xda, f->a.y + 3, 0, b->xdd + 1,
709                              f->fb->fr.fb, f->fb->fz.fb);
710             f->ed->ddf = e_add_df(f->dirct, f->ed->ddf);
711             c = AltC;
712           }
713           /* there is only one case when it cannot assemble the path,
714              that it cannot access wastebasket, then quit the file manager */
715           else
716             c = WPE_ESC;
717         }
718         else if(c == WPE_CR)
719           c = AltT;
720         break;
721 
722       /* file list window activation */
723       case AltF:
724         if(b->df->anz < 1)
725         {
726           c = cold;
727           break;
728         }
729         cold = c;
730         c = e_file_window(1, b->fw, f->fb->ft.fb, f->fb->fz.fb);
731 #if  MOUSE
732         if(c == MBKEY)
733           c = WpeMngMouseInFileManager(f);
734         else if(c < 0)
735           c = WpeMouseInFileDirList(c, 0, f);
736 #endif
737         if(c == BUP)  /* goto file name entry window */
738           c = AltN;
739         else if(c == CCLE) /* goto dir tree window */
740           c = AltT;
741         else if(c == WPE_TAB) /* goto dir entry window */
742           c = AltD;
743         else if(c == WPE_BTAB) /* goto file name entry window */
744           c = AltN;
745         else if(c == WPE_CR)  /* action selected */
746         {
747           if(b->sw == 1)
748             c = AltR;
749           else if(b->sw == 2)
750             c = AltW;
751           else if(b->sw == 4)
752             c = AltS;
753           else if(b->sw == 5)
754             c = AltA;
755           else
756             c = AltE;
757         }
758         if(   (b->sw == 1 && c == AltR)  /* in case of action store the filename */
759            || (b->sw == 2 && c == AltW)
760            || (b->sw == 3 && c == AltE)
761            || (b->sw == 0 && c == AltE)
762            || (b->sw == 4 && (c == AltS || c == AltY)) )
763         {
764           strcpy(filen, *(b->df->name + b->fw->nf));   /* !!! alloc for filen ??? */
765         }
766         break;
767 
768       /* change dir button activation */
769       case AltC:
770         c = cold;
771         /* if the current dir is equal to the "newly" entered dir, break */
772         if(!strcmp(f->ed->dirct, f->dirct))
773           break;
774 
775         /* in wastebasket mode, we do not allow to go out of it through
776            a soft link */
777         if((b->sw == 0) && (f->save == 1))
778         {
779           if(lstat(f->dirct, &buf))
780           {
781             e_error(e_msg[ERR_ACCFILE], 0, f->ed->fb);
782             break;
783           }
784           if(S_ISLNK(buf.st_mode))
785           {
786             /* cannot go out through a softlink, restore dir name */
787             if((f->dirct = REALLOC(f->dirct, strlen(f->ed->dirct) + 1)) == NULL)
788               e_error(e_msg[ERR_LOWMEM], 1, f->fb);
789             else
790               strcpy(f->dirct, f->ed->dirct);
791             break;
792           }
793         }
794 
795         /* change to the desired dir with system error checking */
796         if(chdir(f->dirct))
797         {
798           e_error(e_msg[ERR_WORKDIRACCESS], 0, f->fb);
799 
800           /* we cannot determine where we are, try the home */
801           if((dirtmp = getenv("HOME")) == NULL)
802             e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
803           if(chdir(dirtmp))
804             e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
805         }
806 
807         /* get current directory */
808         dirtmp = WpeGetCurrentDir(f->ed);
809 
810         /* change the shape of the mouse */
811         WpeMouseChangeShape(WpeWorkingShape);
812 
813         FREE(f->dirct);
814         f->dirct = dirtmp;
815 
816         /* free up all relevant structures */
817         freedf(b->df);
818         freedf(b->fw->df);
819         freedf(b->cd);
820         freedf(b->dw->df);
821         freedf(b->dd);
822 
823         /* reset the current dir path in the control structure */
824         if((f->ed->dirct = REALLOC(f->ed->dirct, strlen(f->dirct) + 1)) == NULL)
825           e_error(e_msg[ERR_LOWMEM], 1, f->fb);
826         else
827           strcpy(f->ed->dirct, f->dirct);
828 
829         /* setup current directory structure */
830         b->cd = WpeCreateWorkingDirTree(f->save, cn);
831         /* find all other directories in the current dir */
832         b->dd = e_find_dir(SUDIR, f->ed->flopt & FM_SHOW_HIDDEN_DIRS ? 1 : 0);
833         /* setup the drawing in the dir tree window */
834         b->dw->df = WpeGraphicalDirTree(b->cd, b->dd, cn);
835 
836         nco = b->dw->nf = b->cd->anz - 1;
837         b->dw->ia = b->dw->ja = 0;
838 
839         /* finds all files matching the pattern */
840         b->df = e_find_files(b->rdfile, fmode);
841         /* setup the drawing in the file list window */
842         b->fw->df = WpeGraphicalFileList(b->df, f->ed->flopt >> 9, cn);
843         b->fw->nf = b->fw->ia = 0;
844         b->fw->ja = 12;
845 
846         /* change the shape of the mouse back */
847         WpeMouseRestoreShape();
848         break;
849 
850       /* link file activation */
851       case AltL:
852       /* copy file activation */
853       case AltO:
854       /* move file activation */
855       case AltM:
856         /* moving/copying a file is valid only in mode 0 (open/new file) */
857         if(b->sw != 0)
858         {
859           c = cold;
860           break;
861         }
862         j = c;
863 
864         /* we are coming from files */
865         if(cold == AltF)
866         {
867           if((ftmp = MALLOC(129)) == NULL)
868             e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
869 
870           if(strlen(*(b->df->name + b->fw->nf)) > 128)
871           {
872             strncpy(ftmp, *(b->df->name + b->fw->nf), 128);
873             ftmp[128] = '\0';
874           }
875           else
876             strcpy(ftmp, *(b->df->name + b->fw->nf));
877 
878           /* make the file name editable */
879           c = e_schreib_leiste(ftmp, b->fw->xa, b->fw->ya + b->fw->nf - b->fw->ia,
880                                b->fw->xe - b->fw->xa, 128, f->fb->fr.fb, f->fb->fz.fb);
881           if(c == WPE_CR)
882           {
883             if(j == AltM)
884               e_rename(*(b->df->name + b->fw->nf), ftmp, f); /* move */
885             else if(j == AltL)
886               WpeLinkFile(*(b->df->name + b->fw->nf), ftmp, /* link */
887                           f->ed->flopt & FM_TRY_HARDLINK, f);
888             else if(j == AltO)
889               e_copy(*(b->df->name + b->fw->nf), ftmp, f); /* copy */
890 
891             /* after copying/moving/linking, free up the old structures */
892             freedf(b->df);
893             freedf(b->fw->df);
894 
895             /* generate the new file list */
896             b->df = e_find_files(b->rdfile, fmode);
897             /* setup the drawing of it */
898             b->fw->df = WpeGraphicalFileList(b->df, f->ed->flopt >> 9, cn);
899             b->fw->ia = b->fw->nf = 0;
900             b->fw->ja = b->fw->srcha;
901           }
902           FREE(ftmp);
903         }
904         /* we are coming from dirs */
905         else if(cold == AltT && b->dw->nf >= b->cd->anz)
906         {
907           if((ftmp = MALLOC(129)) == NULL)
908             e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
909 
910           /* selected dir */
911           t = b->dw->nf - b->cd->anz;
912 
913           if(strlen(*(b->dd->name + t)) > 128)
914           {
915             strncpy(ftmp, *(b->dd->name + t), 128);
916             ftmp[128] = '\0';
917           }
918           else
919             strcpy(ftmp, *(b->dd->name + t));
920 
921 
922           /* separate the dir name from other drawings in the line */
923           for(i = 0; *(b->dw->df->name[b->dw->nf] + i) &&
924               (*(b->dw->df->name[b->dw->nf] + i) <= 32 ||
925                *(b->dw->df->name[b->dw->nf] + i) >= 127); i++)
926             ;
927 
928           if(!WpeIsXwin())
929             i += 3;
930           b->dw->ja = i;
931           e_pr_file_window(b->dw, 0, 1, f->fb->ft.fb, f->fb->fz.fb, f->fb->frft.fb);
932           /* make the name editable */
933           c = e_schreib_leiste(ftmp, b->dw->xa, b->dw->ya + b->dw->nf - b->dw->ia,
934                                b->dw->xe - b->dw->xa, 128, f->fb->fr.fb, f->fb->fz.fb);
935           if(c == WPE_CR)
936           {
937             if(j == AltM)
938               e_rename(*(b->dd->name + t), ftmp, f); /* move */
939             else if(j == AltL)
940               e_link(*(b->dd->name + t), ftmp, f); /* link */
941             else if(j == AltO)
942               e_copy(*(b->dd->name + t), ftmp, f); /* copy */
943 
944             /* free up structures */
945             freedf(b->cd);
946             freedf(b->dw->df);
947             freedf(b->dd);
948 
949             /* determine current directory */
950             b->cd = WpeCreateWorkingDirTree(f->save, cn);
951             /* find all other directories in the current */
952             b->dd = e_find_dir(SUDIR, f->ed->flopt & FM_SHOW_HIDDEN_DIRS ? 1 : 0);
953             /* setup drawing */
954             b->dw->df = WpeGraphicalDirTree(b->cd, b->dd, cn);
955             nco = b->dw->nf = b->cd->anz - 1;
956             b->dw->ia = b->dw->ja = 0;
957           }
958           FREE(ftmp);
959         }
960         c = cold;
961         cold = AltN; /* go back to name entry */
962         break;
963 
964       /* remove button activation */
965       case ENTF:
966         if(b->sw != 0)
967         {
968           c = cold;
969           break;
970         }
971       /* remove button activation */
972       case AltR:
973         if(b->sw == 0)
974         {
975           /* coming from file list */
976           if(cold == AltF)
977           {
978             WpeRemove(*(b->df->name + b->fw->nf), f); /* remove the file */
979 
980             /* free up structures */
981             freedf(b->df);
982             freedf(b->fw->df);
983 
984             /* find files according to the pattern */
985             b->df = e_find_files(b->rdfile, fmode);
986 
987             /* setup drawing */
988             b->fw->df = WpeGraphicalFileList(b->df, f->ed->flopt >> 9, cn);
989             b->fw->ia = b->fw->nf = 0;
990             b->fw->ja = b->fw->srcha;
991           }
992           /* coming from the dir tree list and the selected dir is a subdir of
993              the current directory */
994           else if(cold == AltT && b->dw->nf >= b->cd->anz)
995           {
996             t = b->dw->nf - b->cd->anz;
997             WpeRemove(*(b->dd->name + t), f);  /* remove the dir */
998 
999             /* free up structures */
1000             freedf(b->cd);
1001             freedf(b->dw->df);
1002             freedf(b->dd);
1003 
1004             /* determine current directory */
1005             b->cd = WpeCreateWorkingDirTree(f->save, cn);
1006             /* find all other directories in the current */
1007             b->dd = e_find_dir(SUDIR, f->ed->flopt & FM_SHOW_HIDDEN_DIRS ? 1 : 0);
1008             /* setup drawing */
1009             b->dw->df = WpeGraphicalDirTree(b->cd, b->dd, cn);
1010             nco = b->dw->nf = b->cd->anz - 1;
1011             b->dw->ia = b->dw->ja = 0;
1012           }
1013           c = cold;
1014           cold = AltN; /* go back to name entry */
1015           break;
1016         }
1017 
1018       /* edit/execute button activation */
1019       case AltE:
1020         if(   (c == AltE && b->sw != 0 && b->sw != 3)
1021            || (c == AltR && b->sw != 1) )
1022         {
1023           c = cold;
1024           break;
1025         }
1026         if(b->sw == 3) /* file-manager in execution mode */
1027         {
1028           if(cold == AltF)
1029             strcpy(filen, *(b->df->name + b->fw->nf));     /* !!! alloc filen ??? */
1030           if(!WpeIsXwin())
1031           {
1032             outp = e_open_view(0, 0, MAXSCOL - 1, MAXSLNS - 1, f->fb->ws, 1);
1033             fk_locate(0, 0);
1034             fk_cursor(1);
1035 #if  MOUSE
1036             g[0] = 2;
1037             fk_mouse(g);
1038 #endif
1039             e_sys_ini();
1040             printf(e_msg[ERR_EXEC], filen);
1041             fflush(stdout);
1042           }
1043           if ((*e_u_system)(filen))
1044           {
1045             if(!WpeIsXwin())
1046               e_sys_end();
1047             e_error(e_msg[ERR_COMMAND], 0, f->fb);
1048           }
1049           else if(!WpeIsXwin())
1050           {
1051             printf(e_msg[ERR_HITCR]);
1052             fflush(stderr);
1053             fflush(stdout);
1054             fk_getch();
1055           }
1056           if(!WpeIsXwin())
1057           {
1058             e_sys_end();
1059             e_close_view(outp, 1);
1060             fk_cursor(0);
1061 #if MOUSE
1062             g[0] = 1;
1063             fk_mouse(g);
1064 #endif
1065           }
1066           c = cold;
1067           break;
1068         }
1069         else
1070         {
1071           /* if there is only a pattern get back to file selection */
1072           if(strstr(filen, "*") || strstr(filen, "?"))
1073           {
1074             c = AltF;
1075             break;
1076           }
1077           /* there is no open ??? file */
1078           if(b->sw == 0 || !fe)
1079           {
1080             /* close on open request */
1081             if(f->ed->flopt & FM_CLOSE_WINDOW)
1082             {
1083               e_close_window(f);
1084             }
1085             /* editing the file */
1086             e_edit(cn, filen);
1087           }
1088           else
1089           {
1090             /* try to open the file, no success return */
1091             if((fp = fopen(filen, "rb")) == NULL)
1092             {
1093               e_error(e_msg[ERR_ACCFILE], 0, f->fb);
1094               c = cold;
1095               break;
1096             }
1097             if(access(filen, 2) != 0)
1098               f->ins = 8;
1099             e_close_window(f);
1100             e_switch_window(winnum, fe);
1101             fe = cn->f[cn->mxedt];
1102             be = fe->b;
1103             se = fe->s;
1104             f = cn->f[cn->mxedt];
1105             if(be->b.x != 0)
1106             {
1107               e_new_line(be->b.y + 1, be);
1108               if(*(be->bf[be->b.y].s + be->bf[be->b.y].len) != '\0')
1109                 (be->bf[be->b.y].len)++;
1110               for(i = be->b.x; i <= be->bf[be->b.y].len; i++)
1111                 *(be->bf[be->b.y + 1].s + i - be->b.x) = *(be->bf[be->b.y].s + i);
1112               *(be->bf[be->b.y].s + be->b.x) = '\0';
1113               be->bf[be->b.y].len = be->b.x;
1114               be->bf[be->b.y + 1].len = e_str_len(be->bf[be->b.y + 1].s);
1115               be->bf[be->b.y + 1].nrc = e_str_nrc(be->bf[be->b.y + 1].s);
1116             }
1117             se->mark_begin.x = be->b.x;
1118             start = se->mark_begin.y = be->b.y;
1119             dtmd = fe->dtmd;
1120             se->mark_end = e_readin(be->b.x, be->b.y, fp, be, &dtmd);
1121             fclose(fp);
1122 
1123             if(se->mark_begin.x > 0)
1124               start++;
1125             len = se->mark_end.y - start;
1126             e_brk_recalc(f, start, len);
1127 
1128             e_schirm(fe, 1);
1129           }
1130 
1131           /* if there was no error */
1132           dirtmp = WpeGetCurrentDir(cn);
1133           FREE(cn->dirct);
1134           cn->dirct = dirtmp;
1135 
1136           if (svmode >= 0)
1137             cn->flopt = svmode;
1138 
1139           if (svdir != NULL)
1140           {
1141             /* go back to the saved directory */
1142             if (chdir(svdir))
1143             {
1144               e_error(e_msg[ERR_WORKDIRACCESS], 0, cn->fb);
1145 
1146               /* we cannot determine where we are, try the home */
1147               if((dirtmp = getenv("HOME")) == NULL)
1148                 e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
1149               if(chdir(dirtmp))
1150                 e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
1151             }
1152 
1153             /* determine current dir */
1154             dirtmp = WpeGetCurrentDir(cn);
1155 
1156             FREE(cn->dirct);
1157             cn->dirct = dirtmp;
1158 
1159             FREE(svdir);
1160             svdir = NULL;
1161           }
1162 
1163           return(0);
1164         }
1165 
1166       case AltW: /* write activation */
1167       case AltS: /* save activation */
1168         if(   (c == AltW && b->sw != 2)
1169            || (c == AltS && b->sw != 4)
1170            || !fe
1171            || fe->ins == 8 )
1172         {
1173           c = cold;
1174           break;
1175         }
1176         /* only file pattern, return */
1177         if(strstr(filen, "*") || strstr(filen, "?"))
1178         {
1179           c = AltF;
1180           break;
1181         }
1182         /* check whether the file exist */
1183         if(!access(filen, F_OK))
1184         {
1185           if((ftmp = MALLOC(strlen(filen) + 42)) == NULL)
1186             e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
1187 
1188           sprintf(ftmp, "File %s exist\nDo you want to overwrite it ?", filen);
1189           i = e_message(1, ftmp, f);
1190           FREE(ftmp);
1191 
1192           if(i == WPE_ESC)
1193           {
1194             c = WPE_ESC;
1195             break;
1196           }
1197           else if(i == 'N')
1198           {
1199             c = AltF;
1200             break;
1201           }
1202         }
1203         if(b->sw != 4)
1204         {
1205           dtp = fe->dirct;
1206           ftp = fe->datnam;
1207         }
1208         else /* save as mode, current dir and window header will/may change */
1209         {
1210           FREE(fe->dirct);
1211           FREE(fe->datnam);
1212         }
1213 
1214         WpeFilenameToPathFile(filen, &fe->dirct, &fe->datnam);
1215         if(b->sw == 4) /* save as mode */
1216           e_save(fe);
1217         else
1218         {
1219           e_write(se->mark_begin.x, se->mark_begin.y, se->mark_end.x, se->mark_end.y, fe, WPE_BACKUP);
1220           FREE(fe->dirct);  /* restore current dir window header */
1221           FREE(fe->datnam);
1222           fe->dirct = dtp;
1223           fe->datnam = ftp;
1224         }
1225 
1226         if(b->sw == 4 && (f->ed->edopt & ED_SYNTAX_HIGHLIGHT))
1227         {
1228           if(fe->c_sw)
1229             FREE(fe->c_sw);
1230           if(WpeIsProg())
1231             e_add_synt_tl(fe->datnam, fe);
1232           if(fe->c_st)
1233           {
1234             if(fe->c_sw)
1235               FREE(fe->c_sw);
1236             fe->c_sw = e_sc_txt(NULL, fe->b);
1237           }
1238           e_rep_win_tree(f->ed);
1239         }
1240         if(svmode >= 0)
1241           f->ed->flopt = svmode;
1242 
1243         if(svdir != NULL)
1244         {
1245           /* go back to the saved directory */
1246           if(chdir(svdir))
1247           {
1248             e_error(e_msg[ERR_WORKDIRACCESS], 0, cn->fb);
1249 
1250             /* we cannot determine where we are, try the home */
1251             if((dirtmp = getenv("HOME")) == NULL)
1252               e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
1253             if(chdir(dirtmp))
1254               e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
1255           }
1256 
1257           /* determine current dir */
1258           dirtmp = WpeGetCurrentDir(cn);
1259 
1260           FREE(cn->dirct);
1261           cn->dirct = dirtmp;
1262 
1263           FREE(svdir);
1264           svdir = NULL;
1265         }
1266 
1267         e_close_window(f);
1268         return(0);
1269 
1270       /* make dir button activation */
1271       case EINFG:
1272       case AltK:
1273         if(b->sw != 0)
1274         {
1275           c = cold;
1276           break;
1277         }
1278         /* create new directory */
1279         if(WpeMakeNewDir(f) != 0)
1280         {
1281           c = cold;
1282           break;
1283         }
1284 
1285         /* free up old structures */
1286         freedf(b->dd);
1287         freedf(b->dw->df);
1288 
1289         /* create new directory structure */
1290         b->dd = e_find_dir(SUDIR, f->ed->flopt & FM_SHOW_HIDDEN_DIRS ? 1 : 0);
1291         b->dw->df = WpeGraphicalDirTree(b->cd, b->dd, cn);
1292         /* go to the line where the new dir is */
1293         for(i = 0; i < b->dd->anz && strcmp(b->dd->name[i], "new.dir"); i++)
1294           ;
1295         /* set the slidebar variables */
1296         if((b->dw->nf = b->cd->anz + i) >= b->dw->df->anz)
1297           b->dw->nf = b->cd->anz - 1;
1298         if(b->dw->nf - b->dw->ia >= b->dw->ye - b->dw->ya)
1299           b->dw->ia = b->dw->nf + b->dw->ya - b->dw->ye + 1;
1300         else if(b->dw->nf - b->dw->ia < 0)
1301           b->dw->ia = b->dw->nf;
1302         cold = AltT;
1303         /* let the user modify the newly created dir */
1304         c = AltM;
1305         break;
1306 
1307       /* attribute/add file button activation */
1308       case AltA:
1309         /* not valid mode */
1310         if(b->sw != 0 && b->sw != 5)
1311         {
1312           c = cold;
1313           break;
1314         }
1315         /* attribute button */
1316         if(b->sw == 0)
1317         {
1318           if(cold == AltF) /* coming from file list */
1319           {
1320             strcpy(filen, *(b->df->name + b->fw->nf));  /* alloc for filen ??? */
1321             /* change the file attributes */
1322             WpeFileDirAttributes(filen, f);
1323 
1324             /* free up old file list structures */
1325             freedf(b->df);
1326             freedf(b->fw->df);
1327 
1328             /* create new file list */
1329             b->df = e_find_files(b->rdfile, fmode);
1330             /* setup drawing */
1331             b->fw->df = WpeGraphicalFileList(b->df, f->ed->flopt >> 9, cn);
1332           }
1333           else if(cold == AltT && b->dw->nf >= b->cd->anz) /* coming from dir tree */
1334           {
1335             t = b->dw->nf - b->cd->anz;
1336 
1337             if((ftmp = MALLOC(strlen(*(b->dd->name + t)) + 1)) == NULL)
1338               e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
1339 
1340             strcpy(ftmp, *(b->dd->name + t));
1341             /* change the dir attributes */
1342             WpeFileDirAttributes(ftmp, f);
1343 
1344             FREE(ftmp);
1345 
1346             /* free up old file list structures */
1347             freedf(b->dd);
1348             freedf(b->dw->df);
1349             /* create new dir list */
1350             b->dd = e_find_dir(SUDIR, f->ed->flopt & FM_SHOW_HIDDEN_DIRS ? 1 : 0);
1351             /* setup drawing */
1352             b->dw->df = WpeGraphicalDirTree(b->cd, b->dd, cn);
1353           }
1354           c = cold;
1355         }
1356         else if(b->sw == 5) /* it is in project management */
1357         {
1358           FLWND *fw = (FLWND *)cn->f[cn->mxedt - 1]->b;
1359           if (cold != AltN)
1360             strcpy(filen, *(b->df->name + b->fw->nf));
1361           dirtmp = cn->f[cn->mxedt - 1]->dirct;
1362           ftmp = MALLOC(strlen(f->dirct) + strlen(filen) + 2);
1363           len = strlen(dirtmp);
1364           if (strncmp(dirtmp, f->dirct, len) == 0)
1365           {
1366            /* Make path relative to project directory */
1367            sprintf(ftmp, "%s%s", f->dirct + len, filen);
1368           }
1369           else
1370           {
1371            /* Full path */
1372            sprintf(ftmp, "%s%s", f->dirct, filen);
1373           }
1374           fw->df->anz++;
1375           fw->df->name = REALLOC(fw->df->name, fw->df->anz * sizeof(char *));
1376           for(i = fw->df->anz - 1; i > fw->nf; i--)
1377             fw->df->name[i] = fw->df->name[i - 1];
1378           fw->df->name[i] = ftmp;
1379 /* Don't bother notifying the user for each file added to project
1380    sprintf(ftmp, "File added to Project:\n%s",
1381    fw->df->name[i]);
1382    e_message(0, ftmp, f); */
1383           fw->nf++;
1384           if(fw->nf - fw->ia >= fw->ye - fw->ya)
1385             fw->ia = fw->nf + fw->ya - fw->ye + 1;
1386           c = AltN;
1387         }
1388         break;
1389 
1390       /* like save */
1391       case F2:
1392         c = AltS;
1393         break;
1394       /* leave file manager */
1395       case AltBl:
1396         c = WPE_ESC;
1397 
1398       default:
1399         /* not project management */
1400         if(b->sw != 5)
1401         {
1402           if(svmode >= 0)
1403           {
1404             /* restore options */
1405             f->ed->flopt = svmode;
1406           }
1407 
1408           if(svdir != NULL)
1409           {
1410             /* go back to the saved directory */
1411             if(chdir(svdir))
1412             {
1413               e_error(e_msg[ERR_WORKDIRACCESS], 0, cn->fb);
1414 
1415               /* we cannot determine where we are, try the home */
1416               if((dirtmp = getenv("HOME")) == NULL)
1417                 e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
1418               if(chdir(dirtmp))
1419                 e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
1420             }
1421 
1422             /* determine current dir */
1423             dirtmp = WpeGetCurrentDir(f->ed);
1424             FREE(f->ed->dirct);
1425             f->ed->dirct = dirtmp;
1426           }
1427 
1428 
1429           /* the key dispatcher returns zero when something has happened
1430              and this means that the file-manager lost focus, just return */
1431 #ifdef PROG
1432           if(!e_tst_dfkt(f, c) || (WpeIsProg() && !e_prog_switch(f, c)))
1433 #else
1434           if(!e_tst_dfkt(f, c))
1435 #endif
1436           {
1437             if(svdir != NULL)
1438             {
1439               FREE(svdir);
1440               svdir = NULL;
1441             }
1442 
1443             return(0);
1444           }
1445           /* file manager is still active */
1446           if(svmode >= 0)
1447             f->ed->flopt = FM_SHOW_HIDDEN_FILES | FM_SHOW_HIDDEN_DIRS |
1448               FM_MOVE_OVERWRITE | FM_REKURSIVE_ACTIONS;
1449         }
1450         else if(   c == WPE_ESC
1451                 || (!(f->ed->edopt & ED_CUA_STYLE) && c == AF3)
1452                 || (f->ed->edopt & ED_CUA_STYLE && c == CF4))
1453         {
1454           if(svdir != NULL)
1455           {
1456             /* go back to the saved directory */
1457             if(chdir(svdir))
1458             {
1459               e_error(e_msg[ERR_WORKDIRACCESS], 0, cn->fb);
1460 
1461               /* we cannot determine where we are, try the home */
1462               if((dirtmp = getenv("HOME")) == NULL)
1463                 e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
1464               if(chdir(dirtmp))
1465                 e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
1466             }
1467 
1468             /* determine current dir */
1469             dirtmp = WpeGetCurrentDir(cn);
1470 
1471             FREE(cn->dirct);
1472             cn->dirct = dirtmp;
1473 
1474             FREE(svdir);
1475             svdir = NULL;
1476           }
1477 
1478           /* close file manager */
1479           e_close_window(f);
1480 
1481           /* restore options */
1482           if(svmode >= 0)
1483             cn->flopt = svmode;
1484 
1485           return(WPE_ESC);
1486         }
1487         c = cold;
1488         break;
1489     }
1490   }
1491 
1492   /* change to saved directory and lets hope it works */
1493   if(svdir != NULL)
1494   {
1495     /* go back to the saved directory */
1496     if(chdir(svdir))
1497     {
1498       e_error(e_msg[ERR_WORKDIRACCESS], 0, cn->fb);
1499 
1500       /* we cannot determine where we are, try the home */
1501       if((dirtmp = getenv("HOME")) == NULL)
1502         e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
1503       if(chdir(dirtmp))
1504         e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
1505     }
1506 
1507     /* determine current dir */
1508     dirtmp = WpeGetCurrentDir(cn);
1509 
1510     FREE(cn->dirct);
1511     cn->dirct = dirtmp;
1512 
1513     FREE(svdir);
1514     svdir = NULL;
1515   }
1516 
1517   /* restore options */
1518   if(svmode >= 0)
1519     cn->flopt = svmode;
1520 
1521   /* if in save mode or project management */
1522   if(f->save == 1 || b->sw == 5)
1523   {
1524     e_close_window(f);
1525     return(WPE_ESC);
1526   }
1527   else
1528   {
1529     e_close_window(f);
1530     return(0);
1531   }
1532 }
1533 
1534 
WpeGrepFile(char * file,char * string,int sw)1535 int WpeGrepFile(char *file, char *string, int sw)
1536 {
1537   FILE           *fp;
1538   char            str[256];
1539   int             ret, nn;
1540 
1541   if((fp = fopen(file, "r")) == NULL)
1542     return(0);
1543 
1544   nn = strlen(string);
1545   while(fgets(str, 256, fp))
1546   {
1547     if((sw & 32) == 0)
1548     {
1549       if((sw & 128) != 0)
1550         ret = e_strstr(0, strlen(str), str, string);
1551       else
1552         ret = e_ustrstr(0, strlen(str), str, string);
1553     }
1554     else
1555     {
1556       if((sw & 128) != 0)
1557         ret = e_rstrstr(0, strlen(str), str, string, &nn);
1558       else
1559         ret = e_urstrstr(0, strlen(str), str, string, &nn);
1560     }
1561     if(   ret >= 0
1562        && (   !(sw & 64)
1563            || (   isalnum(str[ret + nn]) == 0
1564                && (ret == 0 || isalnum(str[ret - 1]) == 0))))
1565     {
1566       fclose(fp);
1567       return(1);
1568     }
1569   }
1570   fclose(fp);
1571   return(0);
1572 }
1573 
WpeMakeNewDir(FENSTER * f)1574 int WpeMakeNewDir(FENSTER *f)
1575 {
1576   char           *dirct;
1577   int             msk, mode, ret;
1578   struct stat     buf;
1579 
1580   umask(msk = umask(077));
1581   mode = 0777 & ~msk;
1582 
1583   if((dirct = MALLOC(strlen(f->dirct) + 9)) == NULL)
1584     e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
1585 
1586   if(f->dirct[strlen(f->dirct) - 1] != DIRC)
1587     sprintf(dirct, "%s/new.dir", f->dirct);
1588   else
1589     sprintf(dirct, "%snew.dir", f->dirct);
1590 
1591   /* check existence and status (whether it is directory) */
1592   if(stat(dirct, &buf))
1593   {
1594     if((ret = mkdir(dirct, mode)) != 0)
1595       e_error(e_msg[ERR_NONEWDIR], 0, f->ed->fb);
1596   }
1597   else
1598   {
1599     /* check whether the existing file is a directory */
1600     if(!(buf.st_mode & S_IFDIR))
1601     {
1602       e_error(e_msg[ERR_NEWDIREXIST], 0, f->ed->fb);
1603       ret = 1;
1604     }
1605     else
1606       ret = 0;
1607   }
1608 
1609   FREE(dirct);
1610   return(ret);
1611 }
1612 
WpeFileDirAttributes(char * filen,FENSTER * f)1613 int WpeFileDirAttributes(char *filen, FENSTER * f)
1614 {
1615   struct stat     buf[1];
1616   int             mode, ret;
1617   W_OPTSTR       *o = e_init_opt_kst(f);
1618 
1619   /* if cannot access or error */
1620   if(stat(filen, buf))
1621   {
1622     e_error(e_msg[ERR_ACCFILE], 0, f->ed->fb);
1623     return 1;
1624   }
1625   mode = buf->st_mode;
1626   if(o == NULL)
1627     return(1);
1628   o->xa = 14;
1629   o->ya = 4;
1630   o->xe = 62;
1631   o->ye = 13;
1632   o->bgsw = AltO;
1633   o->name = "Attributes";
1634   o->crsw = AltO;
1635   e_add_txtstr(3, 2, "User:", o);
1636   e_add_txtstr(33, 2, "Other:", o);
1637   e_add_txtstr(18, 2, "Group:", o);
1638   e_add_sswstr(4, 3, 0, AltR, mode & 256 ? 1 : 0, "Read   ", o);
1639   e_add_sswstr(4, 4, 0, AltW, mode & 128 ? 1 : 0, "Write  ", o);
1640   e_add_sswstr(4, 5, 1, AltX, mode & 64 ? 1 : 0, "EXecute", o);
1641   e_add_sswstr(19, 3, 2, AltA, mode & 32 ? 1 : 0, "ReAd   ", o);
1642   e_add_sswstr(19, 4, 2, AltI, mode & 16 ? 1 : 0, "WrIte  ", o);
1643   e_add_sswstr(19, 5, 0, AltE, mode & 8 ? 1 : 0, "Execute", o);
1644   e_add_sswstr(34, 3, 3, AltD, mode & 4 ? 1 : 0, "ReaD   ", o);
1645   e_add_sswstr(34, 4, 3, AltT, mode & 2 ? 1 : 0, "WriTe  ", o);
1646   e_add_sswstr(34, 5, 4, AltU, mode & 1, "ExecUte", o);
1647   e_add_bttstr(12, 7, 1, AltO, " Ok ", NULL, o);
1648   e_add_bttstr(29, 7, -1, WPE_ESC, "Cancel", NULL, o);
1649   ret = e_opt_kst(o);
1650   if(ret != WPE_ESC)
1651   {
1652     mode = (o->sstr[0]->num << 8) + (o->sstr[1]->num << 7) +
1653       (o->sstr[2]->num << 6) + (o->sstr[3]->num << 5) +
1654       (o->sstr[4]->num << 4) + (o->sstr[5]->num << 3) +
1655       (o->sstr[6]->num << 2) + (o->sstr[7]->num << 1) +
1656       o->sstr[8]->num;
1657     if(chmod(filen, mode))
1658     {
1659       e_error(e_msg[ERR_CHGPERM], 0, f->ed->fb);
1660       freeostr(o);
1661       return(1);
1662     }
1663   }
1664   freeostr(o);
1665   return(0);
1666 }
1667 
1668 /* determines the current working directory,
1669    it returns the string
1670    in case of an error - if the directory cannot be accessed try to access HOME,
1671                          if it is impossible, give it up and exit with "system error"
1672                        - otherwise exit with "system error"
1673  */
WpeGetCurrentDir(ECNT * cn)1674 char *WpeGetCurrentDir(ECNT *cn)
1675 {
1676   int allocate_size;
1677   char *current_dir = NULL, *check_dir, *dirtmp;
1678   short home;
1679 
1680   home = 0;
1681   allocate_size = 256;
1682   if ((current_dir = (char *)WpeMalloc(allocate_size + 1)) == NULL)
1683   {
1684    e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
1685    return NULL;
1686   }
1687 
1688   do
1689   {
1690     /* allocate space for the directory name */
1691     if ((current_dir = (char *)WpeRealloc(current_dir, allocate_size + 1)) == NULL)
1692     {
1693      e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
1694      return NULL;
1695     }
1696 
1697     check_dir = getcwd(current_dir, allocate_size);
1698     if(check_dir == NULL)
1699     {
1700       switch(errno)
1701       {
1702         case EACCES:  /* directory cannot be read */
1703           if (home == 0)
1704           {
1705             e_error(e_msg[ERR_WORKDIRACCESS], 0, cn->fb);
1706 
1707             /* we cannot determine where we are, try the home */
1708             if ((dirtmp = getenv("HOME")) == NULL)
1709               e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
1710             if (chdir(dirtmp))
1711               e_error(e_msg[ERR_HOMEDIRACCESS], 1, cn->fb);
1712 
1713             home = 1;
1714           }
1715           else
1716             e_error(e_msg[ERR_SYSTEM], 1, cn->fb); /* will not return */
1717           break;
1718         case EINVAL:  /* size is equal to 0 */
1719           allocate_size = 256; /* impossible !!! */
1720           break;
1721         case ERANGE:  /* not enough space for the pathname */
1722           allocate_size <<= 1;
1723           break;
1724         default:      /* System error */
1725           e_error(e_msg[ERR_SYSTEM], 1, cn->fb); /* will not return */
1726           break;
1727       }
1728     }
1729   } while (check_dir == NULL);
1730 
1731   if (current_dir[strlen(current_dir) - 1] != DIRC)
1732    strcat(current_dir, DIRS);
1733   return(current_dir);
1734 }
1735 
1736 /* It always returns the assembled file path, except
1737    when it is in wastebasket mode and there is an error, return NULL */
WpeAssemblePath(char * pth,struct dirfile * cd,struct dirfile * dd,int n,FENSTER * f)1738 char *WpeAssemblePath(char *pth, struct dirfile *cd, struct dirfile *dd, int n,
1739                       FENSTER *f)
1740 {
1741   int             i = 0, k = 0, j = 0, t;
1742   char           *adir= NULL;  /* assembled directory */
1743   int             totall = 0;
1744 
1745 #ifdef UNIX
1746   if (!strcmp(cd->name[0], "Wastebasket"))
1747   {
1748     if ((adir = WpeGetWastefile("")))
1749     {
1750       totall = strlen(adir) + 16;
1751       if ((adir = REALLOC(adir, totall)) == NULL)
1752         e_error(e_msg[ERR_LOWMEM], 1, f->fb);
1753 
1754       strcat(adir, DIRS);
1755       i = strlen(adir);
1756       k++;
1757     }
1758     else
1759     {
1760       /* Error failed to find wastebasket */
1761       e_error(e_msg[ERR_NOWASTE], 0, f->ed->fb);
1762       return NULL;
1763     }
1764   }
1765 #endif
1766   for(; k <= n && k < cd->anz; i++, j++)
1767   {
1768     if(i >= totall-1)
1769     {
1770       totall += 16;
1771       if((adir = REALLOC(adir, totall)) == NULL)
1772         e_error(e_msg[ERR_LOWMEM], 1, f->fb);
1773     }
1774     *(adir + i) = *(*(cd->name + k) + j);
1775 
1776     if(*(adir + i) == '\0' || *(adir + i) == DIRC)
1777     {
1778       *(adir + i) = DIRC;
1779       k++;
1780       j = -1;
1781     }
1782   }
1783   if(n >= k)
1784   {
1785     t = n - cd->anz;
1786     j = 0;
1787 
1788     do
1789     {
1790       if(i >= totall-2)
1791       {
1792         totall += 16;
1793         if((adir = REALLOC(adir, totall)) == NULL)
1794           e_error(e_msg[ERR_LOWMEM], 1, f->fb);
1795       }
1796       *(adir + i) = *(*(dd->name + t) + j);
1797       i++;
1798       j++;
1799     }
1800     while(*(adir + i - 1) != '\0');
1801 
1802 /*
1803     for(j = 0; (*(pth + i) = *(*(dd->name + t) + j)) != '\0'; i++, j++)
1804       ;
1805 */
1806 
1807   }
1808   if(n == 0 || n >= k)
1809     i++;
1810   *(adir + i - 1) = '\0';
1811   return(adir);
1812 }
1813 
1814 
1815 /* create tree structure up to working directory */
WpeCreateWorkingDirTree(int sw,ECNT * cn)1816 struct dirfile *WpeCreateWorkingDirTree(int sw, ECNT *cn)
1817 {
1818   struct dirfile *df;
1819   char           *buf;
1820   char           *tmp, *tmp2;
1821   char          **dftmp;
1822   int             buflen = 256;
1823   int             maxd = 10;    /* inital number of directory levels */
1824   int             i, j, k;
1825 
1826   buf = WpeGetCurrentDir(cn);
1827 
1828   buflen = strlen(buf);
1829   if((tmp = MALLOC(buflen+1)) == NULL)
1830     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
1831 
1832   /* initialise directory list */
1833   if(   ((df = MALLOC(sizeof(struct dirfile))) == NULL)
1834      || ((df->name = MALLOC(sizeof(char *) * maxd)) == NULL) )
1835     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
1836 
1837   df->anz = 0;
1838 
1839   if(sw == 1)                        /* file-manager open to wastebasket */
1840   {
1841     /* instead of saving the real wastebasket dir into the structure
1842        the "Wastebasket" name will appear */
1843     if((tmp2 = WpeGetWastefile("")) == NULL)
1844     {
1845       e_error(e_msg[ERR_NOWASTE], 0, cn->fb);    /* more error check ??? */
1846       i = 0;
1847     }
1848     else
1849     {
1850       i = strlen(tmp2);
1851       /* increase the level in the dir tree */
1852       df->anz = 1;
1853       /* save the name into first level */
1854       if((*(df->name) = MALLOC(12 * sizeof(char))) == NULL)
1855         e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
1856       strcpy(*(df->name), "Wastebasket");
1857 
1858       if(!strncmp(tmp2, buf, i) && buf[i])
1859       {
1860         FREE(tmp2);
1861         i++;
1862       }
1863       else
1864       {
1865         FREE(tmp2);
1866         FREE(buf);
1867         FREE(tmp);
1868         return(df);
1869       }
1870     }
1871   }
1872   else
1873   {
1874     i = 0;
1875   }
1876 
1877 
1878   for(j = 0; i <= buflen; i++, j++)
1879   {
1880     tmp[j] = buf[i];
1881     /* if directory separator or end of string */
1882     if(tmp[j] == DIRC || tmp[j] == '\0')
1883     {
1884       if(buf[i] == '\0' && j == 0)
1885         return(df);
1886       if(df->anz == 0)  /* for the very first time save the '/' sign */
1887         j++;
1888       tmp[j] = '\0';
1889 
1890       /* if we need more space for the directories */
1891       if(df->anz >= maxd)
1892       {
1893         maxd += 10;
1894         dftmp = df->name;
1895         if((df->name = MALLOC(sizeof(char *) * maxd)) == NULL)
1896           e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
1897         for(k = 0; k < maxd - 10; k++)
1898           *(df->name + k) = *(dftmp + k);
1899         FREE(dftmp);
1900       }
1901       /* save the current directory */
1902       if((*(df->name + df->anz) = MALLOC((strlen(tmp) + 1) * sizeof(char))) == NULL)
1903         e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
1904       strcpy(*(df->name + df->anz), tmp);
1905       df->anz++;
1906       j = -1;
1907     }
1908   }
1909   FREE(buf);
1910   FREE(tmp);
1911   return(df);
1912 }
1913 
1914 /* This function creates a wastebasket directory if needed, then returns a
1915    string made up of the last filename in the string pointed to by 'file' and
1916    the wastebasket path.  It returns NULL if there is a memory allocation
1917    error or the pointer to 'path' otherwise.  The original function did no
1918    error checking on wastebasket path creation.  The modified version does --
1919    it also returns NULL on an error in the chdir function or mkdir
1920    function. */
WpeGetWastefile(char * file)1921 char  *WpeGetWastefile(char *file)
1922 {
1923   static char    *wastebasket = NULL;
1924   int             i, lw;
1925   char           *tmp;             /* storage for wastebasket path */
1926   char           *tmp2;
1927 
1928   if(!wastebasket)  /* setup and test access to wastebasket directory */
1929   {
1930     if((tmp2 = getenv("HOME")) == NULL)
1931       return NULL;
1932 
1933     lw = strlen(tmp2) + 1 + strlen(WASTEBASKET) + 1;
1934     /*    HOME          /     WASTEBASKET       \0    */
1935     if((tmp = (char *)MALLOC(lw)) == NULL)
1936       return NULL;
1937 
1938     sprintf(tmp, "%s/%s", tmp2, WASTEBASKET);
1939 
1940     /* if wastebasket dir does not exist, create it with error checking */
1941     if(access(tmp, F_OK))
1942     {
1943       if(mkdir(tmp, 0700))
1944       {
1945         FREE(tmp);
1946         return NULL;
1947       }
1948     }
1949     /* check for wastebasket's permissions */
1950     if(access(tmp, R_OK | W_OK | X_OK))
1951     {
1952       FREE(tmp);
1953       return NULL;
1954     }
1955     wastebasket = tmp;
1956   }
1957 
1958   /* verify that wastebasket directory still exists before proceeding */
1959   if(access(wastebasket, F_OK | R_OK | W_OK | X_OK))
1960   {
1961     /* try to recreate it */
1962     if(mkdir(wastebasket, 0700))
1963       return NULL;
1964   }
1965 
1966   /* return wastebasket directory path if no filename in 'file' */
1967   if(file[0] == '\0')
1968   {
1969     if((tmp2 = MALLOC(strlen(wastebasket)+1)) == NULL)
1970       return NULL;
1971     strcpy(tmp2, wastebasket);
1972     return(tmp2);
1973   }
1974 
1975   /* else get filename from end of 'file' string,
1976      append to wastebasket sting */
1977   for(i = strlen(file) - 1; i >= 0 && file[i] != DIRC; i--)
1978     ;
1979   if((tmp2 = MALLOC(strlen(wastebasket) + strlen(file + 1 + i) + 2)) == NULL)
1980     return NULL;
1981 
1982   sprintf(tmp2, "%s/%s", wastebasket, file + 1 + i);
1983   return(tmp2);
1984 }
1985 
WpeGraphicalFileList(struct dirfile * df,int sw,ECNT * cn)1986 struct dirfile *WpeGraphicalFileList(struct dirfile *df, int sw, ECNT *cn)
1987 {
1988  struct dirfile *edf;
1989  char          **name, **ename, *stmp, str[256];
1990  int             ntmp, n, i, *num;
1991 
1992  /* allocate the same structure as the argument */
1993  if ((edf = MALLOC(sizeof(struct dirfile))) == NULL)
1994   e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
1995 
1996  edf->anz = df->anz;
1997  edf->name = NULL;
1998 
1999 /* OSF and AIX fix, for malloc(0) they return NULL */
2000  if (df->anz)
2001  {
2002   if ((edf->name = MALLOC(df->anz * sizeof(char *))) == NULL)
2003    e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
2004 
2005   if ((num = MALLOC(df->anz * sizeof(int))) == NULL)
2006    e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
2007 
2008   for (i = 0; i < df->anz; i++)
2009   {
2010    e_file_info(*(df->name + i), str, num + i, sw);
2011    if ((*(edf->name + i) = MALLOC(strlen(str) + 1)) == NULL)
2012     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
2013    strcpy(*(edf->name + i), str);
2014   }
2015 
2016   /* sort by time or size mode */
2017   if (sw & 3)
2018   {
2019    if ((ename = MALLOC(df->anz * sizeof(char *))) == NULL)
2020     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
2021 
2022    if ((name = MALLOC(df->anz * sizeof(char *))) == NULL)
2023     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
2024 
2025    for (i = 0; i < df->anz; i++)
2026    {
2027     for (ntmp = num[i], n = i; n > 0 && ntmp > num[n - 1]; n--)
2028     {
2029      *(ename + n) = *(ename + n - 1);
2030      *(name + n) = *(name + n - 1);
2031      num[n] = num[n - 1];
2032     }
2033     *(ename + n) = *(edf->name + i);
2034     *(name + n) = *(df->name + i);
2035     num[n] = ntmp;
2036    }
2037    FREE(edf->name);
2038    FREE(df->name);
2039    edf->name = ename;
2040    df->name = name;
2041   }
2042 
2043   FREE(num);
2044 
2045   /* reverse order */
2046   if (sw & 4)
2047   {
2048    for (i = 0; i < (df->anz) / 2; i++)
2049    {
2050     stmp = edf->name[i];
2051     edf->name[i] = edf->name[edf->anz - i - 1];
2052     edf->name[edf->anz - i - 1] = stmp;
2053     stmp = df->name[i];
2054     df->name[i] = df->name[df->anz - i - 1];
2055     df->name[df->anz - i - 1] = stmp;
2056    }
2057   }
2058  }
2059 
2060  return(edf);
2061 }
2062 
WpeGraphicalDirTree(struct dirfile * cd,struct dirfile * dd,ECNT * cn)2063 struct dirfile *WpeGraphicalDirTree(struct dirfile *cd, struct dirfile *dd, ECNT *cn)
2064 {
2065   extern char    *ctree[5];
2066   struct dirfile *edf;
2067   char            str[256];
2068   int             i = 0, j;
2069 
2070   if((edf = MALLOC(sizeof(struct dirfile))) == NULL)
2071     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
2072 
2073   /* for the OSF and AIX this should never be zero, we are always somewhere */
2074   if(cd->anz + dd->anz > 0)
2075   {
2076     if((edf->name = MALLOC((cd->anz + dd->anz) * sizeof(char *))) == NULL)
2077       e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
2078 
2079     for(i = 0; i < cd->anz; i++)
2080     {
2081       if(!i)
2082 #ifndef DJGPP
2083       {
2084         if(*cd->name[0] == DIRC && *(cd->name[0] + 1) == '\0')
2085           strcpy(str, "Root");
2086         else
2087           strcpy(str, *(cd->name + i));
2088 #else
2089       {
2090         strcpy(str, *(cd->name + i));
2091 #endif
2092       }
2093       else
2094       {
2095         for(str[0] = '\0', j = 0; j < i - 1; j++)
2096           strcat(str, "  ");
2097         if(i == cd->anz - 1 && dd->anz < 1)
2098           strcat(str, ctree[1]);
2099         else if(i == cd->anz - 1)
2100           strcat(str, ctree[2]);
2101         else
2102           strcat(str, ctree[0]);
2103         strcat(str, *(cd->name + i));
2104       }
2105       if((*(edf->name + i) = MALLOC((strlen(str) + 1) * sizeof(char))) == NULL)
2106         e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
2107       strcpy(*(edf->name + i), str);
2108     }
2109 
2110     for(; i < cd->anz + dd->anz; i++)
2111     {
2112       for(str[0] = '\0', j = 0; j < cd->anz - 2; j++)
2113         strcat(str, "  ");
2114       strcat(str, " ");
2115       if(i == cd->anz + dd->anz - 1)
2116         strcat(str, ctree[4]);
2117       else
2118         strcat(str, ctree[3]);
2119       {
2120         int             tttt = i - cd->anz;
2121         strcat(str, *(dd->name + tttt));
2122       }
2123       if((*(edf->name + i) = MALLOC((strlen(str) + 1) * sizeof(char))) == NULL)
2124         e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
2125       strcpy(*(edf->name + i), str);
2126     }
2127   }
2128 
2129   edf->anz = i;
2130   return(edf);
2131 }
2132 
2133 int WpeDelWastebasket(FENSTER *f)
2134 {
2135   char            *tmp;
2136   int             ret, mode = f->ed->flopt;
2137 
2138   WpeMouseChangeShape(WpeWorkingShape);
2139   f->ed->flopt = FM_SHOW_HIDDEN_FILES | FM_SHOW_HIDDEN_DIRS |
2140                  FM_MOVE_OVERWRITE | FM_REKURSIVE_ACTIONS;
2141   if ((tmp = WpeGetWastefile("")))
2142   {
2143     ret = WpeRemoveDir(tmp, "*", f, 0);
2144     FREE(tmp);
2145 
2146     /* Unfortunately there is this racing condition, so
2147        this is necessary to get back the deleted wastebasket. */
2148     if ((tmp = WpeGetWastefile("")))
2149     {
2150       FREE(tmp);
2151       ret = 0;
2152     }
2153     else
2154     {
2155       e_error(e_msg[ERR_NOWASTE], 0, f->ed->fb);
2156       ret = 1;
2157     }
2158   }
2159   else
2160   {
2161     /* Error failed to find wastebasket */
2162     e_error(e_msg[ERR_NOWASTE], 0, f->ed->fb);
2163     ret = 1;
2164   }
2165   f->ed->flopt = mode;
2166   WpeMouseRestoreShape();
2167   return(ret);
2168 }
2169 
2170 int WpeShowWastebasket(FENSTER *f)
2171 {
2172   return(WpeCallFileManager(6, f));
2173 }
2174 
2175 int WpeQuitWastebasket(FENSTER * f)
2176 {
2177   char            *tmp;
2178   int             ret = 0, mode = f->ed->flopt;
2179 
2180   if(mode & FM_PROMPT_DELETE)
2181     f->ed->flopt = FM_SHOW_HIDDEN_FILES | FM_SHOW_HIDDEN_DIRS |
2182                    FM_REMOVE_PROMPT | FM_MOVE_OVERWRITE | FM_REKURSIVE_ACTIONS;
2183   else if(mode & FM_DELETE_AT_EXIT)
2184     f->ed->flopt = FM_SHOW_HIDDEN_FILES | FM_SHOW_HIDDEN_DIRS |
2185                    FM_MOVE_OVERWRITE | FM_REKURSIVE_ACTIONS;
2186 
2187   if((mode & FM_PROMPT_DELETE) || (mode & FM_DELETE_AT_EXIT))
2188   {
2189     WpeMouseChangeShape(WpeWorkingShape);
2190     if ((tmp = WpeGetWastefile("")))
2191     {
2192       ret = WpeRemoveDir(tmp, "*", f, 0);
2193       FREE(tmp);
2194     }
2195     else
2196     {
2197       /* Error failed to find wastebasket */
2198       e_error(e_msg[ERR_NOWASTE], 0, f->ed->fb);
2199       ret = 0;
2200     }
2201     WpeMouseRestoreShape();
2202   }
2203 
2204   f->ed->flopt = mode;
2205   return(ret);
2206 }
2207 
2208 
2209 int WpeRemoveDir(char *dirct, char *file, FENSTER * f, int rec)
2210 {
2211   PIC            *pic = NULL;
2212   char           *tmp;
2213   int             i, ret, svmode = f->ed->flopt;
2214   struct dirfile *dd;
2215 
2216   if(rec > MAXREC)
2217     return(0);
2218 
2219   /* only copy it to the wastebasket */
2220   if(f->ed->flopt & FM_REMOVE_INTO_WB)
2221   {
2222     if ((tmp = WpeGetWastefile(dirct)))
2223     {
2224       i = strlen(tmp);
2225       /* we should not use the fact, that the wasbasket is always something
2226          meaningful !!! */
2227       if(strncmp(tmp, dirct, i))
2228       {
2229         ret = WpeRenameCopyDir(dirct, file, tmp, f, 0, 0);
2230         FREE(tmp);
2231         return(ret);
2232       }
2233       FREE(tmp);
2234     }
2235     else
2236     {
2237       e_error(e_msg[ERR_NOWASTE], 0, f->ed->fb);
2238       return 1;
2239     }
2240   }
2241 
2242   if((tmp = MALLOC(strlen(dirct) + strlen(file)+2)) == NULL)
2243     e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2244 
2245   /* search for files in the directory */
2246   sprintf(tmp, "%s%c%s", dirct, DIRC, file);
2247   dd = e_find_files(tmp, f->ed->flopt & FM_SHOW_HIDDEN_FILES ? 1 : 0);
2248 
2249   /* it is called for the first time and the user should be asked about
2250      the deletion */
2251   if(!rec && (f->ed->flopt & FM_REMOVE_PROMPT) && dd->anz > 0)
2252   {
2253     if((ret = WpeDirDelOptions(f)) < 0)
2254     {
2255       freedf(dd);
2256       FREE(tmp);
2257       return(ret == WPE_ESC ? 1 : 0);
2258     }
2259     if(ret)
2260       f->ed->flopt |= FM_REMOVE_PROMPT;
2261     else
2262       f->ed->flopt &= ~FM_REMOVE_PROMPT;
2263     rec = -1;
2264   }
2265   FREE(tmp);
2266 
2267   /* cleans up the files in the directory */
2268   for(i = 0; i < dd->anz; i++)
2269   {
2270     if((tmp = MALLOC(strlen(dirct) + strlen(dd->name[i])+15)) == NULL)
2271       e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2272 
2273     sprintf(tmp, "Remove File:\n%s%c%s", dirct, DIRC, dd->name[i]);
2274     if(f->ed->flopt & FM_REMOVE_PROMPT)
2275     {
2276       ret = e_message(1, tmp, f);
2277     }
2278     else
2279       ret = 'Y';
2280     if(ret == WPE_ESC)
2281     {
2282       freedf(dd);
2283       FREE(tmp);
2284       f->ed->flopt = svmode;
2285       return(1);
2286     }
2287     else if(ret == 'Y')
2288     {
2289       /* this should definitely fit in */
2290       sprintf(tmp, "%s%c%s", dirct, DIRC, dd->name[i]);
2291 
2292       /* put message out */
2293       if(e_mess_win("Remove", tmp, &pic, f))
2294       {
2295         FREE(tmp);
2296         break;
2297       }
2298 
2299       if(pic)
2300       {
2301         e_close_view(pic, 1);
2302         pic = NULL;
2303       }
2304 
2305       /* try to remove it */
2306       if(remove(tmp))
2307       {
2308         e_error(e_msg[ERR_DELFILE], 0, f->ed->fb);
2309         freedf(dd);
2310         FREE(tmp);
2311         f->ed->flopt = svmode;
2312         return(1);
2313       }
2314     }
2315     FREE(tmp);
2316   }
2317 
2318   if(pic)
2319     e_close_view(pic, 1);
2320   freedf(dd);
2321 
2322   /* if recursive action is specified clean up the
2323      subdirectories as well */
2324   if(f->ed->flopt & FM_REKURSIVE_ACTIONS)
2325   {
2326     if((tmp = MALLOC(strlen(dirct) + strlen(SUDIR)+2)) == NULL)
2327       e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2328 
2329     /* search for subdirectories */
2330     sprintf(tmp, "%s%c%s", dirct, DIRC, SUDIR);
2331     dd = e_find_dir(tmp, f->ed->flopt & FM_SHOW_HIDDEN_DIRS ? 1 : 0);
2332 
2333     /* should the user be asked about deletion ? */
2334     if(!rec && (f->ed->flopt & FM_REMOVE_PROMPT) && dd->anz > 0)
2335     {
2336       if((ret = WpeDirDelOptions(f)) < 0)
2337       {
2338         freedf(dd);
2339         FREE(tmp);
2340         return(ret == WPE_ESC ? 1 : 0);
2341       }
2342       if(ret)
2343         f->ed->flopt |= FM_REMOVE_PROMPT;
2344       else
2345         f->ed->flopt &= ~FM_REMOVE_PROMPT;
2346     }
2347     else if(rec < 0)
2348       rec = 0;
2349 
2350     FREE(tmp);
2351 
2352     /* call recursively itself to delete the subdirectories */
2353     for(rec++, i = 0; i < dd->anz; i++)
2354     {
2355       if((tmp = MALLOC(strlen(dirct) + strlen(dd->name[i])+2)) == NULL)
2356         e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2357 
2358       sprintf(tmp, "%s%c%s", dirct, DIRC, dd->name[i]);
2359       if(WpeRemoveDir(tmp, file, f, rec))
2360       {
2361         freedf(dd);
2362         FREE(tmp);
2363         f->ed->flopt = svmode;
2364         return(1);
2365       }
2366       FREE(tmp);
2367     }
2368     freedf(dd);
2369   }
2370 
2371   f->ed->flopt = svmode;
2372 
2373   /* remove finally the directory itself */
2374   if(rmdir(dirct))
2375   {
2376     e_error(e_msg[ERR_DELFILE], 0, f->ed->fb);
2377     return(1);
2378   }
2379   else
2380     return(0);
2381 }
2382 
2383 int WpeRemove(char *file, FENSTER * f)
2384 {
2385   struct stat     buf;
2386   struct stat     lbuf;
2387   char           *tmp2;
2388   int             ret;
2389 
2390   if(lstat(file, &lbuf))
2391   {
2392     e_error(e_msg[ERR_ACCFILE], 0, f->ed->fb);
2393     return 1;
2394   }
2395 
2396   WpeMouseChangeShape(WpeWorkingShape);
2397 
2398   /* this is important, first we check whether it is a file,
2399      this check works even when it is a pointer, if it is not
2400      a file, then it can be an "invalid" symbolic link, check for that */
2401   if(((stat(file, &buf) == 0) && S_ISREG(buf.st_mode)) || S_ISLNK(lbuf.st_mode))
2402   {
2403     if((f->ed->flopt & FM_REMOVE_INTO_WB))
2404     {
2405       if ((tmp2 = WpeGetWastefile(file)))
2406       {
2407         ret = strlen(tmp2);
2408         if(strncmp(tmp2, file, ret))
2409         {
2410           e_rename(file, tmp2, f);
2411         }
2412         FREE(tmp2);
2413         ret = 0;
2414       }
2415       else
2416       {
2417         e_error(e_msg[ERR_NOWASTE], 0, f->ed->fb);
2418         ret = 1;
2419       }
2420     }
2421     else
2422     {
2423       if(f->ed->flopt & FM_REMOVE_PROMPT)
2424       {
2425         if((tmp2 = MALLOC(strlen(file) + 14)) == NULL)
2426           e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2427 
2428         sprintf(tmp2, "Remove File:\n%s", file);
2429         ret = e_message(1, tmp2, f);
2430         FREE(tmp2);
2431       }
2432       else
2433         ret = 'Y';
2434 
2435       if(ret == 'Y')
2436       {
2437         if(remove(file))
2438         {
2439           e_error(e_msg[ERR_DELFILE], 0, f->ed->fb);
2440           ret = 1;
2441         }
2442         else
2443           ret = 0;
2444       }
2445       else
2446         ret = 0;
2447     }
2448   }
2449   else
2450   {
2451     ret = WpeRemoveDir(file, f->fd.file, f, 0);
2452   }
2453 
2454   WpeMouseRestoreShape();
2455   return(ret);
2456 }
2457 
2458 /* sw = 0  -> renames the directory
2459    sw = 1  -> copies the directory
2460    sw = 2  -> links directory */
2461 int WpeRenameCopyDir(char *dirct, char *file, char *newname, FENSTER *f,
2462                      int rec, int sw)
2463 {
2464   char            *tmp, *ntmp, *mtmp;
2465   int             i, ret, mode;
2466   struct dirfile *dd;
2467   struct stat     buf;
2468   PIC            *pic = NULL;
2469 
2470   if(rec > MAXREC)
2471     return(0);
2472 
2473   /* check whether the dir already exist */
2474   ret = access(newname, F_OK);
2475   /* rename mode and dir does not exist */
2476   if (sw == 0 && ret && file[0] == '*' && file[1] == '\0')
2477   {
2478     if ((ret = rename(dirct, newname)))
2479       e_error(e_msg[ERR_RENFILE], 0, f->ed->fb);
2480     return(ret);
2481   }
2482 
2483   /* directory does not exist */
2484   if (ret != 0)
2485   {
2486     /* get the permissions */
2487     if (stat(dirct, &buf))
2488     {
2489       e_error(e_msg[ERR_ACCFILE], 0, f->ed->fb);
2490       return(1);
2491     }
2492     /* with that permission create the new dir */
2493     if(mkdir(newname, buf.st_mode))
2494     {
2495       e_error(e_msg[ERR_NONEWDIR], 0, f->ed->fb);
2496       return(1);
2497     }
2498   }
2499 
2500   if(f->ed->flopt & FM_REKURSIVE_ACTIONS)
2501   {
2502     if((tmp = MALLOC(strlen(dirct) + 2 + strlen(SUDIR))) == NULL)
2503       e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2504 
2505     sprintf(tmp, "%s%c%s", dirct, DIRC, SUDIR);
2506     dd = e_find_dir(tmp, f->ed->flopt & FM_SHOW_HIDDEN_DIRS ? 1 : 0);
2507     FREE(tmp);
2508 
2509     for(rec++, i = 0; i < dd->anz; i++)
2510     {
2511 
2512       if((tmp = MALLOC(strlen(dirct) + 2 + strlen(dd->name[i]))) == NULL)
2513         e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2514 
2515       if((ntmp = MALLOC(strlen(newname) + 2 + strlen(dd->name[i]))) == NULL)
2516         e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2517 
2518       sprintf(tmp, "%s%c%s", dirct, DIRC, dd->name[i]);
2519       sprintf(ntmp, "%s%c%s", newname, DIRC, dd->name[i]);
2520 
2521       if(WpeRenameCopyDir(tmp, file, ntmp, f, rec, sw))
2522       {
2523         FREE(tmp);
2524         FREE(ntmp);
2525         freedf(dd);
2526         return(1);
2527       }
2528       FREE(tmp);
2529       FREE(ntmp);
2530     }
2531     freedf(dd);
2532   }
2533 
2534   if((tmp = MALLOC(strlen(dirct) + 2 + strlen(file))) == NULL)
2535     e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2536   sprintf(tmp, "%s%c%s", dirct, DIRC, file);
2537   dd = e_find_files(tmp, f->ed->flopt & FM_SHOW_HIDDEN_FILES ? 1 : 0);
2538   FREE(tmp);
2539 
2540   mode = f->ed->flopt;
2541   f->ed->flopt &= ~FM_REMOVE_PROMPT;
2542 
2543   for(i = 0; i < dd->anz; i++)
2544   {
2545     if((ntmp = MALLOC(strlen(newname) + 2 + strlen(dd->name[i]))) == NULL)
2546       e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2547 
2548     sprintf(ntmp, "%s%c%s", newname, DIRC, dd->name[i]);
2549     ret = 'Y';
2550 
2551     if(access(ntmp, 0) == 0)
2552     {
2553       if(f->ed->flopt & FM_MOVE_PROMPT)
2554       {
2555         if((tmp = MALLOC(strlen(ntmp) + 31)) == NULL)
2556           e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2557 
2558         sprintf(tmp, "File %s exist !\nOverwrite File ?", ntmp);
2559         if(pic)
2560         {
2561           e_close_view(pic, 1);
2562           pic = NULL;
2563         }
2564         ret = e_message(1, tmp, f);
2565         FREE(tmp);
2566 
2567         if(ret == 'Y')
2568         {
2569           if(WpeRemove(ntmp, f))
2570           {
2571             FREE(ntmp);
2572             freedf(dd);
2573             return(1);
2574           }
2575         }
2576         else if(ret == WPE_ESC)
2577         {
2578           FREE(ntmp);
2579           freedf(dd);
2580           return(1);
2581         }
2582       }
2583       else if(f->ed->flopt & FM_MOVE_OVERWRITE)
2584       {
2585         if(WpeRemove(ntmp, f))
2586         {
2587           FREE(ntmp);
2588           freedf(dd);
2589           return(1);
2590         }
2591       }
2592     }
2593 
2594     if((tmp = MALLOC(strlen(dirct) + 2 + strlen(dd->name[i]))) == NULL)
2595       e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2596 
2597     sprintf(tmp, "%s%c%s", dirct, DIRC, dd->name[i]);
2598 
2599     if(ret == 'Y')
2600     {
2601       if((mtmp = MALLOC(strlen(tmp) + 2 + strlen(ntmp))) == NULL)
2602         e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2603 
2604       sprintf(mtmp, "%s %s", tmp, ntmp);
2605       if(e_mess_win(!sw ? "Rename" : "Copy", mtmp, &pic, f))
2606       {
2607         FREE(tmp);
2608         FREE(ntmp);
2609         FREE(mtmp);
2610         break;
2611       }
2612       FREE(mtmp);
2613 
2614       if (sw == 0)
2615       {
2616         if ((ret = rename(tmp, ntmp)))
2617         {
2618           e_error(e_msg[ERR_RENFILE], 0, f->ed->fb);
2619           FREE(tmp);
2620           FREE(ntmp);
2621           freedf(dd);
2622           return(1);
2623         }
2624       }
2625       else if (sw == 1)
2626       {
2627         if (WpeCopyFileCont(tmp, ntmp, f))
2628         {
2629           FREE(tmp);
2630           FREE(ntmp);
2631           freedf(dd);
2632           return(1);
2633         }
2634       }
2635       else if(sw == 2)
2636       {
2637         if(WpeLinkFile(tmp, ntmp, f->ed->flopt & FM_TRY_HARDLINK, f))
2638         {
2639           FREE(tmp);
2640           FREE(ntmp);
2641           freedf(dd);
2642           return(1);
2643         }
2644       }
2645     }
2646 
2647     FREE(tmp);
2648     FREE(ntmp);
2649   }
2650 
2651   if(pic)
2652     e_close_view(pic, 1);
2653 
2654   f->ed->flopt = mode;
2655 
2656   if(sw == 0)
2657   {
2658     if(rmdir(dirct))
2659       e_error(e_msg[ERR_DELFILE], 0, f->ed->fb);
2660   }
2661 
2662   freedf(dd);
2663   return(0);
2664 }
2665 
2666 
2667 int WpeRenameCopy(char *file, char *newname, FENSTER *f, int sw)
2668 {
2669  struct stat     buf;
2670  struct stat     lbuf;
2671  char           *tmp, *tmpl;
2672  int             ln = -1, ret = 'Y';
2673  int             allocate_size, retl = 0;
2674 
2675  WpeMouseChangeShape(WpeWorkingShape);
2676 
2677  /* in copy mode check whether it is a link */
2678  if (sw == 0)
2679  {
2680   if (lstat(file, &lbuf))
2681   {
2682    e_error(e_msg[ERR_ACCFILE], 0, f->ed->fb);
2683    return 1;
2684   }
2685 
2686   if (S_ISLNK(lbuf.st_mode))
2687    ln = 1;
2688  }
2689 
2690  if ((stat(file, &buf) == 0) && S_ISDIR(buf.st_mode) && ln < 0)
2691   retl = WpeRenameCopyDir(file, f->fd.file, newname, f, 0, sw);
2692  else
2693  {
2694   /* check whether file exist */
2695   if (access(newname, 0) == 0)
2696   {
2697    if (f->ed->flopt & FM_MOVE_OVERWRITE)
2698    {
2699     if (WpeRemove(newname, f))
2700     {
2701      WpeMouseRestoreShape();
2702      return(1);
2703     }
2704    }
2705    else if (f->ed->flopt & FM_MOVE_PROMPT)
2706    {
2707     if ((tmp = MALLOC(strlen(newname) + 26)) == NULL)
2708      e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2709     sprintf(tmp, "File %s exist\nRemove File ?", newname);
2710     ret = e_message(1, tmp, f);
2711     FREE(tmp);
2712     if (ret == 'Y')
2713     {
2714      if (WpeRemove(newname, f))
2715      {
2716       WpeMouseRestoreShape();
2717       return(1);
2718      }
2719     }
2720    }
2721   }
2722 
2723   if (ret == 'Y')
2724   {
2725    if (sw == 1)
2726    {
2727     retl = WpeCopyFileCont(file, newname, f);
2728    }
2729    else if (sw == 2)
2730    {
2731     retl = WpeLinkFile(file, newname, f->ed->flopt & FM_TRY_HARDLINK, f);
2732    }
2733    else if (sw == 0 && ln < 0) /* rename mode, no softlink */
2734    {
2735     if ((retl = rename(file, newname)) == -1)
2736     {
2737      if (errno == EXDEV)
2738      {
2739       if ((retl = WpeCopyFileCont(file, newname, f)) == 0)
2740       {
2741        if ((retl = remove(file)))
2742         e_error(e_msg[ERR_DELFILE], 0, f->ed->fb);
2743       }
2744      }
2745      else
2746      {
2747       e_error(e_msg[ERR_RENFILE], 0, f->ed->fb);
2748       retl = 1;
2749      }
2750     }
2751    }
2752    else if (sw == 0)
2753    {
2754     allocate_size = 2;
2755     tmp = NULL;
2756     do
2757     {
2758      if (tmp)
2759       FREE(tmp);
2760      allocate_size += 4;
2761      if ((tmp = MALLOC(allocate_size)) == NULL)
2762       e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2763 
2764       ln = readlink(file, tmp, allocate_size-1);
2765     } while (!(ln < allocate_size-1));
2766     tmp[ln] = '\0';
2767 
2768     for (; ln >= 0 && tmp[ln] != DIRC; ln--)
2769      ;
2770     if (ln < 0)
2771     {
2772      if ((tmpl = MALLOC(strlen(f->dirct) + 2 + strlen(tmp))) == NULL)
2773       e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
2774 
2775      sprintf(tmpl, "%s%c%s", f->dirct, DIRC, tmp);
2776      retl = WpeRenameLink(file, newname, tmpl, f);
2777      FREE(tmpl);
2778     }
2779     else
2780     {
2781      retl = WpeRenameLink(file, newname, tmp, f);
2782     }
2783    }
2784   }
2785  }
2786  WpeMouseRestoreShape();
2787  return(retl);
2788 }
2789 
2790 int WpeCopyFileCont(char *oldfile, char *newfile, FENSTER *f)
2791 {
2792   struct stat     buf;
2793   int             ret;
2794   char           *buffer;
2795   FILE           *fpo, *fpn;
2796 
2797   /* get the status of the file */
2798   if(stat(oldfile, &buf))
2799   {
2800     e_error(e_msg[ERR_ACCFILE], 0, f->ed->fb);
2801     return(1);
2802   }
2803 
2804   /* open files for copying */
2805   if((fpo = fopen(oldfile, "rb")) == NULL)
2806   {
2807     e_error(e_msg[ERR_OREADFILE], 0, f->ed->fb);
2808     return(1);
2809   }
2810   if((fpn = fopen(newfile, "wb")) == NULL)
2811   {
2812     e_error(e_msg[ERR_OWRITEFILE], 0, f->ed->fb);
2813     return(1);
2814   }
2815 
2816   /* allocate buffer for copying */
2817   if((buffer = malloc(E_C_BUFFERSIZE)) == NULL)
2818   {
2819     e_error(e_msg[ERR_ALLOC_CBUF], 0, f->ed->fb);
2820     return(1);
2821   }
2822 
2823   /* copy until end of file */
2824   do
2825   {
2826     ret = fread(buffer, 1, E_C_BUFFERSIZE, fpo);
2827     /* we should be able to write the same amount of info */
2828     if (fwrite(buffer, 1, ret, fpn) != ret)
2829     {
2830       fclose(fpo);
2831       fclose(fpn);
2832       free(buffer);
2833       e_error(e_msg[ERR_INCONSCOPY], 0, f->ed->fb);
2834       return(1);
2835     }
2836   } while(!feof(fpo));
2837 
2838   fclose(fpo);
2839   fclose(fpn);
2840   free(buffer);
2841 
2842   /* Well, we just created the file, so theoretically this
2843      should succed. Of course this is a racing condition !!! */
2844   if(chmod(newfile, buf.st_mode))
2845     e_error(e_msg[ERR_CHGPERM], 0, f->ed->fb);
2846   return(0);
2847 }
2848 
2849 #ifndef NOSYMLINKS
2850 /* Link a file according to the required mode
2851    sw != 0 -> symbolic link
2852    sw == 0 -> hard link
2853    When hard linking does not work, it tries to do
2854    symbolic linking
2855 */
2856 int WpeLinkFile(char *fl, char *ln, int sw, FENSTER *f)
2857 {
2858   int ret;
2859 
2860   if(sw || link(fl, ln))
2861   {
2862     ret = symlink(fl, ln);
2863     if (ret)
2864     {
2865       e_error(e_msg[ERR_LINKFILE], 0, f->ed->fb);
2866     }
2867     return(ret);
2868   }
2869   return(0);
2870 }
2871 
2872 int WpeRenameLink(char *old, char *ln, char *fl, FENSTER *f)
2873 {
2874   int ret;
2875 
2876   ret = symlink(fl, ln);
2877   if(ret)
2878   {
2879     e_error(e_msg[ERR_LINKFILE], 0, f->ed->fb);
2880     return(1);
2881   }
2882   else
2883   {
2884     if(remove(old))
2885     {
2886       e_error(e_msg[ERR_DELFILE], 0, f->ed->fb);
2887       return(1);
2888     }
2889     else
2890       return(0);
2891   }
2892 }
2893 #endif
2894 
2895 int e_rename(char *file, char *newname, FENSTER * f)
2896 {
2897   return(WpeRenameCopy(file, newname, f, 0));
2898 }
2899 
2900 int e_rename_dir(char *dirct, char *file, char *newname, FENSTER * f, int rec)
2901 {
2902   return(WpeRenameCopyDir(dirct, file, newname, f, rec, 0));
2903 }
2904 
2905 
2906 int e_link(char *file, char *newname, FENSTER *f)
2907 {
2908   return(WpeRenameCopy(file, newname, f, 2));
2909 }
2910 
2911 
2912 int e_copy(char *file, char *newname, FENSTER * f)
2913 {
2914   return(WpeRenameCopy(file, newname, f, 1));
2915 }
2916 
2917 
2918 int WpeFileManagerOptions(FENSTER * f)
2919 {
2920   int             ret;
2921   W_OPTSTR       *o = e_init_opt_kst(f);
2922 
2923   if(o == NULL)
2924     return(1);
2925 
2926   o->xa = 8;
2927   o->ya = 1;
2928   o->xe = 69;
2929   o->ye = 22;
2930   o->bgsw = AltO;
2931   o->name = "File-Manager-Options";
2932   o->crsw = AltO;
2933   e_add_txtstr(4, 2, "Directories:", o);
2934   e_add_txtstr(35, 13, "Wastebasket:", o);
2935   e_add_txtstr(4, 7, "Move/Copy:", o);
2936   e_add_txtstr(35, 8, "Remove:", o);
2937   e_add_txtstr(35, 2, "Sort Files by:", o);
2938   e_add_txtstr(4, 12, "Links on Files:", o);
2939   e_add_txtstr(4, 16, "On Open:", o);
2940 
2941   e_add_sswstr(5, 3, 12, AltF,
2942                f->ed->flopt & FM_SHOW_HIDDEN_FILES ? 1 : 0,
2943                "Show Hidden Files      ", o);
2944   e_add_sswstr(5, 4, 12, AltD,
2945                f->ed->flopt & FM_SHOW_HIDDEN_DIRS ? 1 : 0,
2946                "Show Hidden Directories", o);
2947   e_add_sswstr(5, 5, 2, AltK,
2948                f->ed->flopt & FM_REKURSIVE_ACTIONS ? 1 : 0,
2949                "ReKursive Actions      ", o);
2950   e_add_sswstr(5, 17, 0, AltC,
2951                f->ed->flopt & FM_CLOSE_WINDOW ? 1 : 0,
2952                "Close File Manager     ", o);
2953 
2954   e_add_sswstr(36, 6, 0, AltR,
2955                f->ed->flopt & FM_REVERSE_ORDER ? 1 : 0,
2956                "Reverse Order    ", o);
2957 
2958   e_add_pswstr(0, 36, 14, 0, AltP, 0, "Prompt for Delete", o);
2959   e_add_pswstr(0, 36, 15, 10, AltE, 0, "Delete at Exit   ", o);
2960   e_add_pswstr(0, 36, 16, 8, AltL, f->ed->flopt & FM_PROMPT_DELETE ? 0 :
2961                (f->ed->flopt & FM_DELETE_AT_EXIT ? 1 : 2), "Don't DeLete     ", o);
2962   e_add_pswstr(1, 36, 3, 0, AltN, 0, "Name             ", o);
2963   e_add_pswstr(1, 36, 4, 1, AltI, 0, "TIme             ", o);
2964   e_add_pswstr(1, 36, 5, 0, AltB, f->ed->flopt & FM_SORT_NAME ? 1 :
2965                (f->ed->flopt & FM_SORT_TIME ? 2 : 0), "Bytes            ", o);
2966   e_add_pswstr(2, 5, 8, 12, AltQ, 0, "Prompt for eQual Files ", o);
2967   e_add_pswstr(2, 5, 9, 1, AltV, 0, "OVerwrite equal Files  ", o);
2968   e_add_pswstr(2, 5, 10, 4, AltT, f->ed->flopt & FM_MOVE_PROMPT ? 0 :
2969                (f->ed->flopt & FM_MOVE_OVERWRITE ? 1 : 2), "Don'T overwrite        ", o);
2970   e_add_pswstr(3, 5, 13, 4, AltH, 0, "Try Hardlink           ", o);
2971   e_add_pswstr(3, 5, 14, 7, AltS,
2972                f->ed->flopt & FM_TRY_HARDLINK ? 1 : 0,
2973                "Always Symbolic Link   ", o);
2974   e_add_pswstr(4, 36, 9, 5, AltW, 0, "into Wastebasket ", o);
2975   e_add_pswstr(4, 36, 10, 0, AltA, 0, "Absolute (Prompt)", o);
2976   e_add_pswstr(4, 36, 11, 6, AltM, f->ed->flopt & FM_REMOVE_INTO_WB ? 0 :
2977                (f->ed->flopt & FM_REMOVE_PROMPT ? 1 : 2), "No ProMpt        ", o);
2978   e_add_bttstr(16, 19, 1, AltO, " Ok ", NULL, o);
2979   e_add_bttstr(38, 19, -1, WPE_ESC, "Cancel", NULL, o);
2980 
2981   ret = e_opt_kst(o);
2982   if(ret != WPE_ESC)
2983   {
2984     f->ed->flopt = o->sstr[0]->num +
2985       (o->sstr[1]->num << 1) +
2986       (o->sstr[2]->num ? FM_REKURSIVE_ACTIONS : 0) +
2987       (o->sstr[3]->num ? FM_CLOSE_WINDOW : 0) +
2988       (o->sstr[4]->num ? FM_REVERSE_ORDER : 0) +
2989       (o->pstr[0]->num ? (o->pstr[0]->num == 1 ? 4 : 0) : 8) +
2990       (o->pstr[2]->num ? (o->pstr[2]->num == 1 ? 16 : 0) : 32) +
2991       (o->pstr[4]->num ? (o->pstr[4]->num == 1 ? 128 : 0) : 64) +
2992       (o->pstr[1]->num ? (o->pstr[1]->num == 1 ? 01000 : 02000) : 0) +
2993       (o->pstr[3]->num ? FM_TRY_HARDLINK : 0);
2994   }
2995 
2996   freeostr(o);
2997   return(0);
2998 }
2999 
3000 int WpeDirDelOptions(FENSTER * f)
3001 {
3002   int             ret;
3003   W_OPTSTR       *o = e_init_opt_kst(f);
3004 
3005   if(o == NULL)
3006     return(1);
3007 
3008   o->xa = 19;
3009   o->ya = 11;
3010   o->xe = 53;
3011   o->ye = 19;
3012   o->bgsw = AltO;
3013   o->name = "Message";
3014   o->crsw = AltO;
3015   e_add_txtstr(4, 2, "Delete Directory:", o);
3016   e_add_pswstr(0, 5, 3, 0, AltD, 0, "Delete without Prompt", o);
3017   e_add_pswstr(0, 5, 4, 0, AltP, 1, "Prompt for Files     ", o);
3018   e_add_bttstr(7, 6, 1, AltO, " Ok ", NULL, o);
3019   e_add_bttstr(22, 6, -1, WPE_ESC, "Cancel", NULL, o);
3020 
3021   ret = e_opt_kst(o);
3022 
3023   ret = (ret == WPE_ESC) ? -1 : o->pstr[0]->num;
3024 
3025   freeostr(o);
3026   return(ret);
3027 }
3028 
3029 int WpeShell(FENSTER * f)
3030 {
3031   PIC            *outp = NULL;
3032   int             g[4];
3033 
3034   if (!WpeIsXwin())
3035   {
3036     outp = e_open_view(0, 0, MAXSCOL - 1, MAXSLNS - 1, f->fb->ws, 1);
3037     fk_locate(0, 0);
3038     fk_cursor(1);
3039 #if  MOUSE
3040     g[0] = 2;
3041     fk_mouse(g);
3042 #endif
3043     (*e_u_s_sys_ini) ();
3044   }
3045   (*e_u_system) (user_shell);
3046   if(!WpeIsXwin())
3047   {
3048     (*e_u_s_sys_end) ();
3049     e_close_view(outp, 1);
3050     fk_cursor(0);
3051 #if  MOUSE
3052     g[0] = 1;
3053     fk_mouse(g);
3054 #endif
3055   }
3056   return(0);
3057 }
3058 
3059 /*   print file */
3060 #ifndef NOPRINTER
3061 int WpePrintFile(FENSTER *f)
3062 {
3063  char           *str, *dp;
3064  int             c, sins = f->ins;
3065 
3066  for (c = f->ed->mxedt; c > 0 && !DTMD_ISTEXT(f->ed->f[c]->dtmd); c--)
3067   ;
3068  if (c <= 0)
3069    return(0);
3070  f = f->ed->f[c];
3071 
3072  if (strcmp(f->ed->print_cmd, "") == 0)
3073  {
3074   return(e_error(e_msg[ERR_NOPRINT], 0, f->fb));
3075  }
3076  if ((str = MALLOC(strlen(f->datnam) + 32 )) == NULL)
3077   e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
3078 
3079  sprintf(str, "File: %s\nDo you want to print it?", f->datnam);
3080  c = e_message(1, str, f);
3081  FREE(str);
3082  if (c != 'Y')
3083   return(0);
3084 
3085  dp = f->dirct;
3086  f->dirct = e_tmp_dir;
3087  f->ins = 0;
3088 
3089  e_save(f);
3090 
3091  f->dirct = dp;
3092  f->ins = sins;
3093 
3094  if ((str = MALLOC(strlen(e_tmp_dir) + 7 +
3095                    strlen(f->ed->print_cmd) + strlen(f->datnam) )) == NULL)
3096   e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
3097 
3098  sprintf(str, "cd %s; %s %s", e_tmp_dir, f->ed->print_cmd, f->datnam);
3099  if (system(str))
3100   e_error(e_msg[ERR_NOTINSTALL], 0, f->fb);
3101 
3102  sprintf(str, "%s/%s", e_tmp_dir, f->datnam);
3103  if (remove(str))
3104   e_error(e_msg[ERR_DELFILE], 0, f->ed->fb);
3105 
3106  FREE(str);
3107  return(0);
3108 }
3109 #else
3110 int WpePrintFile(FENSTER * f)
3111 {
3112   return(e_error(e_msg[ERR_NOPRINT], 0, f->fb));
3113 }
3114 #endif
3115 
3116 struct dirfile *WpeSearchFiles(FENSTER *f, char *dirct, char *file, char *string,
3117                                struct dirfile *df, int sw)
3118 {
3119   struct dirfile *dd;
3120   char          **tname, *tp, *tmp, *tmp2;
3121   int             i;
3122   static int      rec = 0;
3123 
3124   if(rec > MAXREC)
3125     return(df);
3126 
3127   /* if not absolute path is given */
3128   if(*dirct != DIRC)
3129   {
3130     tmp2 = WpeGetCurrentDir(f->ed);
3131 
3132     tmp = REALLOC(tmp2, strlen(tmp2) + strlen(dirct) + 4);
3133     if(tmp == NULL)
3134       e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
3135 
3136     tmp2 = tmp;
3137     if(tmp2[strlen(tmp2) - 1] != DIRC)
3138       sprintf(tmp2 + strlen(tmp2), "%c%s%c", DIRC, dirct, DIRC);
3139     else
3140       sprintf(tmp2 + strlen(tmp2), "%s%c", dirct, DIRC);
3141   }
3142   else
3143   {
3144     if((tmp2 = MALLOC(strlen(dirct) + 2)) == NULL)
3145       e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
3146 
3147     if(dirct[strlen(dirct) - 1] != DIRC)
3148       sprintf(tmp2, "%s%c", dirct, DIRC);
3149     else
3150       sprintf(tmp2, "%s", dirct);
3151   }
3152 
3153   /* assemble total path, dir + filename */
3154   if((tmp = MALLOC(strlen(tmp2) + strlen(file) + 2)) == NULL)
3155     e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
3156   sprintf(tmp, "%s%s", tmp2, file);
3157 
3158   /* initialise structure, only once */
3159   if(df == NULL)
3160   {
3161     if((df = MALLOC(sizeof(struct dirfile))) == NULL)
3162       e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
3163 
3164     df->anz = 0;
3165     if((df->name = MALLOC(sizeof(char *))) == NULL)
3166       e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
3167   }
3168 
3169   /* search all matching file */
3170   dd = e_find_files(tmp, 0);
3171 
3172   /* if we found something */
3173   if(dd && dd->anz > 0)
3174   {
3175 
3176     /* file find */
3177     if(!(sw & 1024))
3178     {
3179       for(i = 0; i < dd->anz; i++)
3180       {
3181         if((tp = MALLOC(strlen(tmp2) + strlen(dd->name[i]) + 2)) == NULL)
3182           e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
3183 
3184         sprintf(tp, "%s%s", tmp2, dd->name[i]);
3185 
3186         df->anz++;
3187 
3188         if((tname = REALLOC(df->name, df->anz * sizeof(char *))) == NULL)
3189           e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
3190 
3191         df->name = tname;
3192         df->name[df->anz - 1] = tp;
3193       }
3194     }
3195     /* file grep */
3196     else
3197     {
3198       for(i = 0; i < dd->anz; i++)
3199       {
3200         if((tp = MALLOC(strlen(tmp2) + strlen(dd->name[i]) + 2)) == NULL)
3201           e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
3202 
3203         sprintf(tp, "%s%s", tmp2, dd->name[i]);
3204 
3205         if(WpeGrepFile(tp, string, sw))
3206         {
3207           df->anz++;
3208           if((tname = REALLOC(df->name, df->anz * sizeof(char *))) == NULL)
3209             e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
3210 
3211           df->name = tname;
3212           df->name[df->anz - 1] = tp;
3213         }
3214         else
3215           FREE(tp);
3216       }
3217     }
3218   }
3219 
3220   freedf(dd);
3221   FREE(tmp2);
3222   FREE(tmp);
3223 
3224   /* whether recursive action */
3225   if(!(sw & 512))
3226     return(df);
3227 
3228   if((tmp = MALLOC(strlen(dirct) + strlen(SUDIR) + 2)) == NULL)
3229     e_error(e_msg[ERR_LOWMEM], 1, f->ed->fb);
3230 
3231   if(dirct[strlen(dirct) - 1] != DIRC)
3232     sprintf(tmp, "%s%c%s", dirct, DIRC, SUDIR);
3233   else
3234     sprintf(tmp, "%s%s", dirct, SUDIR);
3235 
3236   /* find directories */
3237   dd = e_find_dir(tmp, 0);
3238 
3239   FREE(tmp);
3240 
3241   if(!dd)
3242     return(df);
3243 
3244   rec++;
3245   for(i = 0; i < dd->anz; i++)
3246   {
3247     if((tmp = MALLOC(strlen(dirct) + strlen(dd->name[i]) + 3)) == NULL)
3248     {
3249       e_error(e_msg[ERR_LOWMEM], 0, f->ed->fb);
3250       rec--;
3251       freedf(dd);
3252       return(df);
3253     }
3254 
3255     if(dirct[strlen(dirct) - 1] != DIRC)
3256       sprintf(tmp, "%s%c%s", dirct, DIRC, dd->name[i]);
3257     else
3258       sprintf(tmp, "%s%s", dirct, dd->name[i]);
3259 
3260     df = WpeSearchFiles(f, tmp, file, string, df, sw);
3261     FREE(tmp);
3262   }
3263 
3264   freedf(dd);
3265   rec--;
3266   return(df);
3267 
3268 }
3269 
3270 
3271 int WpeGrepWindow(FENSTER * f)
3272 {
3273   FIND           *fd = &(f->ed->fd);
3274   int             ret;
3275   char            strTemp[80];
3276   W_OPTSTR       *o = e_init_opt_kst(f);
3277 
3278   if(!o)
3279     return(-1);
3280   if(e_blck_dup(strTemp, f))
3281   {
3282     strcpy(fd->search, strTemp);
3283     fd->sn = strlen(fd->search);
3284   }
3285   o->xa = 7;
3286   o->ya = 3;
3287   o->xe = 63;
3288   o->ye = 19;
3289   o->bgsw = 0;
3290   o->name = "Grep";
3291   o->crsw = AltO;
3292   e_add_txtstr(4, 4, "Options:", o);
3293   e_add_wrstr(4, 2, 18, 2, 35, 128, 0, AltT, "Text to Find:", fd->search, &f->ed->sdf, o);
3294   e_add_wrstr(4, 10, 17, 10, 36, 128, 0, AltF, "File:", fd->file, &f->ed->fdf, o);
3295   e_add_wrstr(4, 12, 17, 12, 36, WPE_PATHMAX, 0, AltD, "Directory:", fd->dirct, &f->ed->ddf, o);
3296   e_add_sswstr(5, 5, 0, AltC, fd->sw & 128 ? 1 : 0, "Case sensitive    ", o);
3297   e_add_sswstr(5, 6, 0, AltW, fd->sw & 64 ? 1 : 0, "Whole words only  ", o);
3298   e_add_sswstr(5, 7, 0, AltR, fd->sw & 32 ? 1 : 0, "Regular expression", o);
3299   e_add_sswstr(5, 8, 0, AltS, 0, "Search Recursive  ", o);
3300   e_add_bttstr(16, 14, 1, AltO, " Ok ", NULL, o);
3301   e_add_bttstr(34, 14, -1, WPE_ESC, "Cancel", NULL, o);
3302   ret = e_opt_kst(o);
3303   if(ret != WPE_ESC)
3304   {
3305     fd->sw = 1024 + (o->sstr[0]->num << 7) + (o->sstr[1]->num << 6) +
3306       (o->sstr[2]->num << 5) + (o->sstr[3]->num << 9);
3307     strcpy(fd->search, o->wstr[0]->txt);
3308     fd->sn = strlen(fd->search);
3309     strcpy(fd->file, o->wstr[1]->txt);
3310     if (fd->dirct)
3311     {
3312      WpeFree(fd->dirct);
3313     }
3314     fd->dirct = WpeStrdup(o->wstr[2]->txt);
3315   }
3316   freeostr(o);
3317   if(ret != WPE_ESC)
3318     ret = e_data_first(2, f->ed, fd->dirct);
3319   return(ret);
3320 }
3321 
3322 
3323 int WpeFindWindow(FENSTER * f)
3324 {
3325   FIND           *fd = &(f->ed->fd);
3326   int             ret;
3327   W_OPTSTR       *o = e_init_opt_kst(f);
3328 
3329   if(!o)
3330     return(-1);
3331   o->xa = 7;
3332   o->ya = 3;
3333   o->xe = 61;
3334   o->ye = 14;
3335   o->bgsw = 0;
3336   o->name = "Find File";
3337   o->crsw = AltO;
3338   e_add_txtstr(4, 6, "Options:", o);
3339   e_add_wrstr(4, 2, 15, 2, 36, 128, 0, AltF, "File:", fd->file, &f->ed->fdf, o);
3340   e_add_wrstr(4, 4, 15, 4, 36, WPE_PATHMAX, 0, AltD, "Directory:", fd->dirct, &f->ed->ddf, o);
3341   e_add_sswstr(5, 7, 0, AltS, 1, "Search Recursive  ", o);
3342   e_add_bttstr(13, 9, 1, AltO, " Ok ", NULL, o);
3343   e_add_bttstr(33, 9, -1, WPE_ESC, "Cancel", NULL, o);
3344   ret = e_opt_kst(o);
3345   if(ret != WPE_ESC)
3346   {
3347     fd->sw = (o->sstr[0]->num << 9);
3348     strcpy(fd->file, o->wstr[0]->txt);
3349     if (fd->dirct)
3350     {
3351      WpeFree(fd->dirct);
3352     }
3353     fd->dirct = WpeStrdup(o->wstr[1]->txt);
3354   }
3355   freeostr(o);
3356   if(ret != WPE_ESC)
3357     ret = e_data_first(3, f->ed, fd->dirct);
3358   return(ret);
3359 }
3360 
3361 
3362 
3363 
3364 
3365 
3366 
3367 
3368 
3369 
3370 
3371 
3372 
3373 
3374 
3375 
3376 
3377 
3378 
3379 
3380 
3381 
3382 int e_ed_man(char *str, FENSTER * f)
3383 {
3384   char            command[256], tstr[_POSIX_PATH_MAX];
3385   char            cc, hstr[80], nstr[10];
3386   int             mdsv = f->ed->dtmd, bg, i, j = 0;
3387   BUFFER         *b;
3388 
3389   if(!str)
3390     return(0);
3391   while(isspace(*str++));
3392   if(!*--str)
3393     return(0);
3394   for(i = f->ed->mxedt; i >= 0; i--)
3395   {
3396     if(!strcmp(f->ed->f[i]->datnam, str))
3397     {
3398       e_switch_window(f->ed->edt[i], f);
3399       return(0);
3400     }
3401   }
3402   WpeMouseChangeShape(WpeWorkingShape);
3403   sprintf(tstr, "%s/%s", e_tmp_dir, str);
3404   for(i = 0; (hstr[i] = str[i]) && str[i] != '(' && str[i] != ')'; i++);
3405   hstr[i] = '\0';
3406   if(str[i] == '(')
3407   {
3408     for(++i; (nstr[j] = str[i]) && str[i] != ')' && str[i] != '('; i++, j++);
3409 
3410     /* Some SEE ALSO's are list as "foobar(3X)" but are in section 3 not 3X.
3411        This is a quick hack to fix the problem.  -- Dennis */
3412     if(isdigit(nstr[0]))
3413       j = 1;
3414   }
3415   nstr[j] = '\0';
3416 
3417   while(1)
3418   {
3419 #ifdef MAN_S_OPT
3420     if(!nstr[0])
3421       sprintf(command, " man %s > \'%s\' 2> /dev/null", hstr, tstr);
3422     else
3423       sprintf(command, " man -s %s %s > \'%s\' 2> /dev/null", nstr, hstr, tstr);
3424 #else
3425     sprintf(command, " man %s %s > \'%s\' 2> /dev/null", nstr, hstr, tstr);
3426 #endif
3427     system(command);
3428     chmod(tstr, 0400);
3429     f->ed->dtmd = DTMD_HELP;
3430     e_edit(f->ed, tstr);
3431     f->ed->dtmd = mdsv;
3432     f = f->ed->f[f->ed->mxedt];
3433     b = f->b;
3434     if(b->mxlines > 1 || !nstr[1])
3435       break;
3436     nstr[1] = '\0';
3437     chmod(tstr, 0600);
3438     remove(tstr);
3439     e_close_window(f);
3440   }
3441   if(b->mxlines == 1 && b->bf[0].len == 0)
3442   {
3443     e_ins_nchar(f->b, f->s, "No manual entry for ", 0, 0, 20);
3444     e_ins_nchar(f->b, f->s, hstr, b->b.x, b->b.y, strlen(hstr));
3445     e_ins_nchar(f->b, f->s, ".", b->b.x, b->b.y, 1);
3446   }
3447   for(i = 0; i < b->mxlines; i++)
3448     if(b->bf[i].len == 0 && (i == 0 || b->bf[i - 1].len == 0))
3449     {
3450       e_del_line(i, b, f->s);
3451       i--;
3452     }
3453   for(bg = 0; bg < b->bf[0].len && isspace(b->bf[0].s[bg]); bg++);
3454   if(bg == b->bf[0].len)
3455     bg = 0;
3456   for(i = 0;
3457       i < b->mxlines &&
3458       WpeStrnccmp(b->bf[i].s + bg, "\017SEE\005 \017ALSO\005", 12) &&
3459       WpeStrnccmp(b->bf[i].s + bg, "SEE ALSO", 8);
3460       i++);
3461   if(i < b->mxlines)
3462     for(bg = 0, i++; i < b->mxlines && b->bf[i].len > 0 && bg >= 0; i++)
3463     {
3464       bg = 0;
3465       while(b->bf[i].s[bg])
3466       {
3467         for(; isspace(b->bf[i].s[bg]); bg++);
3468         if(!b->bf[i].s[bg])
3469           continue;
3470         for(j = bg + 1;
3471             b->bf[i].s[j] && b->bf[i].s[j] != ',' && b->bf[i].s[j] != '.' &&
3472             b->bf[i].s[j] != ' ' && b->bf[i].s[j] != '(';
3473             j++);
3474         if(b->bf[i].s[j] != '(')
3475         {
3476           bg = -1;
3477           break;
3478         }
3479         if(b->bf[i].s[j - 1] == 5)
3480           e_del_nchar(b, f->s, j - 1, i, 1);
3481         for(j++; b->bf[i].s[j] && b->bf[i].s[j] != ',' && b->bf[i].s[j] != '.';
3482             j++);
3483         if(b->bf[i].s[bg] == 15)
3484           b->bf[i].s[bg] = HFB;
3485         else
3486         {
3487           cc = HFB;
3488           e_ins_nchar(b, f->s, &cc, bg, i, 1);
3489           j++;
3490         }
3491         cc = HED;
3492         e_ins_nchar(b, f->s, &cc, j, i, 1);
3493         j++;
3494         if(b->bf[i].s[j])
3495           j++;
3496         bg = j;
3497       }
3498     }
3499   b->b.x = b->b.y = 0;
3500   chmod(tstr, 0600);
3501   remove(tstr);
3502   WpeMouseRestoreShape();
3503   e_schirm(f, 1);
3504   return(0);
3505 }
3506 
3507 int e_funct(FENSTER * f)
3508 {
3509  char            str[80];
3510 
3511  if (f->ed->hdf && f->ed->hdf->anz > 0)
3512   strcpy(str, f->ed->hdf->name[0]);
3513  else
3514   str[0] = '\0';
3515  if (e_add_arguments(str, "Function", f, 0, AltF, &f->ed->hdf))
3516  {
3517   f->ed->hdf = e_add_df(str, f->ed->hdf);
3518   e_ed_man(str, f);
3519  }
3520  return(0);
3521 }
3522 
3523 struct dirfile *e_make_funct(char *man)
3524 {
3525  struct dirfile *df = NULL, *dout = MALLOC(sizeof(struct dirfile));
3526  char *sustr, *subpath, *manpath;
3527  int ret = 0, n, i = 0, j, k, l = 0;
3528 
3529  manpath = NULL;
3530  WpeMouseChangeShape(WpeWorkingShape);
3531  dout->anz = 0;
3532  dout->name = NULL;
3533  if (getenv("MANPATH"))
3534  {
3535   manpath = strdup(getenv("MANPATH"));
3536  }
3537  if ((!manpath) || (manpath[0] == '\0'))
3538  {
3539   manpath = strdup(
3540 #if (defined(BSD) && (BSD >= 199306)) || (defined(sun) && defined(__svr4__))
3541 		"/usr/share/man:/usr/local/man:/usr/local/man");
3542 #else
3543 		"/usr/man:/usr/local/man:/usr/local/man");
3544 #endif
3545  }
3546  /* Allocate the maximum possible rather than continually realloc. */
3547  sustr = malloc(strlen(manpath) + 10);
3548  while (manpath[i])
3549  {
3550   subpath = manpath + i;
3551   for (n = 0; (manpath[i]) && (manpath[i] != PTHD); i++, n++);
3552   if (manpath[i] == PTHD)
3553   {
3554    manpath[i] = '\0';
3555    i++;
3556   }
3557   sprintf(sustr, "%s/man%s/*", subpath, man);
3558   df = e_find_files(sustr, 0);
3559   if (!df->anz)
3560   {
3561    freedf(df);
3562    continue;
3563   }
3564   for (j = 0; j < df->anz; j++)
3565   {
3566    k = strlen(df->name[j]) - 1;
3567 
3568    /* If the file is gzipped strip the .gz ending. */
3569    if ((k > 2) && (strcmp(df->name[j] + k - 2, ".gz") == 0))
3570    {
3571     k -= 3;
3572     *(df->name[j] + k + 1) = '\0';
3573    }
3574    else if ((k > 3) && (strcmp(df->name[j] + k - 3, ".bz2") == 0))
3575    {
3576     k -= 4;
3577     *(df->name[j] + k + 1) = '\0';
3578    }
3579    else if ((k > 1) && (strcmp(df->name[j] + k - 1, ".Z") == 0))
3580    {
3581     k -= 2;
3582     *(df->name[j] + k + 1) = '\0';
3583    }
3584 
3585    for (k = strlen(df->name[j]) - 1; k >= 0 && *(df->name[j] + k) != '.'; k--)
3586     ;
3587    if (k >= 0 /**(df->name[j]+k)*/ )
3588    {
3589     df->name[j] = REALLOC(df->name[j],
3590       (l = strlen(df->name[j]) + 2) * sizeof(char));
3591     *(df->name[j] + k) = '(';
3592     *(df->name[j] + l - 2) = ')';
3593     *(df->name[j] + l - 1) = '\0';
3594    }
3595   }
3596   if (!dout->name)
3597    dout->name = MALLOC(df->anz * sizeof(char *));
3598   else
3599    dout->name = REALLOC(dout->name, (df->anz + dout->anz) * sizeof(char *));
3600   for (j = 0; j < df->anz; j++)
3601   {
3602    for (k = 0; k < dout->anz; k++)
3603    {
3604     if (!(ret = strcmp(df->name[j], dout->name[k])))
3605     {
3606      FREE(df->name[j]);
3607      break;
3608     }
3609     else if (ret < 0)
3610      break;
3611    }
3612    if (!ret && dout->anz)
3613     continue;
3614    for (l = dout->anz; l > k; l--)
3615     dout->name[l] = dout->name[l - 1];
3616    dout->name[k] = df->name[j];
3617    dout->anz++;
3618   }
3619   FREE(df);
3620  }
3621  free(manpath);
3622  free(sustr);
3623  WpeMouseRestoreShape();
3624  return(dout);
3625 }
3626 
3627 
3628 #ifdef PROG
3629 extern struct dirfile **e_p_df;
3630 #endif
3631 
3632 int e_data_first(int sw, ECNT *cn, char *nstr)
3633 {
3634   extern char    *e_hlp_str[];
3635   extern WOPT    *gblst, *oblst;
3636   FENSTER        *f;
3637   int             i, j;
3638   struct dirfile *df = NULL;
3639   FLWND          *fw;
3640 
3641   if(cn->mxedt >= MAXEDT)
3642   {
3643     e_error(e_msg[ERR_MAXWINS], 0, cn->fb);
3644     return(-1);
3645   }
3646   for(j = 1; j <= MAXEDT; j++)
3647   {
3648     for(i = 1; i <= cn->mxedt && cn->edt[i] != j; i++);
3649     if(i > cn->mxedt)
3650       break;
3651   }
3652   cn->curedt = j;
3653   (cn->mxedt)++;
3654   cn->edt[cn->mxedt] = j;
3655 
3656   if((f = (FENSTER *) MALLOC(sizeof(FENSTER))) == NULL)
3657     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
3658   if((fw = (FLWND *) MALLOC(sizeof(FLWND))) == NULL)
3659     e_error(e_msg[ERR_LOWMEM], 1, cn->fb);
3660   f->fb = cn->fb;
3661   cn->f[cn->mxedt] = f;
3662   f->a = e_set_pnt(22, 3);
3663   f->e = e_set_pnt(f->a.x + 35, f->a.y + 18);
3664   f->winnum = cn->curedt;
3665   f->dtmd = DTMD_DATA;
3666   f->ins = sw;
3667   f->save = 0;
3668   f->zoom = 0;
3669   f->ed = cn;
3670   f->c_sw = NULL;
3671   f->c_st = NULL;
3672   f->pic = NULL;
3673   f->fd.dirct = NULL;
3674 
3675   if(!nstr)
3676     f->dirct = NULL; /* how about a free up ??? */
3677   else
3678   {
3679     f->dirct = MALLOC(strlen(nstr) + 1);
3680     strcpy(f->dirct, nstr);
3681   }
3682   WpeMouseChangeShape(WpeWorkingShape);
3683   if(sw == 1)
3684   {
3685     f->datnam = "Function-Index";
3686     df = e_make_funct(nstr);
3687   }
3688   else if(sw == 2)
3689   {
3690     f->datnam = "Grep";
3691     df = WpeSearchFiles(f, nstr, cn->fd.file,
3692                         cn->fd.search, NULL,
3693                         cn->fd.sw);
3694   }
3695   else if(sw == 3)
3696   {
3697     f->datnam = "Find";
3698     df = WpeSearchFiles(f, nstr, cn->fd.file,
3699                         cn->fd.search, NULL,
3700                         cn->fd.sw);
3701   }
3702   else if(sw == 7)
3703   {
3704     f->datnam = "Windows";
3705     df = e_make_win_list(f);
3706   }
3707 #ifdef PROG
3708   else if(sw == 4)
3709   {
3710     f->datnam = "Project";
3711     df = e_p_df[0];
3712   }
3713   else if(sw == 5)
3714   {
3715     f->datnam = "Variables";
3716     df = e_p_df[1];
3717   }
3718   else if(sw == 6)
3719   {
3720     f->datnam = "Install";
3721     df = e_p_df[2];
3722   }
3723 #endif
3724   f->hlp_str = e_hlp_str[16 + sw];
3725   if(sw < 4)
3726   {
3727     f->blst = gblst;
3728     f->nblst = 4;
3729   }
3730   else
3731   {
3732     f->blst = oblst;
3733     f->nblst = 4;
3734   }
3735   WpeMouseRestoreShape();
3736   f->b = (BUFFER *) fw;
3737   fw->df = df;
3738 
3739   fw->mxa = f->a.x;
3740   fw->mxe = f->e.x;
3741   fw->mya = f->a.y;
3742   fw->mye = f->e.y;
3743   fw->xa = f->a.x + 3;
3744   fw->xe = f->e.x - 13;
3745   fw->ya = f->a.y + 3;
3746   fw->ye = f->e.y - 1;
3747   fw->f = f;
3748   fw->ia = fw->nf = fw->nxfo = fw->nyfo = 0;
3749   fw->srcha = fw->ja = 0;
3750 
3751   if(cn->mxedt > 1 && (f->ins < 5 || f->ins == 7))
3752     e_ed_rahmen(cn->f[cn->mxedt - 1], 0);
3753   e_firstl(f, 1);
3754   e_data_schirm(f);
3755   return(0);
3756 }
3757 
3758 int e_data_schirm(FENSTER * f)
3759 {
3760   int             i, j;
3761   FLWND          *fw = (FLWND *) f->b;
3762 
3763   for(j = f->a.y + 1; j < f->e.y; j++)
3764     for(i = f->a.x + 1; i < f->e.x; i++)
3765       e_pr_char(i, j, ' ', f->fb->nt.fb);
3766 
3767   if(NUM_COLS_ON_SCREEN > 25)
3768   {
3769     if(f->ins < 4 || f->ins == 7)
3770       e_pr_str((f->e.x - 9), f->e.y - 4, "Show", f->fb->nz.fb, 0, -1,
3771                f->fb->ns.fb, f->fb->nt.fb);
3772     else if(f->ins > 3)
3773     {
3774       e_pr_str((f->e.x - 9), f->e.y - 8, "Add", f->fb->nz.fb, 0, -1,
3775                f->fb->ns.fb, f->fb->nt.fb);
3776       e_pr_str((f->e.x - 9), f->e.y - 6, "Edit", f->fb->nz.fb, 0, -1,
3777                f->fb->ns.fb, f->fb->nt.fb);
3778       e_pr_str((f->e.x - 9), f->e.y - 4, "Delete", f->fb->nz.fb, 0, -1,
3779                f->fb->ns.fb, f->fb->nt.fb);
3780       if(f->ins == 4 && f->a.y < f->e.y - 10)
3781       {
3782         e_pr_str((f->e.x - 9), f->e.y - 10, "Options", f->fb->nz.fb,
3783                  0, -1, f->fb->ns.fb, f->fb->nt.fb);
3784       }
3785     }
3786     e_pr_str((f->e.x - 9), f->e.y - 2, "Cancel", f->fb->nz.fb, -1, -1,
3787              f->fb->ns.fb, f->fb->nt.fb);
3788   }
3789 
3790   if(NUM_COLS_ON_SCREEN > 25)
3791   {
3792     fw->xa = f->a.x + 3;
3793     fw->xe = f->e.x - 13;
3794   }
3795   else
3796   {
3797     fw->xa = f->a.x + 3;
3798     fw->xe = f->e.x - 2;
3799   }
3800   fw->mxa = f->a.x;
3801   fw->mxe = f->e.x;
3802   fw->mya = f->a.y;
3803   fw->mye = f->e.y;
3804   fw->xa = f->a.x + 3;
3805   fw->ya = f->a.y + 3;
3806   fw->ye = f->e.y - 1;
3807 #ifdef PROG
3808   if(f->ins == 4)
3809     fw->df = e_p_df[0];
3810 #endif
3811   if(f->ins == 1)
3812     e_pr_str(fw->xa, f->a.y + 2, "Functions:", f->fb->nt.fb, 0, 1,
3813              f->fb->nsnt.fb, f->fb->nt.fb);
3814   else if(f->ins == 3)
3815     e_pr_str(fw->xa, f->a.y + 2, "Directories:", f->fb->nt.fb, 0, 1,
3816              f->fb->nsnt.fb, f->fb->nt.fb);
3817   e_mouse_bar(fw->xe, fw->ya, fw->ye - fw->ya, 0, fw->f->fb->em.fb);
3818   e_mouse_bar(fw->xa, fw->ye, fw->xe - fw->xa, 1, fw->f->fb->em.fb);
3819   e_pr_file_window(fw, 0, 1, f->fb->ft.fb, f->fb->fz.fb, f->fb->frft.fb);
3820   return(0);
3821 }
3822 
3823 int e_data_eingabe(ECNT * cn)
3824 {
3825   FENSTER        *f = cn->f[cn->mxedt];
3826   FLWND          *fw = (FLWND *) f->b;
3827   int             c = AltF;
3828 
3829   fk_cursor(0);
3830   if(f->ins == 7)
3831   {
3832     freedf(fw->df);
3833     fw->df = e_make_win_list(f);
3834   }
3835   while(c != WPE_ESC)
3836   {
3837     if(f->dtmd != DTMD_DATA)
3838       return(0);
3839 #ifdef PROG
3840     if(f->ins == 4)
3841       fw->df = e_p_df[0];
3842 #endif
3843     if(c == AltF)
3844       c = e_file_window(0, fw, f->fb->ft.fb, f->fb->fz.fb);
3845 #if  MOUSE
3846     if(c == MBKEY)
3847       c = e_data_ein_mouse(f);
3848 #endif
3849     if(((c == WPE_CR || c == AltS) && (f->ins < 4 || f->ins == 7)) ||
3850        ((c == AltA || c == EINFG) && (f->ins > 3 && f->ins < 7)))
3851     {
3852       if(f->ins == 1)
3853         e_ed_man(fw->df->name[fw->nf], f);
3854       else if(f->ins == 2)
3855       {
3856         e_edit(f->ed, fw->df->name[fw->nf]);
3857         e_rep_search(f->ed->f[f->ed->mxedt]);
3858       }
3859       else if(f->ins == 3)
3860       {
3861         e_edit(f->ed, fw->df->name[fw->nf]);
3862 
3863 /*        WpeCreateFileManager(0, f->ed, fw->df->name[fw->nf]); */
3864       }
3865       else if(f->ins == 7)
3866         e_switch_window(f->ed->edt[fw->df->anz - fw->nf], f);
3867 #ifdef PROG
3868       else if(f->ins == 4)
3869       {
3870         WpeCreateFileManager(5, f->ed, NULL);
3871         while(WpeHandleFileManager(f->ed) != WPE_ESC);
3872         e_p_df[f->ins - 4] = fw->df;
3873         c = AltF;
3874         f->save = 1;
3875       }
3876       else if(f->ins > 4 && f->ins < 7)
3877       {
3878         e_p_add_df(fw, f->ins);
3879         e_p_df[f->ins - 4] = fw->df;
3880         c = AltF;
3881       }
3882 #endif
3883       if(f->ins < 4 || f->ins == 7)
3884         return(0);
3885     }
3886 #ifdef PROG
3887     else if(f->ins > 3 && f->ins < 7 && (c == AltD || c == ENTF))
3888     {
3889       e_p_del_df(fw, f->ins);
3890       c = AltF;
3891       f->save = 1;
3892     }
3893     else if(f->ins > 4 && f->ins < 7 && (c == AltE || c == WPE_CR))
3894     {
3895       e_p_edit_df(fw, f->ins);
3896       c = AltF;
3897       f->save = 1;
3898     }
3899     else if(f->ins == 4 && (c == AltE || c == WPE_CR))
3900     {
3901       e_edit(f->ed, fw->df->name[fw->nf]);
3902       c = WPE_ESC;
3903     }
3904     else if(f->ins == 4 && c == AltO)
3905     {
3906       e_project_options(f);
3907       c = AltF;
3908     }
3909 #endif
3910     else if(c != AltF)
3911     {
3912       if(c == AltBl)
3913         c = WPE_ESC;
3914       else if(c == WPE_ESC)
3915         c = (f->ed->edopt & ED_CUA_STYLE) ? CF4 : AF3;
3916       if(f->ins == 7 && ((!(f->ed->edopt & ED_CUA_STYLE) && c == AF3)
3917                          || ((f->ed->edopt & ED_CUA_STYLE) && c == CF4)))
3918         e_close_window(f);
3919       if(f->ins == 4 && ((!(f->ed->edopt & ED_CUA_STYLE) && c == AF3)
3920                          || ((f->ed->edopt & ED_CUA_STYLE) && c == CF4)))
3921       {
3922         FLWND          *fw = (FLWND *) f->ed->f[f->ed->mxedt]->b;
3923         fw->df = NULL;
3924         e_close_window(f->ed->f[f->ed->mxedt]);
3925         return(0);
3926       }
3927       if(f->ins == 4 && (!e_tst_dfkt(f, c) || !e_prog_switch(f, c)))
3928         return(0);
3929       if(f->ins > 4 && ((!(f->ed->edopt & ED_CUA_STYLE) && c == AF3)
3930                         || ((f->ed->edopt & ED_CUA_STYLE) && c == CF4)))
3931         return(c);
3932       else if((f->ins < 4 || f->ins == 7) && !e_tst_dfkt(f, c))
3933         return(0);
3934       else
3935         c = AltF;
3936     }
3937   }
3938   return((f->ed->edopt & ED_CUA_STYLE) ? CF4 : AF3);
3939 }
3940 
3941 int e_get_funct_in(char *nstr, FENSTER * f)
3942 {
3943   return(e_data_first(1, f->ed, nstr));
3944 }
3945 
3946 int e_funct_in(FENSTER * f)
3947 {
3948   int             n, xa = 37, ya = 2, num = 8;
3949   OPTK           *opt = MALLOC(num * sizeof(OPTK));
3950   char            nstr[2];
3951 
3952   opt[0].t = "User Commands";
3953   opt[0].x = 0;
3954   opt[0].o = 'U';
3955   opt[1].t = "System Calls";
3956   opt[1].x = 0;
3957   opt[1].o = 'S';
3958   opt[2].t = "C-Lib.-Functions";
3959   opt[2].x = 0;
3960   opt[2].o = 'C';
3961   opt[3].t = "Devices & Netw. I.";
3962   opt[3].x = 0;
3963   opt[3].o = 'D';
3964   opt[4].t = "File Formats";
3965   opt[4].x = 0;
3966   opt[4].o = 'F';
3967   opt[5].t = "Games & Demos";
3968   opt[5].x = 0;
3969   opt[5].o = 'G';
3970   opt[6].t = "Environment, ...";
3971   opt[6].x = 0;
3972   opt[6].o = 'E';
3973   opt[7].t = "Maintenance Com.";
3974   opt[7].x = 0;
3975   opt[7].o = 'M';
3976 
3977   n = e_opt_sec_box(xa, ya, num, opt, f, 1);
3978 
3979   FREE(opt);
3980   if(n < 0)
3981     return(WPE_ESC);
3982 
3983   nstr[0] = '1' + n;
3984   nstr[1] = '\0';
3985   return(e_get_funct_in(nstr, f));
3986 }
3987 
3988 #endif
3989