2 * Miscellaneous support routines..
7 * Jordan Hubbard. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer,
14 * verbatim and that no modifications are made prior to this
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "sysinstall.h"
38 #include <sys/errno.h>
40 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/mount.h>
45 #include <ufs/ufs/ufsmount.h>
46 #include <sys/reboot.h>
47 #include <sys/disklabel.h>
48 #include <fs/msdosfs/msdosfsmount.h>
49 #include <sys/sysctl.h>
51 /* Quick check to see if a file is readable */
53 file_readable(char *fname)
55 if (!access(fname, F_OK))
60 /* Quick check to see if a file is executable */
62 file_executable(char *fname)
64 if (!access(fname, X_OK))
69 /* Concatenate two strings into static storage */
71 string_concat(char *one, char *two)
73 static char tmp[FILENAME_MAX];
75 /* Yes, we're deliberately cavalier about not checking for overflow */
81 /* sane strncpy() function */
83 sstrncpy(char *dst, const char *src, int size)
86 return strncpy(dst, src, size);
89 /* Concatenate three strings into static storage */
91 string_concat3(char *one, char *two, char *three)
93 static char tmp[FILENAME_MAX];
95 /* Yes, we're deliberately cavalier about not checking for overflow */
102 /* Clip the whitespace off the end of a string */
104 string_prune(char *str)
106 int len = str ? strlen(str) : 0;
108 while (len && isspace(str[len - 1]))
113 /* run the whitespace off the front of a string */
115 string_skipwhite(char *str)
117 while (*str && isspace(*str))
122 /* copy optionally and allow second arg to be null */
124 string_copy(char *s1, char *s2)
135 /* convert an integer to a string, using a static buffer */
141 snprintf(buf, 12, "%d", value);
146 directory_exists(const char *dirname)
152 if (!strlen(dirname))
155 tptr = opendir(dirname);
164 pathBaseName(const char *path)
167 char *ret = (char *)path;
169 pt = strrchr(path,(int)'/');
171 if (pt != 0) /* if there is a slash */
173 ret = ++pt; /* start the file after it */
179 /* A free guaranteed to take NULL ptrs */
187 /* A malloc that checks errors */
189 safe_malloc(size_t size)
194 msgFatal("Invalid malloc size of %ld!", (long)size);
197 msgFatal("Out of memory!");
202 /* A realloc that checks errors */
204 safe_realloc(void *orig, size_t size)
209 msgFatal("Invalid realloc size of %ld!", (long)size);
210 ptr = reallocf(orig, size);
212 msgFatal("Out of memory!");
216 /* Create a path biased from the VAR_INSTALL_ROOT variable (if not /) */
218 root_bias(char *path)
220 static char tmp[FILENAME_MAX];
221 char *cp = variable_get(VAR_INSTALL_ROOT);
223 if (!strcmp(cp, "/"))
225 strcpy(tmp, variable_get(VAR_INSTALL_ROOT));
231 * These next routines are kind of specialized just for building item lists
235 /* Add an item to an item list */
237 item_add(dialogMenuItem *list, char *prompt, char *title,
238 int (*checked)(dialogMenuItem *self),
239 int (*fire)(dialogMenuItem *self),
240 void (*selected)(dialogMenuItem *self, int is_selected),
241 void *data, void *aux, int *curr, int *max)
247 list = (dialogMenuItem *)safe_realloc(list, sizeof(dialogMenuItem) * *max);
249 d = &list[(*curr)++];
250 bzero(d, sizeof(*d));
251 d->prompt = prompt ? strdup(prompt) : NULL;
252 d->title = title ? strdup(title) : NULL;
253 d->checked = checked;
255 d->selected = selected;
261 /* Toss the items out */
263 items_free(dialogMenuItem *list, int *curr, int *max)
267 for (i = 0; list[i].prompt; i++) {
268 safe_free(list[i].prompt);
269 safe_free(list[i].title);
282 if (file_readable(ipath) || Fake)
283 return DITEM_SUCCESS;
285 path = strcpy(alloca(strlen(ipath) + 1), ipath);
287 msgDebug("mkdir(%s)\n", path);
289 if (p[0] == '/') /* Skip leading '/'. */
291 for (final = FALSE; !final; ++p) {
292 if (p[0] == '\0' || (p[0] == '/' && p[1] == '\0'))
294 else if (p[0] != '/')
297 if (stat(path, &sb)) {
298 if (errno != ENOENT) {
299 msgConfirm("Couldn't stat directory %s: %s", path, strerror(errno));
300 return DITEM_FAILURE;
303 msgDebug("mkdir(%s..)\n", path);
304 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
305 msgConfirm("Couldn't create directory %s: %s", path,strerror(errno));
306 return DITEM_FAILURE;
311 return DITEM_SUCCESS;
315 Mkdir_command(char *key, void *dir)
317 return (Mkdir((char*)dir));
321 Mount(char *mountp, void *dev)
323 struct ufs_args ufsargs;
325 char mountpoint[FILENAME_MAX];
328 return DITEM_SUCCESS;
330 if (*((char *)dev) != '/') {
331 sprintf(device, "%s/dev/%s", RunningAsInit ? "/mnt" : "", (char *)dev);
332 sprintf(mountpoint, "%s%s", RunningAsInit ? "/mnt" : "", mountp);
336 strcpy(mountpoint, mountp);
338 memset(&ufsargs,0,sizeof ufsargs);
340 if (Mkdir(mountpoint)) {
341 msgConfirm("Unable to make directory mountpoint for %s!", mountpoint);
342 return DITEM_FAILURE;
345 msgDebug("mount %s %s\n", device, mountpoint);
347 ufsargs.fspec = device;
348 if (mount("ufs", mountpoint, RunningAsInit ? MNT_ASYNC | MNT_NOATIME : 0,
349 (caddr_t)&ufsargs) == -1) {
350 msgConfirm("Error mounting %s on %s : %s", device, mountpoint, strerror(errno));
351 return DITEM_FAILURE;
353 return DITEM_SUCCESS;
357 Mount_msdosfs(char *mountp, void *dev)
359 struct msdosfs_args mount_args;
361 char mountpoint[FILENAME_MAX];
364 return DITEM_SUCCESS;
366 if (*((char *)dev) != '/') {
367 sprintf(device, "%s/dev/%s", RunningAsInit ? "/mnt" : "", (char *)dev);
368 sprintf(mountpoint, "%s%s", RunningAsInit ? "/mnt" : "", mountp);
372 strcpy(mountpoint, mountp);
375 if (Mkdir(mountpoint)) {
376 msgConfirm("Unable to make directory mountpoint for %s!", mountpoint);
377 return DITEM_FAILURE;
380 msgDebug("mount %s %s\n", device, mountpoint);
382 memset(&mount_args, 0, sizeof(mount_args));
383 mount_args.fspec = device;
384 mount_args.magic = MSDOSFS_ARGSMAGIC;
385 mount_args.mask = S_IRWXU | S_IRWXG | S_IRWXO;
386 if (mount("msdosfs", mountpoint, RunningAsInit ? MNT_ASYNC|MNT_NOATIME : 0,
387 (caddr_t)&mount_args) == -1) {
388 msgConfirm("Error mounting %s on %s : %s", device, mountpoint, strerror(errno));
389 return DITEM_FAILURE;
391 return DITEM_SUCCESS;
395 openLayoutDialog(char *helpfile, char *title, int x, int y, int width, int height)
398 static char help[FILENAME_MAX];
400 /* We need a curses window */
401 win = newwin(LINES, COLS, 0, 0);
403 /* Say where our help comes from */
405 use_helpline("Press F1 for more information on this screen.");
406 use_helpfile(systemHelpFile(helpfile, help));
408 /* Setup a nice screen for us to splat stuff onto */
409 draw_box(win, y, x, height, width, dialog_attr, border_attr);
410 wattrset(win, dialog_attr);
411 mvwaddstr(win, y, x + (COLS - strlen(title)) / 2, title);
417 initLayoutDialog(WINDOW *win, Layout *layout, int x, int y, int *max)
419 ComposeObj *obj = NULL, *first;
422 /* Loop over the layout list, create the objects, and add them
423 onto the chain of objects that dialog uses for traversal*/
426 while (layout[n].help != NULL) {
427 int t = TYPE_OF_OBJ(layout[n].type);
431 layout[n].obj = NewStringObj(win, layout[n].prompt, layout[n].var,
432 layout[n].y + y, layout[n].x + x, layout[n].len, layout[n].maxlen);
433 ((StringObj *)layout[n].obj)->attr_mask = ATTR_OF_OBJ(layout[n].type);
437 layout[n].obj = NewButtonObj(win, layout[n].prompt, layout[n].var, layout[n].y + y, layout[n].x + x);
441 msgFatal("Don't support this object yet!");
443 AddObj(&obj, t, (void *) layout[n].obj);
447 /* Find the first object in the list */
448 for (first = obj; first->prev; first = first->prev);
453 layoutDialogLoop(WINDOW *win, Layout *layout, ComposeObj **obj, int *n, int max, int *cbutton, int *cancel)
456 int ret, i, len = strlen(layout[*n].help);
458 /* Display the help line at the bottom of the screen */
459 for (i = 0; i < 79; i++)
460 help_line[i] = (i < len) ? layout[*n].help[i] : ' ';
462 use_helpline(help_line);
463 display_helpline(win, LINES - 1, COLS - 1);
466 /* Ask for libdialog to do its stuff */
468 /* Handle special case stuff that libdialog misses. Sigh */
470 case SEL_ESC: /* Bail out */
474 /* This doesn't work for list dialogs. Oh well. Perhaps
475 should special case the move from the OK button ``up''
476 to make it go to the interface list, but then it gets
477 awkward for the user to go back and correct screw up's
478 in the per-interface section */
488 /* The user has pressed enter over a button object */
490 if (cbutton && *cbutton)
507 /* They tried some key combination we don't support - tootle them forcefully! */
524 restorescr(WINDOW *w)
532 * Get a sysctl variable as a string or "<unknown>" if sysctl fails.
533 * Caller must free returned string.
536 getsysctlbyname(const char *sysctlname)
539 size_t sz, buf_sz = 0;
540 const char unk_str[] = "<unknown>";
542 sysctlbyname(sysctlname, NULL, &buf_sz, NULL, 0);
543 buf_sz = MAX(sizeof(unk_str), buf_sz) + 1;
545 buf = (char *)safe_malloc(buf_sz);
547 if (sysctlbyname(sysctlname, buf, &sz, NULL, 0) != -1)
550 strlcpy(buf, unk_str, buf_sz);