diff options
Diffstat (limited to 'sbin/sysinstall/utils.c')
| -rw-r--r-- | sbin/sysinstall/utils.c | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/sbin/sysinstall/utils.c b/sbin/sysinstall/utils.c index 24bd0ada9ae9..4f441ecd4713 100644 --- a/sbin/sysinstall/utils.c +++ b/sbin/sysinstall/utils.c @@ -13,8 +13,17 @@ #include <stdlib.h> #include <stdio.h> #include <stdarg.h> +#include <unistd.h> +#include <string.h> #include <dialog.h> #include <ncurses.h> +#include <errno.h> + +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <sys/param.h> +#include <sys/mount.h> #include "sysinstall.h" @@ -28,6 +37,37 @@ TellEm(char *fmt, ...) vsnprintf(p, 2048, fmt, ap); va_end(ap); dialog_msgbox("Progress", p, 3, 75, 0); + free(p); +} + +void +Fatal(char *fmt, ...) +{ + char *p; + va_list ap; + p = Malloc(2048); + va_start(ap,fmt); + vsnprintf(p, 2048, fmt, ap); + va_end(ap); + dialog_msgbox("Fatal", p, 12, 75, 1); + free(p); + exit(7); +} + +int +AskAbort(char *fmt, ...) +{ + char *p; + va_list ap; + int i; + + p = Malloc(2048); + va_start(ap,fmt); + vsnprintf(p, 2048, fmt, ap); + va_end(ap); + i = dialog_yesno("Abort", p, 12, 75); + free(p); + return i; } void * @@ -39,3 +79,92 @@ Malloc(size_t size) } return p; } + +char * +StrAlloc(char *str) +{ + char *p; + + p = (char *)Malloc(strlen(str) + 1); + strcpy(p,str); + return p; +} + +int +exec(char *cmd, char *args, ...) +{ + int pid, w, status; + char **argv = NULL; + int arg = 0; + int no_args = 0; + va_list ap; + struct stat dummy; + + if (stat(cmd, &dummy) == -1) { + sprintf(errmsg, "Executable %s does not exist\n", cmd); + return(-1); + } + + va_start(ap, args); + do { + if (arg == no_args) { + no_args += 10; + if (!(argv = realloc(argv, no_args * sizeof(char *)))) { + sprintf(errmsg, "Failed to allocate memory during exec of %s\n", cmd); + return(-1); + } + if (arg == 0) + argv[arg++] = (char *)args; + } + } while ((argv[arg++] = va_arg(ap, char *))); + va_end(ap); + + if ((pid = fork()) == 0) { + execv(cmd, argv); + exit(1); + } + + while ((w = wait(&status)) != pid && w != -1) + ; + + free(argv); + if (w == -1) { + sprintf(errmsg, "Child process %s terminated abnormally\n", cmd); + return(-1); + } + + return(0); +} + +void +MountUfs(char *device, char *prefix, char *mountpoint, int do_mkdir) +{ + struct ufs_args ufsargs; + char dbuf[90]; + char pbuf[90]; + + if (prefix) + strcpy(pbuf,prefix); + else + strcpy(pbuf,""); + + strcat(pbuf,mountpoint); + + if(do_mkdir && access(pbuf,R_OK)) { + TellEm("mkdir %s",pbuf); + if (mkdir(pbuf,S_IRWXU) == -1) { + Fatal("Couldn't create directory %s: %s\n", + pbuf,strerror(errno)); + } + } + + strcpy(dbuf,"/dev/"); + strcat(dbuf,device); + + TellEm("mount /dev/%s /mnt%s",dbuf,pbuf); + ufsargs.fspec = dbuf; + if (mount(MOUNT_UFS,pbuf, 0, (caddr_t) &ufsargs) == -1) { + Fatal("Error mounting %s on : %s\n", + dbuf, pbuf, strerror(errno)); + } +} |
