File: panic.c

package info (click to toggle)
oleo 1.6-16
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,640 kB
  • ctags: 3,139
  • sloc: ansic: 39,221; yacc: 1,737; sh: 362; makefile: 88
file content (156 lines) | stat: -rw-r--r-- 3,570 bytes parent folder | download | duplicates (3)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*	Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.

This file is part of Oleo, the GNU Spreadsheet.

Oleo is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Oleo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Oleo; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include "funcdef.h"
#include "sysdef.h"

#include "global.h"
#include "io-generic.h"
#include "io-abstract.h"
#include "info.h"
#include "cmd.h"
#include "panic.h"


extern char **environ;
/* I hope i don't need these since they aren't write for all systems. */
#if 0
extern int dup (int);
extern int close (int);
extern VOIDSTAR sbrk (size_t);
extern VOIDSTAR brk (VOIDSTAR);
#endif



void
panic_write_file (FILE *fp, struct rng *rng)
{
#ifdef atarist
  io_error_msg ("panic save format not implemented");
#else
  int fd;
  VOIDSTAR datend;
  VOIDSTAR datstart;
  unsigned long cnt;

  if (rng)
    {
      io_error_msg ("Can't write partial panic-save files");
      return;
    }
  fd = dup (fileno (fp));
  if (fd < 0)
    {
      io_error_msg ("Couldn't dup save file");
      return;
    }
  datstart = (VOIDSTAR) (&environ + sizeof (char **));
  datend = (VOIDSTAR) sbrk (0);
  if (datend == (char *) -1)
    {
      io_error_msg ("Couldn't sbrk(0)!");
      close (fd);
      return;
    }

  cnt = (char *) datend - (char *) datstart;
  if (write (fd, &cnt, sizeof (cnt)) != sizeof (cnt))
    {
      io_error_msg ("Couldn't write %lu (%d bytes) to save file",
		    cnt, sizeof (cnt));
      close (fd);
      return;
    }
  if (write (fd, datstart, cnt) != cnt)
    {
      io_error_msg ("Couldn't write %lu bytes to save file", cnt);
      close (fd);
      return;
    }
  if (close (fd) < 0)
    {
      io_error_msg ("Couldn't close save file");
      return;
    }

#endif
}

void
panic_read_file (FILE *fp, int ismerge)
{
#ifdef atarist
  io_error_msg ("panic save format not implemented");
#else
  int fd;
  unsigned long cnt;
  VOIDSTAR datstart;

  if (ismerge)
    {
      io_error_msg ("Can't merge panic-save files");
      return;
    }
  if ((fd = dup (fileno (fp))) < 0)
    {
      io_error_msg ("Couldn't dup save file");
      return;
    }
  if (read (fd, (VOIDSTAR) & cnt, sizeof (cnt)) != sizeof (cnt))
    {
      io_error_msg ("Couldn't read data_size (%d bytes) from save file", sizeof (cnt));
      close (fd);
      return;
    }
  datstart = (VOIDSTAR) (&environ + sizeof (char **));
  if ((VOIDSTAR)brk ((char *) datstart + cnt) == (VOIDSTAR) - 1)
    {
      io_error_msg ("Couldn't allocate %lu bytes of memory", cnt);
      close (fd);
      return;
    }
  if (read (fd, datstart, cnt) != cnt)
    {
      io_error_msg ("Couldn't read in %lu bytes of data", cnt);
      close (fd);
      return;
    }
  if (close (fd) < 0)
    io_error_msg ("Couldn't close save file");
  io_recenter_all_win ();
#endif
}

int
panic_set_options (int set_opt, char *option)
{
  return -1;
}


void
panic_show_options (void)
{
  io_text_line
    ("File format:  panic save   (quick-n-dirty data-segment dump)");
}