Changing recurse option from hard limit to something "smarter"

Tom Jansen t.p.j.jansen at student.utwente.nl
Fri Nov 16 17:59:39 GMT 2001


[avoid recursion in smbclient]

You can use libsmbclient ?!

I use the following code to just list recursively... I'm sure you can enhance
it!

Yours,

	Tom
-------------- next part --------------
#include <stdio.h>
#include <libsmbclient.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

/* smblist v1.1 
 * written by Tom Jansen (c) Ninja ISD 2001
 * included in the np3 project 
 */


static void auth_fn (const char *srv, const char *shr,
                     char *wg, int wglen,
                     char *un, int unlen,
                     char *pw, int pwlen)
{
	strcpy(un, "guest");
	strcpy(pw, "");
}



static void read_dir(char * fn, int recursion)
{
	struct smbc_dirent *de;
	char * next;
	int dh;
	
	
	if ( (dh = smbc_opendir(fn)) < 0 ) {
		fprintf(stdout, "E smbc_opendir: %d %s\n", errno, strerror(errno));
		return;
	}
	

	while( (de = smbc_readdir(dh)) )
	{
		switch(de->smbc_type) { 
		case SMBC_DIR:
		case SMBC_FILE_SHARE:
			/* print voor dir */
			if (recursion <= 0) {
				fprintf(stdout,"D %s\n",de->name);
			}
			else {
				next = malloc(strlen(fn) + strlen(de->name) + 2);
				strcpy(next, fn);
				if (fn[strlen(fn)]!='/')strcat(next, "/");
				strcat(next, de->name);
				fprintf(stdout, "D %s\n",next);

				if (recursion > 1 && strcmp(de->name,".") && strcmp(de->name, "..") )
					read_dir(next, recursion - 1);

				free(next);
			}
			break;

		case SMBC_FILE:
			/* print voor file */
			if (recursion <= 0) {
				fprintf(stdout,"F %s\n",de->name);
			}
			else {
				next = malloc(strlen(fn) + strlen(de->name) + 2);
				strcpy(next, fn);
				if (fn[strlen(fn)]!='/')strcat(next, "/");
				strcat(next, de->name);
				fprintf(stdout, "F %s \n",next);
				free(next);
			}
			break;
		default:
			/* MAYOR NAARHEID ! */
			fprintf(stdout,"U %s (%d)\n",de->name, de->smbc_type);
		}
		/*  moet ik struct * de opruimen ? */
		
	}
	smbc_closedir(dh);
}




int main(int argc, char * argv[])
{
	int rec = 0;

	if ( argv[1] == NULL ) {
		fprintf(stdout, "E no argument given.\n");
		fprintf(stdout, "E usage: %s smburl [max recursion depth]\n",argv[0]);
		fprintf(stdout, "E HINT: a recursion depth < 0 gives atomic output.\n");
		exit(12);
	}
	else if (argv[2] && (rec = atoi(argv[2])) );
	   

	if ( smbc_init(&auth_fn, 0) < 0) {
		fprintf(stdout, "E smbc_init: %d %s\n", errno, strerror(errno));
		exit(errno);
	}
   
	read_dir(argv[1], rec);

	exit(0);
}





More information about the samba-technical mailing list