File: genwrite.c

package info (click to toggle)
genpower 1.0.1-13
  • links: PTS
  • area: main
  • in suites: woody
  • size: 264 kB
  • ctags: 44
  • sloc: ansic: 522; sh: 183; makefile: 109
file content (108 lines) | stat: -rw-r--r-- 3,514 bytes parent folder | download | duplicates (4)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/************************************************************************/
/* File Name            : genwrite.c                                    */
/* Program Name         : genpowerd                   Version: 1.0.1    */
/* Author               : Brian White <bcwhite@pobox.com>               */
/* Created              : 1998/01/13                                    */
/* Last Modified By     : Brian White                 Date: 1998/01/13  */
/*                                                                      */
/* Compiler (created)   : GCC 2.7.3                                     */
/* Compiler (env)       : Linux 2.0.33                                  */
/* ANSI C Compatable    : No                                            */
/* POSIX Compatable     : Yes?                                          */
/*                                                                      */
/* Purpose              : Write some characters to the serial port      */
/*                      : with DTR and RTS set as desired.              */
/*                                                                      */
/* Usage                : genwrite [-d] [-r] <serial device> <chars>    */
/*                      : i.e. genpowerd /dev/apc-ups "Y"               */
/*                                                                      */
/* History/Credits      : Some code was taken from "genpowerd", written */
/*                      : by Tom Webster.                               */
/*                                                                      */
/* Copyright            : GNU Copyleft                                  */
/************************************************************************/

#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>


/* Main program. */
int main(int argc, char **argv) {
    int		rts_set = 0;
    int		dtr_set = 0;
    char*	program_name;
    int		fd;

    program_name = argv[0];
    /* Parse the command line */
    while ((argc > 1) && (argv[1][0] == '-')) {
	switch (argv[1][1]) {
	case 'r':
	    rts_set = 1;
	    break;
	case 'd':
	    dtr_set = 1;
	    break;
	default:
	    fprintf(stderr, "\nUsage: %s [-d] [-r] <device> <chars>\n", program_name);
	    exit(1);
	}
	argv++;
	argc--;
    }


    /* Done with options, make sure that one port is specified */
    if (argc != 3) {
	fprintf(stderr, "\nUsage: %s [-d] [-r] <device> <chars>\n", program_name);
	exit(1);
    }                            			/* if (argc != 3) */


    /* Open the device */
    if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
	fprintf(stderr, "%s: %s: %s\n",program_name, argv[1], sys_errlist[errno]);
	exit(1);
    }

    /* Line is opened, so DTR and RTS are high. Clear them (can  */
    /* cause problems with some UPSs if they remain set).        */
    ioctl(fd, TIOCMBIC, TIOCM_RTS);
    ioctl(fd, TIOCMBIC, TIOCM_DTR);

    /* Set line(s) if so requested */
    if (rts_set) {
	ioctl(fd, TIOCMBIS, TIOCM_RTS);
    }
    if (dtr_set) {
	ioctl(fd, TIOCMBIS, TIOCM_DTR);
    }

    /* Delay a bit so things can equalize (0.1 seconds) */
    usleep(100000);

    /* Write out the characters */
    write(fd,argv[2],strlen(argv[2]));

    /* Delay a bit so things can equalize (0.1 seconds) */
    usleep(100000);

    /* Close the path */
    close(fd);

    /* Return cleanly */
    return 0;
}


/*
 * local variables:
 * tab-width: 8
 * end:
 */