1 /* File read.c */
2 /***************************************************************************
3 *  Copyright 2003 -   Steven Shipway <steve@cheshire.demon.co.uk>          *
4 *                     Put "nospam" in subject to avoid spam filter         *
5 *                                                                          *
6 *  This program is free software; you can redistribute it and/or modify    *
7 *  it under the terms of the GNU General Public License as published by    *
8 *  the Free Software Foundation; either version 2 of the License, or       *
9 *  (at your option) any later version.                                     *
10 *                                                                          *
11 *  This program is distributed in the hope that it will be useful,         *
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
14 *  GNU General Public License for more details.                            *
15 *                                                                          *
16 *  You should have received a copy of the GNU General Public License       *
17 *  along with this program; if not, write to the Free Software             *
18 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA               *
19 *  02111-1307, USA.                                                        *
20 ***************************************************************************/
21 
22 
23 #include "wand_head.h"
24 
25 extern int inform_me();
26 
27 extern int edit_mode;
28 extern char *edit_screen;
29 extern char screen[NOOFROWS][ROWLEN+1];
30 
31 extern char screen_name[61];
32 
33 char buffer[80];
34 
35 /****************************************************************************
36 *                                   rscreen                                 *
37 *****************************************************************************/
rscreen(num,maxmoves)38 int rscreen(num,maxmoves)
39 int *maxmoves, num;
40 {
41     int  y,numr;
42     FILE *fp;
43     char name[100];
44     char (*row_ptr)[ROWLEN+1] = screen;
45     if(!edit_mode)
46         sprintf(name,"%s/screen.%d",SCREENPATH,num);
47     else
48     {
49         if(!edit_screen)
50             sprintf(name,"./screen");
51         else
52             sprintf(name,"%s",edit_screen);
53     }
54 
55     if((fp = fopen(name,"r"))== NULL)
56     {
57         if(edit_mode)
58             sprintf(buffer,"Cannot find file %s.",name);
59         else
60             sprintf(buffer,"File for screen %d unavailable.",num);
61         inform_me(buffer,0);
62     }
63     else
64     {
65         for(y = 0;y<NOOFROWS;y++)
66         {
67             fgets(*row_ptr,ROWLEN + 2,fp);
68             numr = strlen( *row_ptr ) - 1;
69             while(numr < ROWLEN) (*row_ptr)[numr++] = ' ';
70             row_ptr++;
71         };
72         fgets(screen_name,60,fp);
73         screen_name[61] = '\0';
74         screen_name[strlen(screen_name)-1] = '\0';
75         if(fscanf(fp,"%d",maxmoves) != 1)
76             *maxmoves=0;
77         fclose(fp);
78     };
79     return (fp == NULL);
80 }
81 
82 /*********************************************************************
83 *                              wscreen                               *
84 **********************************************************************/
wscreen(num,maxmoves)85 int wscreen(num,maxmoves)
86     int maxmoves, num;
87 {
88     int  y,x;
89     FILE *fp;
90     char name[100];
91     char (*row_ptr)[ROWLEN+1] = screen;
92 
93     if(!edit_screen)
94         sprintf(name,"./screen");
95     else
96         sprintf(name,"%s",edit_screen);
97 
98     if((fp = fopen(name,"w")) == NULL)
99     {
100         sprintf(name,"/tmp/screen.%d",getpid());
101         if((fp = fopen(name,"w")) != NULL)
102         {
103             sprintf(buffer,"Written file is %s",name);
104             inform_me(buffer,0);
105         }
106         else
107             err(1,"Could not open %s.\n",buffer);
108     }
109     if(fp == NULL)
110     {
111         inform_me("File for screen cannot be written.",0) ;
112     }
113     else
114     {
115         for(y = 0;y<NOOFROWS;y++)
116         {
117             for(x = 0;x<ROWLEN;x++)
118                 fputc(row_ptr[y][x],fp);
119             fputc('\n',fp);
120         };
121         if( *screen_name == '\0' )
122             fputc('#',fp);
123         else
124             fputs(screen_name,fp);
125         fputc('\n',fp);
126         if(maxmoves != 0)
127             fprintf(fp,"%d\n",maxmoves);
128         fclose(fp);
129     };
130     return (fp == NULL);
131 }
132