Пример #1
0
/**
 * \brief Initializes the program.
 *
 * \param program_args is the program argument vector.
 *
 * \return EXIT_SUCCESS the program was successfully initialized,
 *  otherwise program startup failed.
 * \retval ENOMEM posix error out of memory.
 * \retval ENODATA posix error ENODATA no data available for maximum path length.
 */
int init(const char** program_args)
{
    sprogram_arg0 = program_args[0];

    if (NULL == sprint_buffer)
    {
        sprint_buffer = (char*) malloc(MAX_PRINT_BUFFER * sizeof(char));
        if (NULL == sprint_buffer)
        {
            fprintf(stderr, "%s: %s\n", sprogram_arg0, "Out of memory.\n");
            return ENOMEM;
        }
    }

    /* get maximum directory size */
    smax_path = pathconf(".", _PC_PATH_MAX);
    if (-1 == smax_path)
    {
        smax_path = 0;
        snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "pathconf() () failed: %s.",
                strerror(errno));
        print_error(get_print_buffer());
        return ENODATA;
    }

    if (NULL == spath_buffer)
    {
        spath_buffer = (char*) malloc(smax_path * sizeof(char));
        if (NULL == spath_buffer)
        {
            print_error("malloc() failed: Out of memory.");
            return ENOMEM;
        }
    }
    if (NULL == sbasename_buffer)
    {
        sbasename_buffer = (char*) malloc(smax_path * sizeof(char));
        if (NULL == sbasename_buffer)
        {
            print_error("malloc() failed: Out of memory.");
            return ENOMEM;
        }
    }

    return EXIT_SUCCESS;

}
Пример #2
0
/**
 * \brief Filters the directory entry due to -user parameter.
 *
 * Applies -user filter (if defined) to file_info.
 *
 * \param current_param currently processed parameter index.
 * \param params Program parameter arguments given by user.
 * \param file_info as read from operating system.
 *
 * \return boolean result indicating filter has matched.
 * \retval TRUE name filter matched or not given.
 * \retval FALSE no match found.
 */
static boolean filter_user(const int current_param, const char* const* params, StatType* file_info)
{
    unsigned int search_uid = 0;
    char * end_ptr = NULL;
    struct passwd* pwd = NULL;

    search_uid = strtol(params[current_param + 1], &end_ptr, USERID_BASE);
    if (('\0' != *end_ptr) || user_exist(params[current_param + 1], FALSE))
    {
        /*  string to int conversion failed --> we have a username */
        /*  or special case --> username is pure numeric */
        if (user_exist(params[current_param + 1], FALSE))
        {
            pwd = getpwuid(file_info->st_uid);
            if (pwd != NULL)
            {
                if (strcmp(pwd->pw_name, params[current_param + 1]) == 0)
                {
                    /* parameter of -user is equal to
                     * user name derived from UID */
                    return TRUE;
                }
            }
            return FALSE;
        }
        else
        {
            snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s' is not the name of a known user",
                    params[current_param + 1]);
            print_error(get_print_buffer());
            cleanup(TRUE);
            return FALSE;
        }
    }
    else
    {
        /* successfull string to int conversion */
        /* -> parameter of -user seems to be an UID */
        return (search_uid == file_info->st_uid);
    }
    return TRUE;
}
Пример #3
0
/**
 * \brief Print out last changed date of file on standard output.
 *
 * \param file_info with the file attributes.
 *
 * \return void
 **/
