aboutsummaryrefslogtreecommitdiff
path: root/src/functiondirs.cpp
blob: 99c6c79044233ffce51d9a4ae3932967bfd666d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (C) 2007-2012 Xagasoft, All rights reserved.
 *
 * This file is part of the Xagasoft Build and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "functiondirs.h"

#include <sys/types.h> 
#include <sys/stat.h> 
#include <glob.h>
#include <unistd.h>

#include <bu/plugger.h>
PluginInterface3( pluginFunctionDirs, dirs, FunctionDirs, Function,
        "Mike Buland", 0, 1 );

FunctionDirs::FunctionDirs()
{
}

FunctionDirs::~FunctionDirs()
{
}

Bu::String FunctionDirs::getName() const
{
    return "dirs";
}

Variable FunctionDirs::call( Variable &/*input*/, VarList lParams )
{
    glob_t globbuf;

    int flags = 0;

    for( VarList::const_iterator i = lParams.begin(); i; i++ )
    {
        switch( (*i).getType() )
        {
            case Variable::typeString:
                glob( (*i).getString().getStr(), flags, NULL, &globbuf );
                flags |= GLOB_APPEND;
                break;

            case Variable::typeList:
                throw Bu::ExceptionBase("Lists not supported in glob yet.");
                break;

            default:
                throw Bu::ExceptionBase(
                    "Cannot use a non-string or non-list as a parameter to glob"
                    );
                break;
        }
    }

    Variable vRet( Variable::typeList );
    struct stat s;
    for( size_t j = 0; j < globbuf.gl_pathc; j++ )
    {
        stat( globbuf.gl_pathv[j], &s );
        if( S_ISDIR( s.st_mode ) )
            vRet.append( globbuf.gl_pathv[j] );
    }

    globfree( &globbuf );

    return vRet;
}