Add pkgwrap.c, which was missed in my last commit.
[dragonfly.git] / usr.sbin / pkg_install / lib / pkgwrap.c
1 /*
2  * FreeBSD install - a package for the installation and maintenance
3  * of non-core utilities.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * Maxim Sobolev
15  * 8 September 2002
16  *
17  * $FreeBSD: /repoman/r/ncvs/src/usr.sbin/pkg_install/lib/pkgwrap.c,v 1.1.6.1 2003/08/11 09:22:04 kris Exp $
18  * $DragonFly: src/usr.sbin/pkg_install/lib/Attic/pkgwrap.c,v 1.1 2003/11/19 15:19:45 hmp Exp $
19  */
20
21 #include "lib.h"
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #undef main
28
29 #define SEPARATORS " \t"
30
31 extern char **environ;
32
33 int
34 main(int argc, char **argv)
35 {
36     FILE *f;
37     char buffer[FILENAME_MAX], *cp, *verstr;
38     int len;
39
40     if (getenv("PKG_NOWRAP") != NULL)
41         goto nowrap;
42     f = fopen(PKG_WRAPCONF_FNAME, "r");
43     if (f == NULL)
44         goto nowrap;
45     cp = fgets(buffer, 256, f);
46     fclose(f);
47     if (cp == NULL)
48         goto nowrap;
49     len = strlen(cp);
50     if (cp[len - 1] == '\n')
51         cp[len - 1] = '\0';
52     while (strchr(SEPARATORS, *cp) != NULL)
53         cp++;
54     verstr = cp;
55     cp = strpbrk(cp, SEPARATORS);
56     if (cp == NULL)
57         goto nowrap;
58     *cp = '\0';
59     for (cp = verstr; *cp != '\0'; cp++)
60         if (isdigit(*cp) == 0)
61             goto nowrap;
62     if (atoi(verstr) < PKG_INSTALL_VERSION)
63         goto nowrap;
64     cp++;
65     while (*cp != '\0' && strchr(SEPARATORS, *cp) != NULL)
66         cp++;
67     if (*cp == '\0')
68         goto nowrap;
69     bcopy(cp, buffer, strlen(cp) + 1);
70     cp = strpbrk(buffer, SEPARATORS);
71     if (cp != NULL)
72         *cp = '\0';
73     if (!isdir(buffer))
74         goto nowrap;
75     cp = strrchr(argv[0], '/');
76     if (cp == NULL)
77         cp = argv[0];
78     else
79         cp++;
80     strlcat(buffer, "/", sizeof(buffer));
81     strlcat(buffer, cp, sizeof(buffer));
82     setenv("PKG_NOWRAP", "1", 1);
83     execve(buffer, argv, environ);
84
85 nowrap:
86     unsetenv("PKG_NOWRAP");
87     return(real_main(argc, argv));
88 }