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

📄 extras.c

📁 GSM猫管理程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*SMS Server Tools 3Copyright (C) Keijo Kasvihttp://smstools3.kekekasvi.com/Based on SMS Server Tools 2 from Stefan Fringshttp://www.meinemullemaus.de/This program is free software unless you got it under another license directlyfrom the author. You can redistribute it and/or modify it under the terms ofthe GNU General Public License as published by the Free Software Foundation.Either version 2 of the License, or (at your option) any later version.*/#include <sys/types.h>#include <sys/stat.h>#include <string.h>#include <errno.h>#include <limits.h>#include <unistd.h>#include <stdlib.h>#include <dirent.h>#include <fcntl.h>#include <ctype.h>#include <sys/wait.h>#include <time.h>#include <syslog.h>#include "extras.h"#include "locking.h"#include "smsd_cfg.h"#include "logging.h"#include "alarm.h"int yesno(char *value){  extern char yes_chars[];  extern char no_chars[];  char *p, *p2;  if (*yes_chars)  {    p = yes_chars;    while (*p)    {      if (!(p2 = strchr(p, '\'')))        break;      if (!strncmp(value, p, (int)(p2 -p)))        return 1;      p = p2 +1;    }  }  if (*no_chars)  {    p = no_chars;    while (*p)    {      if (!(p2 = strchr(p, '\'')))        break;      if (!strncmp(value, p, (int)(p2 -p)))        return 0;      p = p2 +1;    }  }  if ((value[0]=='1') ||      (value[0]=='y') ||      (value[0]=='Y') ||      (value[0]=='t') ||      (value[0]=='T') ||      ((value[1]=='n') && (        (value[0]=='o') ||        (value[0]=='O'))      ))    return 1;  else    return 0;}int yesno_check(char*  value){  // This version is used to check config file values.  int result = -1;  if ((value[0]=='1') ||      (value[0]=='y') ||      (value[0]=='Y') ||      (value[0]=='t') ||      (value[0]=='T') ||      ((value[1]=='n') && (        (value[0]=='o') ||        (value[0]=='O'))      ))    result = 1;  else  if ((value[0]=='0') ||      (value[0]=='n') ||      (value[0]=='N') ||      (value[0]=='f') ||      (value[0]=='F') ||      ((value[1]=='f') && (        (value[0]=='o') ||        (value[0]=='O'))      ))    result = 0;  return result;}char *cut_ctrl(char* message) /* removes all ctrl chars */{  // 3.0.9: use dynamic buffer to avoid overflow:  //char tmp[500];  char *tmp;  int posdest=0;  int possource;  int count;  count=strlen(message);  if ((tmp = (char *)malloc(count +1)))  {    for (possource=0; possource<=count; possource++)    {      // 3.1beta7: added unsigned test:      if (((unsigned char)message[possource] >= (unsigned char)' ') || (message[possource]==0))        tmp[posdest++]=message[possource];    }    strcpy(message,tmp);    free(tmp);  }  return message;}int is_blank(char c){  return (c==9) || (c==32);}int line_is_blank(char *line){  int i = 0;  while (line[i])    if (strchr("\t \r\n", line[i]))      i++;    else      break;  return(line[i] == 0);}int movefile( char*  filename,  char*  directory){  char newname[PATH_MAX];  char storage[1024];  int source,dest;  int readcount;  char* cp;  struct stat statbuf;  if (stat(filename,&statbuf)<0)    statbuf.st_mode=0640;  statbuf.st_mode&=07777;  cp=strrchr(filename,'/');  if (cp)    sprintf(newname,"%s%s",directory,cp);  else    sprintf(newname,"%s/%s",directory,filename);  source=open(filename,O_RDONLY);  if (source>=0)  {    dest=open(newname,O_WRONLY|O_CREAT|O_TRUNC,statbuf.st_mode);    if (dest>=0)    {      while ((readcount=read(source,&storage,sizeof(storage)))>0)        if (write(dest,&storage,readcount)<readcount)	{	  close(dest);	  close(source);	  return 0;	}      close(dest);      close(source);      unlink(filename);      return 1;    }    else    {      close(source);      return 0;    }  }  else    return 0;}// 3.0.9: Return values:// 0 = OK.// 1 = lockfile cannot be created. It exists.// 2 = file copying failed.// 3 = lockfile removing failed.int movefilewithdestlock( char*  filename,  char*  directory){  char lockfilename[PATH_MAX];  char* cp;  //create lockfilename in destination  cp=strrchr(filename,'/');  if (cp)    sprintf(lockfilename,"%s%s",directory,cp);  else    sprintf(lockfilename,"%s/%s",directory,filename);  //create lock and move file  if (!lockfile(lockfilename))    return 1;  if (!movefile(filename,directory))  {    unlockfile(lockfilename);    return 2;  }  if (!unlockfile(lockfilename))    return 3;  return 0;}// 3.1beta7: Return values:// 0 = OK.// 1 = lockfile cannot be created. It exists.// 2 = file copying failed.// 3 = lockfile removing failed.int movefilewithdestlock_new(char* filename, char* directory, int keep_fname, int store_original_fname, char *prefix, char *newfilename){  if (newfilename)    *newfilename = 0;  if (keep_fname)  {    char lockfilename[PATH_MAX];    char* cp;    //create lockfilename in destination    cp=strrchr(filename,'/');    if (cp)      sprintf(lockfilename,"%s%s",directory,cp);    else      sprintf(lockfilename,"%s/%s",directory,filename);    //create lock and move file    if (!lockfile(lockfilename))      return 1;    if (!movefile(filename,directory))    {      unlockfile(lockfilename);      return 2;    }    if (!unlockfile(lockfilename))      return 3;    if (newfilename)      strcpy(newfilename, lockfilename);    return 0;  }  else  {    // A new unique name is created to the destination directory.    char newname[PATH_MAX];    int result = 0;    char line[1024];    int in_headers = 1;    FILE *fp;    FILE *fpnew;    size_t n;    char *p;    extern const char *HDR_OriginalFilename;    extern char HDR_OriginalFilename2[];    strcpy(line, prefix);    if (*line)      strcat(line, ".");    sprintf(newname,"%s/%sXXXXXX", directory, line);    close(mkstemp(newname));    if (!lockfile(newname))      result = 1;    unlink(newname);    if (!result)    {      if (!(fpnew = fopen(newname, "w")))        result = 2;      else      {        if (!(fp = fopen(filename, "r")))        {          fclose(fpnew);          unlink(newname);          result = 2;        }        else        {          while (in_headers && fgets(line, sizeof(line), fp))          {            if (line_is_blank(line))            {              if (store_original_fname && *HDR_OriginalFilename2 != '-')              {                p = strrchr(filename, '/');                fprintf(fpnew, "%s %s\n", (*HDR_OriginalFilename2)? HDR_OriginalFilename2 : HDR_OriginalFilename,                        (p)? p +1 : filename);              }              in_headers = 0;            }            fwrite(line, 1, strlen(line), fpnew);          }          while ((n = fread(line, 1, sizeof(line), fp)) > 0)            fwrite(line, 1, n, fpnew);          fclose(fpnew);          fclose(fp);        }      }    }    if (!unlockfile(newname))    {      unlink(newname);      if (!result)        result = 3;    }    else    {      unlink(filename);      if (newfilename)        strcpy(newfilename, newname);    }    return result;  }}char *cutspaces(char *text){  int count;  int Length;  int i;  int omitted;  /* count ctrl chars and spaces at the beginning */  count=0;  while ((text[count]!=0) && ((is_blank(text[count])) || (iscntrl(text[count]))) )    count++;  /* remove ctrl chars at the beginning and \r within the text */  omitted=0;  Length=strlen(text);  for (i=0; i<=(Length-count); i++)    if (text[i+count]=='\r')      omitted++;    else      text[i-omitted]=text[i+count];  Length=strlen(text);  while ((Length>0) && ((is_blank(text[Length-1])) || (iscntrl(text[Length-1]))))  {    text[Length-1]=0;    Length--;  }  return text;}void cut_emptylines(char*  text){  char* posi;  char* found;  posi=text;  while (posi[0] && (found=strchr(posi,'\n')))  {    if ((found[1]=='\n') || (found==text))      memmove(found,found+1,strlen(found));    else      posi++;  }}int is_number( char*  text){  int i;  int Length;  Length=strlen(text);  for (i=0; i<Length; i++)    if (((text[i]>'9') || (text[i]<'0')) && (text[i]!='-'))      return 0;  return 1;}int is_highpriority(char *filename){  FILE *fp;  char line[256];  int result = 0;  // 3.1beta7: language settings used:  extern const char *HDR_Priority;  extern char HDR_Priority2[];  int hlen;  int hlen2;  char *compare;  char *compare2 = 0;  // get_header() and test_header() can be moved to this file,  // but this is faster:  if (*HDR_Priority2 && strcmp(HDR_Priority2, "-"))  {    if (*HDR_Priority2 == '-')      compare2 = HDR_Priority2 +1;    else      compare2 = HDR_Priority2;    hlen2 = strlen(compare2);  }  compare = (char *)HDR_Priority;  hlen = strlen(compare);  if ((fp = fopen(filename, "r")))  {    while (!result && fgets(line, sizeof(line), fp))    {      if (line_is_blank(line))        break;      if ((compare2 && strncmp(line, compare2, hlen2) == 0) ||          strncmp(line, compare, hlen) == 0)      {        cutspaces(strcpy(line, line +hlen));        if (!strcasecmp(line,"HIGH"))          result = 1;        else if (yesno(line) == 1)          result = 1;      }    }    fclose(fp);  }  return result;}int file_is_writable(char *filename){  int result = 0;  FILE *fp;  if ((fp = fopen(filename, "a")))  {    result = 1;    fclose(fp);  }  return result;}int getpdufile(char *filename){  int result = 0;  struct stat statbuf;  DIR* dirdata;  struct dirent* ent;  char tmpname[PATH_MAX];  if (*filename)  {    if (filename[strlen(filename) -1] != '/')    {      if (stat(filename, &statbuf) == 0)        if (S_ISDIR(statbuf.st_mode) == 0)          if (file_is_writable(filename))            result = 1;    }    else if (!strchr(filename, '.'))    {      if (stat(filename, &statbuf) == 0)      {        if (S_ISDIR(statbuf.st_mode))        {          if ((dirdata = opendir(filename)))          {            while ((ent = readdir(dirdata)))            {              if (ent->d_name[0] != '.')              {                sprintf(tmpname, "%s%s", filename, ent->d_name);                stat(tmpname, &statbuf);                if (S_ISDIR(statbuf.st_mode) == 0)                {                  if (file_is_writable(tmpname))                  {                    strcpy(filename, tmpname);                    result = 1;                    break;                  }                }              }            }          }        }      }    }  }  return result;}int getfile(char *dir, char *filename){  DIR* dirdata;  struct dirent* ent;  struct stat statbuf;  int found=0;  time_t mtime = 0;  char fname[PATH_MAX];  char tmpname[PATH_MAX];  int found_highpriority = 0;  int i;#ifdef DEBUGMSG  printf("!! getfile(dir=%s, ...)\n", dir);#endif  // Oldest file is searched. With heavy traffic the first file found is not necesssary the oldest one.  if (!(dirdata = opendir(dir)))  {    // Something has happened to dir after startup check was done successfully.    writelogfile0(LOG_CRIT, process_title, tb_sprintf("Stopping. Cannot open dir %s %s", dir, strerror(errno)));    alarm_handler0(LOG_CRIT, process_title, tb);    abnormal_termination(1);

⌨️ 快捷键说明

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