4 * ROX-Filer, filer for the ROX desktop project
5 * By Thomas Leonard, <tal197@ecs.soton.ac.uk>.
8 /* support.c - (non-GUI) useful routines */
13 #include <sys/param.h>
22 /* Static prototypes */
25 /* Like g_strdup, but does realpath() too (if possible) */
26 char *pathdup(char *path
)
28 char real
[MAXPATHLEN
];
30 g_return_val_if_fail(path
!= NULL
, NULL
);
32 if (realpath(path
, real
))
33 return g_strdup(real
);
35 return g_strdup(path
);
38 /* Join the path to the leaf (adding a / between them) and
39 * return a pointer to a buffer with the result. Buffer is valid until
40 * the next call to make_path.
42 GString
*make_path(char *dir
, char *leaf
)
44 static GString
*buffer
= NULL
;
47 buffer
= g_string_new(NULL
);
49 g_return_val_if_fail(dir
!= NULL
, buffer
);
50 g_return_val_if_fail(leaf
!= NULL
, buffer
);
52 g_string_sprintf(buffer
, "%s%s%s",
54 dir
[0] == '/' && dir
[1] == '\0' ? "" : "/",
60 /* Return our complete host name */
63 static char *name
= NULL
;
69 g_return_val_if_fail(gethostname(buffer
, 4096) == 0,
73 name
= g_strdup(buffer
);
79 /* fork() and run a new program.
80 * Returns the new PID, or 0 on failure.
82 int spawn(char **argv
)
84 return spawn_full(argv
, NULL
, 0);
87 /* As spawn(), but cd to dir first (if dir is non-NULL) */
88 int spawn_full(char **argv
, char *dir
, SpawnFlags flags
)
95 return 0; /* Failure */
98 /* We are the child process */
101 fprintf(stderr
, "chdir() failed: %s\n",
103 execvp(argv
[0], argv
);
104 fprintf(stderr
, "execvp(%s, ...) failed: %s\n",
110 /* We are the parent */
114 void debug_free_string(void *data
)
116 g_print("Freeing string '%s'\n", (char *) data
);
120 char *user_name(uid_t uid
)
122 static char buffer
[40];
123 struct passwd
*passwd
;
125 passwd
= getpwuid(uid
);
127 return passwd
->pw_name
;
128 snprintf(buffer
, sizeof(buffer
), "[%d]", uid
);
132 char *group_name(gid_t gid
)
134 static char buffer
[40];
137 group
= getgrgid(gid
);
139 return group
->gr_name
;
140 snprintf(buffer
, sizeof(buffer
), "[%d]", gid
);