diff mplayer.c @ 178:55206b791586

get_path()
author szabii
date Wed, 21 Mar 2001 00:14:42 +0000
parents d43c7e6412f0
children a8552ad553e0
line wrap: on
line diff
--- a/mplayer.c	Tue Mar 20 22:55:50 2001 +0000
+++ b/mplayer.c	Wed Mar 21 00:14:42 2001 +0000
@@ -99,6 +99,28 @@
 	return parse_config_file(conf, filename);
 }
 
+char *get_path(char *filename){
+	char *homedir;
+	char *buff;
+	static char *config_dir = "/.mplayer";
+	int len;
+
+	if ((homedir = getenv("HOME")) == NULL)
+		return NULL;
+	len = strlen(homedir) + strlen(config_dir) + 1;
+	if (filename == NULL) {
+		if ((buff = (char *) malloc(len)) == NULL)
+			return NULL;
+		sprintf(buff, "%s%s", homedir, config_dir);
+	} else {
+		len += strlen(filename) + 1;
+		if ((buff = (char *) malloc(len)) == NULL)
+			return NULL;
+		sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
+	}
+	return buff;
+}
+
 static int max_framesize=0;
 
 static int dbg_es_sent=0;
@@ -385,9 +407,7 @@
 int out_fmt=0;
 char *dsp="/dev/dsp";
 int force_ni=0;
-char *homedir;
-char conffile[100];
-char confdir[100];
+char *conffile;
 int conffile_fd;
 #include "cfg-mplayer.h"
 
@@ -395,18 +415,23 @@
 
 if (parse_config_file(conf, "/etc/mplayer.conf") < 0)
   exit(1);
-if ((homedir = getenv("HOME")) == NULL) {
+if ((conffile = get_path("")) == NULL) {
   printf("Can't find HOME dir\n");
 } else {
-  snprintf(confdir, 100, "%s/.mplayer", homedir);
-  mkdir(confdir, 0777);
-  snprintf(conffile, 100, "%s/config", confdir);
-  if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0644)) != -1) {
-    write(conffile_fd, default_config, strlen(default_config));
-    close(conffile_fd);
+  mkdir(conffile, 0777);
+  free(conffile);
+  if ((conffile = get_path("config")) == NULL) {
+    printf("get_path(\"config\") sziiiivas\n");
+  } else {
+    if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
+      printf("Creating config file: %s\n", conffile);
+      write(conffile_fd, default_config, strlen(default_config));
+      close(conffile_fd);
+    }
+    if (parse_config_file(conf, conffile) < 0)
+      exit(1);
+    free(conffile);
   }
-  if (parse_config_file(conf, conffile) < 0)
-    exit(1);
 }
 if (parse_command_line(conf, argc, argv, envp, &filename) < 0)
   exit(1);