]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pkg_install/delete/main.c
This commit was generated by cvs2svn to compensate for changes in r170754,
[FreeBSD/FreeBSD.git] / usr.sbin / pkg_install / delete / main.c
1 /*
2  *
3  * FreeBSD install - a package for the installation and maintainance
4  * of non-core utilities.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * Jordan K. Hubbard
16  * 18 July 1993
17  *
18  * This is the delete module.
19  *
20  */
21
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <err.h>
28 #include "lib.h"
29 #include "delete.h"
30
31 static char Options[] = "adDfGhinp:rvxX";
32
33 char    *Prefix         = NULL;
34 Boolean CleanDirs       = FALSE;
35 Boolean Interactive     = FALSE;
36 Boolean NoDeInstall     = FALSE;
37 Boolean Recursive       = FALSE;
38 match_t MatchType       = MATCH_GLOB;
39
40 static void usage __P((void));
41
42 int
43 main(int argc, char **argv)
44 {
45     int ch, error;
46     char **pkgs, **start;
47     char *pkgs_split;
48     const char *tmp;
49     struct stat stat_s;
50
51     pkgs = start = argv;
52     while ((ch = getopt(argc, argv, Options)) != -1)
53         switch(ch) {
54         case 'v':
55             Verbose++;
56             break;
57
58         case 'f':
59             Force = TRUE;
60             break;
61
62         case 'p':
63             Prefix = optarg;
64             break;
65
66         case 'D':
67             NoDeInstall = TRUE;
68             break;
69
70         case 'd':
71             CleanDirs = TRUE;
72             break;
73
74         case 'n':
75             Fake = TRUE;
76             Verbose = TRUE;
77             break;
78
79         case 'a':
80             MatchType = MATCH_ALL;
81             break;
82
83         case 'G':
84             MatchType = MATCH_EXACT;
85             break;
86
87         case 'x':
88             MatchType = MATCH_REGEX;
89             break;
90
91         case 'X':
92             MatchType = MATCH_EREGEX;
93             break;
94
95         case 'i':
96             Interactive = TRUE;
97             break;
98
99         case 'r':
100             Recursive = TRUE;
101             break;
102
103         case 'h':
104         case '?':
105         default:
106             usage();
107             break;
108         }
109
110     argc -= optind;
111     argv += optind;
112
113     /* Get all the remaining package names, if any */
114     while (*argv) {
115         /* Don't try to apply heuristics if arguments are regexs */
116         if (MatchType != MATCH_REGEX)
117             while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) {
118                 *pkgs_split++ = '\0';
119                 /*
120                  * If character after the '/' is alphanumeric, then we've found the
121                  * package name.  Otherwise we've come across a trailing '/' and
122                  * need to continue our quest.
123                  */
124                 if (isalnum(*pkgs_split) || ((MatchType == MATCH_GLOB) && \
125                     strpbrk(pkgs_split, "*?[]") != NULL)) {
126                     *argv = pkgs_split;
127                     break;
128                 }
129             }
130         *pkgs++ = *argv++;
131     }
132
133     /* If no packages, yelp */
134     if (pkgs == start && MatchType != MATCH_ALL)
135         warnx("missing package name(s)"), usage();
136     *pkgs = NULL;
137     tmp = LOG_DIR;
138     (void) stat(tmp, &stat_s);
139     if (!Fake && getuid() && geteuid() != stat_s.st_uid) {
140         if (!Force)
141             errx(1, "you do not own %s, use -f to force", tmp);
142         else
143             warnx("you do not own %s (proceeding anyways)", tmp);
144     }
145     if ((error = pkg_perform(start)) != 0) {
146         if (Verbose)
147             warnx("%d package deletion(s) failed", error);
148         return error;
149     }
150     else
151         return 0;
152 }
153
154 static void
155 usage()
156 {
157     fprintf(stderr, "%s\n%s\n",
158         "usage: pkg_delete [-dDfGinrvxX] [-p prefix] pkg-name ...",
159         "       pkg_delete -a [flags]");
160     exit(1);
161 }