]> git.saurik.com Git - apple/shell_cmds.git/blobdiff - shlock/shlock.c
shell_cmds-162.tar.gz
[apple/shell_cmds.git] / shlock / shlock.c
index 862e4b74d4c110a6028ca09eb0efe80dd6a580af..9dcb645154194b042cb1821445376243e404c3fa 100644 (file)
@@ -1,4 +1,33 @@
-/*     $NetBSD: shlock.c,v 1.3 1998/01/09 08:06:11 perry Exp $ */
+/*     $NetBSD: shlock.c,v 1.10 2008/04/28 20:24:14 martin Exp $       */
+
+/*-
+ * Copyright (c) 2006 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Erik E. Fair.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
 
 /*
 ** Program to produce reliable locks for shell scripts.
 ** Erik E. Fair <fair@clock.org>, May 20, 1997
 */
 
+#include <sys/cdefs.h>
+
+#ifndef lint
+__RCSID("$NetBSD: shlock.c,v 1.10 2008/04/28 20:24:14 martin Exp $");
+#endif
+
 #include <sys/types.h>
 #include <sys/file.h>
 #include <fcntl.h>                     /* Needed on hpux */
@@ -55,9 +90,9 @@
 
 int    Debug = FALSE;
 char   *Pname;
-char   *USAGE = "%s: USAGE: shlock -f file -p pid [-d][-u]\n";
-char   *E_unlk = "%s: unlink(%s): %s\n";
-char   *E_open = "%s: open(%s): %s\n";
+const char USAGE[] = "%s: USAGE: %s [-du] [-p PID] -f file\n";
+const char E_unlk[] = "%s: unlink(%s): %s\n";
+const char E_open[] = "%s: open(%s): %s\n";
 
 #define        dprintf if (Debug) printf
 
@@ -67,17 +102,13 @@ char       *E_open = "%s: open(%s): %s\n";
 ** (and wasn't that sufficient?)
 */
 
-#ifdef __STDC__
 /* the following is in case you need to make the prototypes go away. */
-#define _P(x)  x
-
-char   *xtmpfile _P((char *, pid_t, int));
-int    p_exists _P((pid_t));
-int    cklock _P((char *, int));
-int    mklock _P((char *, pid_t, int));
-void   bad_usage _P((void));
-int    main _P((int, char **));
-#endif /* __STDC__ */
+char   *xtmpfile(char *, pid_t, int);
+int    p_exists(pid_t);
+int    cklock(char *, int);
+int    mklock(char *, pid_t, int);
+void   bad_usage(void);
+int    main(int, char **);
 
 /*
 ** Create a temporary file, all ready to lock with.
@@ -86,17 +117,14 @@ int        main _P((int, char **));
 ** which might not be in the same filesystem.
 */
 char *
-xtmpfile(file, pid, uucpstyle)
-char   *file;
-pid_t  pid;
-int    uucpstyle;
+xtmpfile(char *file, pid_t pid, int uucpstyle)
 {
        int     fd;
        int     len;
        char    *cp, buf[BUFSIZ];
        static char     tempname[BUFSIZ];
 
-       sprintf(buf, "shlock%d", getpid());
+       sprintf(buf, "shlock%ld", (u_long)getpid());
        if ((cp = strrchr(strcpy(tempname, file), '/')) != (char *)NULL) {
                *++cp = '\0';
                (void) strcat(tempname, buf);
@@ -104,7 +132,7 @@ int uucpstyle;
                (void) strcpy(tempname, buf);
        dprintf("%s: temporary filename: %s\n", Pname, tempname);
 
-       sprintf(buf, "%d\n", pid);
+       sprintf(buf, "%ld\n", (u_long)pid);
        len = strlen(buf);
 openloop:
        if ((fd = open(tempname, O_RDWR|O_CREAT|O_EXCL, 0644)) < 0) {
@@ -137,8 +165,8 @@ openloop:
                (write(fd, &pid, sizeof(pid)) != sizeof(pid)) :
                (write(fd, buf, len) < 0))
        {
-               fprintf(stderr, "%s: write(%s,%d): %s\n",
-                       Pname, tempname, pid, strerror(errno));
+               fprintf(stderr, "%s: write(%s,%ld): %s\n",
+                       Pname, tempname, (u_long)pid, strerror(errno));
                (void) close(fd);
                if (unlink(tempname) < 0) {
                        fprintf(stderr, E_unlk,
@@ -155,10 +183,9 @@ openloop:
 ** Send null signal to find out.
 */
 int
-p_exists(pid)
-pid_t  pid;
+p_exists(pid_t pid)
 {
-       dprintf("%s: process %d is ", Pname, pid);
+       dprintf("%s: process %ld is ", Pname, (u_long)pid);
        if (pid <= 0) {
                dprintf("invalid\n");
                return(FALSE);
@@ -196,9 +223,7 @@ pid_t       pid;
 **
 */
 int
-cklock(file, uucpstyle)
-char   *file;
-int    uucpstyle;
+cklock(char *file, int uucpstyle)
 {
        int     fd = open(file, O_RDONLY);
        ssize_t len;
@@ -226,15 +251,13 @@ int       uucpstyle;
 }
 
 int
-mklock(file, pid, uucpstyle)
-char   *file;
-pid_t  pid;
-int    uucpstyle;
+mklock(char *file, pid_t pid, int uucpstyle)
 {
        char    *tmp;
        int     retcode = FALSE;
 
-       dprintf("%s: trying lock <%s> for process %d\n", Pname, file, pid);
+       dprintf("%s: trying lock <%s> for process %ld\n", Pname, file,
+           (u_long)pid);
        if ((tmp = xtmpfile(file, pid, uucpstyle)) == (char *)NULL)
                return(FALSE);
 
@@ -276,16 +299,14 @@ linkloop:
 }
 
 void
-bad_usage()
+bad_usage(void)
 {
-       fprintf(stderr, USAGE, Pname);
+       fprintf(stderr, USAGE, Pname, Pname);
        exit(LOCK_FAIL);
 }
 
 int
-main(ac, av)
-int    ac;
-char   *av[];
+main(int ac, char **av)
 {
        int     x;
        char    *file = (char *)NULL;
@@ -326,8 +347,7 @@ char        *av[];
                                }
                                break;
                        default:
-                               fprintf(stderr, USAGE, Pname);
-                               exit(LOCK_FAIL);
+                               bad_usage();
                        }
                }
        }