1 /*:ts=8*/
2 /*****************************************************************************
3 * FIDOGATE --- Gateway UNIX Mail/News <-> FIDO NetMail/EchoMail
4 *
5 * $Id: sequencer.c,v 4.10 2004/08/22 20:19:11 n0ll Exp $
6 *
7 * Number sequencer using sequence file in LIBDIR
8 *
9 *****************************************************************************
10 * Copyright (C) 1990-2004
11 * _____ _____
12 * | |___ | Martin Junius <mj.at.n0ll.dot.net>
13 * | | | | | | Radiumstr. 18
14 * |_|_|_|@home| D-51069 Koeln, Germany
15 *
16 * This file is part of FIDOGATE.
17 *
18 * FIDOGATE is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 2, or (at your option) any
21 * later version.
22 *
23 * FIDOGATE is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with FIDOGATE; see the file COPYING. If not, write to the Free
30 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
31 *****************************************************************************/
32
33 #include "fidogate.h"
34
35
36
37 /*
38 * Sequencer: read number from file and increment it
39 */
sequencer(char * seqname)40 long sequencer(char *seqname)
41 {
42 return sequencer_nx(seqname, TRUE);
43 }
44
45
sequencer_nx(char * seqname,int err_abort)46 long sequencer_nx(char *seqname, int err_abort)
47 {
48 char filename[MAXPATH];
49 FILE *fp;
50 long seqn;
51
52 /* Open file, create if necessary */
53 BUF_EXPAND(filename, seqname);
54 if( (fp = fopen(filename, RP_MODE)) == NULL )
55 if(errno == ENOENT)
56 fp = fopen(filename, WP_MODE);
57
58 if(fp == NULL)
59 {
60 if(err_abort)
61 {
62 logit("$ERROR: can't access sequencer file %s", filename);
63 exit(EX_OSFILE);
64 }
65 else
66 return ERROR;
67 }
68
69 /* Lock file, get number and increment it */
70 lock_file(fp);
71
72 /* filename[] is also used as a buffer for reading the seq value */
73 if(fgets(filename, sizeof(filename), fp))
74 seqn = atol(filename);
75 else
76 seqn = 0;
77 seqn++;
78 if(seqn < 0)
79 seqn = 0;
80
81 rewind(fp);
82 fprintf(fp, "%ld\n", seqn);
83 rewind(fp);
84
85 fclose(fp);
86
87 return seqn;
88 }
89