modules/ak/ack.cc

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. AK_add_to_ack
  2. AK_add_to_ack_string
  3. AK_ack_file_name_generate
  4. AK_send_ack
  5. AK_delete_ack
  6. AK_log_ack

   1 /***************************************
   2   $Revision: 1.7 $
   3 
   4   AK (Acknowledgement) module
   5 
   6   Status: NOT REVIEWED, NOT TESTED
   7 
   8   Author(s):       Engin Gunduz
   9 
  10   ******************/ /******************
  11   Modification History:
  12         engin (10/06/2000) Created.
  13   ******************/ /******************
  14   Copyright (c) 2000                              RIPE NCC
  15  
  16   All Rights Reserved
  17   
  18   Permission to use, copy, modify, and distribute this software and its
  19   documentation for any purpose and without fee is hereby granted,
  20   provided that the above copyright notice appear in all copies and that
  21   both that copyright notice and this permission notice appear in
  22   supporting documentation, and that the name of the author not be
  23   used in advertising or publicity pertaining to distribution of the
  24   software without specific, written prior permission.
  25   
  26   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  27   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  28   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  29   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  30   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  31   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32  ***************************************/
  33 
  34 
  35 
  36 #include "ack.h"
  37 
  38 /*
  39 
  40   AK_add_to_ack: writes a message to the acknowledgement file.
  41 
  42 */
  43 
  44 void AK_add_to_ack(char * filename, char * fmt, ...){
     /* [<][>][^][v][top][bottom][index][help] */
  45 
  46   va_list ap;  /* points to each unnamed arg in turn */
  47   char *p, *sval;
  48   int ival;
  49   double dval;
  50   FILE * ack_file;
  51   
  52   if(( ack_file = fopen(filename, "a")) == NULL){
  53     fprintf(stderr, "Can't open ack file, %s", filename);
  54   }
  55     
  56   va_start(ap, fmt);
  57   
  58   for(p = fmt; *p; p++){
  59     if (*p != '%') {
  60       fprintf(ack_file, "%c", *p);
  61       continue;
  62     }
  63     switch(*++p) {
  64     case 'd':
  65       ival = va_arg(ap, int);
  66       fprintf(ack_file, "%d", ival);
  67       break;
  68     case 'f':
  69       dval = va_arg(ap, double);  
  70       fprintf(ack_file, "%f", dval);
  71       break;
  72     case 'X':
  73       ival = va_arg(ap, int);
  74       fprintf(ack_file, "%X", ival);
  75       break;
  76     case 'x':
  77       ival = va_arg(ap, int);
  78       fprintf(ack_file, "%x", ival);
  79       break;
  80     case 's':
  81       //for(sval = va_arg(ap, char *); *sval; sval++)
  82       //  putchar(*sval);
  83       sval = va_arg(ap, char *);
  84       fprintf(ack_file, "%s", sval);
  85       break;
  86     default:
  87       putchar(*p);
  88       break;
  89     }
  90   }
  91 
  92   va_end(ap); /* clean up */
  93   fclose(ack_file);
  94 }
  95 
  96 /*  */
  97 void AK_add_to_ack_string(const char * file_name, const string msg){
     /* [<][>][^][v][top][bottom][index][help] */
  98 
  99    ofstream ack_file(file_name, ios::app);
 100 
 101    if(!ack_file){
 102      cerr << "Couldn't open ack file" << endl;
 103      return;
 104    }
 105    ack_file << msg;
 106    ack_file.close();
 107 }
 108 
 109 
 110 
 111 
 112 /*
 113 
 114   AK_ack_file_name_generate: Generates a unique name for temporary acknowledgement
 115      files, and also creates it. 
 116 
 117       tmpdir: temporary directory (without a trailing '/')
 118       prefix: prefix for the temp file
 119       
 120       returns: the generated name. 
 121      
 122 
 123 */
 124 
 125 char * AK_ack_file_name_generate( const char * tmpdir, const char * prefix){
     /* [<][>][^][v][top][bottom][index][help] */
 126 
 127    FILE * ack_file;
 128    char * name;
 129       
 130    /* allocate space for name. 32 should be enough for PID */
 131    name = (char*)malloc(strlen(tmpdir) + strlen(prefix) + 32); 
 132    
 133    sprintf(name, "%s/%s.%i", tmpdir, prefix, getpid());
 134 
 135    /* create the file */
 136    if(( ack_file = fopen(name, "w")) == NULL){
 137      fprintf(stderr, "Can't open ack file, %s", name);
 138    }
 139 
 140    /* close it */
 141    fclose(ack_file);
 142     
 143    return name;
 144 
 145 }
 146 
 147 
 148 /* 
 149 
 150 AK_send_ack: sends the ack message contained in the temp file.
 151  
 152    
 153 */
 154 
 155 void AK_send_ack( const char * filename, const char * to_address, const char * mailercommand){
     /* [<][>][^][v][top][bottom][index][help] */
 156 
 157     char * mail_command_line = NULL;
 158 
 159 
 160     if(to_address != NULL){
 161       mail_command_line = (char *)malloc(strlen(mailercommand) + strlen(filename) + 128);
 162       sprintf(mail_command_line, "%s %s < %s", mailercommand, to_address, filename);
 163       system(mail_command_line);
 164     }
 165 
 166 
 167 }
 168 
 169 
 170 /*
 171 
 172   AK_delete_ack: deletes the temporary acknowledgement file.
 173 
 174 */
 175 
 176 void AK_delete_ack( const char * filename ){
     /* [<][>][^][v][top][bottom][index][help] */
 177 
 178    unlink(filename);   
 179 
 180 }
 181 
 182 /*
 183 
 184 AK_log_ack: logs the acknowledgements in the log_file.
 185 
 186 */
 187 
 188 void AK_log_ack(const char * filename, const char * logfilename){
     /* [<][>][^][v][top][bottom][index][help] */
 189 
 190   FILE * ack_file, * log_file;
 191   char * buf;
 192   time_t cur_time;
 193   char * time_str;
 194 
 195   buf = (char *)malloc(1024);
 196   if(( ack_file = fopen(filename, "r")) == NULL){
 197     fprintf(stderr, "Can't open ack file, %s\n", filename);
 198     return;
 199   }
 200 
 201   if(( log_file = fopen(logfilename, "a")) == NULL){
 202     fprintf(stderr, "Can't open log file, %s\n", logfilename);
 203     return;
 204   }
 205 
 206   /* get time */
 207   cur_time = time(NULL);
 208   time_str = strdup(ctime(&cur_time));
 209   /* cut the '\n' at the end */
 210   time_str[strlen(time_str) - 1] = '\0';
 211 
 212   fprintf(log_file, ">>> time: %s ACK <<<\n\n", time_str);
 213 
 214 
 215   while((buf=fgets(buf, 1023, ack_file)) > 0){
 216     fprintf(log_file, "%s", buf);
 217   }
 218 
 219   free(time_str);
 220   fclose(ack_file);
 221   fclose(log_file);
 222 
 223 }
 224 
 225 
 226 
 227 
 228 
 229 
 230 
 231 

/* [<][>][^][v][top][bottom][index][help] */