All pastes #906340 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-02-16 16:33 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/param.h>

int main() {
    FILE *fd;
    char buffer[BUFSIZ];
    char dest[PATH_MAX];
    char *dirfile = ".config/user-dirs.dirs";
    char *configfile = getenv("XDG_CONFIG_FILE");
    char *home = getenv("HOME");
    char picturesdir[PATH_MAX];

    if ( !configfile ) {
        if ( home ) {
            snprintf( dest, sizeof(dest), "%s/%s", home, dirfile);
        }
        else {
            printf( "no $HOME defined, bailing out!\n");
            return -1;
        }
        configfile = dest;
    }

    if ( ( fd = fopen( configfile, "r" ) ) < 0) {
        printf( "Failed to open %s\n", configfile );
        return -1;
    }

    while(!feof(fd)){

        fgets(buffer, sizeof(buffer), fd);

        if(strncmp(buffer, "XDG_PICTURES_DIR", sizeof("XDG_PICTURES_DIR")-1) == 0)
        {
            int relative = 0;
            int len =0;
            char *p;
            char *d;

            p = buffer;
            p += sizeof("XDG_PICTURES_DIR")-1;

            if (*p != '=')
                break;
            p++;

            if (*p != '"')
                break;
            p++;

            if (strncmp (p, "$HOME", 5) == 0) {
                p+=5;
                relative=1;
            }
            else if (*p != '/') {
                break;
            }

            d = strrchr (p, '"');
            if (!d)
                break;
            *d = 0;
            d = p;

            len = strlen(d);
            if (d[len-1] == '/')
                d[len-1] =0;

            if (relative) {
                snprintf( picturesdir, sizeof(picturesdir), "%s%s", home, d);
            }
            else
                snprintf( picturesdir, sizeof(picturesdir), "%s", d);
        }

    }
    if (strlen(picturesdir) >0) {
        printf ("XDG_PICTURES_DIR is: %s\n", picturesdir);
    }
    else {
        printf ("XDG_PICTURES_DIR not found or invalid!\n");
    }

    return 0;
}








~/.config/user-dirs.dirs
# This file is written by xdg-user-dirs-update
# If you want to change or add directories, just edit the line you're
# interested in. All local changes will be retained on the next run
# Format is XDG_xxx_DIR="$HOME/yyy", where yyy is a shell-escaped
# homedir-relative path, or XDG_xxx_DIR="/yyy", where /yyy is an
# absolute path. No other format is supported.
#
XDG_PICTURES_DIR="$HOME/Desktop"
XDG_DESKTOP_DIR="$HOME/Desktop"
XDG_DOWNLOAD_DIR="$HOME/Загрузки"
XDG_TEMPLATES_DIR="$HOME/Шаблоны"
XDG_PUBLICSHARE_DIR="$HOME/Общедоступные"
XDG_DOCUMENTS_DIR="$HOME/Документы"
XDG_MUSIC_DIR="$HOME/Музыка"
XDG_VIDEOS_DIR="$HOME/Видео"