static char    *sortattr = NULL;
 static int     verbose, not, includeufn, vals2tmp, ldif;
 #ifdef LDAP_CONTROL_PAGEDRESULTS
-static int pageSize;
+static int pageSize = 0;
 static ber_int_t searchControlSize = 0;
 static ber_int_t morePagedResults = 1;
 static struct berval cookie = { 0, NULL };
 #ifdef LDAP_CONTROL_PAGEDRESULTS
        BerElement      *pageber = NULL;
        struct berval   *bvalptr = NULL;
-       int             num = 0, moreEntries, searchControlCrit = 0;
+       int             num = 0, searchControlCrit = 0;
 #endif /* LDAP_CONTROL_PAGEDRESULTS */
 
 
 #ifdef LDAP_CONTROL_PAGEDRESULTS
                } else if ( strcasecmp( control, "pr" ) == 0 ) {
                        /* PagedResults control */
-                       if ( searchControlSize !=0 ) {
+                       if ( pageSize != 0 ) {
                                fprintf( stderr, "PagedResultsControl previously specified" );
                                return EXIT_FAILURE;
                        }
 
 #ifdef LDAP_CONTROL_PAGEDRESULTS
 getNextPage:
-       if ( manageDSAit || noop || valuesReturnFilter || searchControlSize ) {
+       if ( manageDSAit || noop || valuesReturnFilter || pageSize ) {
                int critical = 0;
 #else /* !LDAP_CONTROL_PAGEDRESULTS */
        if ( manageDSAit || noop || valuesReturnFilter ) {
                }
 
 #ifdef LDAP_CONTROL_PAGEDRESULTS
-               if ( searchControlSize ) {
+               if ( pageSize ) {
                        if (( pageber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
                                return EXIT_FAILURE;
                        }
                        printf("\n# with valuesReturnFilter %scontrol: %s",
                                valuesReturnFilter > 1 ? "critical " : "", vrFilter );
                }
+               if ( pageSize ) {
+                       printf("\n# with pagedResults %scontrol: size=%d",
+                               searchControlCrit ? "critical " : "", 
+                               searchControlSize );
+               }
 
                printf( "\n#\n\n" );
        }
        }
 
 #ifdef LDAP_CONTROL_PAGEDRESULTS
-       if ( ( searchControlSize != 0 ) && ( morePagedResults != 0 ) ) { 
+       if ( ( pageSize != 0 ) && ( morePagedResults != 0 ) ) { 
+               char    buf[6];
+               int     i, moreEntries, tmpSize;
+
                /* Loop to get the next pages when 
                 * enter is pressed on the terminal.
                 */
-               printf( "Press Enter for the next %d entries.\n",
+               printf( "Press [size] Enter for the next {%d|size} entries.\n",
                        (int)searchControlSize ); 
+               i = 0;
                moreEntries = getchar();
                while ( moreEntries != EOF && moreEntries != '\n' ) { 
+                       if ( i < sizeof(buf) - 1 ) {
+                               buf[i] = moreEntries;
+                               i++;
+                       }
                        moreEntries = getchar();
                }
+               buf[i] = '\0';
+
+               if ( i > 0 && isdigit( buf[0] ) ) {
+                       num = sscanf( buf, "%d", &tmpSize );
+                       if ( num != 1 ) {
+                               fprintf( stderr, "Invalid value for PagedResultsControl, %s.\n", buf);
+                               return EXIT_FAILURE;
+
+                       }
+                       searchControlSize = (ber_int_t)tmpSize;
+               }
 
                goto getNextPage;       
        }
                        case LDAP_RES_SEARCH_RESULT:
                                rc = print_result( ld, msg, 1 );
 #ifdef LDAP_CONTROL_PAGEDRESULTS
-                               if ( searchControlSize != 0 ) { 
+                               if ( pageSize != 0 ) { 
                                        rc = parse_page_control( ld, msg, &cookie );
                                }
 #endif /* LDAP_CONTROL_PAGEDRESULTS */
 
 done:
 #ifdef LDAP_CONTROL_PAGEDRESULTS
-       if ( searchControlSize == 0 ) { 
+       if ( pageSize == 0 ) { 
                if ( ldif < 2 ) {
                        printf( "\n# numResponses: %d\n", nresponses );
                        if( nentries ) printf( "# numEntries: %d\n", nentries );
 
                if ( op->o_pagedresults_state.ps_cookie == 0 ) {
                        id = 0;
                } else {
+                       if ( op->o_pagedresults_size == 0 ) {
+                               send_search_result( conn, op, LDAP_SUCCESS,
+                                       NULL, "search abandoned by pagedResult size=0",
+                                       NULL, NULL, 0);
+                               goto done;
+                       }
                        for ( id = bdb_idl_first( candidates, &cursor );
                                id != NOID && id <= (ID)( op->o_pagedresults_state.ps_cookie );
                                id = bdb_idl_next( candidates, &cursor ) );
        respcookie = ( PagedResultsCookie )lastid;
        conn->c_pagedresults_state.ps_cookie = respcookie;
        cookie.bv_len = sizeof( respcookie );
-#if 0
-       cookie.bv_val = ber_memalloc( sizeof( respcookie ) );
-       AC_MEMCPY( cookie.bv_val, &respcookie, sizeof( respcookie ) );
-#else
        cookie.bv_val = (char *)&respcookie;
-#endif
-/*
-       conn->c_pagedresults_state.ps_cookie = cookie.bv_val;
-*/
 
+       /*
+        * FIXME: we should consider sending an estimate of the entries
+        * left, after appropriate security check is done
+        */
        ber_printf( ber, "{iO}", 0, &cookie ); 
-#if 0
-       ber_memfree( cookie.bv_val );
-#endif
 
        if ( ber_flatten( ber, &bvalp ) == LBER_ERROR ) {
                goto done;