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