File: sigrot.c

package info (click to toggle)
sigrot 1.1-2
  • links: PTS
  • area: main
  • in suites: hamm, potato, slink
  • size: 72 kB
  • ctags: 34
  • sloc: ansic: 182; makefile: 56
file content (289 lines) | stat: -rw-r--r-- 9,195 bytes parent folder | download
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * sigrot.c
 *
 * sigrot is Copyright (c) 1998 Christopher J. Morrone 
 * It is distributed under the GNU General Public License.
 *
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

/**************************************************************************
 *  The following defines set the standard locations of sigrot's dir and 
 *  files relaive to the $HOME dir.  Edit as desired.
 *
 *  DEST is the file in each user's home dir that is read by mail, news, 
 *      etc. programs that sigrot will write to. (usually .signature)
 *  SOURCE_DIR is the dir in the user's home dir which will contain
 *      sigrot's SOURCE, NEXT, PREFIX and SUFFIX files.
 *  SOURCE is the archive file in the SOURCE_DIR that will contain the list
 *      of signatures.
 *  NEXT is a file in the SOURCE_DIR which contains an integer which tells
 *      sigrot which signature should be read from the archive file next.
 *  PREFIX is a file which contains standard signature information that 
 *      should appear at the top of every .signatur.
 *  SUFFIX is a file which contains standard signature information that
 *      should appear at the bottom of every .signatur.
 **************************************************************************/

#define DEST		".signature"
#define SOURCE_DIR	".sigrot"
#define	SOURCE		"sig_archive"
#define NEXT		"next"
#define PREFIX		"prefix"
#define SUFFIX		"suffix"

/******************************* STOP *************************************
 * It should not be necesarry to edit anything below this line.           
 **************************************************************************/

int exists (char *);
void write_next(int);
int get_next();
void find_next_entry(int *,FILE *);
void write_sig(FILE *);
void copy_file(char *,FILE *);

char Dest[1024], 
     Source_Dir[1024],
     Source[1024], 
     Next[1024], 
     Prefix[1024], 
     Suffix[1024],
     buffer[1024];

int main(int argc, char *argv[])
{
  FILE *inFile,*outFile;
  int next,test=0;

  /* Set the full path names */
  sprintf(Dest, "%s/%s", getenv("HOME"), DEST);
  sprintf(Source_Dir, "%s/%s", getenv("HOME"), SOURCE_DIR);
  sprintf(Source, "%s/%s", Source_Dir, SOURCE);
  sprintf(Next, "%s/%s", Source_Dir, NEXT);
  sprintf(Prefix, "%s/%s", Source_Dir, PREFIX);
  sprintf(Suffix, "%s/%s", Source_Dir, SUFFIX);

  /* Check for sigrot's dir.  Create if it doesn't exist */
  if ((exists(Source_Dir))==0) test=mkdir(Source_Dir,0700);
  if (test!=0) 
    printf("ERROR: Couldn't create dir \"%s\".\n", Source_Dir);

  /* If there are no command line parameters, proceed with normal 
     operation. */
  if (argc==1) { 
    next=get_next(); /* number of the next archive entry to read */
    inFile=fopen(Source,"r");
    if (inFile==NULL)
      fprintf(stderr,"ERROR: Couldn't open \"%s\".\n", Source);
    else {
      find_next_entry(&next,inFile); /* Move file pointer to the 
                                        beginning of the next entry */
      write_sig(inFile);
      fclose(inFile);
      write_next(next); /* Write the number of the entry to be read on the 
			   next call to sigrot */
    }
  }
  else {
  /* The following case statements handle command line parameters. */
    switch(argc) {
      case 2: if ((!strcmp(argv[1], "-r")) || 
                  (!strcmp(argv[1], "-on"))) {
                sprintf(buffer, "%s.bak", Source);
                outFile=fopen(Source,"w");
                copy_file(buffer, outFile);
                fclose(outFile);
                printf("Old signature archive restored.\n");
              }
	      if (!strcmp(argv[1], "-off")) {
                sprintf(buffer, "%s.bak", Source);
                outFile=fopen(buffer,"w");
                copy_file(Source, outFile);
                fclose(outFile);
	        outFile=fopen(Source,"w");
	        fclose(outFile);
                write_next(0);
                printf("Signatures off.\n");
 	      }
              break;
      case 3: if (!strcmp(argv[1], "-w")) {
                sprintf(buffer, "%s.bak", Source);
                outFile=fopen(buffer,"w");
                copy_file(Source, outFile);
                fclose(outFile);
                outFile=fopen(Source,"w");
                copy_file(argv[2], outFile);
                fclose(outFile);
		printf("\"%s\" copied over signature archive.\n", argv[2]);
		printf("Type \"sigrot -r\" to restore the previous archive.\n");
              }
              if (!strcmp(argv[1], "-a")) {
                sprintf(buffer, "%s.bak", Source);
                outFile=fopen(buffer,"w");
                copy_file(Source, outFile);
                fclose(outFile);
		outFile=fopen(Source,"a");
		fprintf(outFile, "\n");
		copy_file(argv[2], outFile);
		fclose(outFile);
                printf("\"%s\" appended to signature archive.\n", argv[2]);
                printf("Type \"sigrot -r\" to restore the previous archive.\n");
	      }
	      break;
      default: printf("Too many arguments.\n");
    }
  }
  return 0;
}

