Can someone tell me what I'm doing wrong?

Ray Van Dolson rayvd at bludgeon.org
Wed May 23 02:25:27 GMT 2001


I'm compiling this with gcc pysmb.c -lsmbclient -o pysmb and get a
segfault when I run it -- ./pysmb smb://BLUDGEON/Files (where BLUDGEON is
a computer name in the workgroup STUDENTS).  This was working earlier too!
I have an older executable that works properly also... so I think my
library is intact.

Anyone see anything blatantly wrong?  It segfaults at sbmc_opendir ...

#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <libsmbclient.h>

#define MAX_BUFF_SIZE 255
#define WORKGROUP "STUDENTS"
#define USERNAME "Guest"
#define PASSWORD ""

void auth_fn(const char *server, const char *share, char *workgroup, int wgmaxlen, char *username, int unmaxlen, char *password, int pwmaxlen) {
  char temp[128];

  strncpy(workgroup,WORKGROUP,8);

  if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
    temp[strlen(temp) - 1] = 0x00;

  if (temp[0]) strncpy(workgroup, temp, wgmaxlen - 1);

	strncpy(username,USERNAME,5);

  if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
    temp[strlen(temp) - 1] = 0x00;

  if (temp[0]) strncpy(username, temp, unmaxlen - 1);

	strncpy(password,PASSWORD,0);

  if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
    temp[strlen(temp) - 1] = 0x00;

  if (temp[0]) strncpy(password, temp, pwmaxlen - 1);

}

int directory_list(char *url, char dirs[512][255]) {
	int error, dirHandle, i;
	char dirbuff[MAX_BUFF_SIZE];
	struct smbc_dirent *dirStruct;

	dirStruct = (struct smbc_dirent *) dirbuff;
	error = smbc_init(auth_fn, 10);

	i = 0;
	dirHandle = smbc_opendir(url);
	while (dirStruct = smbc_readdir(dirHandle)) {
		strncpy(dirs[i++], dirStruct->name, dirStruct->namelen);
	}
	return i;
}

int main(int argc, char** argv) {
	char directories[512][255];
	int i,count;

	i = 0;
	i = directory_list(argv[1],directories);
	for (count = 0; count < i; count++) {
		printf("%s\n",directories[count]);
	}
	printf("Total: %d files\n",i);
}





More information about the samba-technical mailing list