summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2012-12-22 21:38:26 +0000
committerArthur de Jong <arthur@arthurdejong.org>2012-12-22 21:38:26 +0000
commitc7bb19c55c7a902e25bdd33b0d49a2ddcf62e07a (patch)
treeb3a75ef2719bc2f334041460fd21fbdf86a23a82 /common
parentd336cd6b429f8ce40c58ea287f79bbc7c23d0966 (diff)
update C coding style to a more commonly used style
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-pam-ldapd@1873 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'common')
-rw-r--r--common/dict.c168
-rw-r--r--common/dict.h10
-rw-r--r--common/expr.c147
-rw-r--r--common/expr.h10
-rw-r--r--common/nslcd-prot.c30
-rw-r--r--common/nslcd-prot.h453
-rw-r--r--common/set.c18
-rw-r--r--common/set.h8
-rw-r--r--common/tio.c320
-rw-r--r--common/tio.h12
10 files changed, 601 insertions, 575 deletions
diff --git a/common/dict.c b/common/dict.c
index c42b8b4..c69447e 100644
--- a/common/dict.c
+++ b/common/dict.c
@@ -2,7 +2,7 @@
dict.c - dictionary functions
This file is part of the nss-pam-ldapd library.
- Copyright (C) 2007, 2008, 2009, 2010 Arthur de Jong
+ Copyright (C) 2007, 2008, 2009, 2010, 2012 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -72,9 +72,9 @@ struct dictionary {
/* Simple hash function that computes the hash value of a string. */
static uint32_t stringhash(const char *str)
{
- uint32_t hash=0;
- while (*str!='\0')
- hash=3*hash+*str++;
+ uint32_t hash = 0;
+ while (*str != '\0')
+ hash = 3 * hash + *str++;
return hash;
}
@@ -84,34 +84,34 @@ static void growhashtable(DICT *dict)
int i;
int newsize;
struct dict_entry **newtable;
- struct dict_entry *entry,*tmp;
- newsize=dict->size*3+1;
+ struct dict_entry *entry, *tmp;
+ newsize = dict->size * 3 + 1;
/* allocate room for new hashtable */
- newtable=(struct dict_entry **)malloc(newsize*sizeof(struct dict_entry *));
- if (newtable==NULL)
+ newtable = (struct dict_entry **)malloc(newsize * sizeof(struct dict_entry *));
+ if (newtable == NULL)
return; /* allocating memory failed continue to fill the existing table */
/* clear new table */
- for (i=0;i<newsize;i++)
- newtable[i]=NULL;
+ for (i = 0; i < newsize; i++)
+ newtable[i] = NULL;
/* copy old hashtable into new table */
- for (i=0;i<dict->size;i++)
+ for (i = 0; i < dict->size; i++)
{
/* go over elements in linked list */
- entry=dict->table[i];
- while (entry!=NULL)
+ entry = dict->table[i];
+ while (entry != NULL)
{
- tmp=entry;
- entry=entry->next;
+ tmp = entry;
+ entry = entry->next;
/* put in new position */
- tmp->next=newtable[tmp->hash%newsize];
- newtable[tmp->hash%newsize]=tmp;
+ tmp->next = newtable[tmp->hash % newsize];
+ newtable[tmp->hash % newsize] = tmp;
}
}
/* free the old hashtable */
free(dict->table);
/* put new hashtable in place */
- dict->size=newsize;
- dict->table=newtable;
+ dict->size = newsize;
+ dict->table = newtable;
}
DICT *dict_new(void)
@@ -119,37 +119,37 @@ DICT *dict_new(void)
struct dictionary *dict;
int i;
/* allocate room for dictionary information */
- dict=(struct dictionary *)malloc(sizeof(struct dictionary));
- if (dict==NULL)
+ dict = (struct dictionary *)malloc(sizeof(struct dictionary));
+ if (dict == NULL)
return NULL;
- dict->size=DICT_INITSIZE;
- dict->num=0;
+ dict->size = DICT_INITSIZE;
+ dict->num = 0;
/* allocate initial hashtable */
- dict->table=(struct dict_entry **)malloc(DICT_INITSIZE*sizeof(struct dict_entry *));
- if (dict->table==NULL)
+ dict->table = (struct dict_entry **)malloc(DICT_INITSIZE * sizeof(struct dict_entry *));
+ if (dict->table == NULL)
{
free(dict);
return NULL;
}
/* clear the hashtable */
- for (i=0;i<DICT_INITSIZE;i++)
- dict->table[i]=NULL;
+ for (i = 0; i < DICT_INITSIZE; i++)
+ dict->table[i] = NULL;
/* we're done */
return dict;
}
void dict_free(DICT *dict)
{
- struct dict_entry *entry,*etmp;
+ struct dict_entry *entry, *etmp;
int i;
/* free hashtable entries */
- for (i=0;i<dict->size;i++)
+ for (i = 0; i < dict->size; i++)
{
- entry=dict->table[i];
- while (entry!=NULL)
+ entry = dict->table[i];
+ while (entry != NULL)
{
- etmp=entry;
- entry=entry->next;
+ etmp = entry;
+ entry = entry->next;
free(etmp);
}
}
@@ -159,17 +159,16 @@ void dict_free(DICT *dict)
free(dict);
}
-void *dict_get(DICT *dict,const char *key)
+void *dict_get(DICT *dict, const char *key)
{
uint32_t hash;
struct dict_entry *entry;
/* calculate the hash */
- hash=stringhash(key);
+ hash = stringhash(key);
/* loop over the linked list in the hashtable */
- for (entry=dict->table[hash%dict->size];entry!=NULL;entry=entry->next)
+ for (entry = dict->table[hash % dict->size]; entry != NULL; entry = entry->next)
{
- if ( (entry->hash==hash) &&
- (strcmp(entry->key,key)==0) )
+ if ((entry->hash == hash) && (strcmp(entry->key, key) == 0))
return entry->value;
}
/* no matches found */
@@ -180,69 +179,66 @@ const char *dict_getany(DICT *dict)
{
int i;
/* loop over the linked list in the hashtable */
- for (i=0;i<dict->size;i++)
+ for (i = 0; i < dict->size; i++)
if (dict->table[i])
return dict->table[i]->key;
/* no matches found */
return NULL;
}
-int dict_put(DICT *dict,const char *key,void *value)
+int dict_put(DICT *dict, const char *key, void *value)
{
uint32_t hash;
int l;
char *buf;
int idx;
- struct dict_entry *entry,*prev;
+ struct dict_entry *entry, *prev;
/* check if we should grow the hashtable */
- if ( dict->num >= ((dict->size*DICT_LOADPERCENTAGE)/100) )
+ if (dict->num >= ((dict->size * DICT_LOADPERCENTAGE) / 100))
growhashtable(dict);
/* calculate the hash and position in the hashtable */
- hash=stringhash(key);
- idx=hash%dict->size;
+ hash = stringhash(key);
+ idx = hash % dict->size;
/* check if the entry is already present */
- for (entry=dict->table[idx],prev=NULL;
- entry!=NULL;
- prev=entry,entry=entry->next)
+ for (entry = dict->table[idx], prev = NULL; entry != NULL; prev = entry, entry = entry->next)
{
- if ( (entry->hash==hash) &&
- (strcmp(entry->key,key)==0) )
+ if ((entry->hash == hash) && (strcmp(entry->key, key) == 0))
{
/* check if we should unset the entry */
- if (value==NULL)
+ if (value == NULL)
{
/* remove from linked list */
- if (prev==NULL)
- dict->table[idx]=entry->next;
+ if (prev == NULL)
+ dict->table[idx] = entry->next;
else
- prev->next=entry->next;
+ prev->next = entry->next;
/* free entry memory and register removal */
free(entry);
dict->num--;
return 0;
}
/* just set the new value */
- entry->value=value;
+ entry->value = value;
return 0;
}
}
/* if entry should be unset we're done */
- if (value==NULL)
+ if (value == NULL)
return 0;
/* entry is not present, make new entry */
- l=strlen(key)+1;
- buf=(char *)malloc(sizeof(struct dict_entry)+l);
- if (buf==NULL)
+ l = strlen(key) + 1;
+ buf = (char *)malloc(sizeof(struct dict_entry) + l);
+ if (buf == NULL)
return -1;
- entry=(struct dict_entry *)(void *)buf;
- buf+=sizeof(struct dict_entry);
- strcpy(buf,key);
- entry->hash=hash;
- entry->key=buf;
- entry->value=value;
+ entry = (struct dict_entry *)(void *)buf;
+ buf += sizeof(struct dict_entry);
+ strcpy(buf, key);
+ entry->hash = hash;
+ entry->key = buf;
+ entry->value = value;
/* insert into hashtable/linked list */
- entry->next=dict->table[idx];
- dict->table[idx]=entry;
+ entry->next = dict->table[idx];
+ dict->table[idx] = entry;
/* increment number of stored items */
dict->num++;
return 0;
@@ -257,38 +253,38 @@ const char **dict_keys(DICT *dict)
size_t sz;
int num;
/* figure out how much memory to allocate */
- num=0;
- sz=0;
- for (i=0;i<dict->size;i++)
+ num = 0;
+ sz = 0;
+ for (i = 0; i < dict->size; i++)
{
- entry=dict->table[i];
- while (entry!=NULL)
+ entry = dict->table[i];
+ while (entry != NULL)
{
num++;
- sz+=strlen(entry->key)+1;
- entry=entry->next;
+ sz += strlen(entry->key) + 1;
+ entry = entry->next;
}
}
/* allocate the needed memory */
- buf=(char *)malloc((num+1)*sizeof(char *)+sz);
- if (buf==NULL)
+ buf = (char *)malloc((num + 1) * sizeof(char *) + sz);
+ if (buf == NULL)
return NULL;
- values=(const char **)(void *)buf;
- buf+=(num+1)*sizeof(char *);
+ values = (const char **)(void *)buf;
+ buf += (num + 1) * sizeof(char *);
/* fill the array with the keys */
- num=0;
- for (i=0;i<dict->size;i++)
+ num = 0;
+ for (i = 0; i < dict->size; i++)
{
- entry=dict->table[i];
- while (entry!=NULL)
+ entry = dict->table[i];
+ while (entry != NULL)
{
- strcpy(buf,entry->key);
- values[num++]=buf;
- buf+=strlen(buf)+1;
- entry=entry->next;
+ strcpy(buf, entry->key);
+ values[num++] = buf;
+ buf += strlen(buf) + 1;
+ entry = entry->next;
}
}
- values[num]=NULL;
+ values[num] = NULL;
/* done */
return values;
}
diff --git a/common/dict.h b/common/dict.h
index bb244f0..ffb3b44 100644
--- a/common/dict.h
+++ b/common/dict.h
@@ -2,7 +2,7 @@
dict.h - dictionary functions
This file is part of the nss-pam-ldapd library.
- Copyright (C) 2007, 2008, 2009, 2010 Arthur de Jong
+ Copyright (C) 2007, 2008, 2009, 2010, 2012 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -37,15 +37,15 @@ DICT *dict_new(void)
/* Add a relation in the dictionary. The key is duplicated
and can be reused by the caller. The pointer is just stored.
- This function returns non-0 in case of memory allocation
+ This function returns non-zero in case of memory allocation
errors. If the key was previously in use the value
is replaced. All key comparisons are case sensitive. */
-int dict_put(DICT *dict,const char *key,void *value);
+int dict_put(DICT *dict, const char *key, void *value);
/* Look up a key in the dictionary and return the associated
value. NULL is returned if the key is not found in the dictionary.
All key comparisons are case sensitive. */
-void *dict_get(DICT *dict,const char *key)
+void *dict_get(DICT *dict, const char *key)
MUST_USE;
/* Get a key from the dictionary that has a value set. The caller does
@@ -55,7 +55,7 @@ const char *dict_getany(DICT *dict);
/* Delete a key-value association from the dictionary.
All key comparisons are case sensitive. */
-/*void dict_del(DICT *dict,const char *key);*/
+/*void dict_del(DICT *dict, const char *key);*/
/* Remove the dictionary from memory. All allocated storage
for the dictionary and the keys is freed.
diff --git a/common/expr.c b/common/expr.c
index 33f9820..97fcff8 100644
--- a/common/expr.c
+++ b/common/expr.c
@@ -2,7 +2,7 @@
expr.c - limited shell-like expression parsing functions
This file is part of the nss-pam-ldapd library.
- Copyright (C) 2009, 2010, 2011 Arthur de Jong
+ Copyright (C) 2009, 2010, 2011, 2012 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -34,110 +34,112 @@
static inline int my_isalpha(const char c)
{
- return ((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z'));
+ return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
}
static inline int my_isalphanum(const char c)
{
- return my_isalpha(c)||((c>='0')&&(c<='9'));
+ return my_isalpha(c) || ((c >= '0') && (c <= '9'));
}
/* return the part of the string that is a valid name */
-MUST_USE static const char *parse_name(const char *str,int *ptr,char *buffer,size_t buflen)
+MUST_USE static const char *parse_name(const char *str, int *ptr,
+ char *buffer, size_t buflen)
{
- int i=0;
+ int i = 0;
/* clear the buffer */
- buffer[i]='\0';
- /* look for an alpha+alphanumeric* string */
+ buffer[i] = '\0';
+ /* look for an alpha + alphanumeric* string */
if (!my_isalpha(str[*ptr]))
return NULL;
- while (my_isalphanum(str[*ptr])||(str[*ptr]==';'))
+ while (my_isalphanum(str[*ptr]) || (str[*ptr] == ';'))
{
- if ((size_t)i>=buflen)
+ if ((size_t)i >= buflen)
return NULL;
- buffer[i++]=str[(*ptr)++];
+ buffer[i++] = str[(*ptr)++];
}
/* NULL-terminate the string */
- if ((size_t)i>=buflen)
+ if ((size_t)i >= buflen)
return NULL;
- buffer[i++]='\0';
+ buffer[i++] = '\0';
return buffer;
}
/* dummy expander function to always return an empty string */
-static const char *empty_expander(const char UNUSED(*name),void UNUSED(*expander_arg))
+static const char *empty_expander(const char UNUSED(*name),
+ void UNUSED(*expander_arg))
{
return "";
}
/* definition of the parse functions (they call eachother) */
MUST_USE static const char *parse_dollar_expression(
- const char *str,int *ptr,char *buffer,size_t buflen,
- expr_expander_func expander,void *expander_arg);
+ const char *str, int *ptr, char *buffer, size_t buflen,
+ expr_expander_func expander, void *expander_arg);
MUST_USE static const char *parse_expression(
- const char *str,int *ptr,int endat,char *buffer,size_t buflen,
- expr_expander_func expander,void *expander_arg);
+ const char *str, int *ptr, int endat, char *buffer, size_t buflen,
+ expr_expander_func expander, void *expander_arg);
MUST_USE static const char *parse_dollar_expression(
- const char *str,int *ptr,char *buffer,size_t buflen,
- expr_expander_func expander,void *expander_arg)
+ const char *str, int *ptr, char *buffer, size_t buflen,
+ expr_expander_func expander, void *expander_arg)
{
char varname[MAXVARLENGTH];
const char *varvalue;
- if ((buflen<=0)||(buffer==NULL)||(str==NULL)||(ptr==NULL))
+ if ((buflen <= 0) || (buffer == NULL) || (str == NULL) || (ptr == NULL))
return NULL;
- if (str[*ptr]=='{')
+ if (str[*ptr] == '{')
{
(*ptr)++;
/* the first part is always a variable name */
- if (parse_name(str,ptr,varname,sizeof(varname))==NULL)
+ if (parse_name(str, ptr, varname, sizeof(varname)) == NULL)
return NULL;
- varvalue=expander(varname,expander_arg);
- if (varvalue==NULL)
- varvalue="";
- if (str[*ptr]=='}')
+ varvalue = expander(varname, expander_arg);
+ if (varvalue == NULL)
+ varvalue = "";
+ if (str[*ptr] == '}')
{
/* simple substitute */
- if (strlen(varvalue)>=buflen)
+ if (strlen(varvalue) >= buflen)
return NULL;
- strcpy(buffer,varvalue);
+ strcpy(buffer, varvalue);
}
- else if (strncmp(str+*ptr,":-",2)==0)
+ else if (strncmp(str + *ptr, ":-", 2) == 0)
{
/* if variable is not set or empty, substitute remainder */
- (*ptr)+=2;
- if ((varvalue!=NULL)&&(*varvalue!='\0'))
+ (*ptr) += 2;
+ if ((varvalue != NULL) && (*varvalue != '\0'))
{
/* value is set, skip rest of expression and use value */
- if (parse_expression(str,ptr,'}',buffer,buflen,empty_expander,NULL)==NULL)
+ if (parse_expression(str, ptr, '}', buffer, buflen, empty_expander, NULL) == NULL)
return NULL;
- if (strlen(varvalue)>=buflen)
+ if (strlen(varvalue) >= buflen)
return NULL;
- strcpy(buffer,varvalue);
+ strcpy(buffer, varvalue);
}
else
{
/* value is not set, evaluate rest of expression */
- if (parse_expression(str,ptr,'}',buffer,buflen,expander,expander_arg)==NULL)
+ if (parse_expression(str, ptr, '}', buffer, buflen, expander, expander_arg) == NULL)
return NULL;
}
}
- else if (strncmp(str+*ptr,":+",2)==0)
+ else if (strncmp(str + *ptr, ":+", 2) == 0)
{
/* if variable is set, substitute remainer */
- (*ptr)+=2;
- if ((varvalue!=NULL)&&(*varvalue!='\0'))
+ (*ptr) += 2;
+ if ((varvalue != NULL) && (*varvalue != '\0'))
{
/* value is set, evaluate rest of expression */
- if (parse_expression(str,ptr,'}',buffer,buflen,expander,expander_arg)==NULL)
+ if (parse_expression(str, ptr, '}', buffer, buflen, expander, expander_arg) == NULL)
return NULL;
}
else
{
/* value is not set, skip rest of expression and blank */
- if (parse_expression(str,ptr,'}',buffer,buflen,empty_expander,NULL)==NULL)
+ if (parse_expression(str, ptr, '}', buffer, buflen, empty_expander, NULL) == NULL)
return NULL;
- buffer[0]='\0';
+ buffer[0] = '\0';
}
}
else
@@ -147,81 +149,82 @@ MUST_USE static const char *parse_dollar_expression(
else
{
/* it is a simple reference to a variable, like $uidNumber */
- if (parse_name(str,ptr,varname,sizeof(varname))==NULL)
+ if (parse_name(str, ptr, varname, sizeof(varname)) == NULL)
return NULL;
- varvalue=expander(varname,expander_arg);
- if (varvalue==NULL)
- varvalue="";
- if (strlen(varvalue)>=buflen)
+ varvalue = expander(varname, expander_arg);
+ if (varvalue == NULL)
+ varvalue = "";
+ if (strlen(varvalue) >= buflen)
return NULL;
- strcpy(buffer,varvalue);
+ strcpy(buffer, varvalue);
}
return buffer;
}
MUST_USE static const char *parse_expression(
- const char *str,int *ptr,int endat,char *buffer,size_t buflen,
- expr_expander_func expander,void *expander_arg)
+ const char *str, int *ptr, int endat, char *buffer, size_t buflen,
+ expr_expander_func expander, void *expander_arg)
{
- int j=0;
+ int j = 0;
/* go over string */
- while ((str[*ptr]!=endat)&&(str[*ptr]!='\0'))
+ while ((str[*ptr] != endat) && (str[*ptr] != '\0'))
{
switch (str[*ptr])
{
case '$': /* beginning of an expression */
(*ptr)++;
- if ((size_t)j>=buflen)
+ if ((size_t)j >= buflen)
return NULL;
- if (parse_dollar_expression(str,ptr,buffer+j,buflen-j,expander,expander_arg)==NULL)
+ if (parse_dollar_expression(str, ptr, buffer + j, buflen - j,
+ expander, expander_arg) == NULL)
return NULL;
- j=strlen(buffer);
+ j = strlen(buffer);
break;
case '\\': /* escaped character, unescape */
(*ptr)++;
default: /* just copy the text */
- if ((size_t)j>=buflen)
+ if ((size_t)j >= buflen)
return NULL;
- buffer[j++]=str[*ptr];
+ buffer[j++] = str[*ptr];
(*ptr)++;
}
}
/* NULL-terminate buffer */
- if ((size_t)j>=buflen)
+ if ((size_t)j >= buflen)
return NULL;
- buffer[j++]='\0';
+ buffer[j++] = '\0';
return buffer;
}
-MUST_USE const char *expr_parse(const char *str,char *buffer,size_t buflen,
- expr_expander_func expander,void *expander_arg)
-
+MUST_USE const char *expr_parse(const char *str, char *buffer, size_t buflen,
+ expr_expander_func expander, void *expander_arg)
{
- int i=0;
- return parse_expression(str,&i,'\0',buffer,buflen,expander,expander_arg);
+ int i = 0;
+ return parse_expression(str, &i, '\0', buffer, buflen,
+ expander, expander_arg);
}
-SET *expr_vars(const char *str,SET *set)
+SET *expr_vars(const char *str, SET *set)
{
char varname[MAXVARLENGTH];
- int i=0;
+ int i = 0;
/* allocate set if needed */
- if (set==NULL)
- set=set_new();
- if (set==NULL)
+ if (set == NULL)
+ set = set_new();
+ if (set == NULL)
return NULL;
/* go over string */
- while (str[i]!='\0')
+ while (str[i] != '\0')
{
switch (str[i])
{
case '$': /* beginning of a $-expression */
i++;
- if (str[i]=='{')
+ if (str[i] == '{')
i++;
/* the rest should start with a variable name */
- if (parse_name(str,&i,varname,sizeof(varname))!=NULL)
- set_add(set,varname);
+ if (parse_name(str, &i, varname, sizeof(varname)) != NULL)
+ set_add(set, varname);
break;
case '\\': /* escaped character, unescape */
i++;
diff --git a/common/expr.h b/common/expr.h
index 0ff2c28..2f6ca1b 100644
--- a/common/expr.h
+++ b/common/expr.h
@@ -2,7 +2,7 @@
expr.h - limited shell-like expression parsing functions
This file is part of the nss-pam-ldapd library.
- Copyright (C) 2009 Arthur de Jong
+ Copyright (C) 2009, 2012 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -26,16 +26,16 @@
#include "compat/attrs.h"
#include "common/set.h"
-typedef const char *(*expr_expander_func)(const char *name,void *expander_arg);
+typedef const char *(*expr_expander_func) (const char *name, void *expander_arg);
/* Parse the expression and store the result in buffer, using the
expander function to expand variable names to values. If the expression
is invalid or the result didn't fit in the buffer NULL is returned. */
-MUST_USE const char *expr_parse(const char *expr,char *buffer,size_t buflen,
- expr_expander_func expander,void *expander_arg);
+MUST_USE const char *expr_parse(const char *expr, char *buffer, size_t buflen,
+ expr_expander_func expander, void *expander_arg);
/* Return the variable names that are used in expr. If set is NULL a new one
is allocated, otherwise the passed set is added to. */
-SET *expr_vars(const char *expr,SET *set);
+SET *expr_vars(const char *expr, SET *set);
#endif /* not _COMMON__ */
diff --git a/common/nslcd-prot.c b/common/nslcd-prot.c
index 66c10af..87c0066 100644
--- a/common/nslcd-prot.c
+++ b/common/nslcd-prot.c
@@ -41,12 +41,12 @@
/* read timeout is 60 seconds because looking up stuff may take some time
write timeout is 10 secods because nslcd could be loaded with requests */
-#define READ_TIMEOUT 60*1000
-#define WRITE_TIMEOUT 10*1000
+#define READ_TIMEOUT 60 * 1000
+#define WRITE_TIMEOUT 10 * 1000
/* buffer sizes for I/O */
#define READBUFFER_MINSIZE 1024
-#define READBUFFER_MAXSIZE 2*1024*1024
+#define READBUFFER_MAXSIZE 2 * 1024 * 1024
#define WRITEBUFFER_MINSIZE 32
#define WRITEBUFFER_MAXSIZE 32
@@ -65,27 +65,27 @@ TFILE *nslcd_client_open()
TFILE *fp;
int flags;
/* create a socket */
- if ( (sock=socket(PF_UNIX,SOCK_STREAM,0))<0 )
+ if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
return NULL;
/* create socket address structure */
- memset(&addr,0,sizeof(struct sockaddr_un));
- addr.sun_family=AF_UNIX;
- strncpy(addr.sun_path,NSLCD_SOCKET,sizeof(addr.sun_path));
- addr.sun_path[sizeof(addr.sun_path)-1]='\0';
+ memset(&addr, 0, sizeof(struct sockaddr_un));
+ addr.sun_family = AF_UNIX;
+ strncpy(addr.sun_path, NSLCD_SOCKET, sizeof(addr.sun_path));
+ addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
/* close the file descriptor on exec (ignore errors) */
- flags=fcntl(sock,F_GETFL);
- if (flags>=0)
- (void)fcntl(sock,F_SETFD,flags|FD_CLOEXEC);
+ flags = fcntl(sock, F_GETFL);
+ if (flags >= 0)
+ (void)fcntl(sock, F_SETFD, flags | FD_CLOEXEC);
/* connect to the socket */
- if (connect(sock,(struct sockaddr *)&addr,SUN_LEN(&addr))<0)
+ if (connect(sock, (struct sockaddr *)&addr, SUN_LEN(&addr)) < 0)
{
(void)close(sock);
return NULL;
}
/* create a stream object */
- if ((fp=tio_fdopen(sock,READ_TIMEOUT,WRITE_TIMEOUT,
- READBUFFER_MINSIZE,READBUFFER_MAXSIZE,
- WRITEBUFFER_MINSIZE,WRITEBUFFER_MAXSIZE))==NULL)
+ if ((fp = tio_fdopen(sock, READ_TIMEOUT, WRITE_TIMEOUT,
+ READBUFFER_MINSIZE, READBUFFER_MAXSIZE,
+ WRITEBUFFER_MINSIZE, WRITEBUFFER_MAXSIZE)) == NULL)
{
(void)close(sock);
return NULL;
diff --git a/common/nslcd-prot.h b/common/nslcd-prot.h
index d462f59..fe34fe9 100644
--- a/common/nslcd-prot.h
+++ b/common/nslcd-prot.h
@@ -2,7 +2,7 @@
nslcd-prot.h - helper macros for reading and writing in protocol streams
Copyright (C) 2006 West Consulting
- Copyright (C) 2006, 2007, 2009 Arthur de Jong
+ Copyright (C) 2006, 2007, 2009, 2010, 2012 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -45,11 +45,12 @@
/* define a debugging macro to output logging */
#include <string.h>
#include <errno.h>
-#define DEBUG_PRINT(fmt,arg) \
- fprintf(stderr,"%s:%d:%s: " fmt "\n",__FILE__,__LINE__,__PRETTY_FUNCTION__,arg);
+#define DEBUG_PRINT(fmt, arg) \
+ fprintf(stderr, "%s:%d:%s: " fmt "\n", __FILE__, __LINE__, \
+ __PRETTY_FUNCTION__, arg);
#else /* DEBUG_PROT */
/* define an empty debug macro to disable logging */
-#define DEBUG_PRINT(fmt,arg)
+#define DEBUG_PRINT(fmt, arg)
#endif /* not DEBUG_PROT */
#ifdef DEBUG_PROT_DUMP
@@ -57,19 +58,19 @@
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif /* HAVE_STDINT_H */
-static void debug_dump(const void *ptr,size_t size)
+static void debug_dump(const void *ptr, size_t size)
{
int i;
- for (i=0;i<size;i++)
- fprintf(stderr," %02x",((const uint8_t *)ptr)[i]);
- fprintf(stderr,"\n");
+ for (i = 0; i < size; i++)
+ fprintf(stderr, " %02x", ((const uint8_t *)ptr)[i]);
+ fprintf(stderr, "\n");
}
-#define DEBUG_DUMP(ptr,size) \
- fprintf(stderr,"%s:%d:%s:",__FILE__,__LINE__,__PRETTY_FUNCTION__); \
- debug_dump(ptr,size);
+#define DEBUG_DUMP(ptr, size) \
+ fprintf(stderr, "%s:%d:%s:", __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+ debug_dump(ptr, size);
#else /* DEBUG_PROT_DUMP */
/* define an empty debug macro to disable logging */
-#define DEBUG_DUMP(ptr,size)
+#define DEBUG_DUMP(ptr, size)
#endif /* not DEBUG_PROT_DUMP */
@@ -80,74 +81,76 @@ static void debug_dump(const void *ptr,size_t size)
int32_t tmpint32; - temporary variable
*/
-#define WRITE(fp,ptr,size) \
- DEBUG_PRINT("WRITE : var="__STRING(ptr)" size=%d",(int)size); \
- DEBUG_DUMP(ptr,size); \
- if (tio_write(fp,ptr,(size_t)size)) \
- { \
- DEBUG_PRINT("WRITE : var="__STRING(ptr)" error: %s",strerror(errno)); \
- ERROR_OUT_WRITEERROR(fp); \
+#define WRITE(fp, ptr, size) \
+ DEBUG_PRINT("WRITE : var="__STRING(ptr)" size=%d", (int)size); \
+ DEBUG_DUMP(ptr, size); \
+ if (tio_write(fp, ptr, (size_t)size)) \
+ { \
+ DEBUG_PRINT("WRITE : var="__STRING(ptr)" error: %s", \
+ strerror(errno)); \
+ ERROR_OUT_WRITEERROR(fp); \
}
-#define WRITE_INT32(fp,i) \
- DEBUG_PRINT("WRITE_INT32 : var="__STRING(i)" int32=%d",(int)i); \
- tmpint32=htonl((int32_t)(i)); \
- WRITE(fp,&tmpint32,sizeof(int32_t))
-
-#define WRITE_STRING(fp,str) \
- DEBUG_PRINT("WRITE_STRING: var="__STRING(str)" string=\"%s\"",(str)); \
- if ((str)==NULL) \
- { \
- WRITE_INT32(fp,0); \
- } \
- else \
- { \
- WRITE_INT32(fp,strlen(str)); \
- tmpint32=ntohl(tmpint32); \
- if (tmpint32>0) \
- { WRITE(fp,(str),tmpint32); } \
+#define WRITE_INT32(fp, i) \
+ DEBUG_PRINT("WRITE_INT32 : var="__STRING(i)" int32=%d", (int)i); \
+ tmpint32 = htonl((int32_t)(i)); \
+ WRITE(fp, &tmpint32, sizeof(int32_t))
+
+#define WRITE_STRING(fp, str) \
+ DEBUG_PRINT("WRITE_STRING: var="__STRING(str)" string=\"%s\"", (str)); \
+ if ((str) == NULL) \
+ { \
+ WRITE_INT32(fp, 0); \
+ } \
+ else \
+ { \
+ WRITE_INT32(fp, strlen(str)); \
+ tmpint32 = ntohl(tmpint32); \
+ if (tmpint32 > 0) \
+ { \
+ WRITE(fp, (str), tmpint32); \
+ } \
}
-#define WRITE_STRINGLIST(fp,arr) \
- if ((arr)==NULL) \
- { \
- DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d",0); \
- WRITE_INT32(fp,0); \
- } \
- else \
- { \
- /* first determin length of array */ \
- for (tmp3int32=0;(arr)[tmp3int32]!=NULL;tmp3int32++) \
- /*noting*/ ; \
- /* write number of strings */ \
- DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d",(int)tmp3int32); \
- WRITE_INT32(fp,tmp3int32); \
- /* write strings */ \
- for (tmp2int32=0;tmp2int32<tmp3int32;tmp2int32++) \
- { \
- WRITE_STRING(fp,(arr)[tmp2int32]); \
- } \
+#define WRITE_STRINGLIST(fp, arr) \
+ if ((arr) == NULL) \
+ { \
+ DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d", 0); \
+ WRITE_INT32(fp, 0); \
+ } \
+ else \
+ { \
+ /* first determine length of array */ \
+ for (tmp3int32 = 0; (arr)[tmp3int32] != NULL; tmp3int32++) \
+ /* noting */ ; \
+ /* write number of strings */ \
+ DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d", (int)tmp3int32); \
+ WRITE_INT32(fp, tmp3int32); \
+ /* write strings */ \
+ for (tmp2int32 = 0; tmp2int32 < tmp3int32; tmp2int32++) \
+ { \
+ WRITE_STRING(fp, (arr)[tmp2int32]); \
+ } \
}
-#define WRITE_STRINGLIST_EXCEPT(fp,arr,not) \
- /* first determin length of array */ \
- tmp3int32=0; \
- for (tmp2int32=0;(arr)[tmp2int32]!=NULL;tmp2int32++) \
- if (strcmp((arr)[tmp2int32],(not))!=0) \
- tmp3int32++; \
- /* write number of strings (mius one because we intend to skip one) */ \
- DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d",(int)tmp3int32); \
- WRITE_INT32(fp,tmp3int32); \
- /* write strings */ \
- for (tmp2int32=0;(arr)[tmp2int32]!=NULL;tmp2int32++) \
- { \
- if (strcmp((arr)[tmp2int32],(not))!=0) \
- { \
- WRITE_STRING(fp,(arr)[tmp2int32]); \
- } \
+#define WRITE_STRINGLIST_EXCEPT(fp, arr, not) \
+ /* first determine length of array */ \
+ tmp3int32 = 0; \
+ for (tmp2int32 = 0; (arr)[tmp2int32] != NULL; tmp2int32++) \
+ if (strcmp((arr)[tmp2int32], (not)) != 0) \
+ tmp3int32++; \
+ /* write number of strings (mius one because we intend to skip one) */ \
+ DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d", (int)tmp3int32); \
+ WRITE_INT32(fp, tmp3int32); \
+ /* write strings */ \
+ for (tmp2int32 = 0; (arr)[tmp2int32] != NULL; tmp2int32++) \
+ { \
+ if (strcmp((arr)[tmp2int32], (not)) != 0) \
+ { \
+ WRITE_STRING(fp, (arr)[tmp2int32]); \
+ } \
}
-
/* READ macros, used for reading data, on read error they will
call the ERROR_OUT_READERROR or ERROR_OUT_BUFERROR macro
these macros may require the availability of the following
@@ -155,40 +158,43 @@ static void debug_dump(const void *ptr,size_t size)
int32_t tmpint32; - temporary variable
*/
-#define READ(fp,ptr,size) \
- if (tio_read(fp,ptr,(size_t)size)) \
- { \
- DEBUG_PRINT("READ : var="__STRING(ptr)" error: %s",strerror(errno)); \
- ERROR_OUT_READERROR(fp); \
- } \
- DEBUG_PRINT("READ : var="__STRING(ptr)" size=%d",(int)size); \
- DEBUG_DUMP(ptr,size);
-
-#define READ_INT32(fp,i) \
- READ(fp,&tmpint32,sizeof(int32_t)); \
- i=ntohl(tmpint32); \
- DEBUG_PRINT("READ_INT32 : var="__STRING(i)" int32=%d",(int)i);
+#define READ(fp, ptr, size) \
+ if (tio_read(fp, ptr, (size_t)size)) \
+ { \
+ DEBUG_PRINT("READ : var="__STRING(ptr)" error: %s", \
+ strerror(errno)); \
+ ERROR_OUT_READERROR(fp); \
+ } \
+ DEBUG_PRINT("READ : var="__STRING(ptr)" size=%d", (int)(size)); \
+ DEBUG_DUMP(ptr, size);
+
+#define READ_INT32(fp, i) \
+ READ(fp, &tmpint32, sizeof(int32_t)); \
+ (i) = ntohl(tmpint32); \
+ DEBUG_PRINT("READ_INT32 : var="__STRING(i)" int32=%d", (int)(i));
/* read a string in a fixed-size "normal" buffer */
-#define READ_STRING(fp,buffer) \
- /* read the size of the string */ \
- READ(fp,&tmpint32,sizeof(int32_t)); \
- tmpint32=ntohl(tmpint32); \
- DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" strlen=%d",tmpint32); \
- /* check if read would fit */ \
- if (((size_t)tmpint32)>=sizeof(buffer)) \
- { \
- /* will not fit */ \
- tmpint32=(tmpint32-sizeof(buffer))+1; \
- DEBUG_PRINT("READ : buffer %d bytes too small",tmpint32); \
- ERROR_OUT_BUFERROR(fp); \
- } \
- /* read string from the stream */ \
- if (tmpint32>0) \
- { READ(fp,buffer,(size_t)tmpint32); } \
- /* null-terminate string in buffer */ \
- buffer[tmpint32]='\0'; \
- DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" string=\"%s\"",buffer);
+#define READ_STRING(fp, buffer) \
+ /* read the size of the string */ \
+ READ(fp, &tmpint32, sizeof(int32_t)); \
+ tmpint32 = ntohl(tmpint32); \
+ DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" strlen=%d", tmpint32); \
+ /* check if read would fit */ \
+ if (((size_t)tmpint32) >= sizeof(buffer)) \
+ { \
+ /* will not fit */ \
+ tmpint32 = (tmpint32 - sizeof(buffer)) + 1; \
+ DEBUG_PRINT("READ : buffer %d bytes too small", tmpint32); \
+ ERROR_OUT_BUFERROR(fp); \
+ } \
+ /* read string from the stream */ \
+ if (tmpint32 > 0) \
+ { \
+ READ(fp, buffer, (size_t)tmpint32); \
+ } \
+ /* null-terminate string in buffer */ \
+ buffer[tmpint32] = '\0'; \
+ DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" string=\"%s\"", buffer);
/* READ BUF macros that read data into a pre-allocated buffer.
@@ -201,120 +207,123 @@ static void debug_dump(const void *ptr,size_t size)
*/
/* current position in the buffer */
-#define BUF_CUR \
- (buffer+bufptr)
+#define BUF_CUR \
+ (buffer + bufptr)
/* check that the buffer has sz bytes left in it */
-#define BUF_CHECK(fp,sz) \
- if ((bufptr+(size_t)(sz))>buflen) \
- { \
- /* will not fit */ \
- tmpint32=bufptr+(sz)-(buflen); \
- DEBUG_PRINT("READ : buffer %d bytes too small",tmpint32); \
- ERROR_OUT_BUFERROR(fp); \
+#define BUF_CHECK(fp, sz) \
+ if ((bufptr + (size_t)(sz)) > buflen) \
+ { \
+ /* will not fit */ \
+ tmpint32 = bufptr + (sz) - (buflen); \
+ DEBUG_PRINT("READ : buffer %d bytes too small", tmpint32); \
+ ERROR_OUT_BUFERROR(fp); \
}
/* move the buffer pointer */
-#define BUF_SKIP(sz) \
- bufptr+=(size_t)(sz);
+#define BUF_SKIP(sz) \
+ bufptr += (size_t)(sz);
/* move BUF_CUR foreward so that it is aligned to the specified
type width */
-#define BUF_ALIGN(fp,type) \
- /* figure out number of bytes to skip foreward */ \
- tmp2int32=(sizeof(type)-((BUF_CUR-(char *)NULL)%sizeof(type)))%sizeof(type); \
- /* check and skip */ \
- BUF_CHECK(fp,tmp2int32); \
+#define BUF_ALIGN(fp, type) \
+ /* figure out number of bytes to skip foreward */ \
+ tmp2int32 = (sizeof(type) - ((BUF_CUR - (char *)NULL) % sizeof(type))) \
+ % sizeof(type); \
+ /* check and skip */ \
+ BUF_CHECK(fp, tmp2int32); \
BUF_SKIP(tmp2int32);
/* allocate a piece of the buffer to store an array in */
-#define BUF_ALLOC(fp,ptr,type,num) \
- /* align to the specified type width */ \
- BUF_ALIGN(fp,type); \
- /* check that we have enough room */ \
- BUF_CHECK(fp,(size_t)(num)*sizeof(type)); \
- /* store the pointer */ \
- (ptr)=(type *)BUF_CUR; \
- /* reserve the space */ \
- BUF_SKIP((size_t)(num)*sizeof(type));
+#define BUF_ALLOC(fp, ptr, type, num) \
+ /* align to the specified type width */ \
+ BUF_ALIGN(fp, type); \
+ /* check that we have enough room */ \
+ BUF_CHECK(fp, (size_t)(num) * sizeof(type)); \
+ /* store the pointer */ \
+ (ptr) = (type *)BUF_CUR; \
+ /* reserve the space */ \
+ BUF_SKIP((size_t)(num) * sizeof(type));
/* read a binary blob into the buffer */
-#define READ_BUF(fp,ptr,sz) \
- /* check that there is enough room and read */ \
- BUF_CHECK(fp,sz); \
- READ(fp,BUF_CUR,(size_t)sz); \
- /* store pointer and skip */ \
- (ptr)=BUF_CUR; \
+#define READ_BUF(fp, ptr, sz) \
+ /* check that there is enough room and read */ \
+ BUF_CHECK(fp, sz); \
+ READ(fp, BUF_CUR, (size_t)sz); \
+ /* store pointer and skip */ \
+ (ptr) = BUF_CUR; \
BUF_SKIP(sz);
/* read string in the buffer (using buffer, buflen and bufptr)
and store the actual location of the string in field */
-#define READ_BUF_STRING(fp,field) \
- /* read the size of the string */ \
- READ(fp,&tmpint32,sizeof(int32_t)); \
- tmpint32=ntohl(tmpint32); \
- DEBUG_PRINT("READ_BUF_STRING: var="__STRING(field)" strlen=%d",tmpint32); \
- /* check if read would fit */ \
- BUF_CHECK(fp,tmpint32+1); \
- /* read string from the stream */ \
- if (tmpint32>0) \
- { READ(fp,BUF_CUR,(size_t)tmpint32); } \
- /* null-terminate string in buffer */ \
- BUF_CUR[tmpint32]='\0'; \
- DEBUG_PRINT("READ_BUF_STRING: var="__STRING(field)" string=\"%s\"",BUF_CUR); \
- /* prepare result */ \
- (field)=BUF_CUR; \
- BUF_SKIP(tmpint32+1);
+#define READ_BUF_STRING(fp, field) \
+ /* read the size of the string */ \
+ READ(fp, &tmpint32, sizeof(int32_t)); \
+ tmpint32 = ntohl(tmpint32); \
+ DEBUG_PRINT("READ_BUF_STRING: var="__STRING(field)" strlen=%d", tmpint32); \
+ /* check if read would fit */ \
+ BUF_CHECK(fp, tmpint32 + 1); \
+ /* read string from the stream */ \
+ if (tmpint32 > 0) \
+ { \
+ READ(fp, BUF_CUR, (size_t)tmpint32); \
+ } \
+ /* null-terminate string in buffer */ \
+ BUF_CUR[tmpint32] = '\0'; \
+ DEBUG_PRINT("READ_BUF_STRING: var="__STRING(field)" string=\"%s\"", BUF_CUR); \
+ /* prepare result */ \
+ (field) = BUF_CUR; \
+ BUF_SKIP(tmpint32 + 1);
/* read an array from a stram and store it as a null-terminated
array list (size for the array is allocated) */
-#define READ_BUF_STRINGLIST(fp,arr) \
- /* read the number of entries */ \
- READ(fp,&tmp3int32,sizeof(int32_t)); \
- tmp3int32=ntohl(tmp3int32); \
- DEBUG_PRINT("READ_STRLST: var="__STRING(arr)" num=%d",(int)tmp3int32); \
- /* allocate room for *char[num+1] */ \
- BUF_ALLOC(fp,arr,char *,tmp3int32+1); \
- /* read all entries */ \
- for (tmp2int32=0;tmp2int32<tmp3int32;tmp2int32++) \
- { \
- READ_BUF_STRING(fp,(arr)[tmp2int32]); \
- } \
- /* set last entry to NULL */ \
- (arr)[tmp2int32]=NULL;
+#define READ_BUF_STRINGLIST(fp, arr) \
+ /* read the number of entries */ \
+ READ(fp, &tmp3int32, sizeof(int32_t)); \
+ tmp3int32 = ntohl(tmp3int32); \
+ DEBUG_PRINT("READ_STRLST: var="__STRING(arr)" num=%d", (int)tmp3int32); \
+ /* allocate room for *char[num + 1] */ \
+ BUF_ALLOC(fp, arr, char *, tmp3int32 + 1); \
+ /* read all entries */ \
+ for (tmp2int32 = 0; tmp2int32 < tmp3int32; tmp2int32++) \
+ { \
+ READ_BUF_STRING(fp, (arr)[tmp2int32]); \
+ } \
+ /* set last entry to NULL */ \
+ (arr)[tmp2int32] = NULL;
/* SKIP macros for skipping over certain parts of the protocol stream. */
/* skip a number of bytes foreward */
-#define SKIP(fp,sz) \
- DEBUG_PRINT("READ : skip %d bytes",(int)(sz)); \
- /* read (skip) the specified number of bytes */ \
- if (tio_skip(fp,sz)) \
- { \
- DEBUG_PRINT("READ : skip error: %s",strerror(errno)); \
- ERROR_OUT_READERROR(fp); \
+#define SKIP(fp, sz) \
+ DEBUG_PRINT("READ : skip %d bytes", (int)(sz)); \
+ /* read (skip) the specified number of bytes */ \
+ if (tio_skip(fp, sz)) \
+ { \
+ DEBUG_PRINT("READ : skip error: %s", strerror(errno)); \
+ ERROR_OUT_READERROR(fp); \
}
/* read a string from the stream but don't do anything with the result */
-#define SKIP_STRING(fp) \
- /* read the size of the string */ \
- READ(fp,&tmpint32,sizeof(int32_t)); \
- tmpint32=ntohl(tmpint32); \
- DEBUG_PRINT("READ_STRING: skip %d bytes",(int)tmpint32); \
- /* read (skip) the specified number of bytes */ \
- SKIP(fp,tmpint32);
+#define SKIP_STRING(fp) \
+ /* read the size of the string */ \
+ READ(fp, &tmpint32, sizeof(int32_t)); \
+ tmpint32 = ntohl(tmpint32); \
+ DEBUG_PRINT("READ_STRING: skip %d bytes", (int)tmpint32); \
+ /* read (skip) the specified number of bytes */ \
+ SKIP(fp, tmpint32);
/* skip a list of strings */
-#define SKIP_STRINGLIST(fp) \
- /* read the number of entries */ \
- READ(fp,&tmp3int32,sizeof(int32_t)); \
- tmp3int32=ntohl(tmp3int32); \
- DEBUG_PRINT("READ_STRLST: skip %d strings",(int)tmp3int32); \
- /* read all entries */ \
- for (tmp2int32=0;tmp2int32<tmp3int32;tmp2int32++) \
- { \
- SKIP_STRING(fp); \
+#define SKIP_STRINGLIST(fp) \
+ /* read the number of entries */ \
+ READ(fp, &tmp3int32, sizeof(int32_t)); \
+ tmp3int32 = ntohl(tmp3int32); \
+ DEBUG_PRINT("READ_STRLST: skip %d strings", (int)tmp3int32); \
+ /* read all entries */ \
+ for (tmp2int32 = 0; tmp2int32 < tmp3int32; tmp2int32++) \
+ { \
+ SKIP_STRING(fp); \
}
@@ -327,38 +336,46 @@ TFILE *nslcd_client_open(void)
MUST_USE;
/* generic request code */
-#define NSLCD_REQUEST(fp,action,writefn) \
- /* open a client socket */ \
- if ((fp=nslcd_client_open())==NULL) \
- { ERROR_OUT_OPENERROR } \
- /* write a request header with a request code */ \
- WRITE_INT32(fp,(int32_t)NSLCD_VERSION) \
- WRITE_INT32(fp,(int32_t)action) \
- /* write the request parameters (if any) */ \
- writefn; \
- /* flush the stream */ \
- if (tio_flush(fp)<0) \
- { \
- DEBUG_PRINT("WRITE_FLUSH : error: %s",strerror(errno)); \
- ERROR_OUT_WRITEERROR(fp); \
- } \
- /* read and check response version number */ \
- READ(fp,&tmpint32,sizeof(int32_t)); \
- tmpint32=ntohl(tmpint32); \
- if (tmpint32!=(int32_t)NSLCD_VERSION) \
- { ERROR_OUT_READERROR(fp) } \
- /* read and check response request number */ \
- READ(fp,&tmpint32,sizeof(int32_t)); \
- tmpint32=ntohl(tmpint32); \
- if (tmpint32!=(int32_t)(action)) \
- { ERROR_OUT_READERROR(fp) }
+#define NSLCD_REQUEST(fp, action, writefn) \
+ /* open a client socket */ \
+ if ((fp = nslcd_client_open()) == NULL) \
+ { \
+ ERROR_OUT_OPENERROR; \
+ } \
+ /* write a request header with a request code */ \
+ WRITE_INT32(fp, (int32_t)NSLCD_VERSION) \
+ WRITE_INT32(fp, (int32_t)action) \
+ /* write the request parameters (if any) */ \
+ writefn; \
+ /* flush the stream */ \
+ if (tio_flush(fp) < 0) \
+ { \
+ DEBUG_PRINT("WRITE_FLUSH : error: %s", strerror(errno)); \
+ ERROR_OUT_WRITEERROR(fp); \
+ } \
+ /* read and check response version number */ \
+ READ(fp, &tmpint32, sizeof(int32_t)); \
+ tmpint32 = ntohl(tmpint32); \
+ if (tmpint32 != (int32_t)NSLCD_VERSION) \
+ { \
+ ERROR_OUT_READERROR(fp); \
+ } \
+ /* read and check response request number */ \
+ READ(fp, &tmpint32, sizeof(int32_t)); \
+ tmpint32 = ntohl(tmpint32); \
+ if (tmpint32 != (int32_t)(action)) \
+ { \
+ ERROR_OUT_READERROR(fp); \
+ }
/* Read the response code (the result code of the query) from
the stream. */
-#define READ_RESPONSE_CODE(fp) \
- READ(fp,&tmpint32,sizeof(int32_t)); \
- tmpint32=ntohl(tmpint32); \
- if (tmpint32!=(int32_t)NSLCD_RESULT_BEGIN) \
- { ERROR_OUT_NOSUCCESS(fp) }
+#define READ_RESPONSE_CODE(fp) \
+ READ(fp, &tmpint32, sizeof(int32_t)); \
+ tmpint32 = ntohl(tmpint32); \
+ if (tmpint32 != (int32_t)NSLCD_RESULT_BEGIN) \
+ { \
+ ERROR_OUT_NOSUCCESS(fp); \
+ }
#endif /* not COMMON__NSLCD_PROT_H */
diff --git a/common/set.c b/common/set.c
index aec7e83..7b3c2b0 100644
--- a/common/set.c
+++ b/common/set.c
@@ -2,7 +2,7 @@
set.c - set functions
This file is part of the nss-pam-ldapd library.
- Copyright (C) 2008, 2009, 2010 Arthur de Jong
+ Copyright (C) 2008, 2009, 2010, 2012 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -41,27 +41,27 @@ SET *set_new(void)
return (SET *)dict_new();
}
-int set_add(SET *set,const char *value)
+int set_add(SET *set, const char *value)
{
- return dict_put((DICT *)set,value,set);
+ return dict_put((DICT *)set, value, set);
}
char *set_pop(SET *set)
{
const char *key;
char *value;
- key=dict_getany((DICT *)set);
- if (key==NULL)
+ key = dict_getany((DICT *)set);
+ if (key == NULL)
return NULL; /* no more entries in set */
/* remove the entry from the dict and return a copy */
- value=strdup(key);
- dict_put((DICT *)set,key,NULL);
+ value = strdup(key);
+ dict_put((DICT *)set, key, NULL);
return value;
}
-int set_contains(SET *set,const char *value)
+int set_contains(SET *set, const char *value)
{
- return dict_get((DICT *)set,value)!=NULL;
+ return dict_get((DICT *)set, value) != NULL;
}
void set_free(SET *set)
diff --git a/common/set.h b/common/set.h
index d57c3db..0a12923 100644
--- a/common/set.h
+++ b/common/set.h
@@ -2,7 +2,7 @@
set.h - set functions
This file is part of the nss-pam-ldapd library.
- Copyright (C) 2008, 2009, 2010 Arthur de Jong
+ Copyright (C) 2008, 2009, 2010, 2012 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -38,13 +38,13 @@ SET *set_new(void)
/* Add a string in the set. The value is duplicated
and can be reused by the caller.
- This function returns non-0 in case of memory allocation
+ This function returns non-zero in case of memory allocation
errors. All value comparisons are case sensitive. */
-int set_add(SET *set,const char *value);
+int set_add(SET *set, const char *value);
/* Return non-zero if the value is in the set.
All value comparisons are case sensitive. */
-int set_contains(SET *set,const char *value)
+int set_contains(SET *set, const char *value)
MUST_USE;
/* Get an element from the set and removes it from the set.
diff --git a/common/tio.c b/common/tio.c
index 4012364..f4a769a 100644
--- a/common/tio.c
+++ b/common/tio.c
@@ -76,17 +76,17 @@ struct tio_fileinfo {
};
/* build a timeval for comparison to when the operation should be finished */
-static inline void tio_get_deadline(struct timeval *deadline,int timeout)
+static inline void tio_get_deadline(struct timeval *deadline, int timeout)
{
- if (gettimeofday(deadline,NULL))
+ if (gettimeofday(deadline, NULL))
{
/* just blank it in case of errors */
- deadline->tv_sec=0;
- deadline->tv_usec=0;
+ deadline->tv_sec = 0;
+ deadline->tv_usec = 0;
return;
}
- deadline->tv_sec+=timeout/1000;
- deadline->tv_usec+=(timeout%1000)*1000;
+ deadline->tv_sec += timeout / 1000;
+ deadline->tv_usec += (timeout % 1000) * 1000;
}
/* update the timeout to the value that is remaining before deadline
@@ -95,62 +95,63 @@ static inline int tio_time_remaining(const struct timeval *deadline)
{
struct timeval tv;
/* get the current time */
- if (gettimeofday(&tv,NULL))
+ if (gettimeofday(&tv, NULL))
{
/* 1 second default if gettimeofday() is broken */
return 1000;
}
/* calculate time remaining in miliseconds */
- return (deadline->tv_sec-tv.tv_sec)*1000 + (deadline->tv_usec-tv.tv_usec)/1000;
+ return (deadline->tv_sec - tv.tv_sec) * 1000 +
+ (deadline->tv_usec - tv.tv_usec) / 1000;
}
/* open a new TFILE based on the file descriptor */
-TFILE *tio_fdopen(int fd,int readtimeout,int writetimeout,
- size_t initreadsize,size_t maxreadsize,
- size_t initwritesize,size_t maxwritesize)
+TFILE *tio_fdopen(int fd, int readtimeout, int writetimeout,
+ size_t initreadsize, size_t maxreadsize,
+ size_t initwritesize, size_t maxwritesize)
{
struct tio_fileinfo *fp;
- fp=(struct tio_fileinfo *)malloc(sizeof(struct tio_fileinfo));
- if (fp==NULL)
+ fp = (struct tio_fileinfo *)malloc(sizeof(struct tio_fileinfo));
+ if (fp == NULL)
return NULL;
- fp->fd=fd;
+ fp->fd = fd;
/* initialize read buffer */
- fp->readbuffer.buffer=(uint8_t *)malloc(initreadsize);
- if (fp->readbuffer.buffer==NULL)
+ fp->readbuffer.buffer = (uint8_t *)malloc(initreadsize);
+ if (fp->readbuffer.buffer == NULL)
{
free(fp);
return NULL;
}
- fp->readbuffer.size=initreadsize;
- fp->readbuffer.maxsize=maxreadsize;
- fp->readbuffer.start=0;
- fp->readbuffer.len=0;
+ fp->readbuffer.size = initreadsize;
+ fp->readbuffer.maxsize = maxreadsize;
+ fp->readbuffer.start = 0;
+ fp->readbuffer.len = 0;
/* initialize write buffer */
- fp->writebuffer.buffer=(uint8_t *)malloc(initwritesize);
- if (fp->writebuffer.buffer==NULL)
+ fp->writebuffer.buffer = (uint8_t *)malloc(initwritesize);
+ if (fp->writebuffer.buffer == NULL)
{
free(fp->readbuffer.buffer);
free(fp);
return NULL;
}
- fp->writebuffer.size=initwritesize;
- fp->writebuffer.maxsize=maxwritesize;
- fp->writebuffer.start=0;
- fp->writebuffer.len=0;
+ fp->writebuffer.size = initwritesize;
+ fp->writebuffer.maxsize = maxwritesize;
+ fp->writebuffer.start = 0;
+ fp->writebuffer.len = 0;
/* initialize other attributes */
- fp->readtimeout=readtimeout;
- fp->writetimeout=writetimeout;
- fp->read_resettable=0;
+ fp->readtimeout = readtimeout;
+ fp->writetimeout = writetimeout;
+ fp->read_resettable = 0;
#ifdef DEBUG_TIO_STATS
- fp->byteswritten=0;
- fp->bytesread=0;
+ fp->byteswritten = 0;
+ fp->bytesread = 0;
#endif /* DEBUG_TIO_STATS */
return fp;
}
/* wait for any activity on the specified file descriptor using
the specified deadline */
-static int tio_wait(TFILE *fp,int readfd,const struct timeval *deadline)
+static int tio_wait(TFILE *fp, int readfd, const struct timeval *deadline)
{
int timeout;
struct pollfd fds[1];
@@ -158,38 +159,38 @@ static int tio_wait(TFILE *fp,int readfd,const struct timeval *deadline)
while (1)
{
/* figure out the time we need to wait */
- if ((timeout=tio_time_remaining(deadline))<0)
+ if ((timeout = tio_time_remaining(deadline)) < 0)
{
- errno=ETIME;
+ errno = ETIME;
return -1;
}
/* wait for activity */
if (readfd)
{
- fds[0].fd=fp->fd;
- fds[0].events=POLLIN;
+ fds[0].fd = fp->fd;
+ fds[0].events = POLLIN;
/* santiy check for moving clock */
- if (timeout>fp->readtimeout)
- timeout=fp->readtimeout;
+ if (timeout > fp->readtimeout)
+ timeout = fp->readtimeout;
}
else
{
- fds[0].fd=fp->fd;
- fds[0].events=POLLOUT;
+ fds[0].fd = fp->fd;
+ fds[0].events = POLLOUT;
/* santiy check for moving clock */
- if (timeout>fp->writetimeout)
- timeout=fp->writetimeout;
+ if (timeout > fp->writetimeout)
+ timeout = fp->writetimeout;
}
- rv=poll(fds,1,timeout);
- if (rv>0)
+ rv = poll(fds, 1, timeout);
+ if (rv > 0)
return 0; /* we have activity */
- else if (rv==0)
+ else if (rv == 0)
{
/* no file descriptors were available within the specified time */
- errno=ETIME;
+ errno = ETIME;
return -1;
}
- else if (errno!=EINTR)
+ else if (errno != EINTR)
/* some error ocurred */
return -1;
/* we just try again on EINTR */
@@ -206,87 +207,88 @@ int tio_read(TFILE *fp, void *buf, size_t count)
size_t newsz;
size_t len;
/* have a more convenient storage type for the buffer */
- uint8_t *ptr=(uint8_t *)buf;
+ uint8_t *ptr = (uint8_t *)buf;
/* build a time by which we should be finished */
- tio_get_deadline(&deadline,fp->readtimeout);
+ tio_get_deadline(&deadline, fp->readtimeout);
/* loop until we have returned all the needed data */
while (1)
{
/* check if we have enough data in the buffer */
if (fp->readbuffer.len >= count)
{
- if (count>0)
+ if (count > 0)
{
- if (ptr!=NULL)
- memcpy(ptr,fp->readbuffer.buffer+fp->readbuffer.start,count);
+ if (ptr != NULL)
+ memcpy(ptr, fp->readbuffer.buffer + fp->readbuffer.start, count);
/* adjust buffer position */
- fp->readbuffer.start+=count;
- fp->readbuffer.len-=count;
+ fp->readbuffer.start += count;
+ fp->readbuffer.len -= count;
}
return 0;
}
/* empty what we have and continue from there */
- if (fp->readbuffer.len>0)
+ if (fp->readbuffer.len > 0)
{
- if (ptr!=NULL)
+ if (ptr != NULL)
{
- memcpy(ptr,fp->readbuffer.buffer+fp->readbuffer.start,fp->readbuffer.len);
- ptr+=fp->readbuffer.len;
+ memcpy(ptr, fp->readbuffer.buffer + fp->readbuffer.start,
+ fp->readbuffer.len);
+ ptr += fp->readbuffer.len;
}
- count-=fp->readbuffer.len;
- fp->readbuffer.start+=fp->readbuffer.len;
- fp->readbuffer.len=0;
+ count -= fp->readbuffer.len;
+ fp->readbuffer.start += fp->readbuffer.len;
+ fp->readbuffer.len = 0;
}
/* after this point until the read fp->readbuffer.len is 0 */
if (!fp->read_resettable)
{
/* the stream is not resettable, re-use the buffer */
- fp->readbuffer.start=0;
+ fp->readbuffer.start = 0;
}
- else if (fp->readbuffer.start>=(fp->readbuffer.size-4))
+ else if (fp->readbuffer.start >= (fp->readbuffer.size - 4))
{
/* buffer is running empty, try to grow buffer */
- if (fp->readbuffer.size<fp->readbuffer.maxsize)
+ if (fp->readbuffer.size < fp->readbuffer.maxsize)
{
- newsz=fp->readbuffer.size*2;
- if (newsz>fp->readbuffer.maxsize)
- newsz=fp->readbuffer.maxsize;
- tmp=realloc(fp->readbuffer.buffer,newsz);
- if (tmp!=NULL)
+ newsz = fp->readbuffer.size * 2;
+ if (newsz > fp->readbuffer.maxsize)
+ newsz = fp->readbuffer.maxsize;
+ tmp = realloc(fp->readbuffer.buffer, newsz);
+ if (tmp != NULL)
{
- fp->readbuffer.buffer=tmp;
- fp->readbuffer.size=newsz;
+ fp->readbuffer.buffer = tmp;
+ fp->readbuffer.size = newsz;
}
}
/* if buffer still does not contain enough room, clear resettable */
- if (fp->readbuffer.start>=(fp->readbuffer.size-4))
+ if (fp->readbuffer.start >= (fp->readbuffer.size - 4))
{
- fp->readbuffer.start=0;
- fp->read_resettable=0;
+ fp->readbuffer.start = 0;
+ fp->read_resettable = 0;
}
}
/* wait until we have input */
- if (tio_wait(fp,1,&deadline))
+ if (tio_wait(fp, 1, &deadline))
return -1;
/* read the input in the buffer */
- len=fp->readbuffer.size-fp->readbuffer.start;
+ len = fp->readbuffer.size - fp->readbuffer.start;
#ifdef SSIZE_MAX
- if (len>SSIZE_MAX)
- len=SSIZE_MAX;
+ if (len > SSIZE_MAX)
+ len = SSIZE_MAX;
#endif /* SSIZE_MAX */
- rv=read(fp->fd,fp->readbuffer.buffer+fp->readbuffer.start,len);
+ rv = read(fp->fd, fp->readbuffer.buffer + fp->readbuffer.start, len);
/* check for errors */
- if (rv==0)
+ if (rv == 0)
{
- errno=ECONNRESET;
+ errno = ECONNRESET;
return -1;
}
- else if ((rv<0)&&(errno!=EINTR)&&(errno!=EAGAIN))
- return -1; /* something went wrong with the read */
+ else if ((rv < 0) && (errno != EINTR) && (errno != EAGAIN))
+ return -1; /* something went wrong with the read */
/* skip the read part in the buffer */
- fp->readbuffer.len=rv;
+ fp->readbuffer.len = rv;
#ifdef DEBUG_TIO_STATS
- fp->bytesread+=rv;
+ fp->bytesread += rv;
#endif /* DEBUG_TIO_STATS */
}
}
@@ -294,7 +296,7 @@ int tio_read(TFILE *fp, void *buf, size_t count)
/* Read and discard the specified number of bytes from the stream. */
int tio_skip(TFILE *fp, size_t count)
{
- return tio_read(fp,NULL,count);
+ return tio_read(fp, NULL, count);
}
/* Read all available data from the stream and empty the read buffer. */
@@ -304,35 +306,35 @@ int tio_skipall(TFILE *fp)
int rv;
size_t len;
/* clear the read buffer */
- fp->readbuffer.start=0;
- fp->readbuffer.len=0;
- fp->read_resettable=0;
+ fp->readbuffer.start = 0;
+ fp->readbuffer.len = 0;
+ fp->read_resettable = 0;
/* read until we can't read no more */
- len=fp->readbuffer.size;
+ len = fp->readbuffer.size;
#ifdef SSIZE_MAX
- if (len>SSIZE_MAX)
- len=SSIZE_MAX;
+ if (len > SSIZE_MAX)
+ len = SSIZE_MAX;
#endif /* SSIZE_MAX */
while (1)
{
/* see if any data is available */
- fds[0].fd=fp->fd;
- fds[0].events=POLLIN;
- rv=poll(fds,1,0);
+ fds[0].fd = fp->fd;
+ fds[0].events = POLLIN;
+ rv = poll(fds, 1, 0);
/* check the poll() result */
- if (rv==0)
+ if (rv == 0)
return 0; /* no file descriptor ready */
- if ((rv<0)&&((errno==EINTR)||(errno==EAGAIN)))
+ if ((rv < 0) && ((errno == EINTR) || (errno == EAGAIN)))
continue; /* interrupted, try again */
- if (rv<0)
+ if (rv < 0)
return -1; /* something went wrong */
/* read data from the stream */
- rv=read(fp->fd,fp->readbuffer.buffer,len);
- if (rv==0)
+ rv = read(fp->fd, fp->readbuffer.buffer, len);
+ if (rv == 0)
return 0; /* end-of-file */
- if ((rv<0)&&(errno==EWOULDBLOCK))
+ if ((rv < 0) && (errno == EWOULDBLOCK))
return 0; /* we've ready everything we can without blocking */
- if ((rv<0)&&(errno!=EINTR)&&(errno!=EAGAIN))
+ if ((rv < 0) && (errno != EINTR) && (errno != EAGAIN))
return -1; /* something went wrong with the read */
}
}
@@ -344,46 +346,50 @@ static int tio_writebuf(TFILE *fp)
int rv;
/* write the buffer */
#ifdef MSG_NOSIGNAL
- rv=send(fp->fd,fp->writebuffer.buffer+fp->writebuffer.start,fp->writebuffer.len,MSG_NOSIGNAL);
+ rv = send(fp->fd, fp->writebuffer.buffer + fp->writebuffer.start,
+ fp->writebuffer.len, MSG_NOSIGNAL);
#else /* not MSG_NOSIGNAL */
/* on platforms that cannot use send() with masked signals, we change the
signal mask and change it back after the write (note that there is a
race condition here) */
- struct sigaction act,oldact;
+ struct sigaction act, oldact;
/* set up sigaction */
- memset(&act,0,sizeof(struct sigaction));
- act.sa_sigaction=NULL;
- act.sa_handler=SIG_IGN;
+ memset(&act, 0, sizeof(struct sigaction));
+ act.sa_sigaction = NULL;
+ act.sa_handler = SIG_IGN;
sigemptyset(&act.sa_mask);
- act.sa_flags=SA_RESTART;
+ act.sa_flags = SA_RESTART;
/* ignore SIGPIPE */
- if (sigaction(SIGPIPE,&act,&oldact)!=0)
+ if (sigaction(SIGPIPE, &act, &oldact) != 0)
return -1; /* error setting signal handler */
/* write the buffer */
- rv=write(fp->fd,fp->writebuffer.buffer+fp->writebuffer.start,fp->writebuffer.len);
+ rv = write(fp->fd, fp->writebuffer.buffer + fp->writebuffer.start,
+ fp->writebuffer.len);
/* restore the old handler for SIGPIPE */
- if (sigaction(SIGPIPE,&oldact,NULL)!=0)
+ if (sigaction(SIGPIPE, &oldact, NULL) != 0)
return -1; /* error restoring signal handler */
#endif
/* check for errors */
- if ((rv==0)||((rv<0)&&(errno!=EINTR)&&(errno!=EAGAIN)))
+ if ((rv == 0) || ((rv < 0) && (errno != EINTR) && (errno != EAGAIN)))
return -1; /* something went wrong with the write */
/* skip the written part in the buffer */
- if (rv>0)
+ if (rv > 0)
{
- fp->writebuffer.start+=rv;
- fp->writebuffer.len-=rv;
+ fp->writebuffer.start += rv;
+ fp->writebuffer.len -= rv;
#ifdef DEBUG_TIO_STATS
- fp->byteswritten+=rv;
+ fp->byteswritten += rv;
#endif /* DEBUG_TIO_STATS */
/* reset start if len is 0 */
- if (fp->writebuffer.len==0)
- fp->writebuffer.start=0;
+ if (fp->writebuffer.len == 0)
+ fp->writebuffer.start = 0;
/* move contents of the buffer to the front if it will save enough room */
- if (fp->writebuffer.start>=(fp->writebuffer.size/4))
+ if (fp->writebuffer.start >= (fp->writebuffer.size / 4))
{
- memmove(fp->writebuffer.buffer,fp->writebuffer.buffer+fp->writebuffer.start,fp->writebuffer.len);
- fp->writebuffer.start=0;
+ memmove(fp->writebuffer.buffer,
+ fp->writebuffer.buffer + fp->writebuffer.start,
+ fp->writebuffer.len);
+ fp->writebuffer.start = 0;
}
}
return 0;
@@ -394,12 +400,12 @@ int tio_flush(TFILE *fp)
{
struct timeval deadline;
/* build a time by which we should be finished */
- tio_get_deadline(&deadline,fp->writetimeout);
+ tio_get_deadline(&deadline, fp->writetimeout);
/* loop until we have written our buffer */
while (fp->writebuffer.len > 0)
{
/* wait until we can write */
- if (tio_wait(fp,0,&deadline))
+ if (tio_wait(fp, 0, &deadline))
return -1;
/* write one block */
if (tio_writebuf(fp))
@@ -415,15 +421,15 @@ static int tio_flush_nonblock(TFILE *fp)
struct pollfd fds[1];
int rv;
/* see if we can write without blocking */
- fds[0].fd=fp->fd;
- fds[0].events=POLLOUT;
- rv=poll(fds,1,0);
+ fds[0].fd = fp->fd;
+ fds[0].events = POLLOUT;
+ rv = poll(fds, 1, 0);
/* check if any file descriptors were ready (timeout) or we were
interrupted */
- if ((rv==0)||((rv<0)&&(errno==EINTR)))
+ if ((rv == 0) || ((rv < 0) && (errno == EINTR)))
return 0;
/* any other errors? */
- if (rv<0)
+ if (rv < 0)
return -1;
/* so file descriptor will accept writes */
return tio_writebuf(fp);
@@ -434,44 +440,46 @@ int tio_write(TFILE *fp, const void *buf, size_t count)
size_t fr;
uint8_t *tmp;
size_t newsz;
- const uint8_t *ptr=(const uint8_t *)buf;
+ const uint8_t *ptr = (const uint8_t *)buf;
/* keep filling the buffer until we have bufferred everything */
- while (count>0)
+ while (count > 0)
{
/* figure out free size in buffer */
- fr=fp->writebuffer.size-(fp->writebuffer.start+fp->writebuffer.len);
+ fr = fp->writebuffer.size - (fp->writebuffer.start + fp->writebuffer.len);
if (count <= fr)
{
/* the data fits in the buffer */
- memcpy(fp->writebuffer.buffer+fp->writebuffer.start+fp->writebuffer.len,ptr,count);
- fp->writebuffer.len+=count;
+ memcpy(fp->writebuffer.buffer + fp->writebuffer.start +
+ fp->writebuffer.len, ptr, count);
+ fp->writebuffer.len += count;
return 0;
}
else if (fr > 0)
{
/* fill the buffer with data that will fit */
- memcpy(fp->writebuffer.buffer+fp->writebuffer.start+fp->writebuffer.len,ptr,fr);
- fp->writebuffer.len+=fr;
- ptr+=fr;
- count-=fr;
+ memcpy(fp->writebuffer.buffer + fp->writebuffer.start +
+ fp->writebuffer.len, ptr, fr);
+ fp->writebuffer.len += fr;
+ ptr += fr;
+ count -= fr;
}
/* try to flush some of the data that is in the buffer */
if (tio_flush_nonblock(fp))
return -1;
/* if we have room now, try again */
- if (fp->writebuffer.size>(fp->writebuffer.start+fp->writebuffer.len))
+ if (fp->writebuffer.size > (fp->writebuffer.start + fp->writebuffer.len))
continue;
/* try to grow the buffer */
- if (fp->writebuffer.size<fp->writebuffer.maxsize)
+ if (fp->writebuffer.size < fp->writebuffer.maxsize)
{
- newsz=fp->writebuffer.size*2;
- if (newsz>fp->writebuffer.maxsize)
- newsz=fp->writebuffer.maxsize;
- tmp=realloc(fp->writebuffer.buffer,newsz);
- if (tmp!=NULL)
+ newsz = fp->writebuffer.size * 2;
+ if (newsz > fp->writebuffer.maxsize)
+ newsz = fp->writebuffer.maxsize;
+ tmp = realloc(fp->writebuffer.buffer, newsz);
+ if (tmp != NULL)
{
- fp->writebuffer.buffer=tmp;
- fp->writebuffer.size=newsz;
+ fp->writebuffer.buffer = tmp;
+ fp->writebuffer.size = newsz;
continue; /* try again */
}
}
@@ -486,14 +494,15 @@ int tio_close(TFILE *fp)
{
int retv;
/* write any buffered data */
- retv=tio_flush(fp);
+ retv = tio_flush(fp);
#ifdef DEBUG_TIO_STATS
/* dump statistics to stderr */
- fprintf(stderr,"DEBUG_TIO_STATS READ=%d WRITTEN=%d\n",fp->bytesread,fp->byteswritten);
+ fprintf(stderr, "DEBUG_TIO_STATS READ=%d WRITTEN=%d\n", fp->bytesread,
+ fp->byteswritten);
#endif /* DEBUG_TIO_STATS */
/* close file descriptor */
if (close(fp->fd))
- retv=-1;
+ retv = -1;
/* free any allocated buffers */
free(fp->readbuffer.buffer);
free(fp->writebuffer.buffer);
@@ -506,13 +515,14 @@ int tio_close(TFILE *fp)
void tio_mark(TFILE *fp)
{
/* move any data in the buffer to the start of the buffer */
- if ((fp->readbuffer.start>0)&&(fp->readbuffer.len>0))
+ if ((fp->readbuffer.start > 0) && (fp->readbuffer.len > 0))
{
- memmove(fp->readbuffer.buffer,fp->readbuffer.buffer+fp->readbuffer.start,fp->readbuffer.len);
- fp->readbuffer.start=0;
+ memmove(fp->readbuffer.buffer,
+ fp->readbuffer.buffer + fp->readbuffer.start, fp->readbuffer.len);
+ fp->readbuffer.start = 0;
}
/* mark the stream as resettable */
- fp->read_resettable=1;
+ fp->read_resettable = 1;
}
int tio_reset(TFILE *fp)
@@ -521,7 +531,7 @@ int tio_reset(TFILE *fp)
if (!fp->read_resettable)
return -1;
/* reset the buffer */
- fp->readbuffer.len+=fp->readbuffer.start;
- fp->readbuffer.start=0;
+ fp->readbuffer.len += fp->readbuffer.start;
+ fp->readbuffer.start = 0;
return 0;
}
diff --git a/common/tio.h b/common/tio.h
index cd3f370..b489b2e 100644
--- a/common/tio.h
+++ b/common/tio.h
@@ -47,22 +47,22 @@ typedef struct tio_fileinfo TFILE;
/* Open a new TFILE based on the file descriptor. The timeout is set for any
operation (value in milliseconds). */
-TFILE *tio_fdopen(int fd,int readtimeout,int writetimeout,
- size_t initreadsize,size_t maxreadsize,
- size_t initwritesize,size_t maxwritesize)
+TFILE *tio_fdopen(int fd, int readtimeout, int writetimeout,
+ size_t initreadsize, size_t maxreadsize,
+ size_t initwritesize, size_t maxwritesize)
LIKE_MALLOC MUST_USE;
/* Read the specified number of bytes from the stream. */
-int tio_read(TFILE *fp,void *buf,size_t count);
+int tio_read(TFILE *fp, void *buf, size_t count);
/* Read and discard the specified number of bytes from the stream. */
-int tio_skip(TFILE *fp,size_t count);
+int tio_skip(TFILE *fp, size_t count);
/* Read all available data from the stream and empty the read buffer. */
int tio_skipall(TFILE *fp);
/* Write the specified buffer to the stream. */
-int tio_write(TFILE *fp,const void *buf,size_t count);
+int tio_write(TFILE *fp, const void *buf, size_t count);
/* Write out all buffered data to the stream. */
int tio_flush(TFILE *fp);