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.

83 lines
1.7 KiB

  1. --- a/main.cpp
  2. +++ b/main.cpp
  3. @@ -4,11 +4,14 @@
  4. * https://tsdemuxer.googlecode.com/svn/trunk/xupnpd
  5. */
  6. +#include <ctype.h>
  7. #include <stdio.h>
  8. #include <syslog.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. +#include <sys/stat.h>
  13. +#include <sys/types.h>
  14. #include "luacompat.h"
  15. #include "luaxlib.h"
  16. #include "luaxcore.h"
  17. @@ -16,35 +19,36 @@
  18. int main(int argc,char** argv)
  19. {
  20. - const char* p=strrchr(argv[0],'/');
  21. -
  22. - int rc;
  23. -
  24. - if(p)
  25. - {
  26. - char location[512];
  27. - int n=p-argv[0];
  28. - if(n>=sizeof(location))
  29. - n=sizeof(location)-1;
  30. - strncpy(location,argv[0],n);
  31. - location[n]=0;
  32. -
  33. - rc=chdir(location);
  34. -
  35. - argv[0]=(char*)p+1;
  36. - }
  37. -
  38. - const char* root=getenv("XUPNPDROOTDIR");
  39. - if(root && *root)
  40. - rc=chdir(root);
  41. -
  42. - {
  43. - FILE* fp=fopen("xupnpd.lua","r");
  44. - if(fp)
  45. - fclose(fp);
  46. - else
  47. - rc=chdir("/usr/share/xupnpd/");
  48. - }
  49. + int c;
  50. + char *xupnpd_root = "/usr/share/xupnpd/";
  51. + struct stat s;
  52. +
  53. + opterr = 0;
  54. + while ((c = getopt (argc, argv, "d:")) != -1) {
  55. + switch (c) {
  56. + case 'd':
  57. + xupnpd_root = optarg;
  58. + break;
  59. + case '?':
  60. + if (optopt == 'd')
  61. + fprintf(stderr, "Option -%c requires an argument.\n", optopt);
  62. + else if (isprint(optopt))
  63. + fprintf(stderr, "Unknown option \"-%c\".\n", optopt);
  64. + else
  65. + fprintf(stderr, "Unknown option\n");
  66. + return 1;
  67. + default:
  68. + abort();
  69. + }
  70. + }
  71. +
  72. + if(stat(xupnpd_root, &s) != -1 && S_ISDIR(s.st_mode)) {
  73. + c = chdir(xupnpd_root);
  74. + }
  75. + else {
  76. + fprintf(stderr, "Directory %s doesn't exist.\n", xupnpd_root);
  77. + return 1;
  78. + }
  79. lua_State* L=lua_open();
  80. if(L)