/* Replace characters that cannot be used in directory names. */
static void cleanstr(char *s)
{
int i;
for (i = 0; s[i]; ++i) {
switch(s[i]) {
case '/':
#ifdef _WIN32
case '<':
case '>':
case ':':
case '"':
case '\\':
case '|':
case '?':
case '*':
#endif
s[i] = ' ';
}
}
}
/* Create a directory if it does not exist. */
static void makedir(const char *path)
{
if (access(path, F_OK) == -1) {
#ifdef _WIN32
mkdir(path);
#else
mkdir(path, S_IRWXU);
#endif
}
}
/* Create the directory structure for the SNS. */
static void setup_sns(xmlNodePtr node, const char *snsdname)
{
xmlNodePtr cur;
char *content;
char code[256];
for (cur = node->children; cur; cur = cur->next) {
if (xmlStrcmp(cur->name, BAD_CAST "snsCode") == 0) {
content = (char *) xmlNodeGetContent(cur);
/* Test if the SNS directory exists for a specified code. */
static int sns_exists(const char *code, char *dname)
{
DIR *dir;
struct dirent *cur;
int exists = 0;
dir = opendir(".");
while ((cur = readdir(dir))) {
if (strncmp(code, cur->d_name, strlen(code)) == 0) {
exists = 1;
strcpy(dname, cur->d_name);
break;
}
}
closedir(dir);
return exists;
}
/* Place a link to a DM file in to the proper place in the SNS directory hierarchy. */
static void placedm(const char *fname, struct dm_code *code, const char *snsdname, const char *srcdname)
{
char path[PATH_MAX], dname[PATH_MAX], orig[PATH_MAX];
bool link = true;
get_current_dir(orig, PATH_MAX);
strcpy(path, srcdname);
strcat(path, "/");
change_dir(snsdname);
if (sns_exists(code->system_code, dname)) {
change_dir(dname);
if (sns_exists(code->sub_system_code, dname)) {
change_dir(dname);
if (sns_exists(code->sub_sub_system_code, dname)) {
change_dir(dname);
if (sns_exists(code->assy_code, dname)) {
change_dir(dname);
}
}
}
}
strcat(path, fname);
if (access(fname, F_OK) != -1) {
char d[PATH_MAX];
get_current_dir(d, PATH_MAX);
if (strcmp(d, srcdname) != 0) {
unlink(fname);
} else {
link = false;
}
}
static void show_help(void)
{
puts("Usage: " PROG_NAME " [-D <dir>] [-d <dir>] [-cmnpsh?] [<BREX> ...]");
puts("");
puts("Options:");
puts(" -c, --copy Copy files instead of linking.");
puts(" -D, --srcdir <dir> Directory where DMs are stored. Default is current directory.");
puts(" -d, --outdir <dir> Directory to organize DMs in to. Default is \"" DEFAULT_SNS_DNAME "\"");
puts(" -h, -?, --help Show usage message.");
puts(" -m, --move Move files instead of linking.");
puts(" -n, --only-code Only use the SNS code to name directories.");
puts(" -p, --print Print SNS instead of organizing.");
puts(" -s, --symlink Use symbolic links.");
puts(" --version Show version information.");
puts(" <BREX> BREX data module to read SNS structure from.");
LIBXML2_PARSE_LONGOPT_HELP
}