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.

90 lines
2.7 KiB

  1. /* $OpenBSD: opendev.c,v 1.7 2002/06/09 22:18:43 fgsch Exp $ */
  2. /*
  3. * Copyright (c) 2000, Todd C. Miller. All rights reserved.
  4. * Copyright (c) 1996, Jason Downs. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
  16. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <limits.h>
  30. #include <paths.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include "util.h"
  34. /*
  35. * This routine is a generic rewrite of the original code found in
  36. * disklabel(8).
  37. */
  38. int
  39. opendev(path, oflags, dflags, realpath)
  40. char *path;
  41. int oflags;
  42. int dflags;
  43. char **realpath;
  44. {
  45. int fd;
  46. char *slash, *prefix;
  47. static char namebuf[PATH_MAX];
  48. /* Initial state */
  49. if (realpath)
  50. *realpath = path;
  51. fd = -1;
  52. errno = ENOENT;
  53. if (dflags & OPENDEV_BLCK)
  54. prefix = ""; /* block device */
  55. else
  56. prefix = "r"; /* character device */
  57. if ((slash = strchr(path, '/')))
  58. fd = open(path, oflags);
  59. else if (dflags & OPENDEV_PART) {
  60. /*
  61. * First try raw partition (for removable drives)
  62. */
  63. if (snprintf(namebuf, sizeof(namebuf), "%s%s%s%c",
  64. _PATH_DEV, prefix, path, 'a' + getrawpartition())
  65. < sizeof(namebuf)) {
  66. fd = open(namebuf, oflags);
  67. if (realpath)
  68. *realpath = namebuf;
  69. } else
  70. errno = ENAMETOOLONG;
  71. }
  72. if (!slash && fd == -1 && errno == ENOENT) {
  73. if (snprintf(namebuf, sizeof(namebuf), "%s%s%s",
  74. _PATH_DEV, prefix, path) < sizeof(namebuf)) {
  75. fd = open(namebuf, oflags);
  76. if (realpath)
  77. *realpath = namebuf;
  78. } else
  79. errno = ENAMETOOLONG;
  80. }
  81. return (fd);
  82. }