[Autosec-devel] sonar/src expand.c,NONE,1.1 expand.h,NONE,1.1
Brought to you by:
red0x
From: Jacob F. <ph...@us...> - 2004-04-23 04:06:18
|
Update of /cvsroot/autosec/sonar/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10285 Added Files: expand.c expand.h Log Message: Target expansion code --- NEW FILE: expand.c --- /*************************************************************************** expand.c - Command line formating expander ------------------- begin : Mon Apr 12 2004 copyright : (C) 2002 by red0x email : re...@us... rcsid : $Id: expand.c,v 1.1 2004/04/23 04:06:10 pharkas Exp $ ***************************************************************************/ /*************************************************************************** * * * 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. * * * ***************************************************************************/ /** @file expand.c * Expands printf-style characters in a given string */ #include "expand.h" static char *res, *r; static int reslen=0, curlen=0; char *expand_target(char *format, target_t *target, int port) { /* pointers within the format and res string, respectively */ char *f, *portname; int i; /* * we're freeing the previous result, so hopefully the caller realizes * that the return value is only good until the next call of * expand_target() */ if(res!=NULL) free(res); reslen=sizeof(format)*MALLOC_INC; res=(char*)malloc(sizeof(char)*reslen); f=format; r=res; while(*f!='\0') { if(*f=='%') { switch(*(++f)) { /* hostname:portname */ case 'T': /* skip over the 'T' */ f++; r+=print_hostname(r, target); checksize(1); *r++=':'; curlen++; r+=print_portname(r, port); break; /* ip:portnum */ case 't': f++; r+=print_ip(r, target); checksize(1); *r++=':'; curlen++; r+=print_port(r, port); break; /* ip */ case 'i': f++; r+=print_ip(r, target); break; /* hostname */ case 'h': f+=2; break; /* port number */ case 'p': f+=2; break; /* port name */ case 'P': f+=2; break; /* year */ case 'y': f+=2; break; /* month (numeric)*/ case 'm': f+=2; break; /* date */ case 'd': f+=2; break; /* hour */ case 'H': f+=2; break; /* minutes */ case 'M': f+=2; break; /* seconds */ case 'S': f+=2; break; default: checksize(2); *(r)='%'; *(++r)=*f++; r++; curlen+=2; break; } } else { checksize(1); *(r++)=*(f++); curlen++; } } /* terminate the string */ checksize(1); *(res+curlen+1)='\0'; return res; } static void checksize(int i) { if(curlen+i>=reslen) { while(curlen+i>=(reslen*=MALLOC_INC)) { /* TODO: use safe realloc */ res=(char*)realloc(res, reslen); /* res may have been relocated in memory- make sure r is still * at the right place */ r=res+curlen; } } //curlen+=i; } int countdigits(int d) { int num=1; while((d=d/10)>0) num++; return num; } int print_hostname(char *result, target_t *t) { int i=strlen(t->entry); checksize(i); snprintf(result, i+1, "%s", hostname); curlen+=i+1; *(result+i)='\0'; return i; } /* TODO: have this print the actual IP */ int print_ip(char *result, target_t *t) { return print_hostname(result, t); } int print_portname(char *result, int portnum) { int i=countdigits(portnum); checksize(i); snprintf(result, i+1, "%d", portnum); curlen+=i+1; *(result+i)='\0'; return i; } int print_port(char *result, int portnum) { int i=countdigits(portnum); checksize(i); snprintf(result, i+1, "%d", portnum); curlen+=i+1; *(result+i)='\0'; return i; } int main() { target_t t; char *name="test"; t.entry=name; printf("|%s|\n", expand_target("a b c %d %e %T %f %% %T", &t, 80)); return 0; } --- NEW FILE: expand.h --- /*************************************************************************** target.h Target string expansion header file ------------------- begin : Mon Apr 12 2004 copyright : (C) 2002 by red0x email : re...@us... ***************************************************************************/ /*************************************************************************** * * * 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. * * * ***************************************************************************/ /** \file expand.h * header file for target string expansion */ #ifndef TARGET_H #define TARGET_H #include <stdlib.h> #include "mem.h" #include "plugin.h" #define MALLOC_INC 1.5 char *expand_target(char*,target_t*,int); static void checksize(int); int countdigits(int); int print_hostname(char*,target_t*); int print_ip(char*,target_t*); int print_port(char*,int); int print_portname(char*,int); #endif |