edahdi.c revision 1.7 1 1.7 dsl /* $NetBSD: edahdi.c,v 1.7 2009/03/14 15:36:03 dsl Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1996 Leo Weppelman, Waldi Ravens.
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.2 leo * 3. All advertising materials mentioning features or use of this software
16 1.1 leo * must display the following acknowledgement:
17 1.1 leo * This product includes software developed by
18 1.1 leo * Leo Weppelman and Waldi Ravens.
19 1.2 leo * 4. The name of the author may not be used to endorse or promote products
20 1.2 leo * derived from this software without specific prior written permission.
21 1.1 leo *
22 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 leo */
33 1.1 leo
34 1.1 leo /*
35 1.1 leo * This code implements a simple editor for partition id's on disks containing
36 1.1 leo * AHDI partition info.
37 1.1 leo *
38 1.1 leo * Credits for the code handling disklabels goes to Waldi Ravens.
39 1.1 leo *
40 1.1 leo */
41 1.1 leo #include <sys/types.h>
42 1.1 leo #include <sys/param.h>
43 1.1 leo #include <sys/stat.h>
44 1.1 leo #include <sys/disklabel.h>
45 1.1 leo
46 1.1 leo #include <machine/ahdilabel.h>
47 1.1 leo
48 1.1 leo #include <fcntl.h>
49 1.1 leo #include <stdlib.h>
50 1.1 leo #include <curses.h>
51 1.1 leo #include <termios.h>
52 1.1 leo #include <unistd.h>
53 1.1 leo #include <stdio.h>
54 1.1 leo #include <string.h>
55 1.1 leo #include <err.h>
56 1.1 leo #include <ctype.h>
57 1.1 leo
58 1.1 leo /*
59 1.1 leo * Internal partition tables:
60 1.1 leo */
61 1.1 leo typedef struct {
62 1.1 leo char id[4];
63 1.1 leo u_int start;
64 1.1 leo u_int end;
65 1.1 leo u_int rsec;
66 1.1 leo u_int rent;
67 1.1 leo int mod;
68 1.1 leo } part_t;
69 1.1 leo
70 1.1 leo typedef struct {
71 1.1 leo int nparts;
72 1.1 leo part_t *parts;
73 1.1 leo } ptable_t;
74 1.1 leo
75 1.1 leo /*
76 1.1 leo * I think we can savely assume a fixed blocksize - AHDI won't support
77 1.1 leo * something different...
78 1.1 leo */
79 1.1 leo #define BLPM ((1024 * 1024) / DEV_BSIZE)
80 1.1 leo
81 1.1 leo /*
82 1.1 leo * #Partition entries shown on the screen at once
83 1.1 leo */
84 1.1 leo #define MAX_PSHOWN 16 /* #partitions shown on screen */
85 1.1 leo
86 1.1 leo /*
87 1.1 leo * Tokens:
88 1.1 leo */
89 1.1 leo #define T_INVAL 0
90 1.1 leo #define T_QUIT 1
91 1.1 leo #define T_WRITE 2
92 1.1 leo #define T_NEXT 3
93 1.1 leo #define T_PREV 4
94 1.1 leo #define T_NUMBER 5
95 1.1 leo #define T_EOF 6
96 1.1 leo
97 1.1 leo /*
98 1.1 leo * Terminal capability strings (Ok, 1 to start with ;-) )
99 1.1 leo */
100 1.1 leo char *Clr_screen = "";
101 1.1 leo
102 1.6 dsl void ahdi_cksum(void *);
103 1.6 dsl u_int ahdi_getparts(int, ptable_t *, u_int, u_int);
104 1.6 dsl int bsd_label(int, u_int);
105 1.6 dsl int dkcksum(struct disklabel *);
106 1.6 dsl int edit_parts(int, ptable_t *);
107 1.6 dsl void *disk_read(int, u_int, u_int);
108 1.6 dsl void disk_write(int, u_int, u_int, void *);
109 1.6 dsl char *get_id(void);
110 1.6 dsl void get_termcap(void);
111 1.6 dsl int lex(int *);
112 1.6 dsl int show_parts(ptable_t *, int);
113 1.6 dsl void update_disk(ptable_t *, int, int);
114 1.1 leo
115 1.1 leo int
116 1.1 leo main(argc, argv)
117 1.1 leo int argc;
118 1.1 leo char *argv[];
119 1.1 leo {
120 1.1 leo int fd;
121 1.1 leo ptable_t ptable;
122 1.1 leo int rv;
123 1.1 leo struct stat st;
124 1.1 leo
125 1.1 leo if (argc != 2) {
126 1.1 leo char *prog = strrchr(argv[0], '/');
127 1.1 leo
128 1.1 leo if (prog == NULL)
129 1.1 leo prog = argv[0];
130 1.1 leo else prog++;
131 1.1 leo fprintf(stderr, "Usage: %s <raw_disk_device>", prog);
132 1.1 leo exit(1);
133 1.1 leo }
134 1.1 leo if ((fd = open(argv[1], O_RDWR)) < 0)
135 1.1 leo err(1, "Cannot open '%s'.", argv[1]);
136 1.1 leo if (fstat(fd, &st) < 0)
137 1.1 leo err(1, "Cannot stat '%s'.", argv[1]);
138 1.1 leo if (!S_ISCHR(st.st_mode))
139 1.1 leo errx(1, "'%s' must be a character special device.", argv[1]);
140 1.1 leo
141 1.1 leo if ((rv = bsd_label(fd, LABELSECTOR)) < 0)
142 1.1 leo errx(1, "I/O error");
143 1.1 leo if (rv == 0) {
144 1.1 leo warnx("Disk has no ahdi partitions");
145 1.1 leo return (2);
146 1.1 leo }
147 1.1 leo
148 1.1 leo get_termcap();
149 1.1 leo
150 1.1 leo ptable.nparts = 0;
151 1.1 leo ptable.parts = NULL;
152 1.1 leo
153 1.1 leo if ((ahdi_getparts(fd, &ptable, AHDI_BBLOCK, AHDI_BBLOCK) != 0)
154 1.1 leo || (ptable.nparts == 0))
155 1.1 leo exit (1);
156 1.1 leo
157 1.1 leo edit_parts(fd, &ptable);
158 1.1 leo return (0);
159 1.1 leo }
160 1.1 leo
161 1.1 leo int
162 1.7 dsl edit_parts(int fd, ptable_t *ptable)
163 1.1 leo {
164 1.1 leo int scr_base = 0;
165 1.1 leo int value;
166 1.1 leo char *error, *new_id;
167 1.1 leo
168 1.1 leo for (;;) {
169 1.1 leo error = NULL;
170 1.3 leo tputs(Clr_screen, 1, putchar);
171 1.1 leo show_parts(ptable, scr_base);
172 1.1 leo
173 1.1 leo printf("\n\n");
174 1.1 leo printf("q : quit - don't update the disk\n");
175 1.1 leo printf("w : write changes to disk\n");
176 1.1 leo printf("> : next screen of partitions\n");
177 1.1 leo printf("< : previous screen of partitions\n");
178 1.1 leo printf("<nr> : modify id of partition <nr>\n");
179 1.1 leo printf("\n\nCommand? ");
180 1.1 leo fflush(stdout);
181 1.1 leo
182 1.1 leo switch (lex(&value)) {
183 1.1 leo case T_EOF:
184 1.1 leo exit(0);
185 1.1 leo
186 1.1 leo case T_INVAL:
187 1.1 leo error = "Invalid command";
188 1.1 leo break;
189 1.1 leo case T_QUIT :
190 1.1 leo for (value = 0; value < ptable->nparts; value++) {
191 1.1 leo if (ptable->parts[value].mod) {
192 1.1 leo printf("\nThere are unwritten changes."
193 1.1 leo " Quit anyway? [n] ");
194 1.1 leo value = getchar();
195 1.1 leo if ((value == 'y') || (value == 'Y'))
196 1.1 leo exit (0);
197 1.1 leo while (value != '\n')
198 1.1 leo value = getchar();
199 1.1 leo break;
200 1.1 leo }
201 1.1 leo }
202 1.1 leo if (value == ptable->nparts)
203 1.1 leo exit(0);
204 1.1 leo break;
205 1.1 leo case T_WRITE:
206 1.1 leo error = "No changes to write";
207 1.1 leo for (value = 0; value < ptable->nparts; value++) {
208 1.1 leo if (ptable->parts[value].mod) {
209 1.1 leo update_disk(ptable, fd, value);
210 1.1 leo error = "";
211 1.1 leo }
212 1.1 leo }
213 1.1 leo break;
214 1.1 leo case T_NEXT :
215 1.1 leo if ((scr_base + MAX_PSHOWN) < ptable->nparts)
216 1.1 leo scr_base += MAX_PSHOWN;
217 1.1 leo break;
218 1.1 leo case T_PREV :
219 1.1 leo scr_base -= MAX_PSHOWN;
220 1.1 leo if (scr_base < 0)
221 1.1 leo scr_base = 0;
222 1.1 leo break;
223 1.1 leo case T_NUMBER:
224 1.1 leo if (value >= ptable->nparts) {
225 1.1 leo error = "Not that many partitions";
226 1.1 leo break;
227 1.1 leo }
228 1.1 leo if ((new_id = get_id()) == NULL) {
229 1.1 leo error = "Invalid id";
230 1.1 leo break;
231 1.1 leo }
232 1.1 leo strncpy(ptable->parts[value].id, new_id, 3);
233 1.1 leo ptable->parts[value].mod = 1;
234 1.1 leo scr_base = (value / MAX_PSHOWN) * MAX_PSHOWN;
235 1.1 leo break;
236 1.1 leo default :
237 1.1 leo error = "Internal error - unknown token";
238 1.1 leo break;
239 1.1 leo }
240 1.1 leo if (error != NULL) {
241 1.1 leo printf("\n\n%s", error);
242 1.1 leo fflush(stdout);
243 1.1 leo sleep(2);
244 1.1 leo }
245 1.1 leo }
246 1.1 leo }
247 1.1 leo
248 1.1 leo int
249 1.7 dsl show_parts(ptable_t *ptable, int nr)
250 1.1 leo {
251 1.1 leo int i;
252 1.1 leo part_t *p;
253 1.1 leo u_int megs;
254 1.1 leo
255 1.1 leo if (nr >= ptable->nparts)
256 1.1 leo return (0); /* Nothing to show */
257 1.1 leo printf("\n\n");
258 1.1 leo printf("nr root desc id start end MBs\n");
259 1.1 leo
260 1.1 leo p = &ptable->parts[nr];
261 1.1 leo i = nr;
262 1.1 leo for(; (i < ptable->nparts) && ((i - nr) < MAX_PSHOWN); i++, p++) {
263 1.1 leo megs = ((p->end - p->start + 1) + (BLPM >> 1)) / BLPM;
264 1.1 leo printf("%2d%s %8u %4u %s %8u %8u (%3u)\n", i,
265 1.1 leo p->mod ? "*" : " ",
266 1.1 leo p->rsec, p->rent, p->id, p->start, p->end, megs);
267 1.1 leo }
268 1.1 leo return (1);
269 1.1 leo }
270 1.1 leo
271 1.1 leo int
272 1.7 dsl lex(int *value)
273 1.1 leo {
274 1.1 leo char c[1];
275 1.1 leo int rv, nch;
276 1.1 leo
277 1.1 leo rv = T_INVAL;
278 1.1 leo
279 1.1 leo *value = 0;
280 1.1 leo for (;;) {
281 1.1 leo if ((nch = read (0, c, 1)) != 1) {
282 1.1 leo if (nch == 0)
283 1.1 leo return (T_EOF);
284 1.1 leo else return (rv);
285 1.1 leo }
286 1.1 leo switch (*c) {
287 1.1 leo case 'q':
288 1.1 leo rv = T_QUIT;
289 1.1 leo goto out;
290 1.1 leo case 'w':
291 1.1 leo rv = T_WRITE;
292 1.1 leo goto out;
293 1.1 leo case '>':
294 1.1 leo rv = T_NEXT;
295 1.1 leo goto out;
296 1.1 leo case '<':
297 1.1 leo rv = T_PREV;
298 1.1 leo goto out;
299 1.1 leo default :
300 1.4 he if (isspace((unsigned char)*c)) {
301 1.1 leo if (rv == T_INVAL)
302 1.1 leo break;
303 1.1 leo goto out;
304 1.1 leo }
305 1.4 he else if (isdigit((unsigned char)*c)) {
306 1.1 leo *value = (10 * *value) + *c - '0';
307 1.1 leo rv = T_NUMBER;
308 1.1 leo }
309 1.1 leo goto out;
310 1.1 leo }
311 1.1 leo }
312 1.1 leo /* NOTREACHED */
313 1.1 leo out:
314 1.1 leo /*
315 1.1 leo * Flush rest of line before returning
316 1.1 leo */
317 1.1 leo while (read (0, c, 1) == 1)
318 1.1 leo if ((*c == '\n') || (*c == '\r'))
319 1.1 leo break;
320 1.1 leo return (rv);
321 1.1 leo }
322 1.1 leo
323 1.1 leo char *
324 1.1 leo get_id()
325 1.1 leo {
326 1.1 leo static char buf[5];
327 1.1 leo int n;
328 1.1 leo printf("\nEnter new id: ");
329 1.1 leo if (fgets(buf, sizeof(buf), stdin) == NULL)
330 1.1 leo return (NULL);
331 1.1 leo for (n = 0; n < 3; n++) {
332 1.4 he if (!isalpha((unsigned char)buf[n]))
333 1.1 leo return (NULL);
334 1.4 he buf[n] = toupper((unsigned char)buf[n]);
335 1.1 leo }
336 1.1 leo buf[3] = '\0';
337 1.1 leo return (buf);
338 1.1 leo }
339 1.1 leo
340 1.1 leo int
341 1.7 dsl bsd_label(int fd, u_int offset)
342 1.1 leo {
343 1.1 leo u_char *bblk;
344 1.1 leo u_int nsec;
345 1.1 leo int rv;
346 1.1 leo
347 1.1 leo nsec = (BBMINSIZE + (DEV_BSIZE - 1)) / DEV_BSIZE;
348 1.1 leo bblk = disk_read(fd, offset, nsec);
349 1.1 leo if (bblk) {
350 1.1 leo u_int *end, *p;
351 1.1 leo
352 1.1 leo end = (u_int *)&bblk[BBMINSIZE - sizeof(struct disklabel)];
353 1.1 leo rv = 1;
354 1.1 leo for (p = (u_int *)bblk; p < end; ++p) {
355 1.1 leo struct disklabel *dl = (struct disklabel *)&p[1];
356 1.1 leo if ( ( (p[0] == NBDAMAGIC && offset == 0)
357 1.1 leo || (p[0] == AHDIMAGIC && offset != 0)
358 1.1 leo || (u_char *)dl - bblk == 7168
359 1.1 leo )
360 1.1 leo && dl->d_npartitions <= MAXPARTITIONS
361 1.1 leo && dl->d_magic2 == DISKMAGIC
362 1.1 leo && dl->d_magic == DISKMAGIC
363 1.1 leo && dkcksum(dl) == 0
364 1.1 leo ) {
365 1.1 leo rv = 0;
366 1.1 leo break;
367 1.1 leo }
368 1.1 leo }
369 1.1 leo free(bblk);
370 1.1 leo }
371 1.1 leo else rv = -1;
372 1.1 leo
373 1.1 leo return(rv);
374 1.1 leo }
375 1.1 leo
376 1.1 leo int
377 1.7 dsl dkcksum(struct disklabel *dl)
378 1.1 leo {
379 1.1 leo u_short *start, *end, sum = 0;
380 1.1 leo
381 1.1 leo start = (u_short *)dl;
382 1.1 leo end = (u_short *)&dl->d_partitions[dl->d_npartitions];
383 1.1 leo while (start < end)
384 1.1 leo sum ^= *start++;
385 1.1 leo return(sum);
386 1.1 leo }
387 1.1 leo
388 1.1 leo void
389 1.7 dsl ahdi_cksum(void *buf)
390 1.1 leo {
391 1.1 leo unsigned short *p = (unsigned short *)buf;
392 1.1 leo unsigned short csum = 0;
393 1.1 leo int i;
394 1.1 leo
395 1.1 leo p[255] = 0;
396 1.1 leo for(i = 0; i < 256; i++)
397 1.1 leo csum += *p++;
398 1.1 leo *--p = (0x1234 - csum) & 0xffff;
399 1.1 leo }
400 1.1 leo
401 1.1 leo
402 1.1 leo u_int
403 1.1 leo ahdi_getparts(fd, ptable, rsec, esec)
404 1.1 leo int fd;
405 1.1 leo ptable_t *ptable;
406 1.1 leo u_int rsec,
407 1.1 leo esec;
408 1.1 leo {
409 1.1 leo struct ahdi_part *part, *end;
410 1.1 leo struct ahdi_root *root;
411 1.1 leo u_int rv;
412 1.1 leo
413 1.1 leo root = disk_read(fd, rsec, 1);
414 1.1 leo if (!root) {
415 1.1 leo rv = rsec + (rsec == 0);
416 1.1 leo goto done;
417 1.1 leo }
418 1.1 leo
419 1.1 leo if (rsec == AHDI_BBLOCK)
420 1.1 leo end = &root->ar_parts[AHDI_MAXRPD];
421 1.1 leo else end = &root->ar_parts[AHDI_MAXARPD];
422 1.1 leo for (part = root->ar_parts; part < end; ++part) {
423 1.1 leo u_int id = *((u_int32_t *)&part->ap_flg);
424 1.1 leo if (!(id & 0x01000000))
425 1.1 leo continue;
426 1.1 leo if ((id &= 0x00ffffff) == AHDI_PID_XGM) {
427 1.1 leo u_int offs = part->ap_st + esec;
428 1.1 leo rv = ahdi_getparts(fd, ptable, offs,
429 1.1 leo esec == AHDI_BBLOCK ? offs : esec);
430 1.1 leo if (rv)
431 1.1 leo goto done;
432 1.1 leo } else {
433 1.1 leo part_t *p;
434 1.1 leo u_int i = ++ptable->nparts;
435 1.1 leo ptable->parts = realloc(ptable->parts,
436 1.1 leo i * sizeof *ptable->parts);
437 1.1 leo if (ptable->parts == NULL) {
438 1.1 leo fprintf(stderr, "Allocation error\n");
439 1.1 leo rv = 1;
440 1.1 leo goto done;
441 1.1 leo }
442 1.1 leo p = &ptable->parts[--i];
443 1.1 leo *((u_int32_t *)&p->id) = id << 8;
444 1.1 leo p->start = part->ap_st + rsec;
445 1.1 leo p->end = p->start + part->ap_size - 1;
446 1.1 leo p->rsec = rsec;
447 1.1 leo p->rent = part - root->ar_parts;
448 1.1 leo p->mod = 0;
449 1.1 leo }
450 1.1 leo }
451 1.1 leo rv = 0;
452 1.1 leo done:
453 1.1 leo free(root);
454 1.1 leo return(rv);
455 1.1 leo }
456 1.1 leo
457 1.1 leo void *
458 1.1 leo disk_read(fd, start, count)
459 1.1 leo int fd;
460 1.1 leo u_int start,
461 1.1 leo count;
462 1.1 leo {
463 1.1 leo char *buffer;
464 1.1 leo off_t offset;
465 1.1 leo size_t size;
466 1.1 leo
467 1.1 leo size = count * DEV_BSIZE;
468 1.1 leo offset = start * DEV_BSIZE;
469 1.1 leo if ((buffer = malloc(size)) == NULL)
470 1.1 leo errx(1, "No memory");
471 1.1 leo
472 1.1 leo if (lseek(fd, offset, SEEK_SET) != offset) {
473 1.1 leo free(buffer);
474 1.1 leo err(1, "Seek error");
475 1.1 leo }
476 1.1 leo if (read(fd, buffer, size) != size) {
477 1.1 leo free(buffer);
478 1.1 leo err(1, "Read error");
479 1.1 leo exit(1);
480 1.1 leo }
481 1.1 leo return(buffer);
482 1.1 leo }
483 1.1 leo
484 1.1 leo void
485 1.1 leo update_disk(ptable, fd, pno)
486 1.1 leo ptable_t *ptable;
487 1.1 leo int fd, pno;
488 1.1 leo {
489 1.1 leo struct ahdi_root *root;
490 1.1 leo struct ahdi_part *apart;
491 1.1 leo part_t *lpart;
492 1.1 leo u_int rsec;
493 1.1 leo int i;
494 1.1 leo
495 1.1 leo rsec = ptable->parts[pno].rsec;
496 1.1 leo root = disk_read(fd, rsec, 1);
497 1.1 leo
498 1.1 leo /*
499 1.1 leo * Look for additional mods on the same sector
500 1.1 leo */
501 1.1 leo for (i = 0; i < ptable->nparts; i++) {
502 1.1 leo lpart = &ptable->parts[i];
503 1.1 leo if (lpart->mod && (lpart->rsec == rsec)) {
504 1.1 leo apart = &root->ar_parts[lpart->rent];
505 1.1 leo
506 1.1 leo /* Paranoia.... */
507 1.1 leo if ((lpart->end - lpart->start + 1) != apart->ap_size)
508 1.1 leo errx(1, "Updating wrong partition!");
509 1.1 leo apart->ap_id[0] = lpart->id[0];
510 1.1 leo apart->ap_id[1] = lpart->id[1];
511 1.1 leo apart->ap_id[2] = lpart->id[2];
512 1.1 leo lpart->mod = 0;
513 1.1 leo }
514 1.1 leo }
515 1.1 leo if (rsec == 0)
516 1.1 leo ahdi_cksum(root);
517 1.1 leo disk_write(fd, rsec, 1, root);
518 1.1 leo free(root);
519 1.1 leo }
520 1.1 leo
521 1.1 leo void
522 1.1 leo disk_write(fd, start, count, buf)
523 1.1 leo int fd;
524 1.1 leo u_int start,
525 1.1 leo count;
526 1.1 leo void *buf;
527 1.1 leo {
528 1.1 leo off_t offset;
529 1.1 leo size_t size;
530 1.1 leo
531 1.1 leo size = count * DEV_BSIZE;
532 1.1 leo offset = start * DEV_BSIZE;
533 1.1 leo
534 1.1 leo if (lseek(fd, offset, SEEK_SET) != offset)
535 1.1 leo err(1, "Seek error");
536 1.1 leo if (write(fd, buf, size) != size)
537 1.1 leo err(1, "Write error");
538 1.1 leo }
539 1.1 leo
540 1.1 leo void
541 1.1 leo get_termcap()
542 1.1 leo {
543 1.1 leo char *term, tbuf[1024], buf[1024], *p;
544 1.1 leo
545 1.1 leo if ((term = getenv("TERM")) == NULL)
546 1.1 leo warnx("No TERM environment variable!");
547 1.1 leo else {
548 1.1 leo if (tgetent(tbuf, term) != 1)
549 1.1 leo errx(1, "Tgetent failure.");
550 1.1 leo p = buf;
551 1.1 leo if (tgetstr("cl", &p)) {
552 1.1 leo if ((Clr_screen = malloc(strlen(buf) + 1)) == NULL)
553 1.1 leo errx(1, "Malloc failure.");
554 1.1 leo strcpy(Clr_screen, buf);
555 1.1 leo }
556 1.1 leo }
557 1.1 leo }
558