d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek/*
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek * Copyright (C) 2008-2009 Kay Sievers <kay@vrfy.org>
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek *
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek * This program is free software: you can redistribute it and/or modify
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek * it under the terms of the GNU General Public License as published by
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek * the Free Software Foundation, either version 2 of the License, or
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek * (at your option) any later version.
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek *
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek * This program is distributed in the hope that it will be useful,
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek * but WITHOUT ANY WARRANTY; without even the implied warranty of
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek * GNU General Public License for more details.
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek *
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek * You should have received a copy of the GNU General Public License
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek * along with this program. If not, see <http://www.gnu.org/licenses/>.
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek */
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "string-util.h"
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek#include "udevadm-util.h"
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmekstruct udev_device *find_device(struct udev *udev,
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek const char *id,
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek const char *prefix) {
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek assert(udev);
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek assert(id);
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek if (prefix && !startswith(id, prefix))
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering id = strjoina(prefix, id);
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek if (startswith(id, "/dev/")) {
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek struct stat statbuf;
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek char type;
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek if (stat(id, &statbuf) < 0)
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek return NULL;
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek if (S_ISBLK(statbuf.st_mode))
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek type = 'b';
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek else if (S_ISCHR(statbuf.st_mode))
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek type = 'c';
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek else
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek return NULL;
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek return udev_device_new_from_devnum(udev, type, statbuf.st_rdev);
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek } else if (startswith(id, "/sys/"))
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek return udev_device_new_from_syspath(udev, id);
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek else
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek return NULL;
d6170d27b26eeeace966bb9720d66fcac2a6ef1bZbigniew Jędrzejewski-Szmek}