forked from sshipway/wanderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.c
126 lines (116 loc) · 3.18 KB
/
read.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* File read.c */
#include "wand_head.h"
#include "display.h"
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
extern int inform_me();
extern int edit_mode;
extern char *edit_screen;
extern char screen[NOOFROWS][ROWLEN + 1];
extern char *screenpath;
extern char screen_name[61];
char buffer[80];
/****************************************************************************
* rscreen *
*****************************************************************************/
int rscreen(int num, int *maxmoves)
{
int y, numr;
FILE *fp;
char name[100];
char (*row_ptr)[ROWLEN + 1] = screen;
if (!edit_mode)
sprintf(name, "%s/screen.%d", screenpath, num);
else
{
if (!edit_screen)
sprintf(name, "./screen");
else
sprintf(name, "%s", edit_screen);
}
if ((fp = fopen(name, "r")) == NULL)
{
if (edit_mode)
sprintf(buffer, "Cannot find file %s.", name);
else
sprintf(buffer, "File for screen %d unavailable.", num);
inform_me(buffer, 0);
}
else
{
for (y = 0; y < NOOFROWS; y++)
{
if (fgets(*row_ptr, ROWLEN + 2, fp) == NULL)
{
fprintf(stderr, "fgets error\n");
exit(EXIT_FAILURE);
}
numr = strlen(*row_ptr) - 1;
while (numr < ROWLEN)
(*row_ptr)[numr++] = ' ';
row_ptr++;
};
if (fgets(screen_name, 60, fp) == NULL)
{
fprintf(stderr, "fgets error\n");
exit(EXIT_FAILURE);
}
screen_name[61] = '\0';
screen_name[strlen(screen_name) - 1] = '\0';
if (fscanf(fp, "%d", maxmoves) != 1)
*maxmoves = 0;
fclose(fp);
};
return (fp == NULL);
}
/*********************************************************************
* wscreen *
**********************************************************************/
int wscreen(int maxmoves)
{
int y, x;
FILE *fp;
char name[100];
char (*row_ptr)[ROWLEN + 1] = screen;
if (!edit_screen)
sprintf(name, "./screen");
else
sprintf(name, "%s", edit_screen);
if ((fp = fopen(name, "w")) == NULL)
{
sprintf(name, "/tmp/screen.%d", getpid());
if ((fp = fopen(name, "w")) != NULL)
{
sprintf(buffer, "Written file is %s", name);
inform_me(buffer, 0);
}
else
err(1, "Could not open %s.\n", buffer);
}
if (fp == NULL)
{
inform_me("File for screen cannot be written.", 0);
}
else
{
for (y = 0; y < NOOFROWS; y++)
{
for (x = 0; x < ROWLEN; x++)
fputc(row_ptr[y][x], fp);
fputc('\n', fp);
};
if (*screen_name == '\0')
fputc('#', fp);
else
fputs(screen_name, fp);
fputc('\n', fp);
if (maxmoves != 0)
fprintf(fp, "%d\n", maxmoves);
fclose(fp);
};
return (fp == NULL);
}