Solved Opening and writing to a file

I got it to "open" but I do not see that file therefore it is not being written to either.

Opening the file.
Code:
// to keep a file of current image being displayed.
    //I added this mostly for my own use. to delete current image
    //by the use of the image name containted within this file
   // if I do not like that image (anymore).
    struct passwd *p;
    char *uhome = NULL;
    uid_t  uid;

  if ((p = getpwuid(uid = getuid())) == NULL)
    perror("getpwuid() error");
  else {
        uhome = p->pw_dir;
    }
    char configfile[20] = "/mhsetrootimagefile";
    strcat(uhome,configfile);
 
    FILE *fptr;
    fptr = fopen("uhome", "w");
    if (fptr == NULL) {
        fprintf(stderr,"cannot open config file\n");
        exit(1);
    }
    printf("file = %s\n",uhome);
the output of that is.
Code:
[userx@FreeBSD12 mhsetroot]$ ./mhsetroot -f -b green -t 5 ~/Images/wallheaven
file = /home/userx/mhsetrootimagefile
3: /home/userx/Images/wallheaven/wallhaven-x165eo.jpg
it did not exit therefore it is suppose to be opened. but I no see it. So I am a little baffled.
 
Last edited:
Code:
fptr = fopen("uhome", "w");
Remove quotes around uhome. Otherwise, you're trying to open a file literally called uhome in the current working directory.

You also might want to study the strcat() manual page. The way you are appending the configfile name on to the end of the pw_dir field of the password entry is not good.
 
Oh yeah thanks, i got that figured out, I forgot to update in here. sorry. but yeah I don't mess with the file stuff much in Programming and BASH habits must have been in place , when in doubt, quote everything.
 
I don't understand your code. You said in the title of the thread that you want to open and write a file. Good. But then you only open it, never write to it. You should end up with a zero size file: fopen with the "w" mode will create an empty file if it doesn't exist already, and it will take an existing file and cut the size to zero if it already exists. But then you never write to it.

Do you actually end up with a zero size file? What do you mean by "no see it"? Do all the parent directories exist (they probably do, otherwise fopen would have failed)?
 
I don't understand your code. You said in the title of the thread that you want to open and write a file. Good. But then you only open it, never write to it. You should end up with a zero size file: fopen with the "w" mode will create an empty file if it doesn't exist already, and it will take an existing file and cut the size to zero if it already exists. But then you never write to it.

Do you actually end up with a zero size file? What do you mean by "no see it"? Do all the parent directories exist (they probably do, otherwise fopen would have failed)?
the write to that file is somewhere else in the code, and I did not think it relevant to show that part, it is the creating and opening of the file itself that was not working as I intended it to.

where after the strcat it gives me
uhome=/home/userx/mhsetrootimagefile

The 'w' creates if not present. what was actually taking place because I was quoting the variable name. It was using the variable name as the file name in the same dir I was running the program from and not that value that was held within it in the location that it said within the variable, by removing the quotes it gave me the value that was held within the veritable. therefore giving me the results I was intending. problem solved.

(after I had gotten the first part figured out, then I could attack the second part as well, so that just kid of fell into place)

(and for an added bonus, I just lost everything I did due to an oops on a script I was using in Linux and ran in here without completely changing everything I should have, not a real big deal but yeah, I rm /home/userx now it is blank and I cannot log back in .. posting on that in a min).
 
I am re-opening this because now that the after I lost everything in my home, and getting around to doing this again, it too is something I noticed before that happened, and well now it bugs me.
in main (excluting the functions it calls)
Code:
 int main(int argc, char *argv[])
 {
    unsigned char scale = 0, center = 0, fit = 0, mirror = 0, mode = 0;
    char *displayname = NULL, *fname = NULL, *bgcolor = NULL;
    register unsigned char i;
    int ztime = 0;
    
    struct passwd *p;
    char *uhome = NULL;
    uid_t  uid;
    
     if ((p = getpwuid(uid = getuid())) == NULL)
        perror("getpwuid() error");
    else {
            uhome = p->pw_dir;
        }
    char configfile[20] = "/mhsetrootimagefile";
    strcat(uhome,configfile);
   
    FILE *fptr;
     
    if (   (fptr  = fopen(uhome, "w+")) == NULL   ) {
        fprintf(stderr,"cannot open config file\n");
        exit(1);
    }
    printf("file = %s\n",uhome);
    
    
     if (argc < 2) {
        message(argv);
         exit(0);
     }
     
     for (i = 1; i < argc; i++) {
         if (*argv[i] != '-') {
             break;
         }
         if (argv[i][1] == 'd') {
             displayname = argv[++i];
         } else if (argv[i][1] == 'b') {
             bgcolor = argv[++i];
         } else if (argv[i][1] == 's') {
             //scale 
             mode = 1;
         } else if (argv[i][1] == 'c') {
             //center
             mode = 2;
         } else if (argv[i][1] == 'f') {
             //fit 
             mode = 3;
         } else if (argv[i][1] == 'm') {
             //mirror
             mode = 4;
         } else if (argv[i][1] == 'T') {
             ztime = atoi(argv[++i]);
        }  else if (argv[i][1] == 'o' ) {
            fname = strdup(argv[++i]); 
        }
         else if (argv[i][1] == 'x') {
             fprintf(stderr, "Debugging activated.\n");
             debug = 1;
         } else {
             fprintf(stderr, "%s:  Unrecognized option \'%c\'\n\n", *argv, argv[i][1]);
             message(argv);
             exit(2);
         }
     }
     if ( fname == NULL ) {
            fname = argv[i];
            set_image(argc, argv, displayname, fname, bgcolor, mode);
    }
    
    if (ztime > 0) {
                
        GetFiles(fname,0);
        prepare_filelist();
        fname = get_file_name();
        fwrite(fname, strlen(fname)+1, 1, fptr);
        printf("%s\n", fname);
        
        /*
        rewind(fptr);
        char buff[100];
        fread(buff, strlen(fname)+1,1,fptr);
        printf("a: %s\n",buff);
        */
        
        set_image(argc, argv, displayname, fname, bgcolor, mode);
        
        while (! sleep(ztime))
        {
            fname = get_file_name();
            rewind(fptr);
            fwrite(fname, strlen(fname)+1, 1, fptr);
            printf("%s\n", fname);
            set_image(argc, argv, displayname, fname, bgcolor, mode);
            /*
            rewind(fptr);
            char buff[100];
            fread(buff, strlen(fname)+1,1,fptr);
            printf("D: %s\n",buff);
            */
        }
    }
    fclose(fptr);
    
     return 0;
 }
On first call to write to file, before it goes into the loop. When I check it via cat filename, it is empty. then when it goes into the loop and gets a new file to set, and call to fwrite again, then I check the file via cat.

