Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.5 KiB

20 years ago
  1. /* $OpenBSD: opendisk.c,v 1.7 2014/06/30 00:26:22 deraadt Exp $ */
  2. /* $NetBSD: opendisk.c,v 1.4 1997/09/30 17:13:50 phil Exp $ */
  3. /*-
  4. * Copyright (c) 1997 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Luke Mewburn.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  21. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  23. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <sys/types.h>
  32. #include <errno.h>
  33. #include <fcntl.h>
  34. #include <paths.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include "util.h"
  38. int
  39. opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked)
  40. {
  41. int f, rawpart;
  42. snprintf(buf, buflen, "%s", path);
  43. if ((flags & O_CREAT) != 0) {
  44. errno = EINVAL;
  45. return (-1);
  46. }
  47. rawpart = getrawpartition();
  48. if (rawpart < 0)
  49. return (-1); /* sysctl(3) in getrawpartition sets errno */
  50. f = open(buf, flags);
  51. if (f != -1 || errno != ENOENT)
  52. return (f);
  53. snprintf(buf, buflen, "%s%c", path, 'a' + rawpart);
  54. f = open(buf, flags);
  55. if (f != -1 || errno != ENOENT)
  56. return (f);
  57. if (strchr(path, '/') != NULL)
  58. return (-1);
  59. snprintf(buf, buflen, "%s%s%s", _PATH_DEV, iscooked ? "" : "r", path);
  60. f = open(buf, flags);
  61. if (f != -1 || errno != ENOENT)
  62. return (f);
  63. snprintf(buf, buflen, "%s%s%s%c", _PATH_DEV, iscooked ? "" : "r", path,
  64. 'a' + rawpart);
  65. f = open(buf, flags);
  66. return (f);
  67. }