Icinga-core 1.4.0
next gen monitoring
module/idoutils/src/utils.c
Go to the documentation of this file.
00001 /***************************************************************
00002  * UTILS.C - IDO Utils
00003  *
00004  * Copyright (c) 2005-2008 Ethan Galstad 
00005  * Copyright (c) 2009-2011 Icinga Development Team (http://www.icinga.org)
00006  *
00007  **************************************************************/
00008 
00009 #include "../../../include/config.h"
00010 #include "../include/common.h"
00011 #include "../include/utils.h"
00012 
00013 
00014 
00015 
00016 /****************************************************************************/
00017 /* DYNAMIC BUFFER FUNCTIONS                                                 */
00018 /****************************************************************************/
00019 
00020 /* initializes a dynamic buffer */
00021 int ido_dbuf_init(ido_dbuf *db, int chunk_size){
00022 
00023         if(db==NULL)
00024                 return IDO_ERROR;
00025 
00026         db->buf=NULL;
00027         db->used_size=0L;
00028         db->allocated_size=0L;
00029         db->chunk_size=chunk_size;
00030 
00031         return IDO_OK;
00032         }
00033 
00034 
00035 /* frees a dynamic buffer */
00036 int ido_dbuf_free(ido_dbuf *db){
00037 
00038         if(db==NULL)
00039                 return IDO_ERROR;
00040 
00041         if(db->buf!=NULL)
00042                 free(db->buf);
00043         db->buf=NULL;
00044         db->used_size=0L;
00045         db->allocated_size=0L;
00046 
00047         return IDO_OK;
00048         }
00049 
00050 
00051 /* dynamically expands a string */
00052 int ido_dbuf_strcat(ido_dbuf *db, char *buf){
00053         char *newbuf=NULL;
00054         unsigned long buflen=0L;
00055         unsigned long new_size=0L;
00056         unsigned long memory_needed=0L;
00057 
00058         if(db==NULL || buf==NULL)
00059                 return IDO_ERROR;
00060 
00061         /* how much memory should we allocate (if any)? */
00062         buflen=strlen(buf);
00063         new_size=db->used_size+buflen+1;
00064 
00065         /* we need more memory */
00066         if(db->allocated_size<new_size){
00067 
00068                 memory_needed=((ceil(new_size/db->chunk_size)+1)*db->chunk_size);
00069 
00070                 /* allocate memory to store old and new string */
00071                 if((newbuf=(char *)realloc((void *)db->buf,(size_t)memory_needed))==NULL)
00072                         return IDO_ERROR;
00073 
00074                 /* update buffer pointer */
00075                 db->buf=newbuf;
00076 
00077                 /* update allocated size */
00078                 db->allocated_size=memory_needed;
00079 
00080                 /* terminate buffer */
00081                 db->buf[db->used_size]='\x0';
00082                 }
00083 
00084         /* append the new string */
00085         strcat(db->buf,buf);
00086 
00087         /* update size allocated */
00088         db->used_size+=buflen;
00089 
00090         return IDO_OK;
00091         }
00092 
00093 
00094 
00095 /******************************************************************/
00096 /************************* FILE FUNCTIONS *************************/
00097 /******************************************************************/
00098 
00099 /* renames a file - works across filesystems (Mike Wiacek) */
00100 int my_rename(char *source, char *dest){
00101         char buffer[1024]={0};
00102         int rename_result=0;
00103         int source_fd=-1;
00104         int dest_fd=-1;
00105         int bytes_read=0;
00106 
00107 
00108         /* make sure we have something */
00109         if(source==NULL || dest==NULL)
00110                 return -1;
00111 
00112         /* first see if we can rename file with standard function */
00113         rename_result=rename(source,dest);
00114 
00115         /* handle any errors... */
00116         if(rename_result==-1){
00117 
00118                 /* an error occurred because the source and dest files are on different filesystems */
00119                 if(errno==EXDEV){
00120 
00121                         /* open destination file for writing */
00122                         if((dest_fd=open(dest,O_WRONLY|O_TRUNC|O_CREAT|O_APPEND,0644))>0){
00123 
00124                                 /* open source file for reading */
00125                                 if((source_fd=open(source,O_RDONLY,0644))>0){
00126 
00127                                         while((bytes_read=read(source_fd,buffer,sizeof(buffer)))>0)
00128                                                 write(dest_fd,buffer,bytes_read);
00129 
00130                                         close(source_fd);
00131                                         close(dest_fd);
00132                                 
00133                                         /* delete the original file */
00134                                         unlink(source);
00135 
00136                                         /* reset result since we successfully copied file */
00137                                         rename_result=0;
00138                                         }
00139 
00140                                 else{
00141                                         close(dest_fd);
00142                                         return rename_result;
00143                                         }
00144                                 }
00145                         }
00146 
00147                 else{
00148                         return rename_result;
00149                         }
00150                 }
00151 
00152         return rename_result;
00153         }
00154 
00155 
00156 
00157 
00158 /******************************************************************/
00159 /************************ STRING FUNCTIONS ************************/
00160 /******************************************************************/
00161 
00162 /* strip newline, carriage return, and tab characters from beginning and end of a string */
00163 void idomod_strip(char *buffer){
00164         register int x=0;
00165         register int y=0;
00166         register int z=0;
00167 
00168         if(buffer==NULL || buffer[0]=='\x0')
00169                 return;
00170 
00171         /* strip end of string */
00172         y=(int)strlen(buffer);
00173         for(x=y-1;x>=0;x--){
00174                 if(buffer[x]==' ' || buffer[x]=='\n' || buffer[x]=='\r' || buffer[x]=='\t' || buffer[x]==13)
00175                         buffer[x]='\x0';
00176                 else
00177                         break;
00178                 }
00179         /* save last position for later... */
00180         z=x;
00181 
00182         /* strip beginning of string (by shifting) */
00183         for(x=0;;x++){
00184                 if(buffer[x]==' ' || buffer[x]=='\n' || buffer[x]=='\r' || buffer[x]=='\t' || buffer[x]==13)
00185                         continue;
00186                 else
00187                         break;
00188                 }
00189         if(x>0){
00190                 /* new length of the string after we stripped the end */
00191                 y=z+1;
00192                 
00193                 /* shift chars towards beginning of string to remove leading whitespace */
00194                 for(z=x;z<y;z++)
00195                         buffer[z-x]=buffer[z];
00196                 buffer[y-x]='\x0';
00197                 }
00198 
00199         return;
00200         }
 All Data Structures Files Functions Variables Typedefs Defines