C Program To Alarm Check

How to write a C Program to Alarm Check in C programming Language ?


  1. /*Alarm check */
  2. #include <sys/types.h>
  3. #include <sys/ioctl.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <signal.h>
  10. #include <time.h>
  11.  
  12. /* Main program. */
  13. int main(int argc, char **argv)
  14. {
  15.   int fd;
  16.   FILE *log;
  17.  
  18. /*  These TIOCM_* parameters are defined in <linux/termios.h>, which  */
  19. /*  is indirectly included here.                                      */
  20.   int set_bits;
  21.   int flags;
  22.   int sleep_time;
  23.   int pc;
  24.   time_t now;
  25.   char strtime[30];
  26.  
  27.   if (argc != 2) {
  28.         printf("Usage: relays <device> \n");
  29.         exit(1);
  30.   }
  31.  
  32.   /* Open monitor device. */
  33.   if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
  34.     fprintf(stderr, "port: %s: %s\n", argv[1], sys_errlist[errno]);
  35.     exit(1);}
  36.  
  37.  
  38.   /* Get the bits to set from the command line. */
  39.     sscanf(argv[2], "%x", &set_bits);
  40.     sscanf(argv[3], "%d", &sleep_time);
  41.  
  42.  
  43.     /* Set the command line specified bits (& only the command line */
  44.     /* specified bits).                                             */
  45.    
  46.     ioctl(fd, TIOCMSET, &set_bits);
  47.  
  48.     sleep(1);
  49.     ioctl(fd, TIOCMGET, &flags);
  50.  
  51.     time(&now);
  52.     printf("%d\t",now);
  53.     printf("%x\t", flags);
  54.     if (flags & 0x100) { printf ("on\t"); } else { printf ("off\t"); }
  55.     if (flags & 0x80) { printf ("on\t"); } else { printf ("off\t"); }
  56.     if (flags & 0x40) { printf ("on\t"); } else { printf ("off\t"); }
  57.     if (flags & 0x20) { printf ("on\t"); } else { printf ("off\t"); }
  58.     printf("%s",ctime(&now));
  59.  
  60. close(fd);
  61.  
  62. }


Learn More :