aboutsummaryrefslogtreecommitdiff
path: root/riemann.fmi.uni-sofia.bg/programs/nlogin-0.3-pre/utmp_io.c
blob: ab0a6bc4ea7a3f7a473466d04f81d8e873f408bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <unistd.h>
#include <fcntl.h>
#include <utmp.h> /*EXTRACT_UNMOD*/
#include "utmp_defs.h"

#define UTMP_SIZE (sizeof(struct utmp)) /*EXTRACT_UNMOD*/

/* type:  F_RDLCK or F_WRLCK */
struct utmp *utmp_io(int fd, struct utmp *ut, int type) /*EXTRACT_INCL*/ {
  int ret;
  struct flock fl;
  int (*op)() = (type==F_WRLCK) ? (int(*)())write : (int(*)())read;

  fl.l_whence	= SEEK_CUR;
  fl.l_start	= 0;
  fl.l_len	= UTMP_SIZE;
  fl.l_pid	= 0;
  fl.l_type	= type;
  
  if (fcntl(fd, F_SETLKW, &fl)) return 0;
  ret = SAFE_IO(op, fd, ut, UTMP_SIZE);

  fl.l_start	= -UTMP_SIZE;
  fl.l_type	= F_UNLCK;

  fcntl(fd, F_SETLK, &fl);

  if (ret != UTMP_SIZE) return 0;
  return ut;
}