this is when it then has the first file that was set written into the file, then again, repeatedly. So the image that was showing before is the name in the file and not the current file-image-name.
It is always lagging one behind the current file set to screen.

How do I get it to write the file name to the file that is currently being called to be set to the background?
 
I am re-opening this because now that the after I lost everything in my home, and getting around to doing this again, it too is something I noticed before that happened, and well now it bugs me.
in main (excluting the functions it calls)
Code:
int main(int argc, char *argv[])
{
    unsigned char scale = 0, center = 0, fit = 0, mirror = 0, mode = 0;
    char *displayname = NULL, *fname = NULL, *bgcolor = NULL;
    register unsigned char i;
    int ztime = 0;
   
    struct passwd *p;
    char *uhome = NULL;
    uid_t  uid;
   
     if ((p = getpwuid(uid = getuid())) == NULL)
        perror("getpwuid() error");
    else {
            uhome = p->pw_dir;
        }
    char configfile[20] = "/mhsetrootimagefile";
    strcat(uhome,configfile);
  
    FILE *fptr;
    
    if (   (fptr  = fopen(uhome, "w+")) == NULL   ) {
        fprintf(stderr,"cannot open config file\n");
        exit(1);
    }
    printf("file = %s\n",uhome);
   
   
     if (argc < 2) {
        message(argv);
         exit(0);
     }
    
     for (i = 1; i < argc; i++) {
         if (*argv[i] != '-') {
             break;
         }
         if (argv[i][1] == 'd') {
             displayname = argv[++i];
         } else if (argv[i][1] == 'b') {
             bgcolor = argv[++i];
         } else if (argv[i][1] == 's') {
             //scale
             mode = 1;
         } else if (argv[i][1] == 'c') {
             //center
             mode = 2;
         } else if (argv[i][1] == 'f') {
             //fit
             mode = 3;
         } else if (argv[i][1] == 'm') {
             //mirror
             mode = 4;
         } else if (argv[i][1] == 'T') {
             ztime = atoi(argv[++i]);
        }  else if (argv[i][1] == 'o' ) {
            fname = strdup(argv[++i]);
        }
         else if (argv[i][1] == 'x') {
             fprintf(stderr, "Debugging activated.\n");
             debug = 1;
         } else {
             fprintf(stderr, "%s:  Unrecognized option \'%c\'\n\n", *argv, argv[i][1]);
             message(argv);
             exit(2);
         }
     }
     if ( fname == NULL ) {
            fname = argv[i];
            set_image(argc, argv, displayname, fname, bgcolor, mode);
    }
   
    if (ztime > 0) {
               
        GetFiles(fname,0);
        prepare_filelist();
        fname = get_file_name();
        fwrite(fname, strlen(fname)+1, 1, fptr);
        printf("%s\n", fname);
       
        /*
        rewind(fptr);
        char buff[100];
        fread(buff, strlen(fname)+1,1,fptr);
        printf("a: %s\n",buff);
        */
       
        set_image(argc, argv, displayname, fname, bgcolor, mode);
       
        while (! sleep(ztime))
        {
            fname = get_file_name();
            rewind(fptr);
            fwrite(fname, strlen(fname)+1, 1, fptr);
            printf("%s\n", fname);
            set_image(argc, argv, displayname, fname, bgcolor, mode);
            /*
            rewind(fptr);
            char buff[100];
            fread(buff, strlen(fname)+1,1,fptr);
            printf("D: %s\n",buff);
            */
        }
    }
    fclose(fptr);
   
     return 0;
}
On first call to write to file, before it goes into the loop. When I check it via cat filename, it is empty. then when it goes into the loop and gets a new file to set, and call to fwrite again, then I check the file via cat.

this is when it then has the first file that was set written into the file, then again, repeatedly. So the image that was showing before is the name in the file and not the current file-image-name.
It is always lagging one behind the current file set to screen.

How do I get it to write the file name to the file that is currently being called to be set to the background?

Ok, you have a few nasties in here:
uhome is a pointer. It's initial setting is NULL (that's good), but then you assign it to pw_dir of passwd struct. While that's not bad, it's not good either. Why? Read the bugs portion of the manual page. It's a pointer to a static area which can be changed by another call to getpwuid(). Solution? Copy it to another variable:

Code:
uhome = calloc(strlen(p->pw_dir) + 1, 1); // or malloc()
if (uhome == NULL) exit(1);
strcpy(uhome,p->pw_dir);

or you could use strdup()

Later on in your code you append data to uhome. Now that's real bad.
strcat(uhome,configfile);

Where's that going to go? Well, you're hoping that the size of pw_dir has enough room in it to take configfile. It might, it might not. Either way, that's bad programming.

You're better to do this:
Code:
char *temp;
temp = realloc(uhome,strlen(uhome) + strlen(configfile) + 1);
if (temp == NULL) exit(1);
uhome = temp;

Remember to free(uhome) at the end.

Some other notes:
1. Learn to use getopt(); you're re-inventing the wheel.
2. You are performing buffered I/O. Using fflush() will write out inefficiently your current buffer.
3. In your while loop, how are you sure buff[100] is not exceeded by a fname length?
4. You never free fname (unless you're returning static char arrays from get_file_name() )
 
Thanks for the tips... let me go fix that.

I've done getopts in my other apps that basically do the same thing, just more options in the other ones, and I didn't really feel like rewriting even that part of it even by putting getopts in it, as it is taken from another app and I just added the link list from my other program into this one (for now). So that code I wrote in main was just to see if I could get my link list to work in here too and change backgrounds at a set time.

The open file and write to it part is me showing my lack of knowledge in that area of programming.

The buff [100] to read the file. I was just using that to see what was in the file, if it was being written to right after I told it to. it is a temp part of the code to be removed.
 
Ok, you have a few nasties in here:
uhome is a pointer. It's initial setting is NULL (that's good), but then you assign it to pw_dir of passwd struct. While that's not bad, it's not good either. Why? Read the bugs portion of the manual page. It's a pointer to a static area which can be changed by another call to getpwuid(). Solution? Copy it to another variable:

Code:
uhome = calloc(strlen(p->pw_dir) + 1, 1); // or malloc()
if (uhome == NULL) exit(1);
strcpy(uhome,p->pw_dir);

or you could use strdup()

Later on in your code you append data to uhome. Now that's real bad.
strcat(uhome,configfile);

Where's that going to go? Well, you're hoping that the size of pw_dir has enough room in it to take configfile. It might, it might not. Either way, that's bad programming.

You're better to do this:
Code:
char *temp;
temp = realloc(uhome,strlen(uhome) + strlen(configfile) + 1);
if (temp == NULL) exit(1);
uhome = temp;

Remember to free(uhome) at the end.

