2 * krishna balasubramanian 1993
4 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
5 * - added Native Language Support
7 * 1999-04-02 frank zago
8 * - can now remove several id's in the same call
17 #include <sys/types.h>
26 /* for tolower and isupper */
29 #if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
30 /* union semun is defined by including <sys/sem.h> */
32 /* according to X/OPEN we have to define it ourselves */
36 unsigned short int *array;
37 struct seminfo *__buf;
41 static void usage(char *);
45 typedef enum type_id {
52 remove_ids(type_id type, int argc, char **argv) {
54 int ret = 0; /* for gcc */
63 id = strtoul(argv[0], &end, 10);
66 printf (_("invalid id: %s\n"), argv[0]);
71 ret = semctl (id, 0, IPC_RMID, arg);
75 ret = msgctl (id, IPC_RMID, NULL);
79 ret = shmctl (id, IPC_RMID, NULL);
84 printf (_("cannot remove id %s (%s)\n"),
85 argv[0], strerror(errno));
96 static void deprecate_display_usage(void)
99 printf (_("deprecated usage: %s {shm | msg | sem} id ...\n"),
103 static int deprecated_main(int argc, char **argv)
108 deprecate_display_usage();
112 if (!strcmp(argv[1], "shm")) {
113 if (remove_ids(SHM, argc-2, &argv[2]))
116 else if (!strcmp(argv[1], "msg")) {
117 if (remove_ids(MSG, argc-2, &argv[2]))
120 else if (!strcmp(argv[1], "sem")) {
121 if (remove_ids(SEM, argc-2, &argv[2]))
125 deprecate_display_usage();
126 printf (_("unknown resource type: %s\n"), argv[1]);
130 printf (_("resource(s) deleted\n"));
135 /* print the new usage */
137 usage(char *progname)
140 _("usage: %s [ [-q msqid] [-m shmid] [-s semid]\n"
141 " [-Q msgkey] [-M shmkey] [-S semkey] ... ]\n"),
145 int main(int argc, char **argv)
149 char *prog = argv[0];
151 /* if the command is executed without parameters, do nothing */
155 setlocale(LC_ALL, "");
156 bindtextdomain(PACKAGE, LOCALEDIR);
159 /* check to see if the command is being invoked in the old way if so
160 then run the old code */
161 if (strcmp(argv[1], "shm") == 0 ||
162 strcmp(argv[1], "msg") == 0 ||
163 strcmp(argv[1], "sem") == 0)
164 return deprecated_main(argc, argv);
166 /* process new syntax to conform with SYSV ipcrm */
167 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:")) != -1) {
170 int iskey = isupper(c);
172 /* needed to delete semaphores */
176 /* we don't need case information any more */
179 /* make sure the option is in range */
180 if (c != 'q' && c != 'm' && c != 's') {
181 fprintf(stderr, _("%s: illegal option -- %c\n"),
189 /* keys are in hex or decimal */
190 key_t key = strtoul(optarg, NULL, 0);
191 if (key == IPC_PRIVATE) {
193 fprintf(stderr, _("%s: illegal key (%s)\n"),
198 /* convert key to id */
199 id = ((c == 'q') ? msgget(key, 0) :
200 (c == 'm') ? shmget(key, 0, 0) :
208 errmsg = _("permission denied for key");
211 errmsg = _("already removed key");
214 errmsg = _("invalid key");
217 errmsg = _("unknown error in key");
220 fprintf(stderr, "%s: %s (%s)\n",
221 prog, errmsg, optarg);
225 /* ids are in decimal */
226 id = strtoul(optarg, NULL, 10);
229 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
230 (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
231 semctl(id, 0, IPC_RMID, arg));
240 ? _("permission denied for key")
241 : _("permission denied for id");
250 ? _("already removed key")
251 : _("already removed id");
255 ? _("unknown error in key")
256 : _("unknown error in id");
259 fprintf(stderr, _("%s: %s (%s)\n"),
260 prog, errmsg, optarg);
265 /* print usage if we still have some arguments left over */
266 if (optind != argc) {
267 fprintf(stderr, _("%s: unknown argument: %s\n"),
272 /* exit value reflects the number of errors encountered */