Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / share / examples / isdn / contrib / answer.c
1 // $FreeBSD: src/share/examples/isdn/contrib/answer.c,v 1.1.2.1 2001/08/10 14:59:47 obrien Exp $
2 // $DragonFly: src/share/examples/isdn/contrib/answer.c,v 1.2 2003/06/17 04:36:57 dillon Exp $
3 // changed ".g711a" to ".al" (-hm)
4 // Tue Mar  3 02:42:14 MET 1998         dave@turbocat.de
5 // started
6
7 #define BLK_SIZE        2048
8 #define SOX             "/usr/local/bin/sox"
9 #define ALAWULAW        "/usr/local/bin/alaw2ulaw"
10
11 #include <stdio.h>
12 #include <time.h>
13
14         FILE    *device;
15         FILE    *logfile;
16         char    srcNum[30];
17         char    destNum[30];
18         char argbuf[255];
19         char tmpBuf[1024] = "";
20
21
22 void writeToPhone (char *path)
23 {
24         char    buf[BLK_SIZE];
25         FILE    *srcfile;
26         int             i = 0;
27         int             readcount = 0;
28
29         srcfile = fopen(path,"r");
30         if (srcfile) {
31                 for (i=0;i<BLK_SIZE;i++) {
32                         buf[i] = '\0';
33                 }
34                 readcount = BLK_SIZE;
35                 i = 0;
36                 do {
37                 readcount = fread(buf,1, BLK_SIZE, srcfile);
38                 fwrite(buf, 1, readcount, device);
39                         i = readcount + i;
40 //                      fprintf(logfile,"%d read (%d)\n",i,readcount);
41                 } while (readcount == BLK_SIZE);
42         
43                 fclose(srcfile);
44         } else {
45                 fprintf(logfile,"Can't open file '%s'\n",path);
46         }
47 }
48
49 void readFromPhone (char *path)
50 {
51         char    buf[BLK_SIZE];
52         FILE    *destfile;
53         int             i = 0;
54         int             readcount = 0;
55
56         destfile = fopen(path,"a");
57         if (destfile) {
58                 for (i=0;i<BLK_SIZE;i++) {
59                         buf[i] = '\0';
60                 }
61                 readcount = BLK_SIZE;
62                 i = 0;
63                 do {
64                 readcount = fread(buf,1, BLK_SIZE, device);
65                 fwrite(buf, 1, readcount, destfile);
66                         i = readcount + i;
67 //                      fprintf(logfile,"%d read (%d)\n",i,readcount);
68                 } while (readcount == BLK_SIZE);
69         
70                 fclose(destfile);
71         } else {
72                 fprintf(logfile,"Can't open file '%s'\n",path);
73         }
74 }
75
76 void usage (void)
77 {
78         fprintf(stderr,"Usage: answer -D device -d destination -s source\n");
79         exit(1); 
80 }
81
82 const char * argWithName (const char* aName)
83 {
84         // '-D /dev/null -d 82834 -s 3305682834'
85         int i = 0;
86         int optionSeen = 0;
87         int startpos = 0;
88
89         for (i = 0; i < sizeof(tmpBuf);i++) {
90                 tmpBuf[i] = '\0';
91         }
92
93         for (i = 0; i<strlen(argbuf);i++) {
94                 if (optionSeen) {
95                         for (;(i<strlen(argbuf) && (argbuf[i] != ' '));i++) {
96                         }
97                         i++;
98                         startpos = i;
99
100                         for (;(i<strlen(argbuf) && (argbuf[i] != ' '));i++) {
101                         }
102                         strncpy(tmpBuf,&argbuf[startpos], i-startpos);
103
104                         return tmpBuf;
105                 }
106                 if (0 == strncmp(aName,&argbuf[i], strlen(aName))) {
107                         optionSeen = 1;
108                 }
109         }
110
111         usage();
112         return NULL;
113 }
114
115 int main (int argc, const char *argv[]) {
116
117         int i,pos = 0;
118         extern char *optarg;
119         extern int optind;
120         int bflag, ch;
121         char timeStr[50];
122         char outfileName[1024] = "";
123         char cmdStr[2048] = "";
124         time_t now;
125
126         now=time(NULL);
127
128         strftime(timeStr,40,I4B_TIME_FORMAT,localtime(&now));
129
130         logfile = fopen("/var/log/answer.log","a");
131
132         fprintf(logfile,"%s Started\n",timeStr);
133
134         pos=0;
135         for (i=1;i<argc;i++) {
136                 sprintf(&argbuf[strlen(argbuf)],"%s ",argv[i]);
137         }
138         if (strlen(argbuf) > 2) {
139                 argbuf[strlen(argbuf)-1] = '\0';
140         }
141
142
143         device = fopen(argWithName("-D"),"r+");
144         strcpy(destNum, argWithName("-d"));
145         strcpy(srcNum, argWithName("-s"));
146
147                 fprintf(logfile,"device '%s'\n", argWithName("-D"));
148                 fprintf(logfile,"srcNum '%s'\n", srcNum);
149                 fprintf(logfile,"destNum '%s'\n", destNum);
150
151
152         if (device) {
153
154                 strftime(timeStr,40,I4B_TIME_FORMAT,localtime(&now));
155
156                 sprintf(outfileName,"/var/isdn/%s_%s_%s", timeStr, srcNum, destNum);
157
158                 writeToPhone ("/usr/local/lib/isdn/msg.al");
159                 readFromPhone (outfileName);
160
161                 sprintf(cmdStr,"/bin/cat %s | %s | %s -t raw -U -b -r 8000 - -t .au %s.snd", outfileName, ALAWULAW, SOX, outfileName);
162                 fprintf(logfile,"%s\n",cmdStr);
163                 system(cmdStr);
164                 unlink(outfileName);
165
166                 fclose(device);
167         } else {
168                 fprintf(logfile,"Can't open file '%s'\n",argWithName("-D"));
169         }
170
171         now=time(NULL);
172
173         strftime(timeStr,40,I4B_TIME_FORMAT,localtime(&now));
174
175         fprintf(logfile,"%s Done\n",timeStr);
176         fclose(logfile);
177     exit(0);       // insure the process exit status is 0
178     return 0;      // ...and make main fit the ANSI spec.
179 }