BSD 4_3_Net_2 development
[unix-history] / usr / src / contrib / isode / quipu / dish / edit.c
CommitLineData
9e8e5516
C
1/* edit.c - */
2
3#ifndef lint
4static char *rcsid = "$Header: /f/osi/quipu/dish/RCS/edit.c,v 7.2 91/02/22 09:40:31 mrose Interim $";
5#endif
6
7/*
8 * $Header: /f/osi/quipu/dish/RCS/edit.c,v 7.2 91/02/22 09:40:31 mrose Interim $
9 *
10 *
11 * $Log: edit.c,v $
12 * Revision 7.2 91/02/22 09:40:31 mrose
13 * Interim 6.8
14 *
15 * Revision 7.1 90/07/09 14:47:08 mrose
16 * sync
17 *
18 * Revision 7.0 89/11/23 22:20:04 mrose
19 * Release 6.0
20 *
21 */
22
23/*
24 * NOTICE
25 *
26 * Acquisition, use, and distribution of this module and related
27 * materials are subject to the restrictions of a license agreement.
28 * Consult the Preface in the User's Manual for the full terms of
29 * this agreement.
30 *
31 */
32
33
34#include "manifest.h"
35#include "quipu/util.h"
36#include "psap.h"
37#include "tailor.h"
38#ifdef BSD42
39#include <sys/file.h>
40#endif
41#ifdef SYS5
42#include <fcntl.h>
43#endif
44#include <sys/stat.h>
45
46extern char fname[];
47
48#define OPT (!frompipe || rps -> ps_byteno == 0 ? opt : rps)
49#define RPS (!frompipe || opt -> ps_byteno == 0 ? rps : opt)
50extern char frompipe;
51extern PS opt, rps;
52
53editentry (argc, argv)
54int argc;
55char **argv;
56{
57 char str[LINESIZE];
58 char prog[LINESIZE];
59 int res;
60 extern char inbuf[];
61 extern int fd;
62 extern char remote_prob;
63 extern char dad_flag;
64
65 if (argc != 1) {
66 Usage (argv[0]);
67 return (NOTOK);
68 }
69
70 (void) sprintf (str, "%s %s",
71 _isodefile (isodebinpath, "editentry"), fname);
72
73 if (!frompipe)
74 return (system (str) ? NOTOK : OK);
75
76 if (!dad_flag) {
77 (void) sprintf (prog, "e%s\n", str);
78
79 send_pipe_aux (prog);
80
81 if ((res = read_pipe_aux (prog,sizeof prog)) < 1) {
82 (void) fprintf (stderr, "read failure\n");
83 remote_prob = TRUE;
84 return (NOTOK);
85 } else {
86 if ((res == 1) && (*prog == 'e')) {
87 remote_prob = FALSE;
88 return (NOTOK); /* remote error - abandon ! */
89 }
90 if (*fname != '/') {
91 char tempbuf[LINESIZE];
92
93 /* relative path... prefix cwd */
94 *(prog + res) = 0;
95 (void) sprintf (tempbuf, "%s/%s", prog, fname);
96 (void) strcpy (fname, tempbuf);
97 }
98 }
99 } else {
100#ifndef SOCKETS
101 ps_printf (OPT,
102 "operation not allowed when using directory assistance server!\n");
103 return NOTOK;
104#else
105 int cc, i, j;
106 char *cp, *dp;
107 FILE *fp;
108 struct stat st;
109 extern int errno;
110
111 if ((fp = fopen (fname, "r+")) == NULL) {
112 ps_printf (OPT, "unable to open %s for rw: %s\n",
113 fname, sys_errname (errno));
114 return NOTOK;
115 }
116 if (fstat (fileno (fp), &st) == NOTOK
117 || (st.st_mode & S_IFMT) != S_IFREG
118 || (cc = st.st_size) == 0) {
119 ps_printf (OPT, "%s: not a regular file\n", fname);
120out: ;
121 (void) fclose (fp);
122 return NOTOK;
123 }
124
125 (void) sprintf (prog, "e%d\n", cc);
126 send_pipe_aux (prog);
127
128 if ((res = read_pipe_aux (prog, sizeof prog)) < 1) {
129 (void) fprintf (stderr, "read failure\n");
130 remote_prob = TRUE;
131 goto out;
132 }
133 else
134 if ((res == 1) && (*prog == 'e')) {
135 remote_prob = FALSE;
136 goto out;
137 }
138
139 if ((cp = malloc ((unsigned) (cc))) == NULL) {
140 ps_printf (OPT, "out of memory\n");
141 goto out;
142 }
143 for (dp = cp, j = cc; j > 0; dp += i, j -= i)
144 switch (i = fread (dp, sizeof *dp, j, fp)) {
145 case NOTOK:
146 ps_printf (OPT, "error reading %s: %s\n",
147 fname, sys_errname (errno));
148 goto out2;
149
150 case OK:
151 ps_printf (OPT, "premature eof reading %s\n",
152 fname);
153out2: ;
154 free (cp);
155 goto out;
156
157 default:
158 break;
159 }
160
161 send_pipe_aux2 (cp, cc);
162 free (cp), cp = NULL;
163
164 if ((res = read_pipe_aux2 (&cp, &cc)) < 1) {
165 (void) ps_printf (OPT, "read failure\n");
166 remote_prob = TRUE;
167 goto out;
168 }
169 if (res == 1) {
170 if (*cp != 'e')
171 (void) ps_printf (OPT, "remote protocol error: %s\n",
172 cp);
173 goto out;
174 }
175
176 (void) fclose (fp);
177 if ((fp = fopen (fname, "w")) == NULL) {
178 ps_printf (OPT, "unable to re-open %s for writing: %s\n",
179 fname, sys_errname (errno));
180 free (cp);
181 return NOTOK;
182 }
183
184 if (fwrite (cp, sizeof *cp, cc, fp) == 0) {
185 ps_printf (OPT, "error writing %s: %s\n",
186 fname, sys_errname (errno));
187 goto out2;
188 }
189
190 free (cp);
191 (void) fclose (fp);
192#endif
193 }
194
195 return (OK);
196}
197
198
199get_password (str,buffer)
200char * str;
201char * buffer;
202{
203
204 char prog[LINESIZE];
205 int res;
206 extern char inbuf[];
207 extern int fd;
208 extern char remote_prob;
209 char * getpassword ();
210
211 if (frompipe) {
212 (void) sprintf (prog, "p%s\n", str);
213
214 send_pipe_aux (prog);
215
216 if ((res = read_pipe_aux (prog,sizeof prog)) < 1) {
217 (void) fprintf (stderr, "read failure\n");
218 remote_prob = TRUE;
219 return;
220 } else {
221 *(prog+res) = 0;
222 (void) strcpy (buffer, prog + 1);
223 }
224 } else {
225 (void) sprintf (buffer,"Enter password for \"%s\": ",str);
226 (void) strcpy (buffer,getpassword (buffer));
227 }
228}
229
230yesno (str)
231char * str;
232{
233 char prog[LINESIZE];
234 extern char inbuf[];
235 extern int fd;
236 extern char remote_prob;
237 char * getpassword ();
238
239 if (frompipe) {
240 (void) sprintf (prog, "y%s\n", str);
241
242 send_pipe_aux (prog);
243
244 if (read_pipe_aux (prog,sizeof prog) < 1) {
245 (void) fprintf (stderr, "read failure\n");
246 remote_prob = TRUE;
247 return FALSE;
248 }
249 } else {
250 ps_printf (OPT,"%s",str);
251 (void) fgets (prog, sizeof prog, stdin);
252 }
253
254 switch (prog[0]) {
255 case 'y':
256 return OK;
257
258 case 'n':
259 default:
260 return NOTOK;
261
262 case 'N':
263 return DONE;
264 }
265}