Some other notes:
1. Learn to use getopt(); you're re-inventing the wheel.
2. You are performing buffered I/O. Using fflush() will write out inefficiently your current buffer.
3. In your while loop, how are you sure buff[100] is not exceeded by a fname length?
4. You never free fname (unless you're returning static char arrays from get_file_name() )
On this temp = realloc bit, I am assuming all this is doing is asuring that the memory space is large enough for both strings to be added together, because I still needed to strcat them together to get this to work.

Code:
....
 uhome = temp;
strcat(uhome,configfile);
 
On this temp = realloc bit, I am assuming all this is doing is asuring that the memory space is large enough for both strings to be added together, because I still needed to strcat them together to get this to work.

Code:
....
uhome = temp;
strcat(uhome,configfile);
Yes, correct. You place memory allocation into temp to check for failure then give that address to uhome. uhome now has enough space to cater for the pw_dir portion and your filename.
 
Thanks for the tips... let me go fix that.

I've done getopts in my other apps that basically do the same thing, just more options in the other ones, and I didn't really feel like rewriting even that part of it even by putting getopts in it, as it is taken from another app and I just added the link list from my other program into this one (for now). So that code I wrote in main was just to see if I could get my link list to work in here too and change backgrounds at a set time.
Fair enough. It is preferable to copy/paste your previous code because you know it works (or hope so...) ;)

The open file and write to it part is me showing my lack of knowledge in that area of programming.

The buff [100] to read the file. I was just using that to see what was in the file, if it was being written to right after I told it to. it is a temp part of the code to be removed.
No worries you've done a great job. These things I pointed out are just 'gotchas'. Once you know, you never repeat the wrong.

Equally, there's nothing wrong with creating a fixed size variable, say char buff[20000]. RAM is cheap and often plentiful on most systems these days. Some programmers prefer doing this as it takes away the need to free() memory and removes potential memory leaks.

I see in your program you use fread/fwrite and then write away and read the null byte in the string. Your alternative is to use fgets()/fputs() which does not write the null away (fputs) but adds it upon getting the end-of-line character (fgets).

While it's a personal preference, using fgets() will mean you can look at the file that contains the string without seeing control characters like NULL at the end of each line. (I guess most text editors would display it somehow anyway).
 
Fair enough. It is preferable to copy/paste your previous code because you know it works (or hope so...) ;)


No worries you've done a great job. These things I pointed out are just 'gotchas'. Once you know, you never repeat the wrong.

Equally, there's nothing wrong with creating a fixed size variable, say char buff[20000]. RAM is cheap and often plentiful on most systems these days. Some programmers prefer doing this as it takes away the need to free() memory and removes potential memory leaks.

I see in your program you use fread/fwrite and then write away and read the null byte in the string. Your alternative is to use fgets()/fputs() which does not write the null away (fputs) but adds it upon getting the end-of-line character (fgets).

While it's a personal preference, using fgets() will mean you can look at the file that contains the string without seeing control characters like NULL at the end of each line. (I guess most text editors would display it somehow anyway).
to (maybe) satisfy your curiosity and maybe get a few pointers, this is my hack job, linklist c file and header are included on the attachments.


C:
/*
/*

  * Esetroot -- Set the root pixmap.  This program enables non-Enlightenment
  *             users to use Eterm's support for pseudotransparency.
  *
  * Written by Nat Friedman <ndf@mit.edu> with modifications by Gerald Britton
  * <gbritton@mit.edu> and Michael Jennings <mej@eterm.org>
  *
  * clang -I/usr/local/include filelist.c mhsetroot.c -o mhsetroot -lImlib2 -lX11
  *
  * Michael Heras (c)
  * Modified Sat. Oct. 19, 2019
  * added ability to load files from dir, and time delay to
  * set them on background.
  */
  
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>
#include <libast.h>
#include <sys/types.h>
#include <pwd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xos.h>
#include <Imlib2.h>
#include "filelist.h"

void set_pixmap_property(Pixmap p);

Display *Xdisplay;
Screen *scr;
Window Xroot;
int screen;
unsigned char debug = 0;

#define Xdepth   (DefaultDepth(Xdisplay, screen))

