handle fscanf failure

This commit is contained in:
Adrien Schildknecht 2013-05-21 15:54:12 +02:00
parent 58b31130e4
commit c04fa19254

21
xcwd.c
View File

@ -158,8 +158,9 @@ static processes_t getProcesses(void)
tn = fopen(name, "r"); tn = fopen(name, "r");
if (tn == NULL) if (tn == NULL)
continue; continue;
fscanf(tn, "%ld (%32[^)] %*3c %ld", &p->ps[j].pid, p->ps[j].name, if(fscanf(tn, "%ld (%32[^)] %*3c %ld", &p->ps[j].pid,
&p->ps[j].ppid); p->ps[j].name, &p->ps[j].ppid) != 3)
return NULL;
LOG("\t%-20s\tpid=%6ld\tppid=%6ld\n", p->ps[j].name, p->ps[j].pid, LOG("\t%-20s\tpid=%6ld\tppid=%6ld\n", p->ps[j].name, p->ps[j].pid,
p->ps[j].ppid); p->ps[j].ppid);
fclose(tn); fclose(tn);
@ -215,6 +216,13 @@ static void cwdOfDeepestChild(processes_t p, long pid)
return; return;
} }
int getHomeDirectory()
{
LOG("%s", "getenv $HOME...\n");
fprintf(stdout, "%s\n", getenv("HOME"));
return EXIT_FAILURE;
}
int main(int argc, const char *argv[]) int main(int argc, const char *argv[])
{ {
(void)argc; (void)argc;
@ -223,14 +231,13 @@ int main(int argc, const char *argv[])
processes_t p; processes_t p;
long pid; long pid;
Window w = focusedWindow(); Window w = focusedWindow();
if (w == 0) { if (w == 0)
LOG("%s", "getenv $HOME...\n"); return getHomeDirectory();
fprintf(stdout, "%s\n", getenv("HOME"));
return EXIT_FAILURE;
}
pid = windowPid(w); pid = windowPid(w);
p = getProcesses(); p = getProcesses();
if(p == NULL)
return getHomeDirectory();
if (pid != -1) { if (pid != -1) {
qsort(p->ps, p->n, sizeof(struct proc_s), ppidCmp); qsort(p->ps, p->n, sizeof(struct proc_s), ppidCmp);
} }