]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libpkg/pkgwrap.c
- Take libinstall.a out of pkg_install and make it a proper shared library.
[FreeBSD/FreeBSD.git] / lib / libpkg / 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  */
18
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
21
22 #include "pkg.h"
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 extern char **environ;
29
30 void
31 pkg_wrap(long curver, char **argv)
32 {
33     FILE* f;
34     char ver[9];                        /* Format is: 'YYYYMMDD\0' */
35     char buffer[FILENAME_MAX+10];       /* Format is: 'YYYYMMDD <path>' */
36     char cmd[FILENAME_MAX+5];           /* Format is: '<path> -PPq' */
37     char *path, *cp;
38     long ptver, lpver;
39
40     if (getenv("PKG_NOWRAP") != NULL)
41         goto nowrap;
42
43     setenv("PKG_NOWRAP", "1", 1);
44
45     /* Get alternative location for package tools. */
46     if ((f = fopen(PKG_WRAPCONF_FNAME, "r")) == NULL) {
47         goto nowrap;
48     } else {
49         if (get_string(buffer, FILENAME_MAX+9, f) == NULL) {
50             goto nowrap;
51         } else {
52             if ((path = strrchr(buffer, ' ')) == NULL) {
53                 goto nowrap;
54             } else {
55                 *path++ = '\0';
56             }
57         }
58     }
59
60     if ((cp = strrchr(argv[0], '/')) == NULL) {
61         cp = argv[0];
62     } else {
63         cp++;
64     }
65
66     /* Get version of the other pkg_install and libpkg */
67     snprintf(cmd, FILENAME_MAX+10, "%s/%s -PPq", path, cp);
68     if ((f = popen(cmd, "r")) == NULL) {
69         perror("popen()");
70         goto nowrap;
71     } else {
72         if (get_string(ver, 9, f) == NULL)
73             goto nowrap; 
74         else
75             ptver = strtol(ver, NULL, 10);
76         if (get_string(ver, 9, f) == NULL)
77             goto nowrap;
78         else
79             lpver = strtol(ver, NULL, 10);
80         pclose(f);
81     }
82
83     if ((lpver >= LIBPKG_VERSION) && (ptver > curver)) {
84         snprintf(cmd, FILENAME_MAX, "%s/%s", path, cp);
85         execve(cmd, argv, environ);
86     }
87
88 nowrap:
89     unsetenv("PKG_NOWRAP");
90 }