summaryrefslogtreecommitdiff
path: root/src/msm/util.c
blob: c0ac856ded9c8105197324b46820bb870ca8218d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* msm utils */

/* 
 * Copyright (C) 2001 Havoc Pennington
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include "util.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>


void
msm_fatal (const char *format, ...)
{
  va_list args;
  gchar *str;
  
  g_return_if_fail (format != NULL);
  
  va_start (args, format);
  str = g_strdup_vprintf (format, args);
  va_end (args);

  fputs ("Session manager: ", stderr);
  fputs (str, stderr);

  fflush (stderr);
  
  g_free (str);

  exit (1);
}


void
msm_warning (const char *format, ...)
{
  va_list args;
  gchar *str;
  
  g_return_if_fail (format != NULL);
  
  va_start (args, format);
  str = g_strdup_vprintf (format, args);
  va_end (args);

  fputs ("Session manager: ", stderr);
  fputs (str, stderr);

  fflush (stderr);
  
  g_free (str);

  exit (1);
}
     
gboolean
msm_create_dir_and_parents (const char *dir,
                            int         mode,
                            GError    **error)
{
  char *parent;
  GSList *parents;
  GSList *tmp;

  /* This function is crap; GNU fileutils has some really
   * robust code that does this.
   */
  
  parents = NULL;
  parent = g_path_get_dirname (dir);
  while (parent && parent[0] &&
         strcmp (parent, ".") != 0 &&
         strcmp (parent, "..") != 0 &&
         strcmp (parent, "/") != 0 &&
         /* an optimization since we will normally be using a homedir */
         strcmp (parent, g_get_home_dir ()) != 0)
    {
      parents = g_slist_prepend (parents, parent);
      parent = g_path_get_dirname (parent);
    }

  /* Errors are a bit tricky; if we can't create /foo because
   * we lack write perms, and can't create /foo/bar because it exists,
   * but can create /foo/bar/baz, then it's not really an error.
   *
   * We more or less punt, and just display an error for the last mkdir.
   */
  tmp = parents;
  while (tmp != NULL)
    {
      mkdir (tmp->data, mode);

      g_free (tmp->data);
      
      tmp = tmp->next;
    }

  g_slist_free (parents);

  if (mkdir (dir, mode) < 0)
    {
      if (errno != EEXIST)
        {
          g_set_error (error, 
                       G_FILE_ERROR,
                       g_file_error_from_errno (errno),
                       _("Failed to create directory '%s': %s\n"),
                       dir, g_strerror (errno));
          return FALSE;
        }
    }

  return TRUE;
}

const char*
msm_get_work_directory (void)
{
  static const char *dir = NULL;

  if (dir == NULL)
    {
      dir = g_getenv ("SM_SAVE_DIR");
      if (dir == NULL)
        dir = g_strconcat (g_get_home_dir (), "/.msm", NULL);
    }

  /* ignore errors here, we'll catch them later when we
   * try to use the dir
   */
  msm_create_dir_and_parents (dir, 0700, NULL);
  
  return dir;
}

char*
msm_non_glib_strdup (const char *str)
{
  char *new_str;

  if (str)
    {
      new_str = msm_non_glib_malloc (strlen (str) + 1);
      strcpy (new_str, str);
    }
  else
    new_str = NULL;

  return new_str;
}

void*
msm_non_glib_malloc (int bytes)
{
  void *ptr;

  ptr = malloc (bytes);
  if (ptr == NULL)
    g_error ("Failed to allocate %d bytes\n", bytes);

  return ptr;
}