1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <libintl.h>
25 #include <locale.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stddef.h>
29 #include <sys/types.h>
30
31 #include "projent.h"
32 #include "util.h"
33
34 #define SEQU(str1, str2) (strcmp(str1, str2) == 0)
35
36 /*
37 * Print usage
38 */
39 static void
40 usage(void)
41 {
42 (void) fprintf(stderr, gettext(
43 "Usage:\n"
44 "projdel [-f filename] project\n"));
45 }
46
47 /*
48 * main()
49 */
50 int
51 main(int argc, char **argv)
52 {
53 int e, c, ret = 0;
54 int flags;
55 extern char *optarg;
56 extern int optind, optopt;
57 lst_t *plst; /* Projects list */
58 projent_t *ent, *delent;
59 int del;
60 char *pname; /* Project name */
61 lst_t errlst; /* Errors list */
62 char *projfile = PROJF_PATH; /* Project file "/etc/project" */
63
64 lst_create(&errlst);
65
66
67 (void) setlocale(LC_ALL, "");
68 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
69 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */
70 #endif
71 (void) textdomain(TEXT_DOMAIN);
72
73 /* Parse the command line argument list */
74 while ((c = getopt(argc, argv, ":hf:")) != EOF)
75 switch (c) {
76 case 'h':
77 usage();
78 exit(0);
79 break;
80 case 'f':
81 projfile = optarg;
82 break;
83 default:
84 util_add_errmsg(&errlst, gettext(
85 "Invalid option: -%c"), optopt);
86 break;
87 }
88
89 if (optind != argc -1) {
90 (void) fprintf(stderr, gettext("No project name specified\n"));
91 exit(2);
92 }
93
94 /* Name of the project to delete */
95 pname = argv[optind];
96
97 flags = F_PAR_VLD | F_PAR_RES | F_PAR_DUP;
98
99 /* Parse the project file to get the list of the projects */
100 plst = projent_get_lst(projfile, flags, &errlst);
101
102 if (!lst_is_empty(&errlst)) {
103 util_print_errmsgs(&errlst);
104 usage();
105 exit(2);
106 }
107
108 /* Find the project to be deleted */
109 del = 0;
110 for (e = 0; e < lst_size(plst); e++) {
111 ent = lst_at(plst, e);
112 if (SEQU(ent->projname, pname)) {
113 del++;
114 delent = ent;
115 }
116 }
117
118 if (del == 0) {
119 (void) fprintf(stderr, gettext(
120 "Project \"%s\" does not exist\n"), pname);
121 usage();
122 ret = 2;
123 goto out;
124 } else if (del > 1) {
125 (void) fprintf(stderr, gettext(
126 "Duplicate project name \"%s\""), pname);
127 usage();
128 ret = 2;
129 goto out;
130 }
131
132 /* Remove the project entry from the list */
133 (void) lst_remove(plst, delent);
134
135 /* Write out the project file */
136 projent_put_lst(projfile, plst, &errlst);
137
138 if (!lst_is_empty(&errlst)) {
139 util_print_errmsgs(&errlst);
140 usage();
141 ret = 2;
142 }
143 out:
144 projent_free_lst(plst);
145 free(plst);
146 return (ret);
147 }