/contrib/ntp/libntp/findconfig.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 72 lines · 60 code · 8 blank · 4 comment · 15 complexity · a374101c0cb83e30f083a4ec8377a969 MD5 · raw file

  1. #ifdef HAVE_CONFIG_H
  2. # include <config.h>
  3. #endif
  4. #ifdef NEED_HPUX_FINDCONFIG
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/utsname.h>
  10. #include <unistd.h>
  11. const char *
  12. FindConfig(
  13. const char *base
  14. )
  15. {
  16. static char result[BUFSIZ];
  17. char hostname[BUFSIZ], *cp;
  18. struct stat sbuf;
  19. struct utsname unamebuf;
  20. /* All keyed by initial target being a directory */
  21. (void) strcpy(result, base);
  22. if (stat(result, &sbuf) == 0) {
  23. if (S_ISDIR(sbuf.st_mode)) {
  24. /* First choice is my hostname */
  25. if (gethostname(hostname, BUFSIZ) >= 0) {
  26. (void) sprintf(result, "%s/%s", base, hostname);
  27. if (stat(result, &sbuf) == 0) {
  28. goto outahere;
  29. } else {
  30. /* Second choice is of form default.835 */
  31. (void) uname(&unamebuf);
  32. if (strncmp(unamebuf.machine, "9000/", 5) == 0)
  33. cp = unamebuf.machine + 5;
  34. else
  35. cp = unamebuf.machine;
  36. (void) sprintf(result, "%s/default.%s", base, cp);
  37. if (stat(result, &sbuf) == 0) {
  38. goto outahere;
  39. } else {
  40. /* Last choice is just default */
  41. (void) sprintf(result, "%s/default", base);
  42. if (stat(result, &sbuf) == 0) {
  43. goto outahere;
  44. } else {
  45. (void) strcpy(result, "/not/found");
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. outahere:
  53. return(result);
  54. }
  55. #else
  56. #include "ntp_stdlib.h"
  57. const char *
  58. FindConfig(
  59. const char *base
  60. )
  61. {
  62. return base;
  63. }
  64. #endif