diff options
Diffstat (limited to 'mga-bg-res/mga-bg-res.c')
-rw-r--r-- | mga-bg-res/mga-bg-res.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/mga-bg-res/mga-bg-res.c b/mga-bg-res/mga-bg-res.c new file mode 100644 index 0000000..6061587 --- /dev/null +++ b/mga-bg-res/mga-bg-res.c @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> + +#define PATH "/usr/share/mga/backgrounds/" +#define DSTLINK PATH"default.jpg" +#define SRCLINK_PREFIX PATH"Mageia-Default-" +#define SRCLINK_SUFFIX ".jpg" +#define SRCLINK SRCLINK_PREFIX"%s"SRCLINK_SUFFIX +#define RESSIZE 20 +#define SRCSIZE (RESSIZE+strlen(SRCLINK_PREFIX)+strlen(SRCLINK_SUFFIX) + 1) +#define ALLOWCHARS "0123456789x" + +int main(int argc, char* argv[]) { + char src[SRCSIZE]; + char res[RESSIZE]; + int l; + + // check argument + if (argc != 2) { + fprintf(stderr, "Need a resolution argument, eg: '1920x1080'.\n"); + return 1; + } + + // get the res (max RESSIZE) + l = snprintf(res, RESSIZE, argv[1]); + if (l < 5) { + fprintf(stderr, "Malformed resolution argument, eg: '1920x1080'.\n"); + return 2; + } + + // to be sure, set the last char as 0 + if (l < RESSIZE) + res[l] = 0; + else + res[RESSIZE-1] = 0; + + // check if the resolution contains acceptable chars + l = strspn(res, ALLOWCHARS); + if (l < 5) { + fprintf(stderr, "Malformed resolution argument, eg: '1920x1080'.\n"); + return 2; + } + + // to be sure, set the last char as 0 + if (l < RESSIZE) + res[l] = 0; + else + res[RESSIZE-1] = 0; + + // create target string + if (snprintf(src, SRCSIZE, SRCLINK, res) < 0) { + fprintf(stderr, "Unknown error determining symlink target.\n"); + return 3; + } + + // check if symlink target exists + if (access(src, F_OK) < 0) { + fprintf(stderr, "No image for this resolution.\n"); + return 0; + } + + // set symlink + if (symlink(src, DSTLINK) < 0) { + if (errno != EEXIST) { + fprintf(stderr, "Symlink could not be set: %s.\n", strerror(errno)); + return 4; + } + + // since the destination exists, remove and try again + if (unlink(DSTLINK) < 0) { + fprintf(stderr, "Symlink could not be forced: %s.\n", strerror(errno)); + return 5; + } + if (symlink(src, DSTLINK) < 0) { + fprintf(stderr, "Unknown error during forced symlink: %s.\n", strerror(errno)); + return 6; + } + } + return 0; +} |