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

📄 util.c

📁 Linux系统备份源代码 可基于用户自定义策略实现系统、应用数据备份
💻 C
📖 第 1 页 / 共 3 页
字号:
/***************************************************************************    file                 : util.c    begin                : Tue Feb 1 2000    copyright            : (C) 2000 by Henrik Witt-Hansen    email                : bean@daisy.net ***************************************************************************//*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * ***************************************************************************//*base configuration*/#ifdef HAVE_CONFIG_H#include <config.h>#endif/*standard includes*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdarg.h>#include <sys/wait.h>/*local headers*/#include "sitback.h"/*special includes*/#include <time.h>#include <signal.h>#include <errno.h>extern int errno;#ifdef DEBUGint enable_dbg=1;#elseint enable_dbg=0;#endif/********************************************************************  void log(char *message, ... )  Make an entry into the backup/restore log (not the system log)  arguments:    message:  Log message. printt-like format string.    ... ; arguments for the message string.  return:    none, errors are not reported********************************************************************/void log(char *message, ... ){  FILE *log;  time_t timep;  struct tm *currtime;  va_list ap;   /* if we do not have our home directory, write to the console */  if(conf__homedir==NULL)  {    /* write the message to the console */    va_start(ap,message);    vprintf(message,ap);    va_end(ap);  }  else /* else write to the logfile */  {    /*try to open the logfile*/    if((log=fopen("sitback.log","a"))==NULL)      return;    /*check if we got any data to add or just called to make a blank line*/    if(message==NULL)    {      fprintf(log,"\n");      fclose(log);      return;    }    /* write the time of the entry into the log*/    timep=time(NULL);    currtime=localtime(&timep);    fprintf(log,"[%02d/%02d-%04d %02d:%02d] \""               ,currtime->tm_mon+1,currtime->tm_mday,currtime->tm_year+1900               ,currtime->tm_hour,currtime->tm_min);    /* write the message */    va_start(ap,message);    vfprintf(log,message,ap);    va_end(ap);    /* end of entry */    fprintf(log,"\"\n");    /*close the logfile and return*/    fclose(log);  }}/********************************************************************  __dbg()  Write debug message.  Arguments:    formatstring;  printf-like string to print    ...;  arguments for the formatstring  Returns;    none********************************************************************/void __dbg(char *formatstring, ... ){  va_list ap;  static int initialized=0;  FILE *file;  /* only if debug is enabled */  if(enable_dbg)  {    if(!initialized && conf__homedir!=NULL)    {      fprintf(stderr,"[debug] switching to %s/sitback.debug\n",conf__homedir);      unlink("sitback.debug");      initialized=1;    }    if(conf__homedir==NULL)    {      /* write the message */      va_start(ap,formatstring);      vfprintf(stderr,formatstring,ap);      va_end(ap);    }    else    {      /* write the message */      if((file=fopen("sitback.debug","a"))==NULL)        return;      va_start(ap,formatstring);      vfprintf(file,formatstring,ap);      fclose(file);      va_end(ap);    }  }}/********************************************************************  void Wait_for_time_condition()  Wait for the time to match the given time-condition  arguments:    none, uses conf__***  return:    none, errors are not reported********************************************************************/void Wait_for_time_condition(){  time_t timep;  struct tm *currtime;  int count;  debug("Into Wait_for_time_condition\n");  /*return if no timelist exists*/  if(conf__timelist==NULL)    return;  while(1)  {    /*go to the top of the list*/    while(conf__timelist->prev!=NULL)      conf__timelist=conf__timelist->prev;    /*get current time*/    timep=time(NULL);    currtime=localtime(&timep);debug("currtime->tm_wday: %d - %d\n",currtime->tm_wday,conf__timelist->dayofweek);debug("currtime->tm_hourr: %d - %d\n",currtime->tm_hour,conf__timelist->hour);debug("currtime->tm_min: %d - %d\n",currtime->tm_min,conf__timelist->min);    /*check if a valid condition is present in any entry*/    for(count=0;;count++)    {      if(conf__timelist->dayofweek==currtime->tm_wday)      {        debug("Day Of Week match %d in entry %d\n",currtime->tm_wday,count);        if(conf__timelist->hour==currtime->tm_hour)        {          debug("Hour match %d in entry %d\n",currtime->tm_hour,count);          if(conf__timelist->min==currtime->tm_min)          {            debug("Minute match %d in entry %d\n",currtime->tm_min,count);            /*return control to calling function*/            return;          }        }      }      /*next entry*/      if(conf__timelist->next!=NULL)        conf__timelist=conf__timelist->next;      else        break;    }    /*wait 10 seconds before checking again. Make sure we dont miss a condition,      even if we are not called with 10 seconds intervals due to cpu overload or such*/    sleep(10);  }}/********************************************************************  void Next_time_condition()  Return a pointer to a string with a literal description  of the next time condition  arguments:    none, uses conf__***  return:    Pointer to static string with the time********************************************************************/char *Next_time_condition(){  time_t timep;  struct tm *currtime;  static char string[33];  char timestring[20];  long unsigned _now,_time,_next=70000,_first=70000;  int day,hour,min;  char days[7][12]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};  debug("Into Next_time_condition\n");  /*return if no timelist exists*/  if(conf__timelist==NULL)  {    sprintf(string,"(now)");    return string;  }  /*go to the top of the list*/  while(conf__timelist->prev!=NULL)    conf__timelist=conf__timelist->prev;  /*get current time*/  timep=time(NULL);  currtime=localtime(&timep);  sprintf(timestring,"%1d%02d%02d",currtime->tm_wday,currtime->tm_hour,currtime->tm_min);  debug("'timestring' = '%s', for current time\n",timestring);  sscanf(timestring,"%lu",&_now);  debug("'_now' = %lu\n",_now);  /* Find out what comes closest to 'now' */  for(;;)  {    /* build the timestring */    sprintf(timestring,"%1d%02d%02d",conf__timelist->dayofweek,conf__timelist->hour,conf__timelist->min);    debug("'timestring' = '%s', for list time\n",timestring);    sscanf(timestring,"%lu",&_time);      /* closer than any previous time ?? */    if( _time >= _now && _time < _next)    {      debug("_time %05lu >= _now %05lu && _time < _next %05lu\n",_time,_now,_next);      _next=_time;    }    /* Get first entry */    if( _time < _first )      _first=_time;    /*next entry*/    if(conf__timelist->next!=NULL)      conf__timelist=conf__timelist->next;    else      break;  }  debug("Time closest to 'now' is %05lu\n",_next);  debug("First time in list is %05lu\n",_first);  /* Did we find any suitable time ?? */  if( _next==70000 )    _next=_first;  /* write the string to return */  sprintf(timestring,"%05lu",_next);  sscanf(timestring,"%1d%02d%02d",&day,&hour,&min);  snprintf(string,32,"%s at %02d:%02d",days[day],hour,min);  /* return the string */  return string;}/********************************************************************  int PrintReport(char *file)  Print a file to the printer specified as REPORTPRINTER  in the script  arguments:    file:  file to print (normally sitback.report)  return:    0 on success (or what seems to be success)    -1 on obvious error********************************************************************/int PrintReport(char *file){  FILE *out;  char cmd[513];  /*if conf__report_printer starts with '//' or '\\\\', it is    a smb printer, then use smbclient from the samba package.    Else it is a local printer (or local defined printer), the    use lpd with the -P switch*/  if(!memcmp(conf__report_printer,"//",2) || !memcmp(conf__report_printer,"\\\\\\\\",2))  {    debug("Trying to print the report via SMB\n");    UI__Message("Printing report via smb\n");    /*open the temporary script-file*/    if((out=fopen("sitback.sh","w"))==NULL)    {      errcode=9;      UI__Warning("Unable to open temporary script\n");      return -1;    }    /*try to locate smbclient*/    if(system("/opt/samba/bin/smbclient &> /dev/null")!=127)    {      debug("found /opt/samba/bin/smbclient\n");      /*write print script*/      fprintf(out,"# !/bin/sh\n");      fprintf(out,"#\n");      fprintf(out,"# sitback - temporary print script\n\n");      if(conf__smb_user[0]!='\0' && conf__smb_passwd[0]=='\0')        fprintf(out,"/opt/samba/bin/smbclient %s -U %s%%s -N << EOD &> /dev/null\n",conf__report_printer,conf__smb_user);      else if(conf__smb_user[0]!='\0' && conf__smb_passwd[0]!='\0')        fprintf(out,"/opt/samba/bin/smbclient %s -U %s%%%s -N << EOD &> /dev/null\n",conf__report_printer,conf__smb_user,conf__smb_passwd);      else        fprintf(out,"/opt/samba/bin/smbclient %s -U guest%% -N << EOD &> /dev/null\n",conf__report_printer);      fprintf(out,"print sitback.report\n");      fprintf(out,"quit\nEOD\n");      fprintf(out,"exit $?\n");      fclose(out);      chmod("sitback.sh",S_IXUSR | S_IXGRP | S_IXOTH);      if(system("sh sitback.sh"))      {        errcode=20;        UI__Warning("Unable to to print via SMB using '/opt/samba/bin/smbclient'\n");        return -1;      }    }    else if(system("smbclient &> /dev/null")!=127)    {      debug("found smbclient\n");      /*write print script*/      fprintf(out,"# !/bin/sh\n");      fprintf(out,"#\n");      fprintf(out,"# sitback - temporary print script\n\n");      if(conf__smb_user[0]!='\0' && conf__smb_passwd[0]=='\0')        fprintf(out,"smbclient %s -U %s%% -N << EOD &> /dev/null\n",conf__report_printer,conf__smb_user);      else if(conf__smb_user[0]!='\0' && conf__smb_passwd[0]!='\0')        fprintf(out,"smbclient %s -U %s%%%s -N << EOD &> /dev/null\n",conf__report_printer,conf__smb_user,conf__smb_passwd);      else        fprintf(out,"smbclient %s -U guest%% -N << EOD &> /dev/null\n",conf__report_printer);      fprintf(out,"print sitback.report\nEOD\n");      fprintf(out,"exit $?\n");      fclose(out);      chmod("sitback.sh",S_IXUSR | S_IXGRP | S_IXOTH);      if(system("sh sitback.sh"))      {        errcode=20;        UI__Warning("Unable to print via SMB using 'smbclient'\n");        return -1;      }    }    else    {      errcode=21;      UI__Warning("Unable to locate smbclient from the samba package. SMB printing not possible\n");      fclose(out);      return -1;    }  }  else  {    debug("Trying to print the report via LPD\n");    UI__Message("Printing report via lpd\n");    /*let lpr handle this task*/    if(strlen(file)>256)    {      errcode=22;      UI__Warning("Invalid or too long filename\n");      return -1;    }    sprintf(cmd,"lpr -P %s %s",conf__report_printer,file);    if(system(cmd))    {      UI__Warning("Unable to print via LPD using 'lpr'\n");      errcode=23;      return -1;    }  }  /*no problems*/  return 0;}/********************************************************************  int WriteReport(char *file,char *output)  Write the report in 'file' to the file specified as  'output in the script.  arguments:    file:  file to write from (normally sitback.report)    output: name of output file  return:    0 on success (or what seems to be success)    -1 on obvious error********************************************************************/int WriteReport(char *file,char *output){  FILE *in;  FILE *out;  char ch;  char strn[1024];  /*open the source file*/  if((in=fopen(file,"r"))==NULL)  {    errcode=24;    UI__Warning("Unable to  open '%s'\n",file);    return -1;  }  /*open the destination file*/  if((out=fopen(output,"w"))==NULL)  {    errcode=25;    sprintf(strn,"Unable to create or overwrite '%s'\n",output);    UI__Warning(strn);    fclose(in);    return -1;  }

⌨️ 快捷键说明

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