~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to common/common.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
*/
19
19
 
20
20
#include "common.h"
21
 
#include "shared.h"
22
21
 
23
22
#include <ctype.h>
24
23
#include <syslog.h>
26
25
#include <grp.h>
27
26
 
28
27
        int     nut_debug_level = 0;
29
 
        int     upslog_flags = UPSLOG_STDERR;
 
28
        static  int     upslog_flags = UPSLOG_STDERR;
30
29
 
31
 
void xbit_set(int *val, int flag)
 
30
static void xbit_set(int *val, int flag)
32
31
{
33
32
        *val = (*val |= flag);
34
33
}
35
34
 
36
 
void xbit_clear(int *val, int flag)
 
35
static void xbit_clear(int *val, int flag)
37
36
{
38
37
        *val = (*val ^= (*val & flag));
39
38
}
40
39
 
41
 
int xbit_test(int val, int flag)
 
40
static int xbit_test(int val, int flag)
42
41
{
43
42
        return ((val & flag) == flag);
44
43
}
80
79
        close(2);
81
80
 
82
81
        if (pid != 0) 
83
 
                _exit(0);               /* parent */
 
82
                _exit(EXIT_SUCCESS);            /* parent */
84
83
 
85
84
        /* child */
86
85
 
208
207
int snprintfcat(char *dst, size_t size, const char *fmt, ...)
209
208
{
210
209
        va_list ap;
211
 
        int len = strlen(dst);
 
210
        size_t len = strlen(dst);
212
211
        int ret;
213
212
 
214
213
        size--;
241
240
        return p + 1;
242
241
}
243
242
 
244
 
void vupslog(int priority, const char *fmt, va_list va, int use_strerror)
 
243
static void vupslog(int priority, const char *fmt, va_list va, int use_strerror)
245
244
{
246
 
        char buf[LARGEBUF];
247
 
 
248
 
        if (vsnprintf(buf, sizeof(buf), fmt, va) >= sizeof(buf))
249
 
                ; /* overflow should be handled */
 
245
        int     ret;
 
246
        char    buf[LARGEBUF];
 
247
 
 
248
        ret = vsnprintf(buf, sizeof(buf), fmt, va);
 
249
 
 
250
        if ((ret < 0) || (ret >= (int) sizeof(buf)))
 
251
                syslog(LOG_WARNING, "vupslog: vsnprintf needed more than %d bytes",
 
252
                        LARGEBUF);
250
253
 
251
254
        if (use_strerror)
252
255
                snprintfcat(buf, sizeof(buf), ": %s", strerror(errno));
333
336
        va_end(va);
334
337
}
335
338
 
336
 
void vfatal(const char *fmt, va_list va, int use_strerror)
 
339
static void vfatal(const char *fmt, va_list va, int use_strerror)
337
340
{
338
341
        if (xbit_test(upslog_flags, UPSLOG_STDERR_ON_FATAL))
339
342
                xbit_set(&upslog_flags, UPSLOG_STDERR);
351
354
        vfatal(fmt, va, 1);
352
355
        va_end(va);
353
356
 
354
 
        exit(1);
 
357
        exit(EXIT_FAILURE);
355
358
}
356
359
 
357
360
void fatalx(const char *fmt, ...)
362
365
        vfatal(fmt, va, 0);
363
366
        va_end(va);
364
367
 
365
 
        exit(1);
 
368
        exit(EXIT_FAILURE);
366
369
}
367
370
 
368
371
static const char *oom_msg = "Out of memory";
403
406
        return p;
404
407
}
405
408
 
406
 
#ifndef HAVE_CFMAKERAW
407
 
 
408
 
int cfmakeraw(struct termios *termios_p)
 
409
/* modify in - strip all trailing instances of <sep> */
 
410
void rtrim(char *in, char sep)
409
411
{
410
 
        termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
411
 
                                |INLCR|IGNCR|ICRNL|IXON);
412
 
        termios_p->c_oflag &= ~OPOST;
413
 
        termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
414
 
        termios_p->c_cflag &= ~(CSIZE|PARENB);
415
 
        termios_p->c_cflag |= CS8;
416
 
 
417
 
        return 0;
 
412
        char    *p = NULL;
 
413
 
 
414
        p = &in[strlen(in) - 1];
 
415
 
 
416
        while (p >= in) {
 
417
                if (*p != sep)
 
418
                        return;
 
419
 
 
420
                *p-- = '\0';
 
421
        }
418
422
}
419
 
 
420
 
#endif