Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / pkg_install / delete / main.c
1 /*
2  * FreeBSD install - a package for the installation and maintainance
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  * Jordan K. Hubbard
15  * 18 July 1993
16  *
17  * This is the delete module.
18  *
19  * $FreeBSD: src/usr.sbin/pkg_install/delete/main.c,v 1.17.2.8 2002/08/20 06:35:07 obrien Exp $
20  * $DragonFly: src/usr.sbin/pkg_install/delete/Attic/main.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
21  */
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <err.h>
26 #include "lib.h"
27 #include "delete.h"
28
29 static char Options[] = "adDfGhinp:rvx";
30
31 char    *Prefix         = NULL;
32 Boolean CleanDirs       = FALSE;
33 Boolean Interactive     = FALSE;
34 Boolean NoDeInstall     = FALSE;
35 Boolean Recursive       = FALSE;
36 match_t MatchType       = MATCH_GLOB;
37
38 static void usage __P((void));
39
40 int
41 main(int argc, char **argv)
42 {
43     int ch, error;
44     char **pkgs, **start;
45     char *pkgs_split;
46     const char *tmp;
47     struct stat stat_s;
48
49     pkgs = start = argv;
50     while ((ch = getopt(argc, argv, Options)) != -1)
51         switch(ch) {
52         case 'v':
53             Verbose = TRUE;
54             break;
55
56         case 'f':
57             Force = TRUE;
58             break;
59
60         case 'p':
61             Prefix = optarg;
62             break;
63
64         case 'D':
65             NoDeInstall = TRUE;
66             break;
67
68         case 'd':
69             CleanDirs = TRUE;
70             break;
71
72         case 'n':
73             Fake = TRUE;
74             Verbose = TRUE;
75             break;
76
77         case 'a':
78             MatchType = MATCH_ALL;
79             break;
80
81         case 'G':
82             MatchType = MATCH_EXACT;
83             break;
84
85         case 'x':
86             MatchType = MATCH_REGEX;
87             break;
88
89         case 'i':
90             Interactive = TRUE;
91             break;
92
93         case 'r':
94             Recursive = TRUE;
95             break;
96
97         case 'h':
98         case '?':
99         default:
100             usage();
101             break;
102         }
103
104     argc -= optind;
105     argv += optind;
106
107     /* Get all the remaining package names, if any */
108     while (*argv) {
109         /* Don't try to apply heuristics if arguments are regexs */
110         if (MatchType != MATCH_REGEX)
111             while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) {
112                 *pkgs_split++ = '\0';
113                 /*
114                  * If character after the '/' is alphanumeric, then we've found the
115                  * package name.  Otherwise we've come across a trailing '/' and
116                  * need to continue our quest.
117                  */
118                 if (isalpha(*pkgs_split) || ((MatchType == MATCH_GLOB) && \
119                     strpbrk(pkgs_split, "*?[]") != NULL)) {
120                     *argv = pkgs_split;
121                     break;
122                 }
123             }
124         *pkgs++ = *argv++;
125     }
126
127     /* If no packages, yelp */
128     if (pkgs == start && MatchType != MATCH_ALL)
129         warnx("missing package name(s)"), usage();
130     *pkgs = NULL;
131     tmp = LOG_DIR;
132     (void) stat(tmp, &stat_s);
133     if (!Fake && getuid() && geteuid() != stat_s.st_uid) {
134         if (!Force)
135             errx(1, "you do not own %s, use -f to force", tmp);
136         else
137             warnx("you do not own %s (proceeding anyways)", tmp);
138     }
139     if ((error = pkg_perform(start)) != 0) {
140         if (Verbose)
141             warnx("%d package deletion(s) failed", error);
142         return error;
143     }
144     else
145         return 0;
146 }
147
148 static void
149 usage()
150 {
151     fprintf(stderr, "%s\n%s\n",
152         "usage: pkg_delete [-dDfGinrvx] [-p prefix] pkg-name ...",
153         "       pkg_delete -a [flags]");
154     exit(1);
155 }