Re: [wiki] [sites] [dwm] add patch for restoring clients to their tags after restart || ViliamKovac1223

From: Tom Schwindl <schwindl_AT_posteo.de>
Date: Mon, 11 Jul 2022 12:15:12 +0000

Hi Viliam,

> ++void
> ++saveSession(void)
> ++{
> ++ FILE *fw = fopen(SESSION_FILE, "w");
> ++ for (Client *c = selmon->clients; c != NULL; c = c->next) { // get all the clients with their tags and write them to the file
> ++ fprintf(fw, "%lu %u
> ", c->win, c->tags);
> ++ }
> ++ fclose(fw);
> ++}

`fw` should be checked for NULL to prevent surprises.
The line break in fprintf(3) is unnecessary, as well as the curley brackets.

> ++
> ++void
> ++restoreSession(void)
> ++{
> ++ // restore session
> ++ FILE *fr = fopen(SESSION_FILE, "r");
> ++ if (!fr)
> ++ return;
> ++
> ++ char *str = malloc(23 * sizeof(char)); // allocate enough space for excepted input from text file
> ++ while (fscanf(fr, "%[^
> ] ", str) != EOF) { // read file till the end
> ++ long unsigned int winId;
> ++ unsigned int tagsForWin;
> ++ int check = sscanf(str, "%lu %u", &winId, &tagsForWin); // get data
> ++ if (check != 2) // break loop if data wasn't read correctly
> ++ break;
> ++
> ++ for (Client *c = selmon->clients; c ; c = c->next) { // add tags to every window by winId
> ++ if (c->win == winId) {
> ++ c->tags = tagsForWin;
> ++ break;
> ++ }
> ++ }
> ++ }
> ++
> ++ for (Client *c = selmon->clients; c ; c = c->next) { // refocus on windows
> ++ focus(c);
> ++ restack(c->mon);
> ++ }
> ++
> ++ for (Monitor *m = selmon; m; m = m->next) // rearrange all monitors
> ++ arrange(m);
> ++
> ++ free(str);
> ++ fclose(fr);
> ++
> ++ // delete a file
> ++ remove(SESSION_FILE);
> ++}

You should *really* check the return value of malloc(3).
dwm even provides a function, ecalloc(), which you could use here.
You might consider dropping `sizeof(char)`, since `char` is defined
to always have a size of one. But I'd let the readability point count in this case.

The line break in fscanf(3) is unnecessery, too.


Some notes on the style:
Initial declarations in `for` loops shouldn't be used.
The same applies to C99 comments.
You also mix up a lot of declarations and code. Consider splitting them up a bit more

Also, think about what your comments try to document.
It looks like you're commenting for the sake of commenting.

-- 
Best Regards,
Tom Schwindl
Received on Mon Jul 11 2022 - 14:15:12 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 11 2022 - 14:24:38 CEST