Procházet zdrojové kódy

got rid of getcwd(). using __cdecl for signal handler.

valenok před 15 roky
rodič
revize
fcb49d75ce
1 změnil soubory, kde provedl 9 přidání a 7 odebrání
  1. 9 7
      main.c

+ 9 - 7
main.c

@@ -45,10 +45,12 @@
 #define	snprintf _snprintf
 #define	vsnprintf _vsnprintf
 #define	sleep(x) Sleep((x) * 1000)
+#define WINCDECL __cdecl
 #else
 #include <sys/wait.h>
 #include <unistd.h>
 #define DIRSEP '/'
+#define WINCDECL
 #endif // _WIN32
 
 #define MAX_OPTIONS 40
@@ -62,7 +64,7 @@ static struct mg_context *ctx;
 #define	CONFIG_FILE "mongoose.conf"
 #endif /* !CONFIG_FILE */
 
-static void signal_handler(int sig_num) {
+static void WINCDECL signal_handler(int sig_num) {
 #if !defined(_WIN32)
   if (sig_num == SIGCHLD) {
     do {
@@ -177,7 +179,7 @@ static void set_option(char **options, const char *name, const char *value) {
 
 static void process_command_line_arguments(char *argv[], char **options) {
   const char	*config_file = NULL;
-  char line[512], opt[512], val[512], path[PATH_MAX];
+  char line[512], opt[512], val[512], path[PATH_MAX], *p;
   FILE *fp = NULL;
   struct stat st;
   size_t i, line_no = 0;
@@ -185,12 +187,12 @@ static void process_command_line_arguments(char *argv[], char **options) {
   /* Should we use a config file ? */
   if (argv[1] != NULL && argv[2] == NULL) {
     config_file = argv[1];
-  } else if (argv[1] == NULL) {
+  } else if (argv[1] == NULL && (p = strchr(argv[0], DIRSEP)) == NULL) {
     // No command line flags specified. Look where binary lives
-    // TODO(lsm): do proper error handling here
-    getcwd(path, sizeof(path));
-    snprintf(path + strlen(path), sizeof(path) - strlen(path), "%c%s",
-             DIRSEP, CONFIG_FILE);
+    config_file = CONFIG_FILE;
+  } else if (argv[1] == NULL) {
+    snprintf(path, sizeof(path), "%.*s%c%s",
+             (int) (p - argv[0]), argv[0], DIRSEP, CONFIG_FILE);
     if (stat(path, &st) == 0) {
       config_file = path;
     }