void
set_pixmap_property(Pixmap p)
{

     Atom prop_root, prop_mhsetroot, type;
     int format;
     unsigned long length, after;
     unsigned char *data_root, *data_mhsetroot;

     prop_root = XInternAtom(Xdisplay, "_XROOTPMAP_ID", True);
     prop_mhsetroot = XInternAtom(Xdisplay, "MHSETROOT_PMAP_ID", True);

     if (debug) {
         fprintf(stderr, "%s:%d:  set_pixmap_property(0x%08x):  prop_root == 0x%08x, prop_esetroot == 0x%08x\n", __FILE__, __LINE__,
                 (unsigned int) p, (unsigned int) prop_root, (unsigned int) prop_mhsetroot);
     }
     if (prop_root != None && prop_mhsetroot != None) {
         XGetWindowProperty(Xdisplay, Xroot, prop_root, 0L, 1L, False, AnyPropertyType, &type, &format, &length, &after, &data_root);
         if (type == XA_PIXMAP) {
             XGetWindowProperty(Xdisplay, Xroot, prop_mhsetroot, 0L, 1L, False, AnyPropertyType, &type, &format, &length, &after,
                                &data_mhsetroot);
             if (data_root && data_mhsetroot) {
                 if (debug) {
                     fprintf(stderr, "%s:%d:  set_pixmap_property(0x%08x):  data_root == 0x%08x, data_esetroot == 0x%08x\n",
                             __FILE__, __LINE__, (unsigned int) p, (unsigned int) *((Pixmap *) data_root),
                             (unsigned int) *((Pixmap *) data_mhsetroot));
                 }
                 if (type == XA_PIXMAP && *((Pixmap *) data_root) == *((Pixmap *) data_mhsetroot)) {
                     if (debug) {
                         fprintf(stderr, "%s:%d:  set_pixmap_property(0x%08x):  XKillClient() is being called.\n", __FILE__,
                                 __LINE__, (unsigned int) p);
                     }
                     XKillClient(Xdisplay, *((Pixmap *) data_root));
                 }
             }
         }
     }
     /* This will locate the property, creating it if it doesn't exist */
     prop_root = XInternAtom(Xdisplay, "_XROOTPMAP_ID", False);
     prop_mhsetroot = XInternAtom(Xdisplay, "MHSETROOT_PMAP_ID", False);

     /* The call above should have created it.  If that failed, we can't continue. */
     if (prop_root == None || prop_mhsetroot == None) {
         fprintf(stderr, "Esetroot:  creation of pixmap property failed.\n");
         exit(1);
     }
     XChangeProperty(Xdisplay, Xroot, prop_root, XA_PIXMAP, 32, PropModeReplace, (unsigned char *) &p, 1);
     XChangeProperty(Xdisplay, Xroot, prop_mhsetroot, XA_PIXMAP, 32, PropModeReplace, (unsigned char *) &p, 1);
     if (debug) {
         fprintf(stderr, "%s:%d:  set_pixmap_property(0x%08x):  _XROOTPMAP_ID and MHSETROOT_PMAP_ID set to 0x%08x.\n", __FILE__,
                 __LINE__, (unsigned int) p, (unsigned int) p);
     }
     XSetCloseDownMode(Xdisplay, RetainPermanent);
     XFlush(Xdisplay);
}
//#endif
void message(char **argv)
{
    fprintf(stderr, "%s [-display <display_name>] [-bgcolor <color>] [ -Time <time in seconds> ] [-scale] [-center] [-fit] [-mirror] [[ pixmap or [-o <path to files>]]\n", *argv);
    fprintf(stderr, "\t Short options are also recognized (-d, -b, -s, -c, -f, -T, -o, and -m)\n");
}
void set_image(Imlib_Image im, int argc, char **argv, char* displayname, char* fname, char* bgcolor, unsigned char mode)
{
    unsigned char scale = 0, center = 0, fit = 0, mirror = 0;
    //Imlib_Image im;
    Pixmap p = None, temp_pmap = None, m = None;
    register unsigned char i;
    GC gc;
    XGCValues gcv;
    XColor xcolor;
    int w, h, x, y;
   
    switch (mode)
    {
        case 1:
        scale = 1;
        break;
        case 2:
        center = 1;
        break;
        case 3:
        fit = 1;
        break;
        case 4:
        mirror = 1;
        break;
        default:
        break;
    }
  
  
     if (scale) {
         center = 0;
         mirror = 0;
     }

     if (debug) {
         fprintf(stderr, "%s:%d:  Display name is \"%s\"\n", __FILE__, __LINE__, displayname ? displayname : "(nil)");
         fprintf(stderr, "%s:%d:  Background color name is \"%s\"\n", __FILE__, __LINE__, bgcolor ? bgcolor : "(nil)");
         fprintf(stderr, "%s:%d:  Image will be %s\n", __FILE__, __LINE__,
                 scale ? "scaled" : (center ? "centered" : (fit ? "fit" : "tiled")));
         fprintf(stderr, "%s:%d:  Image file is %s\n", __FILE__, __LINE__, fname ? fname : "(nil)");
     }
     if (!displayname) {
         displayname = getenv("DISPLAY");
         if (debug) {
             fprintf(stderr, "%s:%d:  Display name set to %s via getenv(\"DISPLAY\")\n", __FILE__, __LINE__,
                     displayname ? displayname : "(nil)");
         }
     }
     if (!displayname) {
         displayname = ":0.0";
         if (debug) {
             fprintf(stderr, "%s:%d:  Display name defaulted to %s\n", __FILE__, __LINE__, displayname ? displayname : "(nil)");
         }
     }
     if ((Xdisplay = XOpenDisplay(displayname)) == 0) {
         fprintf(stderr, "%s:  Unable to open display %s\n", *argv, displayname);
         exit(1);
     }
     screen = DefaultScreen(Xdisplay);
     Xroot = RootWindow(Xdisplay, screen);
     scr = ScreenOfDisplay(Xdisplay, screen);
     if (debug) {
         fprintf(stderr, "%s:%d:  Chose screen %d\n", __FILE__, __LINE__, screen);
         fprintf(stderr, "%s:%d:  Root window is 0x%08x\n", __FILE__, __LINE__, (unsigned int) Xroot);
         fprintf(stderr, "%s:%d:  Found screen information at %8p\n", __FILE__, __LINE__, scr);
     }
     imlib_context_set_display(Xdisplay);
     imlib_context_set_visual(DefaultVisual(Xdisplay, DefaultScreen(Xdisplay)));
   
   
     //im = imlib_load_image_immediately(fname);
   
   
   
     if (!im) {
         fprintf(stderr, "%s:  Unable to load image file \"%s\".\n", *argv, fname);
         exit(1);
     } else if (debug) {
         fprintf(stderr, "%s:%d:  The Imlib Image is at %8p\n", __FILE__, __LINE__, im);
     }
     imlib_context_set_image(im);
     if (scale) {
         w = scr->width;
         h = scr->height;
     } else if (mirror) {
         w = imlib_image_get_width() * 2;
         h = imlib_image_get_height() * 2;
     } else {
         w = imlib_image_get_width();
         h = imlib_image_get_height();
     }
     if (fit) {
         double x_ratio, y_ratio;

         x_ratio = ((double) scr->width) / ((double) w);
         y_ratio = ((double) scr->height) / ((double) h);
         if (x_ratio > y_ratio) {
             x_ratio = y_ratio;
         }
         w = (int) (w * x_ratio);
         h = (int) (h * x_ratio);
     }

     p = XCreatePixmap(Xdisplay, Xroot, scr->width, scr->height, Xdepth);
     gcv.foreground = gcv.background = BlackPixel(Xdisplay, screen);
     if (bgcolor && XParseColor(Xdisplay, DefaultColormap(Xdisplay, screen), bgcolor, &xcolor)
         && XAllocColor(Xdisplay, DefaultColormap(Xdisplay, screen), &xcolor)) {
         gcv.foreground = gcv.background = xcolor.pixel;
     }
     gc = XCreateGC(Xdisplay, p, (GCForeground | GCBackground), &gcv);
     if (scale) {
         XFillRectangle(Xdisplay, p, gc, 0, 0, w, h);
     }
     if (center || fit) {
         XFillRectangle(Xdisplay, p, gc, 0, 0, scr->width, scr->height);
         x = (scr->width - w) >> 1;
         y = (scr->height - h) >> 1;
     } else {
         x = 0;
         y = 0;
     }
     if (debug) {
         fprintf(stderr, "%s:%d:  Assigned width and height for rendering as %dx%d\n", __FILE__, __LINE__, w, h);
         fprintf(stderr, "%s:%d:  Created %dx%d+%d+%d pixmap 0x%08x\n", __FILE__, __LINE__, scr->width, scr->height, x, y,
                 (unsigned int) p);
         fprintf(stderr, "%s:%d:  Applied Graphics Context %8p to pixmap.\n", __FILE__, __LINE__, gc);
     }
     imlib_context_set_anti_alias(1);
     imlib_context_set_dither(1);
     imlib_context_set_blend(0);
     if (mirror) {
         temp_pmap = XCreatePixmap(Xdisplay, Xroot, w, h, Xdepth);
         imlib_context_set_drawable(temp_pmap);
         imlib_render_image_on_drawable(0, 0);
         imlib_image_flip_horizontal();
         imlib_render_image_on_drawable(imlib_image_get_width(), 0);
         imlib_image_flip_vertical();
         imlib_render_image_on_drawable(imlib_image_get_width(), imlib_image_get_height());
         imlib_image_flip_horizontal();
         imlib_render_image_on_drawable(0, imlib_image_get_height());
     } else {
         imlib_context_set_drawable(Xroot);
         imlib_render_pixmaps_for_whole_image_at_size(&temp_pmap, &m, w, h);
     }
     if (debug) {
         fprintf(stderr, "%s:%d:  Rendered at %dx%d onto pixmap 0x%08x\n", __FILE__, __LINE__, w, h, (unsigned int) temp_pmap);
     }
     if (temp_pmap != None) {
         if (m) {
             XFreePixmap(Xdisplay, m);
             m = None;
         }
         XSetTile(Xdisplay, gc, temp_pmap);
         XSetTSOrigin(Xdisplay, gc, x, y);
         XSetFillStyle(Xdisplay, gc, FillTiled);
         if (center || fit) {
             XFillRectangle(Xdisplay, p, gc, x, y, w, h);
         } else {
             XFillRectangle(Xdisplay, p, gc, x, y, scr->width, scr->height);
         }
         XGrabServer(Xdisplay);
         set_pixmap_property(p);
         XSetWindowBackgroundPixmap(Xdisplay, Xroot, p);
         XClearWindow(Xdisplay, Xroot);
         XUngrabServer(Xdisplay);
         XFlush(Xdisplay);
     }
}