void print_file_change_time(const StatType* file_info)
{
    char * buffer_char = NULL;
    int i = 0;
    int written = 0;
    size_t written_time = 0;

    /* Convert the time into the local time format it. */
    written_time = strftime(get_print_buffer(), MAX_PRINT_BUFFER - 1, "%b %d %H:%M",
            localtime(&file_info->st_mtime));
    if (0 == written_time)
    {
        snprintf(get_print_buffer(), MAX_PRINT_BUFFER,
                "strftime() failed: Could not print file changed time\n.");
        print_error(get_print_buffer());
        return;
    }

    /* as strftime Format Parameter %e is not supported on Annubis
     * we have to adjust the remove leading 0 in the day */
    buffer_char = get_print_buffer();
    for (i = 0; i < MAX_PRINT_BUFFER; i++)
    {
        if (' ' == buffer_char[i])
        {
            if ('0' == buffer_char[i + 1])
            {
                buffer_char[i + 1] = ' ';
            }
            break;
        }
    }

    written = fprintf(stdout, "%s", get_print_buffer());
    if (written < 0)
    {
        print_error("fprintf() failed: Could not write time to stdout.\n");
    }

}
Пример #4
0
char* orte_rmaps_base_print_mapping(orte_mapping_policy_t mapping)
{
    char *ret, *map, *mymap, *tmp;
    orte_rmaps_print_buffers_t *ptr;

    if (ORTE_MAPPING_CONFLICTED & ORTE_GET_MAPPING_DIRECTIVE(mapping)) {
        return "CONFLICTED";
    }

    ptr = get_print_buffer();
    if (NULL == ptr) {
        ORTE_ERROR_LOG(ORTE_ERR_OUT_OF_RESOURCE);
        return orte_rmaps_print_null;
    }
    /* cycle around the ring */
    if (ORTE_RMAPS_PRINT_NUM_BUFS == ptr->cntr) {
        ptr->cntr = 0;
    }

    switch(ORTE_GET_MAPPING_POLICY(mapping)) {
    case ORTE_MAPPING_BYNODE:
        map = "BYNODE";
        break;
    case ORTE_MAPPING_BYBOARD:
        map = "BYBOARD";
        break;
    case ORTE_MAPPING_BYNUMA:
        map = "BYNUMA";
        break;
    case ORTE_MAPPING_BYSOCKET:
        map = "BYSOCKET";
        break;
    case ORTE_MAPPING_BYL3CACHE:
        map = "BYL3CACHE";
        break;
    case ORTE_MAPPING_BYL2CACHE:
        map = "BYL2CACHE";
        break;
    case ORTE_MAPPING_BYL1CACHE:
        map = "BYL1CACHE";
        break;
    case ORTE_MAPPING_BYCORE:
        map = "BYCORE";
        break;
    case ORTE_MAPPING_BYHWTHREAD:
        map = "BYHWTHREAD";
        break;
    case ORTE_MAPPING_BYSLOT:
        map = "BYSLOT";
        break;
    case ORTE_MAPPING_SEQ:
        map = "SEQUENTIAL";
        break;
    case ORTE_MAPPING_BYUSER:
        map = "BYUSER";
        break;
    case ORTE_MAPPING_BYDIST:
        map = "MINDIST";
        break;
    default:
        if (ORTE_MAPPING_PPR & ORTE_GET_MAPPING_DIRECTIVE(mapping)) {
            map = "PPR";
        } else {
            map = "UNKNOWN";
        }
    }
    if (0 != strcmp(map, "PPR") && (ORTE_MAPPING_PPR & ORTE_GET_MAPPING_DIRECTIVE(mapping))) {
        asprintf(&mymap, "%s[PPR]:", map);
    } else {
        asprintf(&mymap, "%s:", map);
    }
    if (ORTE_MAPPING_NO_USE_LOCAL & ORTE_GET_MAPPING_DIRECTIVE(mapping)) {
        asprintf(&tmp, "%sNO_USE_LOCAL,", mymap);
        free(mymap);
        mymap = tmp;
    }
    if (ORTE_MAPPING_NO_OVERSUBSCRIBE & ORTE_GET_MAPPING_DIRECTIVE(mapping)) {
        asprintf(&tmp, "%sNOOVERSUBSCRIBE,", mymap);
        free(mymap);
        mymap = tmp;
    } else if (ORTE_MAPPING_SUBSCRIBE_GIVEN & ORTE_GET_MAPPING_DIRECTIVE(mapping)) {
        asprintf(&tmp, "%sOVERSUBSCRIBE,", mymap);
        free(mymap);
        mymap = tmp;
    }
    if (ORTE_MAPPING_SPAN & ORTE_GET_MAPPING_DIRECTIVE(mapping)) {
        asprintf(&tmp, "%sSPAN,", mymap);
        free(mymap);
        mymap = tmp;
    }

    /* remove the trailing mark */
    mymap[strlen(mymap)-1] = '\0';

    snprintf(ptr->buffers[ptr->cntr], ORTE_RMAPS_PRINT_MAX_SIZE, "%s", mymap);
    free(mymap);
    ret = ptr->buffers[ptr->cntr];
    ptr->cntr++;

    return ret;
}
Пример #5
0
/**
 *
 * \brief Iterates through directory.
 *
 * \param dir_name directory where to iterate through.
 * \param params is the program argument vector.
 *
 * \return void
 */
