scandir(3) problems with names w/spaces

I am writing a command line utility under MacOS 10.4.11 using XCode and I have run into a problem that has me buffalo'd. I have managed to create a utility that reads command line arguments and processes option letters and directory names, much as ls(1) would. However, when I pass in a directory name containing spaces, the scandir utility returns a -1 (fail). Now, I was careful to make sure that the directory name was enclosed in quotes on the command line and when I look in the debugger when I call scandir, the directory name variable contains the entire directory name (e.g. ~/dir1/dir2/dir 3). But for some reason scandir can't handle the name, even though it has handled everything else I've thrown at it, relative directory names, full names, names using the "~" like the example. Any ideas anyone?


Thanks a million!

MacBook w/2Ghz Core-Duo & 2GB Ram, Mac OS X (10.4.11)

Posted on Jul 10, 2012 7:07 PM

Reply
7 replies

Jul 11, 2012 6:45 AM in response to NoGreenHorn

Escaping them usually works well if you know what they are but they may be variables with unknown content.


Don't forget that single and double quotes behave differently - you could try changing to the other type.


Could you post the bash script here?


Sometimes it is practical to use backticks or brackets to overcome problems.


You might find these helpful:


http://tldp.org/LDP/Bash-Beginners-Guide/html/


http://www.gnu.org/software/bash/manual/html_node

Jul 12, 2012 5:43 PM in response to Neville Hillyer

This is not a bash script. It's "C". I've been writing software since the 70's and C since the 80's, but this library function is not helping. Here is the source for the function containing the call:


/*

* process_dir.c

* parser

*

* Created by Vic on 6/24/12.

* Copyright 2012 __MyCompanyName__. All rights reserved.

*

*/

#include <stdio.h>

#include <stdlib.h>

#include <dirent.h>

#include <sys/types.h>

#include "file_select.h"

#include "options.h"

#include "options_struct.h"

#include "long_dir_list.h"

#include "short_dir_list.h"


int process_dir (char *dir_name){

int files, i;

struct dirent **namelist;

extern struct options_struct options[option_last+1];


printf ("I have received directory name: %s\n", dir_name);


if (!options[option_f].set)

files = scandir (dir_name, &namelist, file_select, alphasort);

else

files = scandir (dir_name, &namelist, file_select, NULL);

if (files < 0)

exit -1;

else if (files > 0){

if (options[option_l].set){

long_dir_list (dir_name, files, namelist);

} else {

short_dir_list (dir_name, files, namelist);

}

}

for (i = files - 1; i >= 0; i--)

free (namelist[i]);

free (namelist);

return 0;

}


As you can see from the pictures below, before the call the debugger shows that the directory is in the dir_name variable. After, the return value from the call is -1, indicating that the call has failed. And yes, I have checked several times, and the directory and all of the subdirectories under it are intact.


User uploaded fileUser uploaded file

Jul 12, 2012 7:11 PM in response to BDAqua

Yes, I have. It works great with relative pathnames, with just the name of a local directory, with full path names from the root ("/Users/vic/....." whatever) as long as there are no spaces or special characters in any of the pathnames. And yes, I have tried passing several in at one time and it processes each one successfully as long as there are no spaces, etc. I have even tried just "../.." and it processed that without a problem. It seems that this library implementation of scandir(3) just has a problem with special characters in the input directory name. At this point, I'm almost tempted to write my own version without this bug!!


Thanks in advance for any help anyone can provide!!!!!!!!!

Jul 13, 2012 2:58 AM in response to BDAqua

URL encoding probably only works in URL environments.


scandir may never have been updated to include Apple path names with spaces.


Do what I do to debug bash, JavaScript and AppleScript - you will probably have to do this within scandir source code:


some script

z="bad name"

echo $z

more script


Keep doing this until you discover what is happening.


You have posted to the wrong forum.


You should take this to the UNIX area of Mac OS X Technologies:


https://discussions.apple.com/community/mac_os/mac_os_x_technologies?view=discus sions#/?per_page=50&tagSet=1048


Or better still the Developer forum:


https://developer.apple.com/devforums/


Memership is free.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

scandir(3) problems with names w/spaces

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.