2 * Miscellaneous support routines..
4 * $FreeBSD: src/release/sysinstall/misc.c,v 1.40.2.1 2001/09/27 06:51:09 murray Exp $
5 * $DragonFly: src/release/sysinstall/Attic/misc.c,v 1.3 2003/08/08 04:18:36 dillon Exp $
8 * Jordan Hubbard. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer,
15 * verbatim and that no modifications are made prior to this
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "sysinstall.h"
39 #include <sys/errno.h>
41 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <vfs/ufs/ufsmount.h>
47 #include <sys/reboot.h>
48 #include <sys/disklabel.h>
50 /* Quick check to see if a file is readable */
52 file_readable(char *fname)
54 if (!access(fname, F_OK))
59 /* Quick check to see if a file is executable */
61 file_executable(char *fname)
63 if (!access(fname, X_OK))
68 /* Concatenate two strings into static storage */
70 string_concat(char *one, char *two)
72 static char tmp[FILENAME_MAX];
74 /* Yes, we're deliberately cavalier about not checking for overflow */
80 /* sane strncpy() function */
82 sstrncpy(char *dst, const char *src, int size)
85 return strncpy(dst, src, size);
88 /* Concatenate three strings into static storage */
90 string_concat3(char *one, char *two, char *three)
92 static char tmp[FILENAME_MAX];
94 /* Yes, we're deliberately cavalier about not checking for overflow */
101 /* Clip the whitespace off the end of a string */
103 string_prune(char *str)
105 int len = str ? strlen(str) : 0;
107 while (len && isspace(str[len - 1]))
112 /* run the whitespace off the front of a string */
114 string_skipwhite(char *str)
116 while (*str && isspace(*str))
121 /* copy optionally and allow second arg to be null */
123 string_copy(char *s1, char *s2)
134 /* convert an integer to a string, using a static buffer */
140 snprintf(buf, 12, "%d", value);
145 directory_exists(const char *dirname)
151 if (!strlen(dirname))
154 tptr = opendir(dirname);
163 pathBaseName(const char *path)
166 char *ret = (char *)path;
168 pt = strrchr(path,(int)'/');
170 if (pt != 0) /* if there is a slash */
172 ret = ++pt; /* start the file after it */
178 /* A free guaranteed to take NULL ptrs */
186 /* A malloc that checks errors */
188 safe_malloc(size_t size)
193 msgFatal("Invalid malloc size of %ld!", (long)size);
196 msgFatal("Out of memory!");
201 /* A realloc that checks errors */
203 safe_realloc(void *orig, size_t size)
208 msgFatal("Invalid realloc size of %ld!", (long)size);
209 ptr = realloc(orig, size);
211 msgFatal("Out of memory!");
215 /* Create a path biased from the VAR_INSTALL_ROOT variable (if not /) */
217 root_bias(char *path)
219 static char tmp[FILENAME_MAX];
220 char *cp = variable_get(VAR_INSTALL_ROOT);
222 if (!strcmp(cp, "/"))
224 strcpy(tmp, variable_get(VAR_INSTALL_ROOT));
230 * These next routines are kind of specialized just for building item lists
234 /* Add an item to an item list */
236 item_add(dialogMenuItem *list, char *prompt, char *title,
237 int (*checked)(dialogMenuItem *self),
238 int (*fire)(dialogMenuItem *self),
239 void (*selected)(dialogMenuItem *self, int is_selected),
240 void *data, int aux, int *curr, int *max)
246 list = (dialogMenuItem *)realloc(list, sizeof(dialogMenuItem) * *max);
248 d = &list[(*curr)++];
249 bzero(d, sizeof(*d));
250 d->prompt = prompt ? strdup(prompt) : NULL;
251 d->title = title ? strdup(title) : NULL;
252 d->checked = checked;
254 d->selected = selected;
260 /* Toss the items out */
262 items_free(dialogMenuItem *list, int *curr, int *max)
266 for (i = 0; list[i].prompt; i++) {
267 safe_free(list[i].prompt);
268 safe_free(list[i].title);
281 if (file_readable(ipath) || Fake)
282 return DITEM_SUCCESS;
284 path = strcpy(alloca(strlen(ipath) + 1), ipath);
286 msgDebug("mkdir(%s)\n", path);
288 if (p[0] == '/') /* Skip leading '/'. */
290 for (final = FALSE; !final; ++p) {
291 if (p[0] == '\0' || (p[0] == '/' && p[1] == '\0'))
293 else if (p[0] != '/')
296 if (stat(path, &sb)) {
297 if (errno != ENOENT) {
298 msgConfirm("Couldn't stat directory %s: %s", path, strerror(errno));
299 return DITEM_FAILURE;
302 msgDebug("mkdir(%s..)\n", path);
303 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
304 msgConfirm("Couldn't create directory %s: %s", path,strerror(errno));
305 return DITEM_FAILURE;
310 return DITEM_SUCCESS;
314 Mount(char *mountp, void *dev)
316 struct ufs_args ufsargs;
318 char mountpoint[FILENAME_MAX];
321 return DITEM_SUCCESS;
323 if (*((char *)dev) != '/') {
324 sprintf(device, "%s/dev/%s", RunningAsInit ? "/mnt" : "", (char *)dev);
325 sprintf(mountpoint, "%s%s", RunningAsInit ? "/mnt" : "", mountp);
329 strcpy(mountpoint, mountp);
331 memset(&ufsargs,0,sizeof ufsargs);
333 if (Mkdir(mountpoint)) {
334 msgConfirm("Unable to make directory mountpoint for %s!", mountpoint);
335 return DITEM_FAILURE;
338 msgDebug("mount %s %s\n", device, mountpoint);
340 ufsargs.fspec = device;
341 if (mount("ufs", mountpoint, RunningAsInit ? MNT_ASYNC | MNT_NOATIME : 0,
342 (caddr_t)&ufsargs) == -1) {
343 msgConfirm("Error mounting %s on %s : %s", device, mountpoint, strerror(errno));
344 return DITEM_FAILURE;
346 return DITEM_SUCCESS;
350 openLayoutDialog(char *helpfile, char *title, int x, int y, int width, int height)
353 static char help[FILENAME_MAX];
355 /* We need a curses window */
356 win = newwin(LINES, COLS, 0, 0);
358 /* Say where our help comes from */
360 use_helpline("Press F1 for more information on this screen.");
361 use_helpfile(systemHelpFile(helpfile, help));
363 /* Setup a nice screen for us to splat stuff onto */
364 draw_box(win, y, x, height, width, dialog_attr, border_attr);
365 wattrset(win, dialog_attr);
366 mvwaddstr(win, y, x + (COLS - strlen(title)) / 2, title);
372 initLayoutDialog(WINDOW *win, Layout *layout, int x, int y, int *max)
374 ComposeObj *obj = NULL, *first;
377 /* Loop over the layout list, create the objects, and add them
378 onto the chain of objects that dialog uses for traversal*/
381 while (layout[n].help != NULL) {
382 int t = TYPE_OF_OBJ(layout[n].type);
386 layout[n].obj = NewStringObj(win, layout[n].prompt, layout[n].var,
387 layout[n].y + y, layout[n].x + x, layout[n].len, layout[n].maxlen);
388 ((StringObj *)layout[n].obj)->attr_mask = ATTR_OF_OBJ(layout[n].type);
392 layout[n].obj = NewButtonObj(win, layout[n].prompt, layout[n].var, layout[n].y + y, layout[n].x + x);
396 msgFatal("Don't support this object yet!");
398 AddObj(&obj, t, (void *) layout[n].obj);
402 /* Find the first object in the list */
403 for (first = obj; first->prev; first = first->prev);
408 layoutDialogLoop(WINDOW *win, Layout *layout, ComposeObj **obj, int *n, int max, int *cbutton, int *cancel)
411 int ret, i, len = strlen(layout[*n].help);
413 /* Display the help line at the bottom of the screen */
414 for (i = 0; i < 79; i++)
415 help_line[i] = (i < len) ? layout[*n].help[i] : ' ';
417 use_helpline(help_line);
418 display_helpline(win, LINES - 1, COLS - 1);
421 /* Ask for libdialog to do its stuff */
423 /* Handle special case stuff that libdialog misses. Sigh */
425 case SEL_ESC: /* Bail out */
429 /* This doesn't work for list dialogs. Oh well. Perhaps
430 should special case the move from the OK button ``up''
431 to make it go to the interface list, but then it gets
432 awkward for the user to go back and correct screw up's
433 in the per-interface section */
443 /* The user has pressed enter over a button object */
445 if (cbutton && *cbutton)
462 /* They tried some key combination we don't support - tootle them forcefully! */
479 restorescr(WINDOW *w)