Initial import from FreeBSD RELENG_4:
[games.git] / usr.sbin / i4b / isdntel / files.c
1 /*
2  * Copyright (c) 1997, 1999 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      isdntel - isdn4bsd telephone answering machine support
28  *      ======================================================
29  *
30  *      $Id: files.c,v 1.8 1999/12/13 21:25:26 hm Exp $ 
31  *
32  * $FreeBSD: src/usr.sbin/i4b/isdntel/files.c,v 1.6.2.1 2001/08/01 17:45:06 obrien Exp $
33  *
34  *      last edit-date: [Mon Dec 13 21:54:06 1999]
35  *
36  *----------------------------------------------------------------------------*/
37
38 #include "defs.h"
39
40 /*---------------------------------------------------------------------------*
41  *      create a doubly linked list in sorted order, return pointer to new
42  *      first element of list
43  *---------------------------------------------------------------------------*/
44 struct onefile *store
45   (register struct onefile *new,                /* new entry to store into list */
46    register struct onefile *top)                /* current first entry in list */
47 {
48         register struct onefile *old, *p;
49
50         if (last == NULL)                       /* enter very first element ? */
51         {
52                 new->next = NULL;
53                 new->prev = NULL;
54                 last = new;                     /* init last */
55                 return (new);                   /* return new first */
56         }
57         p = top;                                /* p = old first element */
58         old = NULL;
59         while (p)
60         {
61                 if ((strcmp(p->fname, new->fname)) < 0) /* current less new ? */
62                 {
63                         old = p;
64                         p = p->next;
65                 }
66                 else
67                 {                               /* current >= new */
68
69                         if (p->prev)
70                         {
71                                 p->prev->next = new;
72                                 new->next = p;
73                                 new->prev = p->prev;
74                                 p->prev = new;
75                                 return (top);
76                         }
77                         new->next = p;
78                         new->prev = NULL;
79                         p->prev = new;
80                         return (new);
81                 }
82         }
83         old->next = new;
84         new->next = NULL;
85         new->prev = old;
86         last = new;
87         return (first);
88 }
89
90 /*---------------------------------------------------------------------------*
91  *      read current directory and build up a doubly linked sorted list
92  *---------------------------------------------------------------------------*/
93 int
94 fill_list(void)
95 {
96 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
97         register struct dirent *dp;
98 #else
99         register struct direct *dp;
100 #endif
101         register struct onefile *new_entry;
102         register DIR *dirp;
103         int flcnt = 0;
104         char tmp[80];
105         char *s, *d;
106         
107         if ((dirp = opendir(spooldir)) == NULL)
108                 fatal("cannot open spooldirectory %s!\n", spooldir);
109
110         for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
111         {
112                 if(!isdigit(*(dp->d_name)))
113                         continue;
114
115                 if ((new_entry = (struct onefile *) malloc(sizeof(struct onefile))) == NULL)
116                 {
117                         fatal("files.c, fill_list(): structure onefile malloc failed");
118                 }
119
120                 /* alloc filename memory and copy name into it */
121
122                 if ((new_entry->fname = (char *) malloc(strlen(dp->d_name) + 1)) == NULL)
123                 {
124                         fatal("files.c, fill_list(): malloc filename string memory failed");
125                 }
126
127                 strcpy(new_entry->fname, dp->d_name);
128
129                 /* fill in remaining fields from filename */
130
131                 tmp[0] = dp->d_name[4]; /* day msb */
132                 tmp[1] = dp->d_name[5]; /* day lsb */
133                 tmp[2] = '.';
134                 tmp[3] = dp->d_name[2]; /* month msb */
135                 tmp[4] = dp->d_name[3]; /* month lsb */
136                 tmp[5] = '.';
137                 tmp[6] = dp->d_name[0]; /* year msb */
138                 tmp[7] = dp->d_name[1]; /* year lsb */
139                 tmp[8] = '\0';
140
141                 if((new_entry->date = (char *) malloc(strlen(tmp) + 1)) == NULL)
142                 {
143                         fatal("files.c, fill_list(): malloc date string memory failed");
144                 }
145
146                 strcpy(new_entry->date, tmp);
147                 
148                 tmp[0]  = dp->d_name[6]; /* hour msb */
149                 tmp[1] = dp->d_name[7]; /* hour lsb */
150                 tmp[2] = ':';
151                 tmp[3] = dp->d_name[8]; /* minute msb */
152                 tmp[4] = dp->d_name[9]; /* minute lsb */
153                 tmp[5] = ':';
154                 tmp[6] = dp->d_name[10]; /* second msb */
155                 tmp[7] = dp->d_name[11]; /* second lsb */
156                 tmp[8] = '\0';
157                 
158                 if((new_entry->time = (char *) malloc(strlen(tmp) + 1)) == NULL)
159                 {
160                         fatal("files.c, fill_list(): malloc time string memory failed");
161                 }
162
163                 strcpy(new_entry->time, tmp);
164
165                 /* destination number */
166                 
167                 s = &dp->d_name[13];
168                 d = &tmp[0];
169
170                 while(*s && (*s != '-'))
171                         *d++ = *s++;
172
173                 *d = '\0';
174                 
175                 if((new_entry->dstnumber = (char *) malloc(strlen(tmp) + 1)) == NULL)
176                 {
177                         fatal("files.c, fill_list(): malloc dstnumber string memory failed");
178                 }
179
180                 strcpy(new_entry->dstnumber, tmp);
181
182                 /* source number */
183                 
184                 s++;
185                 d = &tmp[0];
186
187                 while(*s && (*s != '-'))
188                         *d++ = *s++;
189
190                 *d = '\0';
191                 
192                 if((new_entry->srcnumber = (char *) malloc(strlen(tmp) + 1)) == NULL)
193                 {
194                         fatal("files.c, fill_list(): malloc srcnumber string memory failed");
195                 }
196
197                 strcpy(new_entry->srcnumber, tmp);
198
199                 /* length in seconds */
200                 
201                 s++;
202                 d = &tmp[0];
203
204                 while(*s && (*s != '-'))
205                         *d++ = *s++;
206
207                 *d = '\0';
208                 
209                 if((new_entry->seconds = (char *) malloc(strlen(tmp) + 1)) == NULL)
210                 {
211                         fatal("files.c, fill_list(): malloc seconds string memory failed");
212                 }
213
214                 strcpy(new_entry->seconds, tmp);
215
216                 /* search for alias and add if found */
217                 
218                 new_entry->alias = get_alias(new_entry->srcnumber);
219                 
220                 /* sort entry into linked list */
221
222                 first = store(new_entry, first);
223
224                 flcnt++;                        /* increment file count */
225         }
226         closedir(dirp);                         /* close current dir */
227         return(flcnt);                          /* ok return */
228 }
229
230 /*---------------------------------------------------------------------------*
231  *      free the current malloc'ed list
232  *---------------------------------------------------------------------------*/
233 void
234 free_list(void)
235 {
236         register struct onefile *dir;
237         register struct onefile *tmp;
238
239         dir = first;                            /* start of linked list */
240
241         while (dir)                             /* free all */
242         {
243                 tmp = dir->next;                /* save ptr to next entry */
244                 free(dir->fname);               /* free filename space */
245                 free(dir->date);
246                 free(dir->time);
247                 free(dir->srcnumber);
248                 free(dir->dstnumber);
249                 free(dir->seconds);             
250                 free(dir);                      /* free struct space */
251                 dir = tmp;                      /* ptr = ptr to next entry */
252         }
253         first = NULL;                           /* first ptr = NULL */
254         last = NULL;                            /* last ptr = NULL */
255 }
256
257 /*---------------------------------------------------------------------------*
258  *      delete a file
259  *---------------------------------------------------------------------------*/
260 void
261 delete(struct onefile *this)
262 {
263         char buffer[MAXPATHLEN+1];
264
265         if(this == NULL)
266                 return;
267                 
268         sprintf(buffer, "%s", this->fname);
269         
270         unlink(buffer);
271
272         free_list();
273
274         wclear(main_w);
275
276         init_files(cur_pos);
277 }
278
279 /*---------------------------------------------------------------------------*
280  *      reread the spool directory
281  *---------------------------------------------------------------------------*/
282 void
283 reread(void)
284 {
285         free_list();
286
287         wclear(main_w);
288
289         init_files(cur_pos);
290 }
291
292 /*---------------------------------------------------------------------------*
293  *      play a file
294  *---------------------------------------------------------------------------*/
295 void
296 play(struct onefile *this)
297 {
298         char buffer[MAXPATHLEN+1];
299
300         if(this == NULL)
301                 return;
302                 
303         sprintf(buffer, playstring, this->fname);
304         
305         system(buffer);
306 }
307
308 /*---------------------------------- EOF -------------------------------------*/