From dd23185130bfd3c0aff54d4338fe74fc35ee3a0d Mon Sep 17 00:00:00 2001 From: Michiel Broek Date: Wed, 31 Aug 2005 20:16:52 +0000 Subject: [PATCH] Coded for OpenBSD --- unix/mbuseradd.c | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/unix/mbuseradd.c b/unix/mbuseradd.c index b0c50594..c3d99df9 100644 --- a/unix/mbuseradd.c +++ b/unix/mbuseradd.c @@ -45,6 +45,10 @@ #include #include +#if defined(__OpenBSD__) +#include +#endif + #include "xmalloc.h" #include "mbuseradd.h" @@ -174,7 +178,15 @@ int main(int argc, char *argv[]) struct passwd *pwent, *pwuser; struct group *gr; pid_t ppid; +#if defined(__OpenBSD__) +#define ARG_SIZE 60 + static char **s, buf[ARG_SIZE]; + size_t siz = 100; + char **p; + int mib[4]; +#else FILE *fp; +#endif if (argc != 5) Help(); @@ -221,29 +233,37 @@ int main(int argc, char *argv[]) * Find out the name of our parent. */ temp = calloc(PATH_MAX, sizeof(char)); -#if defined(__OpenBSD__) -#define ARG_SIZE 60 - static char **s, buf[ARG_SIZE]; - size_t siz = 100; - char **p; - int mib[4]; + ppid = getppid(); +#if defined(__OpenBSD__) + /* + * Systems that use sysctl to get process information + */ mib[0] = CTL_KERN; mib[1] = KERN_PROC_ARGS; mib[2] = ppid; mib[3] = KERN_PROC_ARGV; - if (sysctl(mib, 4, s, &siz, NULL, 0) == 0) - break; + if ((s = realloc(s, siz)) == NULL) { + fprintf(stderr, "mbuseradd: no memory\n"); + exit(1); + } + if (sysctl(mib, 4, s, &siz, NULL, 0) == -1) { + perror(""); + fprintf(stderr, "mbuseradd: sysctl call failed\n"); + exit(1); + } buf[0] = '\0'; - s = malloc(size, sizeof(char *)); for (p = s; *p != NULL; p++) { if (p != s) strlcat(buf, " ", sizeof(buf)); strlcat(buf, *p, sizeof(buf)); } - fprintf("%s\n", buf); + printf("%s\n", buf); + parent = xstrcpy(buf); #else - ppid = getppid(); + /* + * Systems with /proc filesystem like Linux, FreeBSD + */ snprintf(temp, PATH_MAX, "/proc/%d/cmdline", ppid); if ((fp = fopen(temp, "r")) == NULL) { fprintf(stderr, "mbuseradd: can't read %s\n", temp); @@ -252,14 +272,14 @@ int main(int argc, char *argv[]) fgets(temp, PATH_MAX-1, fp); fclose(fp); parent = xstrcpy(Basename(temp)); +#endif if (strcmp((char *)"-mbnewusr", parent)) { - fprintf(stderr, "mbpasswd: illegal parent\n"); + fprintf(stderr, "mbuseradd: illegal parent\n"); free(temp); free(parent); exit(1); } -#endif memset(args, 0, sizeof(args));