File: ctrlfile.c

package info (click to toggle)
saoimage 1.19-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 3,256 kB
  • ctags: 3,610
  • sloc: ansic: 36,050; makefile: 215; sh: 11
file content (229 lines) | stat: -rw-r--r-- 6,530 bytes parent folder | download | duplicates (5)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifndef lint
static char SccsId[] = "%W%  %G%";
#endif

/* Module:	ctrlfile.c (Control File)
 * Purpose:	Prompt for file name and open file for specified purpose
 * Subroutine:	open_output_file()
 * Subroutine:	open_input_file()
 * Subroutine:	swap_bytes()
 * UNIX calls:	fopen(), lstat(), unlink(), extern int errno
 * Copyright:	1989 Smithsonian Astrophysical Observatory
 *		You may do anything you like with this file except remove
 *		this copyright.  The Smithsonian Astrophysical Observatory
 *		makes no representations about the suitability of this
 *		software for any purpose.  It is provided "as is" without
 *		express or implied warranty.
 * Modified:	{0} Michael VanHilst	initial version		  9 July 1989
 *		{n} <who> -- <does what> -- <when>
 */

#include <stdio.h>		/* FILE, stderr, NULL, etc. */
#include <ctype.h>		/* macros for isupper, tolower, etc */
#include <sys/types.h>		/* needed for lstat */
#include <sys/stat.h>		/* needed for lstat, struct stat */
#include <errno.h>		/* ENOENT */
#include <X11/Xlib.h>		/* X window stuff */
#include "hfiles/edit.h"	/* EditStruct */

#ifdef VMS
#define lstat stat
#define unlink delete
#endif

/* May not get defined on some systems, redeclaration doesn't seem to hurt */
extern int errno;

/*
 * Subroutine:	open_output_file
 * Purpose:	Open a file for writing, as specified by user
 * Post state:	Sets pointer to file, open and ready for writing
 * Returns:	2 = append, 1 = new file, 0 = user decided not to, -1 = error
 */
int open_output_file ( fd, edit, one_popup_row, prompt )
     FILE **fd;
     EditStruct *edit;
     int one_popup_row;		/* i: put-promt-before-edit-line, else above */
     char *prompt;
{
  int exist;
  static EditStruct *instruct = NULL;
  static char *response;
  int reply;
  char filename[128];
  char open_type[4];
  int done;				/* l: return status */
  int get_edit_input();
  EditStruct *init_edit_popup();
  void clear_edit_buf(), unmap_popwin();
  static int file_exists();

  open_type[0] = 0;
  if( (get_edit_input(edit, one_popup_row, 1, 0, prompt) <= 0) ||
      (edit->char_cnt == 0) ) {
    unmap_popwin();
    return( 0 );
  }
  strncpy(filename, edit->string, edit->char_cnt);
  filename[edit->char_cnt] = '\0';
  /* check if file already exists */
  exist = file_exists(filename);
  done = -3;
  if( exist >0 ) {
    /* if it exists, get instructions on how to respond */
    if( instruct == NULL ) {
      instruct = init_edit_popup(NULL, 8);
      response = instruct->string;
    }
    do {
      clear_edit_buf (instruct);
      reply =
	get_edit_input(instruct, 1, 0, 0,
		       "Enter 'o' (overwrite), 'a' (append), 'q' (quit): ");
      if( reply <= 0 ) {
        done = 0;
      } else if( instruct->char_cnt > 0 ) {
	if( isupper(response[0]) )
	  response[0] = tolower(response[0]);
	/* quit, do nothing */
	if( response[0] == 'q') {
	  /* quit */
	  done = 0;
	} else if( response[0] == 'o') {
	  /* overwrite existing file */
	  strcpy(open_type, "w");
	  if( unlink(filename) <0 ) {
	    (void)fprintf(stderr, "Error: can't delete file\n");
	    perror (filename);
	    done = -1;
	  } else
	    done = 1;
	} else if( response[0] == 'a') {
	  /* append to end of existing file */
	  strcpy(open_type, "a");
	  done = 2;
	} else
	  /* else unknown response */
	  XBell(edit->display, 20);
      }
    } while( done == -3 );
  } else if( exist ==0 ) {
    /* file does not yet exist */
    strcpy(open_type, "a");
    done = 1;
  } else {
    /* problem accessing file */
    (void)fprintf(stderr, "Error attempting to access file\n");
    perror(filename);
    done = -1;
  }
  unmap_popwin();
  if( done > 0 ) {
    /* open the file appropriately */
    if( (*fd=fopen(filename, open_type)) == NULL ) {
      (void)fprintf(stderr, "Error attempting to open file\n");
      perror(filename);
      done = -1;
    }
  }
  if( done < 0 )
    XBell(edit->display, 20);
  return( done );
}

/*
 * Subroutine:	open_input_file
 * Purpose:	Open a file for reading, as specified by user
 * Note:	Sets pointer of file open and ready for reading
 * Returns:	1 on success, 0 if user decided not to, -1 on error
 */
int open_input_file ( fd, edit, one_popup_row, prompt )
     FILE **fd;
     EditStruct *edit;
     int one_popup_row;		/* i: put-promt-before-edit-line, else above */
     char *prompt;
{
  char filename[132];
  int exist;
  int get_edit_input();
  static int file_exists();

  if( get_edit_input(edit, one_popup_row, 1, 1, prompt) <= 0 )
    return( 0 );
  strncpy(filename, edit->string, edit->char_cnt);
  filename[edit->char_cnt] = '\0';
  /* see if it exists */
  exist = file_exists(filename);
  if( exist ==0 ) {
    (void)fprintf(stderr, "Error: file %s does not exist\n", filename);
    return( -1 );
  } else if( exist <0 ) {
    (void)fprintf(stderr, "Error attempting to access file\n");
    perror(filename);
    return( -1 );
  }
  /* open the cursor file */
  if( (*fd = fopen(filename, "r")) == NULL ) {
    (void)fprintf(stderr, "Error attemping to open file\n");
    perror(filename);
    return( -1 );
  }
  return( 1 );
}


/*
 * Subroutine:	file_exists
 * Purpose:	does a file exist for opening (1=yes, 0=no, -1=problem)
 */
static int file_exists ( name )
     char *name;
{
  int i;
  struct stat buf;

  i = lstat(name, &buf);
  if( i ==0 )
    return(1);            /* found the file */
  else if( errno == ENOENT )
    return(0);            /* didn't find the file */
  else
#ifndef VMS
    return(-1);           /* a real error of some sort */
#else
    return(0);		  /* VMS doesn't set errno properly in this case */
#endif
}


/*
 * Subroutine:	swap_bytes
 * Purpose:	Swap bytes in place, (swab is not guaranteed to work in place)
 */
void swap_bytes ( array, nbytes )
     char *array;
     int nbytes;
{
  register char *low_byte, *high_byte, *last_byte;
  register unsigned temp;
  register int next=2;

  /* swap successive pairs of bytes */
  low_byte = array;
  high_byte = array+1;
  last_byte = array + nbytes;
  while( high_byte < last_byte ) {
    temp = *low_byte;
    *low_byte = *high_byte;
    *high_byte = temp;
    /* there is no hardware incr on the 68000, so why bother with ++? */
    low_byte += next;
    high_byte += next;
  }
}