resurrecting-open-source-projects / scrot

SCReenshOT - command line screen capture utility
Other
512 stars 51 forks source link

File option cleanup #155

Closed ifohancroft closed 3 years ago

ifohancroft commented 3 years ago

When I initially implemented the --file / -F option, the naming of the thumbnail was done in the options.c file, at the end of the options parsing function, by introducing an extra check if there is a filename and a thumbnail. However, I have felt that there must be a better way / place to do that.

I have since found out that in main.c after the parsing of the options, there is already a check if a filename exists and naming of both the thumbnail and the filename is done there. This felt like the perfect place.

So, from options.c, I removed:

if (opt.thumb && opt.outputFile)
        opt.thumbFile = nameThumbnail(opt.outputFile);

In main.c I changed this:

if (!opt.outputFile) {
        opt.outputFile = strdup("%Y-%m-%d-%H%M%S_$wx$h_scrot.png");
        opt.thumbFile = strdup("%Y-%m-%d-%H%M%S_$wx$h_scrot-thumb.png");
    } else
        scrotHaveFileExtension(opt.outputFile, &haveExtension);

To this:

if (!opt.outputFile) {
        opt.outputFile = strdup("%Y-%m-%d-%H%M%S_$wx$h_scrot.png");
        opt.thumbFile = strdup("%Y-%m-%d-%H%M%S_$wx$h_scrot-thumb.png");
    } else {
        opt.thumbFile = nameThumbnail(opt.outputFile);
        scrotHaveFileExtension(opt.outputFile, &haveExtension);
    }

However, there turned out to be a bug inside of nameThumbnail, that was breaking it when the call to it was moved to main.c This also fixes that, as well as converts the function to using strlcpy and strlcat instead of strncpy and strlcat.

ifohancroft commented 3 years ago

The changes should be done. Let me know if I missed something.

Btw in optionsNameThumbnail, I had to revert newName back to char* newName; otherwise there were warnings during compilation that passing it as the first parameter to strlcpy and strlcat are discarding the 'const' part.

P.S. What is the different between strdup and strndup and in the contexts of scrot, is one preferred over the other?