HAMMER Utilities: MFC all changes as of 20080924 - through 'hammer cleanup'.
[dragonfly.git] / usr.sbin / i4b / isdntel / files.c
blob8e3bbebdfb410dbc916727a5556eac3cb2dd8ae1
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.8 1999/12/13 21:25:26 hm Exp $
32 * $FreeBSD: src/usr.sbin/i4b/isdntel/files.c,v 1.6.2.1 2001/08/01 17:45:06 obrien Exp $
33 * $DragonFly: src/usr.sbin/i4b/isdntel/files.c,v 1.6 2005/10/30 23:00:57 swildner Exp $
35 * last edit-date: [Mon Dec 13 21:54:06 1999]
37 *----------------------------------------------------------------------------*/
39 #include "defs.h"
41 /*---------------------------------------------------------------------------*
42 * create a doubly linked list in sorted order, return pointer to new
43 * first element of list
44 *---------------------------------------------------------------------------*/
45 struct onefile *
46 store(struct onefile *new, /* new entry to store into list */
47 struct onefile *top) /* current first entry in list */
49 struct onefile *old, *p;
51 if (last == NULL) /* enter very first element ? */
53 new->next = NULL;
54 new->prev = NULL;
55 last = new; /* init last */
56 return (new); /* return new first */
58 p = top; /* p = old first element */
59 old = NULL;
60 while (p)
62 if ((strcmp(p->fname, new->fname)) < 0) /* current less new ? */
64 old = p;
65 p = p->next;
67 else
68 { /* current >= new */
70 if (p->prev)
72 p->prev->next = new;
73 new->next = p;
74 new->prev = p->prev;
75 p->prev = new;
76 return (top);
78 new->next = p;
79 new->prev = NULL;
80 p->prev = new;
81 return (new);
84 old->next = new;
85 new->next = NULL;
86 new->prev = old;
87 last = new;
88 return (first);
91 /*---------------------------------------------------------------------------*
92 * read current directory and build up a doubly linked sorted list
93 *---------------------------------------------------------------------------*/
94 int
95 fill_list(void)
97 #if defined(__DragonFly__)
98 struct dirent *dp;
99 #else
100 struct direct *dp;
101 #endif
102 struct onefile *new_entry;
103 DIR *dirp;
104 int flcnt = 0;
105 char tmp[80];
106 char *s, *d;
108 if ((dirp = opendir(spooldir)) == NULL)
109 fatal("cannot open spooldirectory %s!\n", spooldir);
111 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
113 if(!isdigit(*(dp->d_name)))
114 continue;
116 if ((new_entry = (struct onefile *) malloc(sizeof(struct onefile))) == NULL)
118 fatal("files.c, fill_list(): structure onefile malloc failed");
121 /* alloc filename memory and copy name into it */
123 if ((new_entry->fname = (char *) malloc(strlen(dp->d_name) + 1)) == NULL)
125 fatal("files.c, fill_list(): malloc filename string memory failed");
128 strcpy(new_entry->fname, dp->d_name);
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 = (char *) malloc(strlen(tmp) + 1)) == NULL)
144 fatal("files.c, fill_list(): malloc date string memory failed");
147 strcpy(new_entry->date, tmp);
149 tmp[0] = dp->d_name[6]; /* hour msb */
150 tmp[1] = dp->d_name[7]; /* hour lsb */
151 tmp[2] = ':';
152 tmp[3] = dp->d_name[8]; /* minute msb */
153 tmp[4] = dp->d_name[9]; /* minute lsb */
154 tmp[5] = ':';
155 tmp[6] = dp->d_name[10]; /* second msb */
156 tmp[7] = dp->d_name[11]; /* second lsb */
157 tmp[8] = '\0';
159 if((new_entry->time = (char *) malloc(strlen(tmp) + 1)) == NULL)
161 fatal("files.c, fill_list(): malloc time string memory failed");
164 strcpy(new_entry->time, tmp);
166 /* destination number */
168 s = &dp->d_name[13];
169 d = &tmp[0];
171 while(*s && (*s != '-'))
172 *d++ = *s++;
174 *d = '\0';
176 if((new_entry->dstnumber = (char *) malloc(strlen(tmp) + 1)) == NULL)
178 fatal("files.c, fill_list(): malloc dstnumber string memory failed");
181 strcpy(new_entry->dstnumber, tmp);
183 /* source number */
185 s++;
186 d = &tmp[0];
188 while(*s && (*s != '-'))
189 *d++ = *s++;
191 *d = '\0';
193 if((new_entry->srcnumber = (char *) malloc(strlen(tmp) + 1)) == NULL)
195 fatal("files.c, fill_list(): malloc srcnumber string memory failed");
198 strcpy(new_entry->srcnumber, tmp);
200 /* length in seconds */
202 s++;
203 d = &tmp[0];
205 while(*s && (*s != '-'))
206 *d++ = *s++;
208 *d = '\0';
210 if((new_entry->seconds = (char *) malloc(strlen(tmp) + 1)) == NULL)
212 fatal("files.c, fill_list(): malloc seconds string memory failed");
215 strcpy(new_entry->seconds, tmp);
217 /* search for alias and add if found */
219 new_entry->alias = get_alias(new_entry->srcnumber);
221 /* sort entry into linked list */
223 first = store(new_entry, first);
225 flcnt++; /* increment file count */
227 closedir(dirp); /* close current dir */
228 return(flcnt); /* ok return */
231 /*---------------------------------------------------------------------------*
232 * free the current malloc'ed list
233 *---------------------------------------------------------------------------*/
234 void
235 free_list(void)
237 struct onefile *dir;
238 struct onefile *tmp;
240 dir = first; /* start of linked list */
242 while (dir) /* free all */
244 tmp = dir->next; /* save ptr to next entry */
245 free(dir->fname); /* free filename space */
246 free(dir->date);
247 free(dir->time);
248 free(dir->srcnumber);
249 free(dir->dstnumber);
250 free(dir->seconds);
251 free(dir); /* free struct space */
252 dir = tmp; /* ptr = ptr to next entry */
254 first = NULL; /* first ptr = NULL */
255 last = NULL; /* last ptr = NULL */
258 /*---------------------------------------------------------------------------*
259 * delete a file
260 *---------------------------------------------------------------------------*/
261 void
262 delete(struct onefile *this)
264 char buffer[MAXPATHLEN+1];
266 if(this == NULL)
267 return;
269 sprintf(buffer, "%s", this->fname);
271 unlink(buffer);
273 free_list();
275 wclear(main_w);
277 init_files(cur_pos);
280 /*---------------------------------------------------------------------------*
281 * reread the spool directory
282 *---------------------------------------------------------------------------*/
283 void
284 reread(void)
286 free_list();
288 wclear(main_w);
290 init_files(cur_pos);
293 /*---------------------------------------------------------------------------*
294 * play a file
295 *---------------------------------------------------------------------------*/
296 void
297 play(struct onefile *this)
299 char buffer[MAXPATHLEN+1];
301 if(this == NULL)
302 return;
304 sprintf(buffer, playstring, this->fname);
306 system(buffer);
309 /*---------------------------------- EOF -------------------------------------*/