int main(int argc, char *argv[])
{
    unsigned char scale = 0, center = 0, fit = 0, mirror = 0, mode = 0;
    char *displayname = NULL, *bgcolor = NULL;
    char  *fname = NULL;
    register unsigned char i;
    int ztime = 60, ztimeflag = 0;
  
    Imlib_Image image, Imbuff;
  
    struct passwd *p;
    char *uhome = NULL;
    uid_t  uid;
  
     if ((p = getpwuid(uid = getuid())) == NULL)
        perror("getpwuid() error");
    else {
            uhome = calloc(strlen(p->pw_dir) + 1, 1);
            if (uhome == NULL) {
                printf("cound not aocculate uhome\n");
                exit(1);
            }
                strcpy(uhome, p->pw_dir);
        }
    char configfile[20] = "/mhsetrootimagefile";
    char *temp;
    temp = realloc(uhome, strlen(uhome) + strlen(configfile) +1);
    if (temp == NULL) {
        fprintf(stderr, "Could not accolate configfile\n");
        exit(1);
    }
    uhome = temp;
    strcat(uhome,configfile);
      
    FILE *fptr;
   
    if (   (fptr  = fopen(uhome, "w")) == NULL   ) {
        fprintf(stderr,"cannot open config file\n");
        exit(1);
    }
    printf("Config File = %s\n",uhome);
      
     if (argc < 2) {
        message(argv);
         exit(0);
     }
   
     for (i = 1; i < argc; i++) {
         if (*argv[i] != '-') {
             break;
         }
         if (argv[i][1] == 'd') {
             displayname = argv[++i];
         } else if (argv[i][1] == 'b') {
             bgcolor = argv[++i];
         } else if (argv[i][1] == 's') {
             //scale = 1;
             mode = 1;
         } else if (argv[i][1] == 'c') {
             //center = 1;
             mode = 2;
         } else if (argv[i][1] == 'f') {
             //fit = 1;
             mode = 3;
         } else if (argv[i][1] == 'm') {
             //mirror = 1;
             mode = 4;
         } else if (argv[i][1] == 'T') {
             ztime = atoi(argv[++i]);
             if(ztime < 30 ) {
                 ztime = 30;
             }
             ztimeflag = 1;
        }  else if (argv[i][1] == 'o' ) {
            fname = strdup(argv[++i]);
            ztimeflag = 1;
        }
         else if (argv[i][1] == 'x') {
             fprintf(stderr, "Debugging activated.\n");
             debug = 1;
         } else {
             fprintf(stderr, "%s:  Unrecognized option \'%c\'\n\n", *argv, argv[i][1]);
             message(argv);
             exit(2);
         }
     }
     if ( fname == NULL ) {
            fname = argv[i];
            image = imlib_load_image_without_cache (fname);
            set_image(image, argc, argv, displayname, fname, bgcolor, mode);
            fname = strdup(   imlib_image_get_filename () );
            fwrite(fname, strlen(fname)+1, 1, fptr);
            fflush(fptr);
        }
  
    char * ftemp = NULL;
  
    if (ztimeflag) {
        printf("Change Images time set at %d seconds\n", ztime);
        GetFiles(fname,0);
        prepare_filelist();
  
        image = check_image_err(Imbuff);
        set_image(image, argc, argv, displayname, fname, bgcolor, mode);
        fname = strdup(   imlib_image_get_filename () );
        fwrite(fname, strlen(fname)+1, 1, fptr);
        fflush(fptr);
              
        while (! sleep(ztime))
        {
            image = check_image_err(Imbuff);
            set_image(image, argc, argv, displayname, fname, bgcolor, mode);
            fname = strdup(   imlib_image_get_filename () );
            rewind(fptr);
            fwrite(fname, strlen(fname)+1, 1, fptr);
            fflush(fptr);
        }
    }
    fclose(fptr);
    free(fname);
  
     return 0;
}

as far between fwrite, fread, and fgets, and fputs, I do not work with files as such much, I only know if I am going to use fwrite, then I want to keep it in the family and use fread. I just don't want to play mix them up. because of end line and null and all of that which one does one thing and the otehr does not and trying to keep that all straight .

on my other laptop clang works in compiling it using the same commands and such, here on this laptop it does not.

Code:
userx@FreeBSD.net:/usr/home/userx/Downloads/mhsetroot-2.0
$ clang -I/usr/local/include filelist.c mhsetroot.c -o mhsetroot -lImlib2 -lX11
/usr/bin/ld: error: unable to find library -lImlib2
/usr/bin/ld: error: unable to find library -lX11
clang: error: linker command failed with exit code 1 (use -v to see invocation)

//
userx@FreeBSD.net:/usr/home/userx/Downloads/mhsetroot-2.0
$ gcc9 -I/usr/local/include filelist.c mhsetroot.c -o mhsetroot -lImlib2 -lX11

both running FreeBSD -p10.
 

Attachments

  • filelist.c.txt
    11.9 KB · Views: 111
  • filelist.h.txt
    3.2 KB · Views: 118
to (maybe) satisfy your curiosity and maybe get a few pointers, this is my hack job, linklist c file and header are included on the attachments.


C:
/*
/*

  * Esetroot -- Set the root pixmap.  This program enables non-Enlightenment
  *             users to use Eterm's support for pseudotransparency.
  *
  * Written by Nat Friedman <ndf@mit.edu> with modifications by Gerald Britton
  * <gbritton@mit.edu> and Michael Jennings <mej@eterm.org>
  *
  * clang -I/usr/local/include filelist.c mhsetroot.c -o mhsetroot -lImlib2 -lX11
  *
  * Michael Heras (c)
  * Modified Sat. Oct. 19, 2019
  * added ability to load files from dir, and time delay to
  * set them on background.
  */

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>
#include <libast.h>
#include <sys/types.h>
#include <pwd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xos.h>
#include <Imlib2.h>
#include "filelist.h"