/*******************************************************************
 * exists
 *
 * Return non-zero if directory exists and path components up to at
 * are accessable, 0 if not.
 *******************************************************************/
int exists (char *directory) 
{
  struct stat buf;
  return (!stat (directory, &buf) && S_ISDIR(buf.st_mode));
}

/*******************************************************************
 * get_next
 *
 * Access file named in the "#define NEXT" line.
 * Read in interger and return it.
 * If file doesn't exist, return 1.
 *******************************************************************/
int get_next()
{
  FILE *inFile;
  int next;

  inFile=fopen(Next,"r");
  if (inFile==NULL)
    next=1;
  else {
    fscanf(inFile,"%d",&next);
    fclose(inFile);
  };
  return next;
}

/*******************************************************************
 * write_next
 *
 * Increments the value of the current archive entry, and writes it
 * to the file named in the "#define NEXT" line.
 *******************************************************************/
void write_next(int next) {
  FILE *outFile;

  outFile=fopen(Next,"w");
  if (outFile==NULL)
    fprintf(stderr,"ERROR: Couldn't open \"%s\".\n", Next);
  else
    next+=1;
    fprintf(outFile,"%d\n",next);
  fclose(outFile);
}

/*******************************************************************
 * find_next_entry
 *
 * Takes as arguments an integer, and a file already opened for 
 * input.  It then searches for occurances of a double \n, or the
 * end of the file.  If it finds the occurance "next" of a double \n,
 * it leaves the file pointer there (the beginning of the next 
 * archive entry) and exits.  If it hits the EOF before the "next"
 * occurance of a double \n is found, it returns 1. (So the first 
 * entry in the archive will be read.)
 *******************************************************************/
void find_next_entry(int *next, FILE *inFile)
{
  int localnext=*next;
  char previous,current;

  if (localnext!=1) {
    previous=getc(inFile);
    while(localnext>1) {
      current=getc(inFile);
      if ((current=='\n') && (previous=='\n')) localnext-=1;
      if (current==EOF) {
        fseek(inFile,0L,0);
        localnext=1;
        *next=1;
      }
      previous=current;
    }
  }
}

/*******************************************************************
 * write_sig
 * 
 * First opens the file DEST for output.
 * Then calls copy_file to copy the contents of PREFIX into DEST, if
 * PREFIX exists.
 * Next it copies from the SOURCE file and appends to DEST until
 * it finds a double \n or the EOF in SOURCE.
 * Finally, if SUFFIX exists, it calls copy_file to copy the contents of 
 * SUFFIX and append it to DEST.
 *******************************************************************/
void write_sig(FILE *inFile)
{
  FILE *outFile;
  int test=0;
  char previous,current;

  outFile=fopen(Dest,"w");
  if (outFile==NULL)
    fprintf(stderr,"ERROR: Couldn't open \"%s\".\n", Dest);
  else {
    copy_file(Prefix,outFile);
    previous=fgetc(inFile);
    if (previous==EOF) test=1;
    while(test!=1) {
      current=fgetc(inFile);
      if ((current==EOF) || ((current=='\n') && (previous=='\n')))
        test=1;
      fputc(previous, outFile);
      previous=current;
    }
    copy_file(Suffix,outFile);
    fclose(outFile);
  }
}

/*******************************************************************
 * copy_file
 *
 * Takes as arguments a string which contains the full pathname of a 
 * file, and a file already opened for output.
 * It then opens the file, named in the string, for input.
 * Then it copies the entire contents of the input file and appends 
 * it to the output file.
 *******************************************************************/
void copy_file (char *string,FILE *outFile)
{
  FILE *inFile;
  char tmp=0;

  inFile=fopen(string,"r");
  if (inFile!=NULL) {
    while(tmp!=EOF) {
      tmp=fgetc(inFile);
      if (tmp!=EOF) fputc(tmp,outFile);
    }
    fclose(inFile);
  }
}

/*************************  Thats all folks!  *************************/