Settings.c 15.7 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
/*
htop - Settings.c
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2004-2011 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
8
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "Settings.h"
9
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
10

David Hunt's avatar
David Hunt committed
11
#include "StringUtils.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
12
#include "Vector.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
13
#include "CRT.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
14

Hisham Muhammad's avatar
Hisham Muhammad committed
15
16
17
18
19
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

Hisham Muhammad's avatar
Hisham Muhammad committed
20
21
22
#define DEFAULT_DELAY 15

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
23
#include "Process.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
24
#include <stdbool.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
25

Hisham Muhammad's avatar
Hisham Muhammad committed
26
27
28
29
30
typedef struct {
   int len;
   char** names;
   int* modes;
} MeterColumnSettings;
31

Hisham Muhammad's avatar
Hisham Muhammad committed
32
typedef struct Settings_ {
Hisham Muhammad's avatar
Hisham Muhammad committed
33
34
35
36
   char* filename;
   
   MeterColumnSettings columns[2];

37
38
39
   char** screens;
   int nScreens;

Hisham Muhammad's avatar
Hisham Muhammad committed
40
41
   ProcessField* fields;
   int flags;
Hisham Muhammad's avatar
Hisham Muhammad committed
42
43
   int colorScheme;
   int delay;
Hisham Muhammad's avatar
Hisham Muhammad committed
44

45
   int cpuCount;
Hisham Muhammad's avatar
Hisham Muhammad committed
46
47
48
49
50
51
   int direction;
   ProcessField sortKey;

   bool countCPUsFromZero;
   bool detailedCPUTime;
   bool treeView;
52
   bool showProgramPath;
Hisham Muhammad's avatar
Hisham Muhammad committed
53
54
55
56
57
58
59
60
61
62
63
64
   bool hideThreads;
   bool shadowOtherUsers;
   bool showThreadNames;
   bool hideKernelThreads;
   bool hideUserlandThreads;
   bool highlightBaseName;
   bool highlightMegabytes;
   bool highlightThreads;
   bool updateProcessNames;
   bool accountGuestInCPUMeter;
   bool headerMargin;

Hisham Muhammad's avatar
Hisham Muhammad committed
65
   bool changed;
Hisham Muhammad's avatar
Hisham Muhammad committed
66
67
} Settings;

Hisham Muhammad's avatar
Hisham Muhammad committed
68
69
70
71
#ifndef Settings_cpuId
#define Settings_cpuId(settings, cpu) ((settings)->countCPUsFromZero ? (cpu) : (cpu)+1)
#endif

Hisham Muhammad's avatar
Hisham Muhammad committed
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
static void writeList(FILE* fd, char** list, int len) {
   const char* sep = "";
   for (int i = 0; i < len; i++) {
      fprintf(fd, "%s%s", sep, list[i]);
      sep = " ";
   }
   fprintf(fd, "\n");
}

static char** readQuotedList(char* line, int* size) {
   *size = 0;
   char** list = xCalloc(sizeof(char*), 1);
   int start = 0;
   for (;;) {
      while (line[start] && line[start] == ' ') {
         start++;
      }
      if (line[start] != '"') {
         break;
      }
      start++;
      int close = start;
      while (line[close] && line[close] != '"') {
         close++;
      }
      int len = close - start;
      char* item = xMalloc(len + 1);
      strncpy(item, line + start, len);
      item[len] = '\0';
      list[*size] = item;
      (*size)++;
      list = xRealloc(list, sizeof(char*) * (*size + 1));
      start = close + 1;
   }
   list[*size] = NULL;
   return list;
}

static void writeQuotedList(FILE* fd, char** list, int len) {
   const char* sep = "";
   for (int i = 0; i < len; i++) {
      fprintf(fd, "%s\"%s\"", sep, list[i]);
      sep = " ";
   }
   fprintf(fd, "\n");
}

Hisham Muhammad's avatar
Hisham Muhammad committed
121
void Settings_delete(Settings* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
122
123
124
125
126
127
   free(this->filename);
   free(this->fields);
   for (unsigned int i = 0; i < (sizeof(this->columns)/sizeof(MeterColumnSettings)); i++) {
      String_freeArray(this->columns[i].names);
      free(this->columns[i].modes);
   }
128
   String_freeArray(this->screens);
Hisham Muhammad's avatar
Hisham Muhammad committed
129
130
131
   free(this);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
132
static void Settings_readMeters(Settings* this, char* line, int column) {
Hisham Muhammad's avatar
Hisham Muhammad committed
133
   char* trim = String_trim(line);
134
135
   int nIds;
   char** ids = String_split(trim, ' ', &nIds);
Hisham Muhammad's avatar
Hisham Muhammad committed
136
   free(trim);
Hisham Muhammad's avatar
Hisham Muhammad committed
137
   this->columns[column].names = ids;
Hisham Muhammad's avatar
Hisham Muhammad committed
138
139
}

140
141
142
143
144
145
static void Settings_readScreens(Settings* this, char* line) {
   char* trim = String_trim(line);
   this->screens = readQuotedList(trim, &(this->nScreens));
   free(trim);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
146
static void Settings_readMeterModes(Settings* this, char* line, int column) {
Hisham Muhammad's avatar
Hisham Muhammad committed
147
   char* trim = String_trim(line);
148
149
   int nIds;
   char** ids = String_split(trim, ' ', &nIds);
Hisham Muhammad's avatar
Hisham Muhammad committed
150
   free(trim);
Hisham Muhammad's avatar
Hisham Muhammad committed
151
   int len = 0;
152
   for (int i = 0; ids[i]; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
153
154
155
      len++;
   }
   this->columns[column].len = len;
Hisham's avatar
Hisham committed
156
   int* modes = xCalloc(len, sizeof(int));
Hisham Muhammad's avatar
Hisham Muhammad committed
157
158
   for (int i = 0; i < len; i++) {
      modes[i] = atoi(ids[i]);
Hisham Muhammad's avatar
Hisham Muhammad committed
159
160
   }
   String_freeArray(ids);
Hisham Muhammad's avatar
Hisham Muhammad committed
161
   this->columns[column].modes = modes;
Hisham Muhammad's avatar
Hisham Muhammad committed
162
163
}

164
static void Settings_defaultMeters(Settings* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
165
   int sizes[] = { 3, 3 };
166
   if (this->cpuCount > 4) {
Hisham Muhammad's avatar
Hisham Muhammad committed
167
168
169
      sizes[1]++;
   }
   for (int i = 0; i < 2; i++) {
Hisham's avatar
Hisham committed
170
171
      this->columns[i].names = xCalloc(sizes[i] + 1, sizeof(char*));
      this->columns[i].modes = xCalloc(sizes[i], sizeof(int));
172
      this->columns[i].len = sizes[i];
Hisham Muhammad's avatar
Hisham Muhammad committed
173
174
175
   }
   
   int r = 0;
176
   if (this->cpuCount > 8) {
Hisham's avatar
Hisham committed
177
      this->columns[0].names[0] = xStrdup("LeftCPUs2");
178
179
180
      this->columns[0].modes[0] = BAR_METERMODE;
      this->columns[1].names[r] = xStrdup("RightCPUs2");
      this->columns[1].modes[r++] = BAR_METERMODE;
181
   } else if (this->cpuCount > 4) {
Hisham's avatar
Hisham committed
182
      this->columns[0].names[0] = xStrdup("LeftCPUs");
183
184
185
      this->columns[0].modes[0] = BAR_METERMODE;
      this->columns[1].names[r] = xStrdup("RightCPUs");
      this->columns[1].modes[r++] = BAR_METERMODE;
186
   } else {
Hisham's avatar
Hisham committed
187
      this->columns[0].names[0] = xStrdup("AllCPUs");
188
      this->columns[0].modes[0] = BAR_METERMODE;
Hisham Muhammad's avatar
Hisham Muhammad committed
189
   }
Hisham's avatar
Hisham committed
190
   this->columns[0].names[1] = xStrdup("Memory");
191
   this->columns[0].modes[1] = BAR_METERMODE;
Hisham's avatar
Hisham committed
192
   this->columns[0].names[2] = xStrdup("Swap");
193
   this->columns[0].modes[2] = BAR_METERMODE;
Hisham Muhammad's avatar
Hisham Muhammad committed
194
   
195
196
197
198
199
200
   this->columns[1].names[r] = xStrdup("Tasks");
   this->columns[1].modes[r++] = TEXT_METERMODE;
   this->columns[1].names[r] = xStrdup("LoadAverage");
   this->columns[1].modes[r++] = TEXT_METERMODE;
   this->columns[1].names[r] = xStrdup("Uptime");
   this->columns[1].modes[r++] = TEXT_METERMODE;
Hisham Muhammad's avatar
Hisham Muhammad committed
201
202
}

203
204
205
206
207
208
209
static void Settings_defaultScreens(Settings* this) {
   this->screens = xMalloc(sizeof(char*) * 3);
   this->screens[0] = xStrdup("Overview");
   this->screens[1] = xStrdup("I/O");
   this->screens[2] = NULL;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
210
211
212
213
214
215
216
static void readFields(ProcessField* fields, int* flags, const char* line) {
   char* trim = String_trim(line);
   int nIds;
   char** ids = String_split(trim, ' ', &nIds);
   free(trim);
   int i, j;
   *flags = 0;
217
   for (j = 0, i = 0; i < Platform_numberOfFields && ids[i]; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
218
219
      // This "+1" is for compatibility with the older enum format.
      int id = atoi(ids[i]) + 1;
220
      if (id > 0 && Process_fields[id].name && id < Platform_numberOfFields) {
Hisham Muhammad's avatar
Hisham Muhammad committed
221
222
223
224
         fields[j] = id;
         *flags |= Process_fields[id].flags;
         j++;
      }
225
   }
Hisham's avatar
Hisham committed
226
   fields[j] = NULL_PROCESSFIELD;
Hisham Muhammad's avatar
Hisham Muhammad committed
227
   String_freeArray(ids);
228
229
}

230
static bool Settings_read(Settings* this, const char* fileName) {
231
   FILE* fd;
232
233
   
   CRT_dropPrivileges();
234
   fd = fopen(fileName, "r");
235
   CRT_restorePrivileges();
236
   if (!fd)
Hisham Muhammad's avatar
Hisham Muhammad committed
237
      return false;
238
   
239
240
   bool didReadMeters = false;
   bool didReadFields = false;
241
242
243
244
245
   for (;;) {
      char* line = String_readLine(fd);
      if (!line) {
         break;
      }
246
      int nOptions;
247
248
      char** option = String_split(line, '=', &nOptions);
      free (line);
249
250
251
252
      if (nOptions < 2) {
         String_freeArray(option);
         continue;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
253
      if (String_eq(option[0], "fields")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
254
         readFields(this->fields, &(this->flags), option[1]);
255
         didReadFields = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
256
257
      } else if (String_eq(option[0], "sort_key")) {
         // This "+1" is for compatibility with the older enum format.
Hisham Muhammad's avatar
Hisham Muhammad committed
258
         this->sortKey = atoi(option[1]) + 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
259
      } else if (String_eq(option[0], "sort_direction")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
260
         this->direction = atoi(option[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
261
      } else if (String_eq(option[0], "tree_view")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
262
         this->treeView = atoi(option[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
263
      } else if (String_eq(option[0], "hide_threads")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
264
         this->hideThreads = atoi(option[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
265
      } else if (String_eq(option[0], "hide_kernel_threads")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
266
         this->hideKernelThreads = atoi(option[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
267
      } else if (String_eq(option[0], "hide_userland_threads")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
268
         this->hideUserlandThreads = atoi(option[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
269
      } else if (String_eq(option[0], "shadow_other_users")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
270
         this->shadowOtherUsers = atoi(option[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
271
      } else if (String_eq(option[0], "show_thread_names")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
272
         this->showThreadNames = atoi(option[1]);
273
274
      } else if (String_eq(option[0], "show_program_path")) {
         this->showProgramPath = atoi(option[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
275
      } else if (String_eq(option[0], "highlight_base_name")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
276
         this->highlightBaseName = atoi(option[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
277
      } else if (String_eq(option[0], "highlight_megabytes")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
278
         this->highlightMegabytes = atoi(option[1]);
279
      } else if (String_eq(option[0], "highlight_threads")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
280
         this->highlightThreads = atoi(option[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
281
      } else if (String_eq(option[0], "header_margin")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
282
         this->headerMargin = atoi(option[1]);
283
      } else if (String_eq(option[0], "expand_system_time")) {
284
         // Compatibility option.
Hisham Muhammad's avatar
Hisham Muhammad committed
285
         this->detailedCPUTime = atoi(option[1]);
286
      } else if (String_eq(option[0], "detailed_cpu_time")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
287
         this->detailedCPUTime = atoi(option[1]);
288
      } else if (String_eq(option[0], "cpu_count_from_zero")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
289
         this->countCPUsFromZero = atoi(option[1]);
290
      } else if (String_eq(option[0], "update_process_names")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
291
         this->updateProcessNames = atoi(option[1]);
292
      } else if (String_eq(option[0], "account_guest_in_cpu_meter")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
293
         this->accountGuestInCPUMeter = atoi(option[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
294
295
296
297
      } else if (String_eq(option[0], "delay")) {
         this->delay = atoi(option[1]);
      } else if (String_eq(option[0], "color_scheme")) {
         this->colorScheme = atoi(option[1]);
298
         if (this->colorScheme < 0 || this->colorScheme >= LAST_COLORSCHEME) this->colorScheme = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
299
      } else if (String_eq(option[0], "left_meters")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
300
         Settings_readMeters(this, option[1], 0);
301
         didReadMeters = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
302
      } else if (String_eq(option[0], "right_meters")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
303
         Settings_readMeters(this, option[1], 1);
304
         didReadMeters = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
305
      } else if (String_eq(option[0], "left_meter_modes")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
306
         Settings_readMeterModes(this, option[1], 0);
307
         didReadMeters = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
308
      } else if (String_eq(option[0], "right_meter_modes")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
309
         Settings_readMeterModes(this, option[1], 1);
310
         didReadMeters = true;
311
312
      } else if (String_eq(option[0], "screens")) {
         Settings_readScreens(this, option[1]);
Hisham Muhammad's avatar
Hisham Muhammad committed
313
314
315
316
      }
      String_freeArray(option);
   }
   fclose(fd);
317
318
319
   if (!this->screens) {
      Settings_defaultScreens(this);
   }
320
   if (!didReadMeters) {
321
      Settings_defaultMeters(this);
Hisham Muhammad's avatar
Hisham Muhammad committed
322
   }
323
   return didReadFields;
Hisham Muhammad's avatar
Hisham Muhammad committed
324
325
}

Hisham Muhammad's avatar
Hisham Muhammad committed
326
327
static void writeFields(FILE* fd, ProcessField* fields, const char* name) {
   fprintf(fd, "%s=", name);
328
   const char* sep = "";
Hisham Muhammad's avatar
Hisham Muhammad committed
329
330
   for (int i = 0; fields[i]; i++) {
      // This "-1" is for compatibility with the older enum format.
331
332
      fprintf(fd, "%s%d", sep, (int) fields[i]-1);
      sep = " ";
Hisham Muhammad's avatar
Hisham Muhammad committed
333
334
335
336
337
   }
   fprintf(fd, "\n");
}

static void writeMeters(Settings* this, FILE* fd, int column) {
338
   writeList(fd, this->columns[column].names, this->columns[column].len);
Hisham Muhammad's avatar
Hisham Muhammad committed
339
340
341
}

static void writeMeterModes(Settings* this, FILE* fd, int column) {
342
   const char* sep = "";
Hisham Muhammad's avatar
Hisham Muhammad committed
343
   for (int i = 0; i < this->columns[column].len; i++) {
344
345
      fprintf(fd, "%s%d", sep, this->columns[column].modes[i]);
      sep = " ";
Hisham Muhammad's avatar
Hisham Muhammad committed
346
347
348
349
   }
   fprintf(fd, "\n");
}

Hisham Muhammad's avatar
Hisham Muhammad committed
350
351
bool Settings_write(Settings* this) {
   FILE* fd;
352

353
   CRT_dropPrivileges();
Hisham Muhammad's avatar
Hisham Muhammad committed
354
   fd = fopen(this->filename, "w");
355
356
   CRT_restorePrivileges();

Hisham Muhammad's avatar
Hisham Muhammad committed
357
358
359
   if (fd == NULL) {
      return false;
   }
360
   fprintf(fd, "# Beware! This file is rewritten by htop when settings are changed in the interface.\n");
Hisham Muhammad's avatar
Hisham Muhammad committed
361
   fprintf(fd, "# The parser is also very primitive, and not human-friendly.\n");
Hisham Muhammad's avatar
Hisham Muhammad committed
362
   writeFields(fd, this->fields, "fields");
Hisham Muhammad's avatar
Hisham Muhammad committed
363
   // This "-1" is for compatibility with the older enum format.
Hisham Muhammad's avatar
Hisham Muhammad committed
364
365
366
367
368
369
370
   fprintf(fd, "sort_key=%d\n", (int) this->sortKey-1);
   fprintf(fd, "sort_direction=%d\n", (int) this->direction);
   fprintf(fd, "hide_threads=%d\n", (int) this->hideThreads);
   fprintf(fd, "hide_kernel_threads=%d\n", (int) this->hideKernelThreads);
   fprintf(fd, "hide_userland_threads=%d\n", (int) this->hideUserlandThreads);
   fprintf(fd, "shadow_other_users=%d\n", (int) this->shadowOtherUsers);
   fprintf(fd, "show_thread_names=%d\n", (int) this->showThreadNames);
371
   fprintf(fd, "show_program_path=%d\n", (int) this->showProgramPath);
Hisham Muhammad's avatar
Hisham Muhammad committed
372
373
374
375
376
377
378
379
380
   fprintf(fd, "highlight_base_name=%d\n", (int) this->highlightBaseName);
   fprintf(fd, "highlight_megabytes=%d\n", (int) this->highlightMegabytes);
   fprintf(fd, "highlight_threads=%d\n", (int) this->highlightThreads);
   fprintf(fd, "tree_view=%d\n", (int) this->treeView);
   fprintf(fd, "header_margin=%d\n", (int) this->headerMargin);
   fprintf(fd, "detailed_cpu_time=%d\n", (int) this->detailedCPUTime);
   fprintf(fd, "cpu_count_from_zero=%d\n", (int) this->countCPUsFromZero);
   fprintf(fd, "update_process_names=%d\n", (int) this->updateProcessNames);
   fprintf(fd, "account_guest_in_cpu_meter=%d\n", (int) this->accountGuestInCPUMeter);
Hisham Muhammad's avatar
Hisham Muhammad committed
381
382
   fprintf(fd, "color_scheme=%d\n", (int) this->colorScheme);
   fprintf(fd, "delay=%d\n", (int) this->delay);
Hisham Muhammad's avatar
Hisham Muhammad committed
383
384
385
386
   fprintf(fd, "left_meters="); writeMeters(this, fd, 0);
   fprintf(fd, "left_meter_modes="); writeMeterModes(this, fd, 0);
   fprintf(fd, "right_meters="); writeMeters(this, fd, 1);
   fprintf(fd, "right_meter_modes="); writeMeterModes(this, fd, 1);
387
388
389
   if (this->nScreens > 0) {
      fprintf(fd, "screens="); writeQuotedList(fd, this->screens, this->nScreens);
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
390
391
392
   fclose(fd);
   return true;
}
393

Hisham Muhammad's avatar
Hisham Muhammad committed
394
395
Settings* Settings_new(int cpuCount) {
  
Hisham's avatar
Hisham committed
396
   Settings* this = xCalloc(1, sizeof(Settings));
Hisham Muhammad's avatar
Hisham Muhammad committed
397
398
399
400
401
402
403
404
405
406
407
408
409
410

   this->sortKey = PERCENT_CPU;
   this->direction = 1;
   this->hideThreads = false;
   this->shadowOtherUsers = false;
   this->showThreadNames = false;
   this->hideKernelThreads = false;
   this->hideUserlandThreads = false;
   this->treeView = false;
   this->highlightBaseName = false;
   this->highlightMegabytes = false;
   this->detailedCPUTime = false;
   this->countCPUsFromZero = false;
   this->updateProcessNames = false;
411
   this->cpuCount = cpuCount;
412
   this->showProgramPath = true;
Hisham's avatar
Hisham committed
413
   this->highlightThreads = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
414
   
Hisham's avatar
Hisham committed
415
   this->fields = xCalloc(Platform_numberOfFields+1, sizeof(ProcessField));
Hisham Muhammad's avatar
Hisham Muhammad committed
416
417
418
   // TODO: turn 'fields' into a Vector,
   // (and ProcessFields into proper objects).
   this->flags = 0;
419
   ProcessField* defaults = Platform_defaultFields;
Hisham Muhammad's avatar
Hisham Muhammad committed
420
421
422
423
424
   for (int i = 0; defaults[i]; i++) {
      this->fields[i] = defaults[i];
      this->flags |= Process_fields[defaults[i]].flags;
   }

425
426
427
   char* legacyDotfile = NULL;
   char* rcfile = getenv("HTOPRC");
   if (rcfile) {
Hisham's avatar
Hisham committed
428
      this->filename = xStrdup(rcfile);
429
430
431
432
433
434
435
   } else {
      const char* home = getenv("HOME");
      if (!home) home = "";
      const char* xdgConfigHome = getenv("XDG_CONFIG_HOME");
      char* configDir = NULL;
      char* htopDir = NULL;
      if (xdgConfigHome) {
Hisham Muhammad's avatar
Hisham Muhammad committed
436
         this->filename = String_cat(xdgConfigHome, "/htop/htoprc");
Hisham's avatar
Hisham committed
437
         configDir = xStrdup(xdgConfigHome);
438
439
         htopDir = String_cat(xdgConfigHome, "/htop");
      } else {
Hisham Muhammad's avatar
Hisham Muhammad committed
440
         this->filename = String_cat(home, "/.config/htop/htoprc");
441
442
443
444
         configDir = String_cat(home, "/.config");
         htopDir = String_cat(home, "/.config/htop");
      }
      legacyDotfile = String_cat(home, "/.htoprc");
445
446
      
      CRT_dropPrivileges();
447
448
      (void) mkdir(configDir, 0700);
      (void) mkdir(htopDir, 0700);
449
450
      free(htopDir);
      free(configDir);
451
      struct stat st;
452
453
      int err = lstat(legacyDotfile, &st);
      if (err || S_ISLNK(st.st_mode)) {
454
455
456
         free(legacyDotfile);
         legacyDotfile = NULL;
      }
457
      CRT_restorePrivileges();
458
   }
459
460
461
   this->colorScheme = 0;
   this->changed = false;
   this->delay = DEFAULT_DELAY;
462
463
464
465
   bool ok = false;
   if (legacyDotfile) {
      ok = Settings_read(this, legacyDotfile);
      if (ok) {
466
467
468
469
         // Transition to new location and delete old configuration file
         if (Settings_write(this))
            unlink(legacyDotfile);
      }
470
471
472
473
474
475
      free(legacyDotfile);
   }
   if (!ok) {
      ok = Settings_read(this, this->filename);
   }
   if (!ok) {
476
477
478
      this->changed = true;
      // TODO: how to get SYSCONFDIR correctly through Autoconf?
      char* systemSettings = String_cat(SYSCONFDIR, "/htoprc");
479
      ok = Settings_read(this, systemSettings);
480
481
      free(systemSettings);
   }
482
483
   if (!ok) {
      Settings_defaultMeters(this);
484
      Settings_defaultScreens(this);
485
486
487
488
489
      this->hideKernelThreads = true;
      this->highlightMegabytes = true;
      this->highlightThreads = true;
      this->headerMargin = true;
   }
490
491
   return this;
}
Hisham Muhammad's avatar
Hisham Muhammad committed
492
493
494
495
496
497
498

void Settings_invertSortOrder(Settings* this) {
   if (this->direction == 1)
      this->direction = -1;
   else
      this->direction = 1;
}