static int do_dir(const char* dir_name, const char* const* params)
{
    DIR* dirhandle = NULL;
    struct dirent* dirp = NULL;

    /*open directory catch error*/
    dirhandle = opendir(dir_name);
    if (NULL == dirhandle)
    {
        snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s': %s", dir_name, strerror(errno));
        print_error(get_print_buffer());
        return EXIT_SUCCESS;
    }

    errno = 0;
    while ((dirp = readdir(dirhandle)))
    {
        /* fetch each file from directory, until pointer is NULL */
        StatType file_info;

        if ((strcmp(dirp->d_name, ".") == 0) || (strcmp(dirp->d_name, "..") == 0))
        {
            /* '.' and '..' are not interesting */
            continue;
        }

        /* build complete path to file (DIR/FILE) */
        snprintf(get_path_buffer(), get_max_path_length(), "%s/%s", dir_name, dirp->d_name);
        /* get information about the file and catch errors */
        if (-1 == lstat(get_path_buffer(), &file_info))
        {
            snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s': %s", get_path_buffer(),
                    strerror(errno));
            print_error(get_print_buffer());
            /* check next file */
            continue;
        }

        if (S_ISDIR(file_info.st_mode))
        {
            char* next_path = NULL;
            do_file(get_path_buffer(), &file_info, params);

#if DEBUG_OUTPUT
            snprintf(get_print_buffer(), MAX_PRINT_BUFFER,
                    "Move into directory %s.\n", dirp->d_name);
#endif /* DEBUG_OUTPUT */
            debug_print(get_print_buffer());
            /* recursion for each directory in current directory */
            next_path = (char*) malloc(get_max_path_length() * sizeof(char));
            if (NULL == next_path)
            {
                print_error("malloc() failed: Out of memory.");
                if (closedir(dirhandle) < 0)
                {
                    snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s': closedir() failed: %s.",
                            dir_name, strerror(errno));
                    print_error(get_print_buffer());
                }
                return EXIT_FAILURE;
            }
            strcpy(next_path, get_path_buffer());
            if (EXIT_FAILURE == do_dir(next_path, params))
            {
                if (closedir(dirhandle) < 0)
                {
                    snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s': closedir() failed: %s.",
                            dir_name, strerror(errno));
                    print_error(get_print_buffer());
                }
                free(next_path);
                return EXIT_FAILURE;
            }
            free(next_path);
        }
        else
        {
            do_file(get_path_buffer(), &file_info, params);
        }
        errno = 0; /* reset errno for next call to readdir() */
    }
    if (0 != errno)
    {
        snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s': readdir() failed: %s.", dir_name,
                strerror(errno));
        print_error(get_print_buffer());
    }

    if (closedir(dirhandle) < 0)
    {
        snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s':closedir() failed: %s.", dir_name,
                strerror(errno));
        print_error(get_print_buffer());
    }

    return EXIT_SUCCESS;

}
Пример #6
0
/**
 *
 * \brief main implements a a simple replacement for Linux find.
 *
 * This is the main entry point for any C program.
 *
 * \param argc the number of arguments.
 * \param argv the arguments itself (including the program name in argv[0]).
 *
 * \return EXIT_SUCCESS on success  EXIT_FAILURE on error.
 * \retval EXIT_SUCCESS Program ended successfully.
 * \retval EXIT_FAILURE Program ended with failure.
 */