void set_pixmap_property(Pixmap p);

Display *Xdisplay;
Screen *scr;
Window Xroot;
int screen;
unsigned char debug = 0;

#define Xdepth   (DefaultDepth(Xdisplay, screen))

void
set_pixmap_property(Pixmap p)
{

     Atom prop_root, prop_mhsetroot, type;
     int format;
     unsigned long length, after;
     unsigned char *data_root, *data_mhsetroot;

     prop_root = XInternAtom(Xdisplay, "_XROOTPMAP_ID", True);
     prop_mhsetroot = XInternAtom(Xdisplay, "MHSETROOT_PMAP_ID", True);

     if (debug) {
         fprintf(stderr, "%s:%d:  set_pixmap_property(0x%08x):  prop_root == 0x%08x, prop_esetroot == 0x%08x\n", __FILE__, __LINE__,
                 (unsigned int) p, (unsigned int) prop_root, (unsigned int) prop_mhsetroot);
     }
     if (prop_root != None && prop_mhsetroot != None) {
         XGetWindowProperty(Xdisplay, Xroot, prop_root, 0L, 1L, False, AnyPropertyType, &type, &format, &length, &after, &data_root);
         if (type == XA_PIXMAP) {
             XGetWindowProperty(Xdisplay, Xroot, prop_mhsetroot, 0L, 1L, False, AnyPropertyType, &type, &format, &length, &after,
                                &data_mhsetroot);
             if (data_root && data_mhsetroot) {
                 if (debug) {
                     fprintf(stderr, "%s:%d:  set_pixmap_property(0x%08x):  data_root == 0x%08x, data_esetroot == 0x%08x\n",
                             __FILE__, __LINE__, (unsigned int) p, (unsigned int) *((Pixmap *) data_root),
                             (unsigned int) *((Pixmap *) data_mhsetroot));
                 }
                 if (type == XA_PIXMAP && *((Pixmap *) data_root) == *((Pixmap *) data_mhsetroot)) {
                     if (debug) {
                         fprintf(stderr, "%s:%d:  set_pixmap_property(0x%08x):  XKillClient() is being called.\n", __FILE__,
                                 __LINE__, (unsigned int) p);
                     }
                     XKillClient(Xdisplay, *((Pixmap *) data_root));
                 }
             }
         }
     }
     /* This will locate the property, creating it if it doesn't exist */
     prop_root = XInternAtom(Xdisplay, "_XROOTPMAP_ID", False);
     prop_mhsetroot = XInternAtom(Xdisplay, "MHSETROOT_PMAP_ID", False);

     /* The call above should have created it.  If that failed, we can't continue. */
     if (prop_root == None || prop_mhsetroot == None) {
         fprintf(stderr, "Esetroot:  creation of pixmap property failed.\n");
         exit(1);
     }
     XChangeProperty(Xdisplay, Xroot, prop_root, XA_PIXMAP, 32, PropModeReplace, (unsigned char *) &p, 1);
     XChangeProperty(Xdisplay, Xroot, prop_mhsetroot, XA_PIXMAP, 32, PropModeReplace, (unsigned char *) &p, 1);
     if (debug) {
         fprintf(stderr, "%s:%d:  set_pixmap_property(0x%08x):  _XROOTPMAP_ID and MHSETROOT_PMAP_ID set to 0x%08x.\n", __FILE__,
                 __LINE__, (unsigned int) p, (unsigned int) p);
     }
     XSetCloseDownMode(Xdisplay, RetainPermanent);
     XFlush(Xdisplay);
}
//#endif
void message(char **argv)
{
    fprintf(stderr, "%s [-display <display_name>] [-bgcolor <color>] [ -Time <time in seconds> ] [-scale] [-center] [-fit] [-mirror] [[ pixmap or [-o <path to files>]]\n", *argv);
    fprintf(stderr, "\t Short options are also recognized (-d, -b, -s, -c, -f, -T, -o, and -m)\n");
}
void set_image(Imlib_Image im, int argc, char **argv, char* displayname, char* fname, char* bgcolor, unsigned char mode)
{
    unsigned char scale = 0, center = 0, fit = 0, mirror = 0;
    //Imlib_Image im;
    Pixmap p = None, temp_pmap = None, m = None;
    register unsigned char i;
    GC gc;
    XGCValues gcv;
    XColor xcolor;
    int w, h, x, y;

    switch (mode)
    {
        case 1:
        scale = 1;
        break;
        case 2:
        center = 1;
        break;
        case 3:
        fit = 1;
        break;
        case 4:
        mirror = 1;
        break;
        default:
        break;
    }


     if (scale) {
         center = 0;
         mirror = 0;
     }

     if (debug) {
         fprintf(stderr, "%s:%d:  Display name is \"%s\"\n", __FILE__, __LINE__, displayname ? displayname : "(nil)");
         fprintf(stderr, "%s:%d:  Background color name is \"%s\"\n", __FILE__, __LINE__, bgcolor ? bgcolor : "(nil)");
         fprintf(stderr, "%s:%d:  Image will be %s\n", __FILE__, __LINE__,
                 scale ? "scaled" : (center ? "centered" : (fit ? "fit" : "tiled")));
         fprintf(stderr, "%s:%d:  Image file is %s\n", __FILE__, __LINE__, fname ? fname : "(nil)");
     }
     if (!displayname) {
         displayname = getenv("DISPLAY");
         if (debug) {
             fprintf(stderr, "%s:%d:  Display name set to %s via getenv(\"DISPLAY\")\n", __FILE__, __LINE__,
                     displayname ? displayname : "(nil)");
         }
     }
     if (!displayname) {
         displayname = ":0.0";
         if (debug) {
             fprintf(stderr, "%s:%d:  Display name defaulted to %s\n", __FILE__, __LINE__, displayname ? displayname : "(nil)");
         }
     }
     if ((Xdisplay = XOpenDisplay(displayname)) == 0) {
         fprintf(stderr, "%s:  Unable to open display %s\n", *argv, displayname);
         exit(1);
     }
     screen = DefaultScreen(Xdisplay);
     Xroot = RootWindow(Xdisplay, screen);
     scr = ScreenOfDisplay(Xdisplay, screen);
     if (debug) {
         fprintf(stderr, "%s:%d:  Chose screen %d\n", __FILE__, __LINE__, screen);
         fprintf(stderr, "%s:%d:  Root window is 0x%08x\n", __FILE__, __LINE__, (unsigned int) Xroot);
         fprintf(stderr, "%s:%d:  Found screen information at %8p\n", __FILE__, __LINE__, scr);
     }
     imlib_context_set_display(Xdisplay);
     imlib_context_set_visual(DefaultVisual(Xdisplay, DefaultScreen(Xdisplay)));


     //im = imlib_load_image_immediately(fname);



     if (!im) {
         fprintf(stderr, "%s:  Unable to load image file \"%s\".\n", *argv, fname);
         exit(1);
     } else if (debug) {
         fprintf(stderr, "%s:%d:  The Imlib Image is at %8p\n", __FILE__, __LINE__, im);
     }
     imlib_context_set_image(im);
     if (scale) {
         w = scr->width;
         h = scr->height;
     } else if (mirror) {
         w = imlib_image_get_width() * 2;
         h = imlib_image_get_height() * 2;
     } else {
         w = imlib_image_get_width();
         h = imlib_image_get_height();
     }
     if (fit) {
         double x_ratio, y_ratio;

         x_ratio = ((double) scr->width) / ((double) w);
         y_ratio = ((double) scr->height) / ((double) h);
         if (x_ratio > y_ratio) {
             x_ratio = y_ratio;
         }
         w = (int) (w * x_ratio);
         h = (int) (h * x_ratio);
     }

     p = XCreatePixmap(Xdisplay, Xroot, scr->width, scr->height, Xdepth);
     gcv.foreground = gcv.background = BlackPixel(Xdisplay, screen);
     if (bgcolor && XParseColor(Xdisplay, DefaultColormap(Xdisplay, screen), bgcolor, &xcolor)
         && XAllocColor(Xdisplay, DefaultColormap(Xdisplay, screen), &xcolor)) {
         gcv.foreground = gcv.background = xcolor.pixel;
     }
     gc = XCreateGC(Xdisplay, p, (GCForeground | GCBackground), &gcv);
     if (scale) {
         XFillRectangle(Xdisplay, p, gc, 0, 0, w, h);
     }
     if (center || fit) {
         XFillRectangle(Xdisplay, p, gc, 0, 0, scr->width, scr->height);
         x = (scr->width - w) >> 1;
         y = (scr->height - h) >> 1;
     } else {
         x = 0;
         y = 0;
     }
     if (debug) {
         fprintf(stderr, "%s:%d:  Assigned width and height for rendering as %dx%d\n", __FILE__, __LINE__, w, h);
         fprintf(stderr, "%s:%d:  Created %dx%d+%d+%d pixmap 0x%08x\n", __FILE__, __LINE__, scr->width, scr->height, x, y,
                 (unsigned int) p);
         fprintf(stderr, "%s:%d:  Applied Graphics Context %8p to pixmap.\n", __FILE__, __LINE__, gc);
     }
     imlib_context_set_anti_alias(1);
     imlib_context_set_dither(1);
     imlib_context_set_blend(0);
     if (mirror) {
         temp_pmap = XCreatePixmap(Xdisplay, Xroot, w, h, Xdepth);
         imlib_context_set_drawable(temp_pmap);
         imlib_render_image_on_drawable(0, 0);
         imlib_image_flip_horizontal();
         imlib_render_image_on_drawable(imlib_image_get_width(), 0);
         imlib_image_flip_vertical();
         imlib_render_image_on_drawable(imlib_image_get_width(), imlib_image_get_height());
         imlib_image_flip_horizontal();
         imlib_render_image_on_drawable(0, imlib_image_get_height());
     } else {
         imlib_context_set_drawable(Xroot);
         imlib_render_pixmaps_for_whole_image_at_size(&temp_pmap, &m, w, h);
     }
     if (debug) {
         fprintf(stderr, "%s:%d:  Rendered at %dx%d onto pixmap 0x%08x\n", __FILE__, __LINE__, w, h, (unsigned int) temp_pmap);
     }
     if (temp_pmap != None) {
         if (m) {
             XFreePixmap(Xdisplay, m);
             m = None;
         }
         XSetTile(Xdisplay, gc, temp_pmap);
         XSetTSOrigin(Xdisplay, gc, x, y);
         XSetFillStyle(Xdisplay, gc, FillTiled);
         if (center || fit) {
             XFillRectangle(Xdisplay, p, gc, x, y, w, h);
         } else {
             XFillRectangle(Xdisplay, p, gc, x, y, scr->width, scr->height);
         }
         XGrabServer(Xdisplay);
         set_pixmap_property(p);
         XSetWindowBackgroundPixmap(Xdisplay, Xroot, p);
         XClearWindow(Xdisplay, Xroot);
         XUngrabServer(Xdisplay);
         XFlush(Xdisplay);
     }
}


