No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / isdn / isdntel / files.c
blobd3ddfd09d5f486805bd38b95f08958f4a1af1ab4
1 /*
2 * Copyright (c) 1997, 1999 Hellmuth Michaelis. All rights reserved.
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.
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.
25 *---------------------------------------------------------------------------
27 * isdntel - isdn4bsd telephone answering machine support
28 * ======================================================
30 * $Id: files.c,v 1.5 2004/10/30 08:31:39 dsl Exp $
32 * $FreeBSD$
34 * last edit-date: [Sat Jan 6 13:04:44 2001]
36 *----------------------------------------------------------------------------*/
38 #include "defs.h"
40 struct onefile * store(struct onefile *new, struct onefile *top);
41 void free_list(void);
43 /*---------------------------------------------------------------------------*
44 * create a doubly linked list in sorted order, return pointer to new
45 * first element of list
46 *---------------------------------------------------------------------------*/
47 struct onefile *store
48 (struct onefile *new, /* new entry to store into list */
49 struct onefile *top) /* current first entry in list */
51 struct onefile *old, *p;
53 if (last == NULL) /* enter very first element ? */
55 new->next = NULL;
56 new->prev = NULL;
57 last = new; /* init last */
58 return (new); /* return new first */
60 p = top; /* p = old first element */
61 old = NULL;
62 while (p)
64 if ((strcmp(p->fname, new->fname)) < 0) /* current less new ? */
66 old = p;
67 p = p->next;
69 else
70 { /* current >= new */
72 if (p->prev)
74 p->prev->next = new;
75 new->next = p;
76 new->prev = p->prev;
77 p->prev = new;
78 return (top);
80 new->next = p;
81 new->prev = NULL;
82 p->prev = new;
83 return (new);
86 old->next = new;
87 new->next = NULL;
88 new->prev = old;
89 last = new;
90 return (first);
93 /*---------------------------------------------------------------------------*
94 * read current directory and build up a doubly linked sorted list
95 *---------------------------------------------------------------------------*/
96 int
97 fill_list(void)
99 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
100 register struct dirent *dp;
101 #else
102 register struct direct *dp;
103 #endif
104 register struct onefile *new_entry;
105 register DIR *dirp;
106 int flcnt = 0;
107 char tmp[80];
108 char *s, *d;
110 if ((dirp = opendir(spooldir)) == NULL)
111 fatal("cannot open spooldirectory %s!\n", spooldir);
113 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
115 if (!isdigit((unsigned char)*(dp->d_name)))
116 continue;
118 if ((new_entry = (struct onefile *) malloc(sizeof(struct onefile))) == NULL)
120 fatal("files.c, fill_list(): structure onefile malloc failed");
123 /* alloc filename memory and copy name into it */
125 if ((new_entry->fname = strdup(dp->d_name)) == NULL)
127 fatal("files.c, fill_list(): malloc filename string memory failed");
130 /* fill in remaining fields from filename */
132 tmp[0] = dp->d_name[4]; /* day msb */
133 tmp[1] = dp->d_name[5]; /* day lsb */
134 tmp[2] = '.';
135 tmp[3] = dp->d_name[2]; /* month msb */
136 tmp[4] = dp->d_name[3]; /* month lsb */
137 tmp[5] = '.';
138 tmp[6] = dp->d_name[0]; /* year msb */
139 tmp[7] = dp->d_name[1]; /* year lsb */
140 tmp[8] = '\0';
142 if ((new_entry->date = strdup(tmp)) == NULL)
144 fatal("files.c, fill_list(): malloc date string memory failed");
147 tmp[0] = dp->d_name[6]; /* hour msb */
148 tmp[1] = dp->d_name[7]; /* hour lsb */
149 tmp[2] = ':';
150 tmp[3] = dp->d_name[8]; /* minute msb */
151 tmp[4] = dp->d_name[9]; /* minute lsb */
152 tmp[5] = ':';
153 tmp[6] = dp->d_name[10]; /* second msb */
154 tmp[7] = dp->d_name[11]; /* second lsb */
155 tmp[8] = '\0';
157 if ((new_entry->time = strdup(tmp)) == NULL)
159 fatal("files.c, fill_list(): malloc time string memory failed");
162 /* destination number */
164 s = &dp->d_name[13];
165 d = &tmp[0];
167 while(*s && (*s != '-'))
168 *d++ = *s++;
170 *d = '\0';
172 if ((new_entry->dstnumber = strdup(tmp)) == NULL)
174 fatal("files.c, fill_list(): malloc dstnumber string memory failed");
177 /* source number */
179 s++;
180 d = &tmp[0];
182 while(*s && (*s != '-'))
183 *d++ = *s++;
185 *d = '\0';
187 if ((new_entry->srcnumber = strdup(tmp)) == NULL)
189 fatal("files.c, fill_list(): malloc srcnumber string memory failed");
192 /* length in seconds */
194 s++;
195 d = &tmp[0];
197 while(*s && (*s != '-'))
198 *d++ = *s++;
200 *d = '\0';
202 if ((new_entry->seconds = strdup(tmp)) == NULL)
204 fatal("files.c, fill_list(): malloc seconds string memory failed");
207 /* search for alias and add if found */
209 new_entry->alias = get_alias(new_entry->srcnumber);
211 /* sort entry into linked list */
213 first = store(new_entry, first);
215 flcnt++; /* increment file count */
217 closedir(dirp); /* close current dir */
218 return(flcnt); /* ok return */
221 /*---------------------------------------------------------------------------*
222 * free the current malloc'ed list
223 *---------------------------------------------------------------------------*/
224 void
225 free_list(void)
227 register struct onefile *dir;
228 register struct onefile *tmp;
230 dir = first; /* start of linked list */
232 while (dir) /* free all */
234 tmp = dir->next; /* save ptr to next entry */
235 free(dir->fname); /* free filename space */
236 free(dir->date);
237 free(dir->time);
238 free(dir->srcnumber);
239 free(dir->dstnumber);
240 free(dir->seconds);
241 free(dir); /* free struct space */
242 dir = tmp; /* ptr = ptr to next entry */
244 first = NULL; /* first ptr = NULL */
245 last = NULL; /* last ptr = NULL */
248 /*---------------------------------------------------------------------------*
249 * delete a file
250 *---------------------------------------------------------------------------*/
251 void
252 delete(struct onefile *this)
254 char buffer[MAXPATHLEN+1];
256 if (this == NULL)
257 return;
259 snprintf(buffer, sizeof(buffer), "%s", this->fname);
261 unlink(buffer);
263 free_list();
265 wclear(main_w);
267 init_files(cur_pos);
270 /*---------------------------------------------------------------------------*
271 * reread the spool directory
272 *---------------------------------------------------------------------------*/
273 void
274 reread(void)
276 free_list();
278 wclear(main_w);
280 init_files(cur_pos);
283 /*---------------------------------------------------------------------------*
284 * play a file
285 *---------------------------------------------------------------------------*/
286 void
287 play(struct onefile *this)
289 char buffer[MAXPATHLEN+1];
291 if (this == NULL)
292 return;
294 snprintf(buffer, sizeof(buffer), playstring, this->fname);
296 system(buffer);
299 /*---------------------------------- EOF -------------------------------------*/