3 * locking routines as modified by Petri Kutvonen
10 #if (FILOCK && BSD) || SVR4
11 #include <sys/types.h>
26 #if defined(SVR4) && ! defined(__linux__)
27 #include <sys/systeminfo.h>
29 int gethostname(char *name
, int namelen
)
31 return sysinfo(SI_HOSTNAME
, name
, namelen
);
37 /**********************
39 * if successful, returns NULL
40 * if file locked, returns username of person locking the file
41 * if other error, returns "LOCK ERROR: explanation"
43 *********************/
44 char *dolock(char *fname
)
47 static char lname
[MAXLOCK
], locker
[MAXNAME
+ 1];
51 strcat(strcpy(lname
, fname
), ".lock~");
53 /* check that we are not being cheated, qname must point to */
54 /* a regular file - even this code leaves a small window of */
55 /* vulnerability but it is rather hard to exploit it */
58 if (lstat(lname
, &sbuf
) == 0)
60 if (stat(lname
, &sbuf
) == 0)
63 if (!S_ISREG(sbuf
.st_mode
))
65 if (!(((sbuf
.st_mode
) & 070000) == 0)) /* SysV R2 */
67 return "LOCK ERROR: not a regular file";
70 fd
= open(lname
, O_RDWR
| O_CREAT
, 0666);
79 return "LOCK ERROR: cannot access lock file";
81 if ((n
= read(fd
, locker
, MAXNAME
)) < 1) {
82 lseek(fd
, 0, SEEK_SET
);
83 /* strcpy(locker, getlogin()); */
85 strcat(locker
+ strlen(locker
), "@");
86 gethostname(locker
+ strlen(locker
), 64);
87 write(fd
, locker
, strlen(locker
));
91 locker
[n
> MAXNAME
? MAXNAME
: n
] = 0;
96 /*********************
98 * undolock -- unlock the file fname
100 * if successful, returns NULL
101 * if other error, returns "LOCK ERROR: explanation"
103 *********************/
105 char *undolock(char *fname
)
107 static char lname
[MAXLOCK
];
109 strcat(strcpy(lname
, fname
), ".lock~");
110 if (unlink(lname
) != 0) {
111 if (errno
== EACCES
|| errno
== ENOENT
)
117 return "LOCK ERROR: cannot remove lock file";