int main(int argc, char *argv[])
{
    unsigned char scale = 0, center = 0, fit = 0, mirror = 0, mode = 0;
    char *displayname = NULL, *bgcolor = NULL;
    char  *fname = NULL;
    register unsigned char i;
    int ztime = 60, ztimeflag = 0;

    Imlib_Image image, Imbuff;

    struct passwd *p;
    char *uhome = NULL;
    uid_t  uid;

     if ((p = getpwuid(uid = getuid())) == NULL)
        perror("getpwuid() error");
    else {
            uhome = calloc(strlen(p->pw_dir) + 1, 1);
            if (uhome == NULL) {
                printf("cound not aocculate uhome\n");
                exit(1);
            }
                strcpy(uhome, p->pw_dir);
        }
    char configfile[20] = "/mhsetrootimagefile";
    char *temp;
    temp = realloc(uhome, strlen(uhome) + strlen(configfile) +1);
    if (temp == NULL) {
        fprintf(stderr, "Could not accolate configfile\n");
        exit(1);
    }
    uhome = temp;
    strcat(uhome,configfile);
   
    FILE *fptr;

    if (   (fptr  = fopen(uhome, "w")) == NULL   ) {
        fprintf(stderr,"cannot open config file\n");
        exit(1);
    }
    printf("Config File = %s\n",uhome);
   
     if (argc < 2) {
        message(argv);
         exit(0);
     }

     for (i = 1; i < argc; i++) {
         if (*argv[i] != '-') {
             break;
         }
         if (argv[i][1] == 'd') {
             displayname = argv[++i];
         } else if (argv[i][1] == 'b') {
             bgcolor = argv[++i];
         } else if (argv[i][1] == 's') {
             //scale = 1;
             mode = 1;
         } else if (argv[i][1] == 'c') {
             //center = 1;
             mode = 2;
         } else if (argv[i][1] == 'f') {
             //fit = 1;
             mode = 3;
         } else if (argv[i][1] == 'm') {
             //mirror = 1;
             mode = 4;
         } else if (argv[i][1] == 'T') {
             ztime = atoi(argv[++i]);
             if(ztime < 30 ) {
                 ztime = 30;
             }
             ztimeflag = 1;
        }  else if (argv[i][1] == 'o' ) {
            fname = strdup(argv[++i]);
            ztimeflag = 1;
        }
         else if (argv[i][1] == 'x') {
             fprintf(stderr, "Debugging activated.\n");
             debug = 1;
         } else {
             fprintf(stderr, "%s:  Unrecognized option \'%c\'\n\n", *argv, argv[i][1]);
             message(argv);
             exit(2);
         }
     }
     if ( fname == NULL ) {
            fname = argv[i];
            image = imlib_load_image_without_cache (fname);
            set_image(image, argc, argv, displayname, fname, bgcolor, mode);
            fname = strdup(   imlib_image_get_filename () );
            fwrite(fname, strlen(fname)+1, 1, fptr);
            fflush(fptr);
        }

    char * ftemp = NULL;

    if (ztimeflag) {
        printf("Change Images time set at %d seconds\n", ztime);
        GetFiles(fname,0);
        prepare_filelist();

        image = check_image_err(Imbuff);
        set_image(image, argc, argv, displayname, fname, bgcolor, mode);
        fname = strdup(   imlib_image_get_filename () );
        fwrite(fname, strlen(fname)+1, 1, fptr);
        fflush(fptr);
           
        while (! sleep(ztime))
        {
            image = check_image_err(Imbuff);
            set_image(image, argc, argv, displayname, fname, bgcolor, mode);
            fname = strdup(   imlib_image_get_filename () );
            rewind(fptr);
            fwrite(fname, strlen(fname)+1, 1, fptr);
            fflush(fptr);
        }
    }
    fclose(fptr);
    free(fname);

     return 0;
}