int main(int argc, const char* argv[])
{
    int result = EXIT_FAILURE;
    char* start_dir = NULL;
    char* found_dir = NULL;
    boolean path_given = FALSE;

    StatType stbuf;
    int current_argument = 1; /* the first argument is the program name anyway */
    int test_char = '\0';

    result = init(argv);
    if (EXIT_SUCCESS != result)
    {
        cleanup(TRUE);
    }

    if (argc <= 1)
    {
        print_usage();
        return EXIT_SUCCESS;
    }
    test_char = *argv[1];
    path_given = (test_char == '-') ? FALSE : TRUE;

    /* check the input arguments first */
    while (current_argument < argc)
    {
        if (0 == strcmp(PARAM_STR_USER, argv[current_argument]))
        {
            /* found -user */
            if ((current_argument + 1) < argc)
            {
                current_argument += 2;
                continue;
            }
            else
            {
                print_error("Missing argument to `-user'.");
                cleanup(TRUE);
            }
        }

        if (0 == strcmp(PARAM_STR_NOUSER, argv[current_argument]))
        {
            /* found -nouser */
            current_argument += 1;
            continue;
        }
        if (0 == strcmp(PARAM_STR_LS, argv[current_argument]))
        {
            /* found -ls */
            current_argument += 1;
            continue;
        }
        if (0 == strcmp(PARAM_STR_PRINT, argv[current_argument]))
        {
            /* found -print */
            current_argument += 1;
            continue;
        }
        if (0 == strcmp(PARAM_STR_NAME, argv[current_argument]))
        {
            /* found -name */
            if (argc > (current_argument + 1))
            {
                current_argument += 2;
                continue;
            }
            else
            {
                print_error("Missing argument to `-name'.");
                cleanup(TRUE);
            }
        }

        if (0 == strcmp(PARAM_STR_PATH, argv[current_argument]))
        {
            /* found -path */
            if (argc > (current_argument + 1))
            {
                current_argument += 2;
                continue;
            }
            else
            {
                print_error("Missing argument to `-path'.");
                cleanup(TRUE);
            }
        }

        if (0 == strcmp(PARAM_STR_TYPE, argv[current_argument]))
        {
            /* found -type */
            if (argc > (current_argument + 1))
            {
                const char* next_argument = argv[current_argument + 1];
                if (strlen(next_argument) > 1)
                {
                    snprintf(get_print_buffer(), MAX_PRINT_BUFFER,
                            "Argument of -type must be one character of these `%s'.",
                            PARAM_STR_TYPE_VALS);
                    print_error(get_print_buffer());
                    return EXIT_FAILURE;
                }

                test_char = (int) (*next_argument);
                if (NULL == strchr(PARAM_STR_TYPE_VALS, test_char))
                {
                    snprintf(get_print_buffer(), MAX_PRINT_BUFFER,
                            "Argument -type unknown options of %s: %c.", PARAM_STR_TYPE,
                            *next_argument);
                    print_error(get_print_buffer());
                    return EXIT_FAILURE;
                }
                current_argument += 2;
                continue;
            }
            else
            {
                print_error("Missing argument to `-type'.");
                cleanup(TRUE);
            }
        }

        if (current_argument > 1)
        {
            /* we have an unknown option */
            snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "Invalid predicate `%s'.",
                    argv[current_argument]);
            print_error(get_print_buffer());
            cleanup(TRUE);
        }
        ++current_argument;
    }

    /* determine the directory for start */
    get_path_buffer()[0] = '\0';
    start_dir = (char*) malloc(get_max_path_length() * sizeof(char));
    if (NULL == start_dir)
    {
        free(start_dir);
        start_dir = NULL;
        print_error("malloc() failed: Out of memory.");
        cleanup(TRUE);
    }

    /* build complete path to file (DIR/FILE) */
    snprintf(get_path_buffer(), get_max_path_length(), "%s", argv[1]);

    parameter_directory_given = TRUE;
    /*get information about the file and catch errors*/
    if (!path_given)
    {
        /* no search path defined - we set it to work directory and start */
        parameter_directory_given = FALSE;
        result = do_dir(".", argv);
    }
    else if (-1 != lstat(get_path_buffer(), &stbuf))
    {
        /*search path defined */
        result = do_file(argv[1], &stbuf, argv);
        if (S_ISDIR(stbuf.st_mode))
        {
            found_dir = get_path_buffer();
            strcpy(start_dir, found_dir);
            /* this condition should not happen, but its better to be safe */
            if (NULL == found_dir)
            {
                /* use current directory */
                parameter_directory_given = FALSE;
                strcpy(start_dir, ".");
            }
            result = do_dir(start_dir, argv);
        }
    }

    /* cleanup */
    free(start_dir);
    start_dir = NULL;
    cleanup(FALSE);

    return result;
}