]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/tmplout.c
s/<stdlib.h>/<ac/stdlib.h>/
[openldap] / libraries / libldap / tmplout.c
index 9e5f9af0fb10d6385fa681f4bb866fc7ae1dcfc0..72b213947eadd879082aea43c8881f7774da6cc6 100644 (file)
@@ -10,7 +10,8 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <stdlib.h>
+
+#include <ac/stdlib.h>
 
 #include <ac/ctype.h>
 #include <ac/string.h>
@@ -29,6 +30,8 @@
 #include "ldap-int.h"
 #include "ldapconfig.h"
 
+#define writeptype ldap_writeptype
+
 /* local functions */
 static int do_entry2text LDAP_P((
        LDAP *ld, char *buf, char *base, LDAPMessage *entry,
@@ -64,8 +67,8 @@ static int searchaction LDAP_P((
 #define OCATTRNAME             "objectClass"
 
 
-#define NONFATAL_LDAP_ERR( err )       ( err == LDAP_SUCCESS || \
-       err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
+#define NONFATAL_LDAP_ERR( err )       ( (err) == LDAP_SUCCESS || \
+       (err) == LDAP_TIMELIMIT_EXCEEDED || (err) == LDAP_SIZELIMIT_EXCEEDED )
 
 #define DEF_LDAP_URL_PREFIX    "ldap:///"
 
@@ -149,9 +152,9 @@ do_entry2text(
     }
 
     if ( buf == NULL ) {
-       if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
+       if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
            ld->ld_errno = LDAP_NO_MEMORY;
-           free( dn );
+           LDAP_FREE( dn );
            return( ld->ld_errno );
        }
        freebuf = 1;
@@ -256,7 +259,7 @@ do_entry2text(
                freevals = 1;
            }
 
-               *attr = TOUPPER( *attr );
+           *attr = TOUPPER( (unsigned char) *attr );
 
            err = do_vals2text( ld, buf, vals, attr, labelwidth,
                    LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol, 
@@ -271,9 +274,9 @@ do_entry2text(
        }
     } else {
        for ( rowp = ldap_first_tmplrow( tmpl );
-               NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
+               NONFATAL_LDAP_ERR( err ) && rowp != NULL;
                rowp = ldap_next_tmplrow( tmpl, rowp )) {
-           for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
+           for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULL;
                    colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
                vals = NULL;
                if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
@@ -309,14 +312,14 @@ do_entry2text(
                if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
                        == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
                        colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
-                       TOUPPER( vals[ 0 ][ 0 ] ) != 'T' ) {
+                       TOUPPER( (unsigned char) vals[ 0 ][ 0 ] ) != 'T' ) {
                    show = 0;
                }
 
                if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
                    if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
                        if ( colp->ti_attrname == NULL || ( show &&
-                               TOUPPER( vals[ 0 ][ 0 ] ) == 'T' )) {
+                               TOUPPER( (unsigned char) vals[ 0 ][ 0 ] ) == 'T' )) {
                            err = searchaction( ld, buf, base, entry, dn, colp,
                                    labelwidth, rdncount, writeproc,
                                    writeparm, eol, urlprefix );
@@ -343,9 +346,9 @@ do_entry2text(
        (*writeproc)( writeparm, buf, strlen( buf ));
     }
 
-    free( dn );
+    LDAP_FREE( dn );
     if ( freebuf ) {
-       free( buf );
+       LDAP_FREE( buf );
     }
 
     return( err );
@@ -433,7 +436,7 @@ do_entry2text_search(
     timeout.tv_sec = SEARCH_TIMEOUT_SECS;
     timeout.tv_usec = 0;
 
-    if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
+    if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
        ld->ld_errno = LDAP_NO_MEMORY;
        return( ld->ld_errno );
     }
@@ -453,7 +456,7 @@ do_entry2text_search(
 
     if ( dn == NULL ) {
        if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
-           free( buf );
+           LDAP_FREE( buf );
            if ( freetmpls ) {
                ldap_free_templates( tmpllist );
            }
@@ -515,7 +518,7 @@ do_entry2text_search(
                fetchattrs, 0, &timeout, &ldmp );
 
     if ( freedn ) {
-       free( dn );
+       LDAP_FREE( dn );
     }
     if ( fetchattrs != NULL ) {
        ldap_value_free( fetchattrs );
@@ -526,14 +529,14 @@ do_entry2text_search(
        if ( freetmpls ) {
             ldap_free_templates( tmpllist );
         }
-       free( buf );
+       LDAP_FREE( buf );
        return( ld->ld_errno );
     }
 
     err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
            writeproc, writeparm, eol, rdncount, opts, urlprefix );
 
-    free( buf );
+    LDAP_FREE( buf );
     if ( freetmpls ) {
        ldap_free_templates( tmpllist );
     }
@@ -627,7 +630,7 @@ do_vals2text(
     }
 
     if ( buf == NULL ) {
-       if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
+       if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
            ld->ld_errno = LDAP_NO_MEMORY;
            return( ld->ld_errno );
        }
@@ -680,7 +683,7 @@ do_vals2text(
            p = s = outval;
            while (( s = strchr( s, '$' )) != NULL ) {
                *s++ = '\0';
-               while ( isspace( *s )) {
+               while ( isspace( (unsigned char) *s )) {
                    ++s;
                }
                if ( html ) {
@@ -696,7 +699,7 @@ do_vals2text(
            break;
 
        case LDAP_SYN_BOOLEAN:
-           outval = TOUPPER( outval[ 0 ] ) == 'T' ? "TRUE" : "FALSE";
+           outval = TOUPPER((unsigned char) outval[0]) == 'T' ? "TRUE" : "FALSE";
            ++writeoutval;
            break;
 
@@ -709,13 +712,13 @@ do_vals2text(
        case LDAP_SYN_LABELEDURL:
            if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
                *p++ = '\0';
-               while ( isspace( *p )) {
+               while ( isspace( (unsigned char) *p )) {
                    ++p;
                }
                s = outval;
            } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
                *s++ = '\0';
-               while ( isspace( *s )) {
+               while ( isspace( (unsigned char) *s )) {
                    ++s;
                }
                p = outval;
@@ -753,7 +756,7 @@ do_vals2text(
     }
 
     if ( freebuf ) {
-       free( buf );
+       LDAP_FREE( buf );
     }
 
     return( LDAP_SUCCESS );
@@ -768,9 +771,9 @@ max_label_len( struct ldap_disptmpl *tmpl )
 
     maxlen = 0;
 
-    for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
+    for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULL;
            rowp = ldap_next_tmplrow( tmpl, rowp )) {
-       for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
+       for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULL;
                colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
            if (( len = strlen( colp->ti_label )) > maxlen ) {
                maxlen = len;
@@ -848,10 +851,10 @@ output_dn( char *buf, char *dn, int width, int rdncount,
 
 
 
-#define HREF_CHAR_ACCEPTABLE( c )      (( c >= '-' && c <= '9' ) ||    \
-                                        ( c >= '@' && c <= 'Z' ) ||    \
-                                        ( c == '_' ) ||                \
-                                        ( c >= 'a' && c <= 'z' ))
+#define HREF_CHAR_ACCEPTABLE( c )      (( (c) >= '-' && (c) <= '9' ) || \
+                                        ( (c) >= '@' && (c) <= 'Z' ) || \
+                                        ( (c) == '_' )               || \
+                                        ( (c) >= 'a' && (c) <= 'z' ))
 
 static void
 strcat_escaped( char *s1, char *s2 )
@@ -874,7 +877,7 @@ strcat_escaped( char *s1, char *s2 )
 }
 
 
-#define GET2BYTENUM( p )       (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
+#define GET2BYTENUM( p )       (( *(p) - '0' ) * 10 + ( *((p)+1) - '0' ))
 
 static char *
 time2text( char *ldtimestr, int dateonly )
@@ -883,20 +886,32 @@ time2text( char *ldtimestr, int dateonly )
     char               *p, *timestr, zone, *fmterr = "badly formatted time";
     time_t             gmttime;
     char               timebuf[32];
+       int ndigits;
 
-    memset( (char *)&t, 0, sizeof( struct tm ));
-    if ( (int) strlen( ldtimestr ) < 13 ) {
-       return( fmterr );
+       if (strlen( ldtimestr ) < 12 ) {
+               return( fmterr );
+       }
+
+    for ( ndigits=0; isdigit((unsigned char) ldtimestr[ndigits]); ndigits++) {
+               ; /* EMPTY */
     }
 
-    for ( p = ldtimestr; p - ldtimestr < 12; ++p ) {
-       if ( !isdigit( *p )) {
+       if ( ndigits != 12 && ndigits != 14) {
            return( fmterr );
        }
-    }
+       
+    memset( (char *)&t, 0, sizeof( struct tm ));
 
     p = ldtimestr;
+
+       if( ndigits == 14) {
+               /* came with a century */
+               /* POSIX says tm_year should be year - 1900 */
+       t.tm_year = 100 * GET2BYTENUM( p ) - 1900;
+               p += 2;
+       }
     t.tm_year = GET2BYTENUM( p ); p += 2;
+
     t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
     t.tm_mday = GET2BYTENUM( p ); p += 2;
     t.tm_hour = GET2BYTENUM( p ); p += 2;
@@ -926,14 +941,34 @@ time2text( char *ldtimestr, int dateonly )
        This routine was supplied by Mike Accetta at CMU many years ago.
  */
 
-static int     dmsize[] = {
+static const int dmsize[] = {
     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
 };
 
 #define        dysize(y)       \
        (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
 
-#define        YEAR(y)         ((y) >= 100 ? (y) : (y) + 1900)
+/*
+ * Y2K YEAR
+ */
+       /* per POSIX tm_year should be offset by 1900 */
+#define YEAR_POSIX(y)          ((y) + 1900)
+
+       /*
+        * year is < 1900, year is offset by 1900
+        */
+#define YEAR_CAREFUL(y)                ((y) < 1900 ? (y) + 1900 : (y))
+
+       /*
+       ** if year is < 1990 and < 70 must be offset by 2000 as Unix epoch
+       ** started in 1970.  if year is < 1990 but >= 70, offset by 1900.
+       ** if year is >= 1900, it must be the real year.
+       */ 
+#define YEAR_PEDANTIC(y)               ((y) < 1900 \
+                                               ? ((y) < 70 ? (y) + 2000 ? (y) + 1900) \
+                                               : (y))
+
+#define YEAR(y) YEAR_CAREFUL(y)
 
 /* \f */
 
@@ -1051,7 +1086,7 @@ searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
 
     if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
        if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
-           if (( members = (char **)malloc( (count + 1) * sizeof(char *)))
+           if (( members = (char **)LDAP_MALLOC( (count + 1) * sizeof(char *)))
                    == NULL ) {
                err = LDAP_NO_MEMORY;
            } else {