2 * krishna balasubramanian 1993
4 * 1999-02-22 Arkadiusz Miç§iewicz <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>
24 /* 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]);
72 ret
= semctl (id
, 0, IPC_RMID
, arg
);
78 ret
= msgctl (id
, IPC_RMID
, NULL
);
83 ret
= shmctl (id
, IPC_RMID
, NULL
);
88 printf (_("cannot remove id %s (%s)\n"),
89 argv
[0], strerror(errno
));
100 static void deprecate_display_usage(void)
103 printf (_("deprecated usage: %s {shm | msg | sem} id ...\n"),
107 static int deprecated_main(int argc
, char **argv
)
112 deprecate_display_usage();
116 if (!strcmp(argv
[1], "shm")) {
117 if (remove_ids(SHM
, argc
-2, &argv
[2]))
120 else if (!strcmp(argv
[1], "msg")) {
121 if (remove_ids(MSG
, argc
-2, &argv
[2]))
124 else if (!strcmp(argv
[1], "sem")) {
125 if (remove_ids(SEM
, argc
-2, &argv
[2]))
129 deprecate_display_usage();
130 printf (_("unknown resource type: %s\n"), argv
[1]);
134 printf (_("resource(s) deleted\n"));
139 /* print the new usage */
141 usage(char *progname
)
144 _("usage: %s [ [-q msqid] [-m shmid] [-s semid]\n"
145 " [-Q msgkey] [-M shmkey] [-S semkey] ... ]\n"),
149 int main(int argc
, char **argv
)
153 char *prog
= argv
[0];
155 /* if the command is executed without parameters, do nothing */
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 */
200 id
= ((c
== 'q') ? msgget(key
, 0) :
201 (c
== 'm') ? shmget(key
, 0, 0) :
204 id
= (c
== 'm') ? shmget(key
, 0, 0) :
205 ((c
== 's') ? semget(key
, 0, 0) : -1);
212 errmsg
= _("permission denied for key");
215 errmsg
= _("already removed key");
218 errmsg
= _("invalid key");
221 errmsg
= _("unknown error in key");
224 fprintf(stderr
, "%s: %s (%s)\n",
225 prog
, errmsg
, optarg
);
229 /* ids are in decimal */
230 id
= strtoul(optarg
, NULL
, 10);
234 result
= ((c
== 'q') ? msgctl(id
, IPC_RMID
, NULL
) :
235 (c
== 'm') ? shmctl(id
, IPC_RMID
, NULL
) :
236 semctl(id
, 0, IPC_RMID
, arg
));
238 result
= (c
== 'm') ? shmctl(id
, IPC_RMID
, NULL
) :
239 ((c
== 's') ? semctl(id
, 0, IPC_RMID
) : -1);
248 ? _("permission denied for key")
249 : _("permission denied for id");
258 ? _("already removed key")
259 : _("already removed id");
263 ? _("unknown error in key")
264 : _("unknown error in id");
267 fprintf(stderr
, _("%s: %s (%s)\n"),
268 prog
, errmsg
, optarg
);
273 /* print usage if we still have some arguments left over */
274 if (optind
!= argc
) {
275 fprintf(stderr
, _("%s: unknown argument: %s\n"),
280 /* exit value reflects the number of errors encountered */