This was originally written sometime in the dark and backward abysm of
time back in the 1980s.
+If you are on a 32-but machine or using a compiler where you can't count
+on a memory address being the same size as a long int, see the WARNING
+comments in the code.
+
The latest version of this code lives at <http://www.catb.org/~esr/semex/>.
The MIT/X license applies.
nsems = 1;
perms = 0660; /* read & alter by owning user & group */
semflg = 0;
-
- sc = sscanf(cmdline, "c %ld %d %s %o", &key,&nsems,strbuf,&perms);
+ long int wordlenhack;
+
+ /*
+ * WARNING: wordlenhack should avoid trouble if the width of the
+ * key_t typedef happens not to be long int, but it is possible
+ * to imagine a compiler screwing this up (especially on remnant
+ * 32-bit machines).
+ */
+ sc = sscanf(cmdline, "c %ld %d %s %o",
+ &wordlenhack, &nsems, strbuf, &perms);
+ key = wordlenhack;
if (sc >= 3)
{
semid = semget(key, nsems, semflg);
if (tty)
{
- (void) printf("semget(%ld, %d, %04o) ", key, nsems, semflg);
+ /*
+ * WARNING: The long int cast could result in some
+ * perverse output if key is not long int; it's
+ * probably actually a memory address making the
+ * identity likely on modern compilers. But it is
+ * possible to imagine a compiler screwing this up
+ * (especially on remnant 32-bit machines)
+ */
+ (void) printf("semget(%ld, %d, %04o) ", (long int)key, nsems, semflg);
if (semid == -1)
(void) printf("failed: %s\n", strerror(errno));
else