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