📄 init.c
字号:
/* done */ debug("Archive is remote. Host name is '%s', user name is '%s'\n",conf__rhost,conf__ruser); } else { debug("Archive is local\n"); } break; } case 'i': { /*make sure we did not get to much data. Dont rely on strlen yet*/ if(memchr(argv[__argc],'\0',256)==NULL) { errcode=6; printf("The archive id string is too long. Maximum 256 characters\n"); return -1; } /*ok, now we can do strlen*/ /*store the device or filename*/ strcpy(conf__archive_id,argv[__argc]); /*check for invalid characters*/ for(count=0;count<strlen(conf__archive_id);count++) { if(conf__archive_id[count]==' ') conf__archive_id[count]='_'; if(conf__archive_id[count]=='/') conf__archive_id[count]='_'; if(conf__archive_id[count]=='.') conf__archive_id[count]='_'; if(conf__archive_id[count]==',') conf__archive_id[count]='_'; if(conf__archive_id[count]=='\\') conf__archive_id[count]='_'; } debug("Archive Id forced to be \"%s\"\n",conf__archive_id); break; } case 'j': { if(strlen(argv[__argc])>=1024) { printf("Script or command name is too long for '-j' switch..\n"); errcode=6; return -1; } strcpy(conf__before,argv[__argc]); break; } case 'k': { if(strlen(argv[__argc])>=1024) { printf("Script or command name is too long for '-j' switch..\n"); errcode=6; return -1; } strcpy(conf__after,argv[__argc]); break; } case 'x': { debug("Reading data for exclude list\n"); /* do we already have an exclude list */ if(conf__exclude_files!=NULL) { printf("Already have one exclude list (-x switch duplicated)\n"); errcode=62; return -1; } /* allocate buffer */ if((conf__exclude_files=(char*) malloc(strlen(argv[__argc])+1))==NULL) { printf("Failed to allocate buffer for exclude list\n"); errcode=11; return -1; } /*get the exclude list. Translate and check for errors */ strcpy(conf__exclude_files,argv[__argc]); for(count=0;count<strlen(conf__exclude_files);count++) { if(conf__exclude_files[count]==' ') { printf("Blankspaces not allowed in exclude list\n"); errcode=63; return -1; } if(conf__exclude_files[count]==',') conf__exclude_files[count]='\n'; } debug("Exclude list from command line '%s'\n",conf__exclude_files); break; } case 's': { debug("Reading data for script directory\n"); snprintf(conf__scriptdir,255,argv[__argc]); if(conf__scriptdir[strlen(conf__scriptdir)-1]!='/') strcat(conf__scriptdir,"/"); debug("Script directory is '%s'\n",conf__scriptdir); break; } default: { errcode=254; printf("Oups.. try to read data for unknown switch '-%c'\n",prev_switch); return -1; } } } /*clear the prev_switch variable to indicate that we got the data we were waiting for*/ prev_switch=0; } } /*if we are expecting data at this stage, something is wrong*/ if(prev_switch!=0 && prev_switch!='m' && prev_switch!='c') { errcode=4; printf("Missing data for the '-%c' switch\n",prev_switch); return -1; } /*return the number of arguments handled*/ return __argc-1;}/******************************************************************** int __parse_timeinfo(char *timestring) Parse a string with time-info arguments: timestring: zero-terminated string with time-info return: 0 on success -1 on error********************************************************************/int __parse_timestring(char *timestring){ int stage=0; char yy=0; unsigned int count=0; int dayofweek=-1; int hour=-1; int minute=-1; char strn[1024]; debug("timestring is '%s'\n",timestring); /*simpel stage-machine parser to read the time info*/ for(count=0;count<strlen(timestring);count++) { /*next character*/ yy=timestring[count]; if(stage==0) { switch(yy) { case ' ': break; /*if timeinfo is read from the script, blankspaces may occur*/ case '0': case '1': case '2': case '3': case '4': case '5': case '6': dayofweek=yy-'0'; stage=1; break; default : sprintf(strn,"Invalid day-of-week number '%c' in time string\n",yy); UI__Warning(strn); return -1; } } else if(stage==1) { switch(yy) { case ';': stage=4; break; default : sprintf(strn,"Bad time string at position %d\n",count); UI__Warning(strn); return -1; } } else if(stage==4) { switch(yy) { case '0': case '1': case '2': hour=(yy-'0')*10; stage=5; break; default : sprintf(strn,"Hey, no more than 24 hours in a day\n"); UI__Warning(strn); return -1; } } else if(stage==5) { switch(yy) { case '0': case '1': case '2': case '3': case '4': hour+=yy-'0'; stage=6; break; case '5': case '6': case '7': case '8': case '9': { if(hour==20) { sprintf(strn,"Hey, no more than 24 hours in a day\n"); UI__Warning(strn); return -1; } hour+=yy-'0'; stage=6; break; } default : sprintf(strn,"Day-of-week number must be 2 characters wide (fex. '02')\n"); UI__Warning(strn); return -1; } } else if(stage==6) { switch(yy) { case ';': stage=7; break; default : sprintf(strn,"Bad time string at position %d\n",count); UI__Warning(strn); return -1; } } else if(stage==7) { switch(yy) { case '0': case '1': case '2': case '3': case '4': case '5': minute=(yy-'0')*10; stage=8; break; default : sprintf(strn,"Hey, a minute has only 60 minutes\n"); UI__Warning(strn); return -1; } } else if(stage==8) { switch(yy) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': minute+=yy-'0'; stage=9; break; default : sprintf(strn,"Minute number must be 2 characters wide. (f.ex. '09')\n"); UI__Warning(strn); return -1; } } if(stage==9) break; } /*check that we got it all*/ if(stage!=9) { UI__Warning("Incomplete time-string. Try sitback -h to get help\n"); return -1; } debug("Script runs day %d at %02d:%02d o'clock.\n",dayofweek,hour,minute); /*add this time to the list*/ if(__add_time(dayofweek,hour,minute)==-1) return -1; /*no problems*/ return 0;}/******************************************************************** int __parse_script() Parse the script that is given as argument arguments: none return: number of arguments handled -1 on error 0 on success********************************************************************/int __parse_script(){ FILE *script; int size; char *buffer; char *p,*end; char prev_char; char *suffix; long int val; char strn[1024]; int count; char *new_list; unsigned prev_length; char location[1024]; /* Return if there is no script. A script it not always needed, f.ex. is it perfectly ok to set the archive-id just using -a and -i to set archive and new id string. We may also want to do other things, f.ex. list. */ if(conf__script_name[0]=='\0') return 0; /*try to open the script*/ sprintf(location,"%s%s",conf__scriptdir,conf__script_name); if((script=fopen(location,"r"))==NULL) { errcode=9; sprintf(strn,"Unable to open '%s'. Check spelling and/or case\n",conf__script_name); UI__Warning(strn); sprintf(strn,"System say's: %s\n",strerror(errno)); return -1; } UI__Message("Script: '%s'\n",conf__script_name); /*get size of script*/ fseek(script,0,SEEK_END); size=ftell(script); rewind(script); debug("Size of backup/restore script is %d bytes\n",size); /*get a buffer to hold the script*/ if((buffer=(char*) malloc(size+1))==NULL) { fclose(script); errcode=11; sprintf(strn,"Unable to create temporary buffer, maybe out of virtual. memory ?\n"); UI__Warning(strn); return -1; } /*read the script*/ fread(buffer,size,1,script); buffer[size]='\0'; /*close the script, we are done with it*/ fclose(script); debug("backup/restore script contains...\n|-->\n%s\n<--|\n",buffer); /*Parse the script, build the target-list and get various settings*/ p=buffer; do { /*skip to next command line, or end of buffer*/ while(*p=='\r' || *p=='\n' || *p==' ') p++; /*skip lines that is preceeded by #*/ if(*p=='#') { while((*p!='\r' && *p!='\n' && *p!='\0') || *p=='#') p++; while(*p=='\r' || *p=='\n' || *p==' ') p++; } /*Keyword 'TIME'*/ if(!strncasecmp(p,"TIME=",5)) { debug("found keyword 'TIME' at position %d\n",p-buffer); /*end of command*/ end=p; while(*end!='\0' && *end!='\n' && *end!=' ' && *end!='\r') end++; /*Pass the string to the time-string parser. It will terminate as soon as all data has been read thus ignoring any trailing characters*/ p+=5; prev_char=*end; *end='\0'; if(__parse_timestring(p)==-1) { free(buffer); errcode=7; /*Error message has been given by the parser*/ return -1; } *end=prev_char; /*next command*/ p=end; } /*Keyword 'TARGET'*/ if(!strncasecmp(p,"ARCHIVE=",8)) { debug("found keyword 'ARCHIVE' at position %d\n",p-buffer); /*end of command*/ end=p; while(*end!='\0' && *end!='\n' && *end!=' ' && *end!='\r') end++; /*make sure the string dont exceed 256 characters*/ if((end-p-8)>256) { free(buffer); errcode=6; sprintf(strn,"ARCHIVE string in script is too long. Maximum 256 characters\n"); UI__Warning(strn); return -1; } /*Dont override target string from command-line*/ if(conf__archive[0]=='\0') { memcpy(conf__org_archive,p+8,end-p-8); conf__org_archive[end-p-8]='\0'; debug("archive set to \"%s\"\n",conf__org_archive); } else { debug("script does not override archive-name from command-line\n"); } strcpy(conf__archive,conf__org_archive); /* check if the device is remote */ if(strchr(conf__archive,':')!=NULL) { /* do we have a username */ if(strchr(conf__archive,'@')!=NULL) { p=conf__archive; while(*p!='@') p++; memcpy(conf__ruser,conf__archive,p-conf__archive); conf__ruser[p-conf__archive]='\0'; p++; } else { strcpy(conf__ruser,__get_local_user()); p=conf__archive; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -