⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 danpage.c

📁 使用Modem发送文字信息给网上传呼
💻 C
📖 第 1 页 / 共 2 页
字号:
 fprintf(stderr, errstr); cleanup(2); exit(2);}int init (void) { pid_t pid; if (strstr(device, "/dev/") == 0) {  if (strchr(device,'/')) abort("Invalid device name");  else sprintf(device,"/dev/%s",device); } if (!ignorelock) {  do {   sprintf(lockfile1,"%s/LCK..", lockdir);   if ( (i=readlink(device,devbuf,devbufsiz)) == -1)    strcat(lockfile1, (char *)(rindex(device,'/')+1));   else {    devbuf[i]=0;    strcat(lockfile1, devbuf);   }   if ((lock=open (lockfile1, O_RDWR | O_CREAT | O_EXCL, 0444)) == -1) {    if (!wait_for_lock) {     sprintf(str, "Failed opening LCK file %s",lockfile1);     abort(str);    }   }   else {    pid = getpid(); write(lock, &pid, sizeof(pid)); close(lock);   }  } while ( wait_for_lock && (lock == -1) ); } alarm(30); if ((modem = open(device, O_RDWR, 0)) == -1) {  sprintf(str, "Failed opening modem device %s",device); abort(str); }  if (tcgetattr(modem, &t) || tcgetattr(modem, &t_old)) return (-1); t.c_cc[VMIN] = 1; t.c_cc[VTIME] = 0; t.c_iflag &= ~(BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP |	          INLCR | IGNCR | ICRNL | IXON | ICANON); t.c_iflag |= (IGNBRK | IGNPAR | ISTRIP); t.c_oflag = (0); t.c_lflag = (0); t.c_cflag &= ~(CSTOPB | CSIZE | PARODD); t.c_cflag |= (HUPCL | CLOCAL | CREAD | CS7 | PARENB);switch(bps) {  case 300   : speed = B300; break;  case 1200  : speed = B1200; break;  case 2400  : speed = B2400; break;  case 4800  : speed = B4800; break;  case 9600  : speed = B9600; break;  case 19200 : speed = B19200; break;  case 38400 : speed = B38400; break;  default    : speed = B1200; } if (cfsetispeed(&t, speed) == -1) return (-1); if (cfsetospeed(&t, speed) == -1) return (-1); if (tcflush(modem, TCIFLUSH) == -1) return (-1); if (tcsetattr(modem,TCSANOW, &t) == -1) return (-1); return (0);}void timed_out(void) { abort("Timed out.");}void sig_term(void) { abort("Received SIGTERM (Termination signal from kernel).");}void sig_int(void) { abort("Received SIGINT (User Interrupt).");}void sig_segv(void) { abort("Received SIGSEGV (Segmentation Fault)" \       " Please send log file to danbaker@iquest.net ");}void hangup_modem(void) { sleep(3); write(modem, "+++", 3); sleep(3); write(modem, hangup_str, strlen(hangup_str));}void usage(void) { fprintf(stderr, "%sUsage:\n" " danpage -p <pager#> [-c <central#>] [-d <device>] [-r <redials>]\n" \ "         [-t <length>] [-m <length>] [-M <length>] [-b <bits/sec>]\n" \ "         [-w] [-s] [-q] [-i] [-u]\n" \ " (takes message from standard input, end with ^D)\n" \ "For more instructions, type 'man danpage'\n",copyright); quiet = TRUE; wrt_log("Displayed usage message and exited.\n"); exit(1);}int calc_sum(char *string1) {int i,isum = 0; for (i=0;i<strlen(string1);i++) isum += string1[i]; for (i=2; i>=0; i--) sum[2-i] = (char) (0x30 | ((isum >> (i*4))& 0x000f)); sum[3] = 0;}void read_msg(void) { int i = 0; maxmessage = truncatesize - strlen(sig) - 1; while(( (read(STDIN_FILENO, message+i, 1)) == 1 ) && (i < maxmessage)) {  if (message[i] == CR) message[i] = LF;  if ((message[i] != LF) && iscntrl(message[i])) message[i] = ' ';  if ( (message[i] == LF) && (i > 0) && (message[i-1] == LF) )   continue;  i++; } message[i] = 0; while ( strlen(message) && (message[strlen(message)-1] == LF) )  message[strlen(message)-1] = 0; /* strip off any trailing linefeeds */}void waitfor(char ch) {char c; do if (read(modem, &c , 1) == 1) log_status_msg(c);  while (c != ch);}int waitfor_ack(void) {int Linux_Rules = TRUE; while(Linux_Rules) {  while (read(modem, &ch, 1) != 1); /* wait for a character */  log_status_msg(ch);  if ((ch == ACK) || (ch == NAK) || (ch == EOT) || (ch == RS)) return(ch); }}void init_modem(void) { if (strlen(modem_init)) {  wrt_log("Initializing modem...\n");  write(modem, modem_init, strlen(modem_init));   write(modem, "\r", 1);  waitfor('K'); }}int dial_modem(void) {int i; do {  redials--;  wrt_log("Dialing...\n");  sleep(1);  write(modem, dial_prefix, strlen(dial_prefix));  write(modem, central, strlen(central));  write(modem, dial_suffix, strlen(dial_suffix));  if ((i=connect_modem()) == 0) {   wrt_log("Connect!\n");   return 0;  }  else if (i==1) {   wrt_log("BUSY\n");   continue;  } } while (redials >= 0); abort("BUSY");}int connect_modem(void) {char c;int i;char line[maxpacket]; for (i=0; i<strlen(line);i++) line[i] = 0; i = 0; while (i<maxpacket) {  if (read(modem, &c, 1)==1) {   log_status_msg(c);   line[i] = c; line[i+1] = 0; i++;   if (strstr(line,"CONNECT") != 0) return 0;   if (strstr(line,"BUSY") != 0) return 1;   if (strstr(line,"NO DIALTONE") != 0) abort("NO DIALTONE");  } } }void freadln(int fd) {int rdstat;char c;int i = 0; while (((rdstat = read(fd, &c, 1)) == 1) && (i<199) && (c != '\n') ) {  if ((c == EOF) || (rdstat != 1)) { line[i] = 0; done_rc = TRUE; return; }  line[i] = c; i++; } line[i] = 0; if (rdstat != 1) done_rc = TRUE;}void get_parms(void) {char ch; nxtparmptr = 0;/* scan to first non-space,non-tab,non'=' char , set parmptr*/ ch = parmptr[0]; while ( (ch==' ') || (ch=='=') || (ch=='\t') ) {parmptr++; ch = parmptr[0]; } ptr = parmptr;/* scan to next space, tab, or '=' char, set to zero */ ch = ptr[0]; while ( (ch != ' ') && (ch != '=') && !iscntrl(ch) ) {  if ( (ch == 0) || (ch == CR) || (ch == LF) )   {nxtparmptr = 0; return;}  ptr++;  if (ptr[0] == 0) return;  ch = ptr[0]; } ptr[0] = 0; ptr++; if (ptr[0] == 0) return;/* scan to next non-space,non-tab,non'=' char, set nxtparmptr*/ ch = ptr[0]; while ( (ch == ' ') || (ch == '=') || (ch == '\t') ) {ptr++; ch = ptr[0]; } nxtparmptr = ptr; if (parmptr) {  if (str_nocase_equ(parmptr,"central") || str_nocase_equ(parmptr,"device")) {   while (isgraph(ptr[0])) ptr++;   ptr[0] = 0;  }  else if ( str_nocase_equ(parmptr,"redials")          || str_nocase_equ(parmptr,"bps")         || str_nocase_equ(parmptr,"truncate")         || str_nocase_equ(parmptr,"packetsize")         || str_nocase_equ(parmptr,"splitsize") ) {   while (isdigit(ptr[0])) ptr++;   ptr[0] = 0;  }  else if (str_nocase_equ(parmptr,"sig")) {    while (isprint(ptr[0])) ptr++;    ptr[0] = 0;  } }}int str_nocase_equ(char *str1, char *str2) { return ( strncasecmp(str1, str2, strlen(str1)) == 0 );}void read_rc(char *rc, int funct) { /* open rc file if possible */ done_rc = FALSE; if (rc_fd = open(rc, O_RDONLY, 0)) {  do {   freadln(rc_fd);   if (line && strlen(line)) {    if ((line[0] != '#') && (line[0] != ';')) {     parmptr = line;     get_parms();     if (parmptr && strlen(parmptr)) {      if (funct == PARM) {       if (str_nocase_equ(parmptr, "modem_init")) {        if (nxtparmptr && strlen(nxtparmptr))         strcpy(modem_init, nxtparmptr);        else modem_init[0] = 0;       }       if (str_nocase_equ(parmptr, "modem_restore")) {        if (nxtparmptr && strlen(nxtparmptr))         strcpy(modem_restore, nxtparmptr);        else modem_restore[0] = 0;       }       else if (str_nocase_equ(parmptr, "central")) {        if (nxtparmptr && strlen(nxtparmptr))         strcpy(central, nxtparmptr);       }       else if (str_nocase_equ(parmptr, "device")) {        if (nxtparmptr && strlen(nxtparmptr))         strcpy(device, nxtparmptr);       }       else if (str_nocase_equ(parmptr, "redials")) {        if (nxtparmptr && strlen(nxtparmptr))         redials = atoi(nxtparmptr);       }       else if (str_nocase_equ(parmptr, "bps")) {        if (nxtparmptr && strlen(nxtparmptr))         bps = atoi(nxtparmptr);       }       else if (str_nocase_equ(parmptr, "truncate")) {        if (nxtparmptr && strlen(nxtparmptr))         truncatesize = atoi(nxtparmptr);       }       else if (str_nocase_equ(parmptr, "packetsize")) {        if (nxtparmptr && strlen(nxtparmptr))         packetsize = atoi(nxtparmptr);       }       else if (str_nocase_equ(parmptr, "splitsize")) {        if (nxtparmptr && strlen(nxtparmptr))         splitsize = atoi(nxtparmptr);       }       else if ( (str_nocase_equ(parmptr, "sig")) ) {        if (nxtparmptr && strlen(nxtparmptr))         strcpy(sig, nxtparmptr);       }      }      else if ((funct == ALIAS) && (str_nocase_equ(parmptr, account))) {       if ( !found_alias && nxtparmptr && strlen(nxtparmptr))        strcpy(account, nxtparmptr);       found_alias = TRUE;      }     }    }   }  } while ( !done_rc && !( (funct==ALIAS) && found_alias));  close(rc_fd); }}char *safestring(char *str) { int i,j; for (i=0,j=0; i<strlen(str); i++,j++) {  if ( ( isprint(str[i]) ) || ( ( i==strlen(str) - 1 ) && (str[i]==LF) ) )   safestr[j]=str[i];  else if ( str[i]=='\r' ) {   safestr[j++]='\\';   safestr[j]='r';  }  else if ( str[i]=='\n' ) {   safestr[j++]='\\';   safestr[j]='n';  }  else if ( str[i]=='\t' ) {   safestr[j++]='\\';   safestr[j]='t';  }  else if ( str[i]=='\f' ) {   safestr[j++]='\\';   safestr[j]='f';  }  else {   safestr[j++]='^';   safestr[j]=((str[i] + 0x40) & 0x7f);  } } safestr[j]=0; return (safestr);}void log_status_msg(char ch) { if (ch=='\r') ch = '\n'; status_msg[statptr++]=ch; status_msg[statptr]=0; if (ch == '\n') {  if ( ( statptr > 4 ) && ( strncmp(status_msg,"ID=",3) != 0 ) )   wrt_log(safestring(status_msg));  statptr=0; status_msg[statptr]=0; }}void build_packet(void) { if (packetnum == 1)  max_pkt_len = packetsize - strlen(account) - 8; else  max_pkt_len = packetsize - 7; lastpacket = ( strlen(pkt_ptr) <= max_pkt_len); if (packetnum == 1)  sprintf(packet, "%c%s\r", STX, account); else  sprintf(packet, "%c", STX); copy_pkt(); if (lastpacket)  sprintf(packet, "%s\r%c", packet, ETX); else  sprintf(packet, "%s%c", packet, US); calc_sum(packet); strcat(packet, sum); strcat(packet, "\r");}void send_packet(void) { do {  retrans--;  sprintf(str, "Writing segment %d, packet %d ...\n", segmentnum, packetnum);  wrt_log(str);  write(modem, packet, strlen(packet));  result = waitfor_ack();  if (result == ACK) {   wrt_log("Received ACK - Success!\n");   packetnum++;   break;  }  else if (result == NAK) {   wrt_log("Received NAK - retransmitting...");   continue;  }  else if (result == RS)   abort("Data format error- possibly incorrect pager ID");  else if (result == EOT) abort("Received EOT - Forced disconnect!"); } while (retrans > 0); if (result == NAK) abort("Page rejected- unknown error");}void copy_pkt(void) { int i; i = strlen(packet); while ( strlen(pkt_ptr) && (i < packetsize) ) {  packet[i++] = pkt_ptr[0];  pkt_ptr++; } packet[i] = 0;}void copy_segment(void) { int i, chars, continued; continued = FALSE; i = 0; chars = splitsize; if ( strlen(message_ptr) > splitsize ) {  continued = TRUE;  chars -= 4; } while ( strlen(message_ptr) && (i < chars) ) {  segment[i++] = message_ptr[0];  message_ptr++; } segment[i] = 0; if (continued) strcat (segment, " ...");}/* End of danpage.c */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -