Initial commit to Gerrit
[profile/ivi/quota.git] / common.c
1 /*
2  *
3  *      Common things for all utilities
4  *
5  *      Jan Kara <jack@suse.cz> - sponsored by SuSE CR
6  *
7  *      Jani Jaakkola <jjaakkol@cs.helsinki.fi> - syslog support
8  */
9
10 #include "config.h"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <syslog.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19
20 #include "pot.h"
21 #include "common.h"
22
23 static int enable_syslog=0;
24
25 void use_syslog(void)
26 {
27         openlog(progname,0,LOG_DAEMON);
28         enable_syslog=1;
29 }
30
31 static void do_syslog(int level, const char *format, va_list args)
32 {
33         char buf[1024];
34         int i, j;
35         
36         vsnprintf(buf,sizeof(buf),format,args);
37         /* This while removes newlines from the log, so that
38          * syslog() will be called once for every line */
39         for (i = 0; buf[i]; i = j) {
40                 for (j = i; buf[j] && buf[j] != '\n'; j++);
41                 if (buf[j] == '\n')
42                         buf[j++] = '\0';
43                 syslog(level, "%s", buf + i);
44         }
45 }
46
47 void die(int ret, char *fmtstr, ...)
48 {
49         va_list args;
50
51         va_start(args, fmtstr);
52         if (enable_syslog) {
53                 do_syslog(LOG_CRIT, fmtstr, args);
54                 syslog(LOG_CRIT, "Exiting with status %d", ret);
55         } else {
56                 fprintf(stderr, "%s: ", progname);
57                 vfprintf(stderr, fmtstr, args);
58         }
59         va_end(args);
60         exit(ret);
61 }
62
63 void errstr(char *fmtstr, ...)
64 {
65         va_list args;
66
67         va_start(args, fmtstr);
68         if (enable_syslog)
69                 do_syslog(LOG_ERR, fmtstr, args);
70         else {
71                 fprintf(stderr, "%s: ", progname);
72                 vfprintf(stderr, fmtstr, args);
73         }
74         va_end(args);
75 }
76
77 void *smalloc(size_t size)
78 {
79         void *ret = malloc(size);
80
81         if (!ret) {
82                 fputs("Not enough memory.\n", stderr);
83                 exit(3);
84         }
85         return ret;
86 }
87
88 void *srealloc(void *ptr, size_t size)
89 {
90         void *ret = realloc(ptr, size);
91
92         if (!ret) {
93                 fputs("Not enough memory.\n", stderr);
94                 exit(3);
95         }
96         return ret;
97 }
98
99 void sstrncpy(char *d, const char *s, size_t len)
100 {
101         strncpy(d, s, len);
102         d[len - 1] = 0;
103 }
104
105 void sstrncat(char *d, const char *s, size_t len)
106 {
107         strncat(d, s, len);
108         d[len - 1] = 0;
109 }
110
111 char *sstrdup(const char *s)
112 {
113         char *r = strdup(s);
114
115         if (!r) {
116                 puts("Not enough memory.");
117                 exit(3);
118         }
119         return r;
120 }
121
122 void version(void)
123 {
124         printf(_("Quota utilities version %s.\n"), PACKAGE_VERSION);
125         printf(_("Compiled with:%s\n"), COMPILE_OPTS);
126         printf(_("Bugs to %s\n"), MY_EMAIL);
127 }