as far between fwrite, fread, and fgets, and fputs, I do not work with files as such much, I only know if I am going to use fwrite, then I want to keep it in the family and use fread. I just don't want to play mix them up. because of end line and null and all of that which one does one thing and the otehr does not and trying to keep that all straight .

on my other laptop clang works in compiling it using the same commands and such, here on this laptop it does not.

Code:
userx@FreeBSD.net:/usr/home/userx/Downloads/mhsetroot-2.0
$ clang -I/usr/local/include filelist.c mhsetroot.c -o mhsetroot -lImlib2 -lX11
/usr/bin/ld: error: unable to find library -lImlib2
/usr/bin/ld: error: unable to find library -lX11
clang: error: linker command failed with exit code 1 (use -v to see invocation)

//
userx@FreeBSD.net:/usr/home/userx/Downloads/mhsetroot-2.0
$ gcc9 -I/usr/local/include filelist.c mhsetroot.c -o mhsetroot -lImlib2 -lX11

both running FreeBSD -p10.

Ok, you have memory leaking with fname. The reason is, you re-use it (that's ok) but you don't free it before doing so. That means the next time you use strdup() or copy into that variable you lose reference to the memory it held previously. If this program runs for a long time, you will see its memory grow and grow.

The solution is to free(fname) after it's used. The most opportune time seems to be after you've used fwrite() to write it to the file. So add free(fname) after every fwrite() involving fname.

Then remove free(fname) right before the "return 0" statement at the end.

As to your compilation issues, find where the libraries are and add an -L /usr/local/lib/ (or whatever the path is) to your compilation command.
ie. find /usr -name "*Imlib2*". You can also read up on ldconfig but in the interim, specifying -L on the command line will suffice once you find the libraries.


New:
I've not had time to go through your files, but some other issues are:
mylist *list_time_jump(mylist *l, int num)
You call it with num = 1, which means it will go through the loop. That means the initial test if() before the loop is redundant. I'm not sure if this is intended or not? If it is, perhaps wrap an if(num==0) {... return (ret); } around it. Believe me, it might be obvious to you what you're attempting to do NOW but in a year's time when you've discovered a bug, you will wish you did this and put comments in it.

I always find it good form to free lists (and their data structure elements) once finished with. It really doesn't matter though if the program runs forever and will only ever be killed by the kernel because it will clean-up anyway. It's just a habit you should get into.
 
Ok, you have memory leaking with fname. The reason is, you re-use it (that's ok) but you don't free it before doing so. That means the next time you use strdup() or copy into that variable you lose reference to the memory it held previously. If this program runs for a long time, you will see its memory grow and grow.

The solution is to free(fname) after it's used. The most opportune time seems to be after you've used fwrite() to write it to the file. So add free(fname) after every fwrite() involving fname.

Then remove free(fname) right before the "return 0" statement at the end.

As to your compilation issues, find where the libraries are and add an -L /usr/local/lib/ (or whatever the path is) to your compilation command.
ie. find /usr -name "*Imlib2*". You can also read up on ldconfig but in the interim, specifying -L on the command line will suffice once you find the libraries.
yeah I as just looking into that as far as I thought so on the leak part, as when i run it for a length of time it locks up my system, (in linux) I am / was just running it again in FreeBSD to see if I am getting the same thing.

and yeah thanks I forgot about the -L .. as I said in my other FBSD it did not do this to me.

this here works on this laptop/system.
Code:
clang -L/usr/local/lib  -I/usr/local/include filelist.c mhsetroot.c -o mhsetroot -lImlib2 -lX11
code:
C:
   if ( fname == NULL ) {
            fname = argv[i];
            image = imlib_load_image_without_cache (fname);
            set_image(image, argc, argv, displayname, fname, bgcolor, mode);
            fname = strdup(   imlib_image_get_filename () );
            //fwrite(fname, strlen(fname)+1, 1, fptr);
            fputs(fname,fptr);
            fflush(fptr);
            free(fname);
        }
   
    if (ztimeflag) {
        printf("Change Images time set at %d seconds\n", ztime);
        GetFiles(fname,0);
        prepare_filelist();
   
        image = check_image_err(Imbuff);
        set_image(image, argc, argv, displayname, fname, bgcolor, mode);
        fname = strdup(   imlib_image_get_filename () );
        //fwrite(fname, strlen(fname)+1, 1, fptr);
        fputs(fname,fptr);
        fflush(fptr);
        free(fname);
               
        while (! sleep(ztime))
        {
            image = check_image_err(Imbuff);
            set_image(image, argc, argv, displayname, fname, bgcolor, mode);
            fname = strdup(   imlib_image_get_filename () );
            rewind(fptr);
            //fwrite(fname, strlen(fname)+1, 1, fptr);
            fputs(fname,fptr);
            fflush(fptr);
            free(fname);
           
        }
    }
    fclose(fptr);
   
     return 0;
}

I changed it to fputs because of this.
Code:
//with fwrite
userx@FreeBSD.net:~
$ echo  $(cat mhsetrootimagefile)
bash: warning: command substitution: ignored null byte in input
/media/data/variety/wallhaven-papers/wallhaven-4g2xvd.jpg

//with fputs
userx@FreeBSD.net:~
$ echo  $(cat mhsetrootimagefile)
/media/data/variety/wallhaven-papers/wallhaven-4g5erd.jpg
thanks again.... !!
 
Back
Top