Prepare to eliminate multiply uucplock.c copies

This commit is contained in:
Andrey A. Chernov 1996-03-04 10:35:45 +00:00
parent 8a02c104f9
commit 70a41bdd99
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=14368

View File

@ -32,14 +32,16 @@
*/
#ifndef lint
static char sccsid[] = "@(#)uucplock.c 8.1 (Berkeley) 6/6/93";
static const char sccsid[] = "@(#)uucplock.c 8.1 (Berkeley) 6/6/93";
#endif /* not lint */
#include <sys/types.h>
#include <sys/file.h>
#include <sys/dir.h>
#include <errno.h>
#ifndef USE_PERROR
#include <syslog.h>
#endif
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
@ -62,7 +64,7 @@ int uu_lock (char *ttyname)
{
int fd;
pid_t pid;
char tbuf[MAXNAMLEN];
char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
(void)sprintf(tbuf, _PATH_UUCPLOCK LOCKFMT, ttyname);
fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660);
@ -73,11 +75,19 @@ int uu_lock (char *ttyname)
*/
fd = open(tbuf, O_RDWR, 0);
if (fd < 0) {
#ifndef USE_PERROR
syslog(LOG_ERR, "lock open: %m");
#else
perror("lock open");
#endif
return(-1);
}
if ((pid = get_pid (fd)) == -1) {
#ifndef USE_PERROR
syslog(LOG_ERR, "lock read: %m");
#else
perror("lock read");
#endif
(void)close(fd);
return(-1);
}
@ -90,8 +100,12 @@ int uu_lock (char *ttyname)
* The process that locked the file isn't running, so
* we'll lock it ourselves
*/
if (lseek(fd, 0L, L_SET) < 0) {
if (lseek(fd, (off_t) 0, L_SET) < 0) {
#ifndef USE_PERROR
syslog(LOG_ERR, "lock lseek: %m");
#else
perror("lock lseek");
#endif
(void)close(fd);
return(-1);
}
@ -99,7 +113,11 @@ int uu_lock (char *ttyname)
}
pid = getpid();
if (!put_pid (fd, pid)) {
#ifndef USE_PERROR
syslog(LOG_ERR, "lock write: %m");
#else
perror("lock write");
#endif
(void)close(fd);
(void)unlink(tbuf);
return(-1);
@ -110,7 +128,7 @@ int uu_lock (char *ttyname)
int uu_unlock (char *ttyname)
{
char tbuf[MAXNAMLEN];
char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
(void)sprintf(tbuf, _PATH_UUCPLOCK LOCKFMT, ttyname);
return(unlink(tbuf));
@ -118,27 +136,26 @@ int uu_unlock (char *ttyname)
static int put_pid (int fd, pid_t pid)
{
char buf [32];
char buf[32];
int len;
len = sprintf (buf, "%10ld\n", pid);
return write (fd, buf, len) == len;
len = sprintf (buf, "%10ld\n", pid);
return write (fd, buf, len) == len;
}
static pid_t get_pid (int fd)
{
int bytes_read;
char buf [32];
char buf[32];
pid_t pid;
bytes_read = read (fd, buf, sizeof (buf) - 1);
if (bytes_read > 0) {
buf [bytes_read] = '\0';
pid = strtol (buf, (char **) NULL, 10);
}
else
pid = -1;
return pid;
bytes_read = read (fd, buf, sizeof (buf) - 1);
if (bytes_read > 0) {
buf[bytes_read] = '\0';
pid = strtol (buf, (char **) NULL, 10);
} else
pid = -1;
return pid;
}
/* end of uucplock.c */