]> git.ipfire.org Git - thirdparty/util-linux.git/blame - login-utils/nologin.c
whereis: use xstrncpy()
[thirdparty/util-linux.git] / login-utils / nologin.c
CommitLineData
88407b93
KZ
1/*
2 * Copyright (C) 2013 Karel Zak <kzak@redhat.com>
3 */
4
5#include <stdio.h>
bd299782 6#include <sys/stat.h>
88407b93
KZ
7#include <sys/types.h>
8#include <fcntl.h>
9#include <string.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <getopt.h>
13
14#include "c.h"
15#include "nls.h"
16#include "pathnames.h"
17
18/*
19 * Always return EXIT_FAILURE (1), don't try to be smart!
20 */
21
86be6a32 22static void __attribute__((__noreturn__)) usage(void)
88407b93 23{
86be6a32 24 FILE *out = stdout;
88407b93 25 fputs(USAGE_HEADER, out);
88407b93
KZ
26 fprintf(out,
27 _(" %s [options]\n"), program_invocation_short_name);
28
451dbcfa
BS
29 fputs(USAGE_SEPARATOR, out);
30 fputs(_("Politely refuse a login.\n"), out);
31
88407b93 32 fputs(USAGE_OPTIONS, out);
f45f3ec3 33 printf(USAGE_HELP_OPTIONS(16));
88407b93 34
f45f3ec3 35 printf(USAGE_MAN_TAIL("nologin(8)"));
88407b93
KZ
36 exit(EXIT_FAILURE);
37}
38
39int main(int argc, char *argv[])
40{
dd732fa2 41 int c, fd = -1;
bd299782 42 struct stat st;
88407b93 43 static const struct option longopts[] = {
87918040
SK
44 { "help", 0, NULL, 'h' },
45 { "version", 0, NULL, 'V' },
46 { NULL, 0, NULL, 0 }
88407b93
KZ
47 };
48
49 setlocale(LC_ALL, "");
50 bindtextdomain(PACKAGE, LOCALEDIR);
51 textdomain(PACKAGE);
52
53 while ((c = getopt_long(argc, argv, "hV", longopts, NULL)) != -1) {
54 switch (c) {
55 case 'h':
86be6a32 56 usage();
88407b93 57 case 'V':
2c308875 58 print_version(EXIT_FAILURE); /* yes FAILURE! */
88407b93 59 default:
677ec86c 60 errtryhelp(EXIT_FAILURE);
88407b93
KZ
61 }
62 }
63
64 fd = open(_PATH_NOLOGIN_TXT, O_RDONLY);
dd732fa2
KZ
65 if (fd < 0)
66 goto dflt;
67
bd299782 68 c = fstat(fd, &st);
dd732fa2
KZ
69 if (c < 0 || !S_ISREG(st.st_mode))
70 goto dflt;
71 else {
88407b93
KZ
72 char buf[BUFSIZ];
73 ssize_t rd;
74
75 while ((rd = read(fd, buf, sizeof(buf))) > 0)
76 ignore_result( write(STDOUT_FILENO, buf, rd) );
dd732fa2 77
88407b93 78 close(fd);
dd732fa2
KZ
79 return EXIT_FAILURE;
80 }
88407b93 81
dd732fa2
KZ
82dflt:
83 if (fd >= 0)
84 close(fd);
85 fprintf(stdout, _("This account is currently not available.\n"));
88407b93
KZ
86 return EXIT_FAILURE;
87}