~israeldahl/jwm-settings-manager/trunk

« back to all changes in this revision

Viewing changes to src/common.cpp

  • Committer: Israel Dahl
  • Date: 2018-01-16 14:17:27 UTC
  • Revision ID: israeldahl@gmail.com-20180116141727-1eqofcuc0c5ukzdw
Update main branch to be current

* Windows
  - configure button actions
 * Desktop
   - file manager preferences
 * Themes
   - parse directories and ignore non-xml files

Show diffs side-by-side

added added

removed removed

Lines of Context:
178
178
         * @param filename the filename get the directory from
179
179
         */
180
180
        std::string get_directory_from_filename(std::string filename){
 
181
                filename=translate_home(filename);
181
182
                unsigned int finder=filename.rfind("/");
182
183
                if(finder<filename.length()){
183
184
                        filename=filename.erase(finder,std::string::npos);
186
187
                return filename;
187
188
        }
188
189
        /** use the get_gtk_item function to get an icon theme, or use hicolor if nothing is found*/    
189
 
        std::string get_gtk_icon_theme(){return get_gtk_item("icon","hicolor");}
 
190
        std::string get_gtk_icon_theme(){return get_gtk_themeitem("icon","hicolor");}
190
191
        /** get the gtk theme using the get_gtk_item function, or return Raleigh if nothing is found*/
191
 
        std::string get_gtk_widget_theme(){return get_gtk_item("gtk","Raleigh");}
 
192
        std::string get_gtk_widget_theme(){return get_gtk_themeitem("gtk","Raleigh");}
192
193
        /** get something from org.gnome.desktop.interface *-theme
193
194
         * @param itemToGet this will be icon OR gtk only
194
195
         * @param defaultTheme the default theme to return
195
196
         */
196
 
        std::string get_gtk_item(std::string itemToGet, std::string defaultTheme){
 
197
        std::string get_gtk_item(std::string itemToGet, std::string defaultItem){
 
198
                std::string temp=sed_i(itemToGet,"-","_");
 
199
                std::string tmpGTK="gtk-"+itemToGet;
 
200
                return get_gtk_item(itemToGet,temp,tmpGTK,tmpGTK,defaultItem);
 
201
        }
 
202
        std::string get_gtk_item(std::string itemToGet,std::string configItem, std::string defaultItem){
 
203
                std::string temp=sed_i(itemToGet,"-","_");
 
204
                return get_gtk_item(itemToGet,temp,configItem,configItem,defaultItem);
 
205
        }
 
206
        std::string get_gtk_item(std::string itemToGetgtk3,std::string itemToGetgtk2,std::string gtk3fileopt,std::string gtk2fileopt, std::string defaultItem){
 
207
                std::string gtkrc2_result, gtkrc3_result,gtk2;
 
208
                std::string GSETTINGS=term_out("which gsettings");
 
209
                std::string GCONF2=term_out("which gconftool-2");
 
210
                if(GSETTINGS.find("gsettings")<GSETTINGS.length()){
 
211
                        gtkrc3_result=term_out(GSETTINGS+" get org.gnome.desktop.interface "+itemToGetgtk3);
 
212
                        gtkrc3_result=remove_cruft(gtkrc3_result,"\'");
 
213
                        gtkrc3_result=remove_cruft(gtkrc3_result,"\'");
 
214
                        if(gtkrc3_result.compare("")!=0){return gtkrc3_result;}
 
215
                }
 
216
                if(itemToGetgtk2.compare("")!=0){
 
217
                        if(GCONF2.find("gconftool-2")<GCONF2.length()){
 
218
                                gtk2=term_out(GCONF2+" --get /desktop/gnome/interface/"+itemToGetgtk2);
 
219
                                if(gtk2.compare("")!=0){return gtk2;}
 
220
                        }
 
221
                }
 
222
                const char* home = getenv("HOME");
 
223
                if(home==NULL){return defaultItem;}
 
224
                std::string HOME=home;
 
225
                //CHECK/SET GTKRC FILES
 
226
                std::string GTKRC2=HOME + "/.gtkrc-2.0";
 
227
                const char* xdg_config_home=getenv("XDG_CONFIG_HOME");
 
228
                std::string XDG_CONFIG_HOME;
 
229
                if (xdg_config_home!=NULL){
 
230
                        XDG_CONFIG_HOME=xdg_config_home;
 
231
                }
 
232
                else{
 
233
                        XDG_CONFIG_HOME=HOME +"/.config";
 
234
                }
 
235
                if(gtk3fileopt.compare("")!=0){
 
236
                        std::string GTKRC3=XDG_CONFIG_HOME + "/gtk-3.0/settings.ini";
 
237
                        if(test_file(GTKRC3.c_str())){
 
238
                                gtkrc3_result=get_line_with_equal(GTKRC3,gtk3fileopt);
 
239
                                gtkrc3_result=remove_cruft(gtkrc3_result,gtk3fileopt);
 
240
                                gtkrc3_result=remove_cruft(gtkrc3_result,"\"");
 
241
                                gtkrc3_result=remove_cruft(gtkrc3_result,"\"");
 
242
                                if(gtkrc3_result.compare("")!=0){return gtkrc3_result;}
 
243
                        }
 
244
                }
 
245
                if(gtk2fileopt.compare("")!=0){
 
246
                        if(test_file(GTKRC2.c_str())){
 
247
                                gtkrc2_result=get_line_with_equal(GTKRC2,gtk2fileopt);
 
248
                                gtkrc2_result=remove_cruft(gtkrc2_result,gtk2fileopt);
 
249
                                gtkrc2_result=remove_cruft(gtkrc2_result,"\"");
 
250
                                gtkrc2_result=remove_cruft(gtkrc2_result,"\"");
 
251
                                if(gtkrc2_result.compare("")!=0){return gtkrc2_result;}
 
252
                        }
 
253
                }
 
254
                return defaultItem;
 
255
        }
 
256
        std::string get_gtk_themeitem(std::string itemToGet, std::string defaultTheme){
197
257
                std::string gtkrc2_result, gtkrc3_result,gtk2;
198
258
                if((itemToGet.compare("icon")!=0)&&(itemToGet.compare("")!=0)){
199
259
                        itemToGet="";
251
311
                }
252
312
                return defaultTheme;
253
313
        }
 
314
        /** this function gets the owner of the filename provided
 
315
         * @param filename
 
316
         */
 
317
        std::string get_file_owner(std::string filename){
 
318
                std::string result;
 
319
                struct stat info;
 
320
                if(stat(filename.c_str(), &info)==0){
 
321
                        struct passwd *pw = getpwuid(info.st_gid);
 
322
                        if(pw!=0){result=pw->pw_name;}
 
323
                }
 
324
                return result;
 
325
        }
 
326
        /** this function gets the group owner of the filename provided
 
327
         * @param filename
 
328
         */
 
329
        std::string get_file_group(std::string filename){
 
330
                std::string result;
 
331
                struct stat info;
 
332
                if(stat(filename.c_str(), &info)==0){
 
333
                        struct group  *gr = getgrgid(info.st_gid);
 
334
                        if(gr!=0){result=gr->gr_name;}
 
335
                }
 
336
                return result;
 
337
        }
254
338
        /** This function is used for files like *.desktop files to get a value
255
339
         * @param filename the filename to look in
256
340
         * @param line the line to look for
315
399
        }
316
400
        /** This function dereferences a symlink to the actual file*/
317
401
        std::string get_symlinkpath(std::string symlink){
318
 
                std::vector<char> buf(400);
319
 
                size_t len;
320
 
                do{
321
 
                        buf.resize(buf.size() + 100);
322
 
                        len = ::readlink(symlink.c_str(), &(buf[0]), buf.size());
323
 
                }
324
 
                while(buf.size() == len);
325
 
                if (len > 0){
326
 
                        buf[len] = '\0';
327
 
                        return (std::string(&(buf[0])));
328
 
                }
329
 
                echo_error("Error with symlink");
330
 
                return "";
 
402
                struct stat statinfo;
 
403
                if((lstat(symlink.c_str(), &statinfo)>0)){return symlink;}
 
404
                if ((!S_ISLNK (statinfo.st_mode) && statinfo.st_nlink > 1)
 
405
                ||
 
406
                (S_ISLNK (statinfo.st_mode))){
 
407
                        std::vector<char> buf(400);
 
408
                        size_t len;
 
409
                        do{
 
410
                                buf.resize(buf.size() + 100);
 
411
                                len = ::readlink(symlink.c_str(), &(buf[0]), buf.size());
 
412
                        }
 
413
                        while(buf.size() == len);
 
414
                        if (len > 0){
 
415
                                buf[len] = '\0';
 
416
                                return (std::string(&(buf[0])));
 
417
                        }
 
418
                }
 
419
                else{return symlink;}
 
420
                return symlink;
 
421
        }
 
422
        std::string get_user_name(){
 
423
                std::string temp;
 
424
                struct passwd *pw;
 
425
                uid_t uid;
 
426
                uid = geteuid ();
 
427
                pw = getpwuid (uid);
 
428
                if(pw!=0){temp=pw->pw_name;}
 
429
                return temp;
 
430
                
331
431
        }
332
432
    /** Return the FIRST match of the 'args' from a file
333
433
     * this is like  line=`grep -m 1 $args $filename`
668
768
                        closedir(mydir);
669
769
                }
670
770
                else{echo_error(dir+" cannot be opened\nMUST EXIT");}
671
 
                echo("Final Answer="+finalAnswer);
 
771
                if(finalAnswer.compare("")!=0)echo("Final Answer="+finalAnswer);
672
772
                return finalAnswer;
673
773
        }
674
774
        //R
822
922
                //echo_error("test_file_in_vector_path... found NO files");
823
923
                return "";
824
924
        }
 
925
        /** translate ~/ OR $HOME into full user's path to $HOME
 
926
         * @param pathORfilename any path or filename
 
927
         */
 
928
        std::string translate_home(std::string pathORfilename){
 
929
                std::string filename=pathORfilename;
 
930
                unsigned int tilde=filename.find("~/");
 
931
                if(tilde<filename.length()){
 
932
                        filename=filename.erase(0,tilde+1);
 
933
                        filename=home_path()+filename;
 
934
                }
 
935
                unsigned int HOMEVAR=filename.find("$HOME");
 
936
                if(HOMEVAR<filename.length()){
 
937
                        filename=filename.erase(0,HOMEVAR+1);
 
938
                        filename=home_path()+filename;
 
939
                }
 
940
                if(filename.compare("")==0)return pathORfilename;
 
941
                return filename;
 
942
        }
 
943
        std::string process_back_dir_in_filename(std::string filename){
 
944
                unsigned int finder = filename.find("../");
 
945
                std::string pwd=linuxcommon::current_directory();
 
946
                std::string tempPWD=pwd;
 
947
                std::string tempLoop=filename;
 
948
                int counter=3;
 
949
                echo_error("Initial process back dir="+tempLoop);
 
950
                while(finder<tempLoop.length()){
 
951
                        tempLoop=tempLoop.substr(finder+counter,std::string::npos);
 
952
                        unsigned int slash=tempPWD.rfind("/");
 
953
                        if(slash==tempPWD.length()-1){
 
954
                                tempPWD=tempPWD.substr(0,slash);
 
955
                                slash=tempPWD.rfind("/");
 
956
                        }
 
957
                        tempPWD=tempPWD.substr(0,slash);
 
958
                        echo_error("temploop="+tempLoop+"\ntempPWD"+tempPWD);
 
959
                        tempLoop=tempPWD+tempLoop;
 
960
                        if(tempLoop.compare("")!=0){filename=tempLoop;}
 
961
                        finder = tempLoop.find("../");
 
962
                }
 
963
                if(filename.find("/")>1){filename="/"+filename;}
 
964
                echo_error("returning:"+filename);
 
965
                return filename;
 
966
        }
 
967
        //P
 
968
        /** This will dereference symlinks as well as translate ~/ and $HOME as well as ../
 
969
         * @param filename the filename to process
 
970
         */
 
971
        std::string process_filename(std::string filename){
 
972
                echo("Processing="+filename);
 
973
                std::string tmp=process_back_dir_in_filename(filename);
 
974
                echo(tmp);
 
975
                std::string tmp2=get_symlinkpath(tmp);
 
976
                if(tmp2.compare("")!=0)tmp=tmp2;
 
977
                echo(tmp);
 
978
                std:: string tmp3=translate_home(tmp);
 
979
                if(tmp3.compare("")!=0)tmp=tmp3;
 
980
                echo(tmp);
 
981
                return tmp;
 
982
        }
825
983
        //Q
826
984
        /** change characters XML doesn't like into ones it does
827
985
         * @param input the string to modify
929
1087
        }
930
1088
        #endif
931
1089
        ///VECTOR FUNCTIONS/////////////////////////////////////////////////
 
1090
        /** get a vector list of all possible current system groups*/
 
1091
        std::vector <std::string> all_groups(){
 
1092
                std::vector <std::string> GROUPvEC;
 
1093
                struct group *groupsPtr = getgrent();
 
1094
                if(groupsPtr != NULL){
 
1095
                        for(groupsPtr = getgrent();groupsPtr!=NULL;groupsPtr = getgrent()){
 
1096
                                GROUPvEC.push_back(groupsPtr->gr_name);
 
1097
                        }
 
1098
                }
 
1099
                else{echo_error("No groups found on system!");}
 
1100
                setgrent();
 
1101
                endgrent();
 
1102
                return GROUPvEC;
 
1103
        }
 
1104
        /** get a vector list of all $USER groups */
 
1105
        std::vector <std::string> get_current_user_groups(){
 
1106
                std::vector <std::string> USERgroups;
 
1107
                std::string tmp=get_user_name();
 
1108
                if(tmp.compare("")!=0)return get_user_groups(tmp);
 
1109
                        const char* user=getenv("USER");
 
1110
                if(user==NULL){
 
1111
                        echo_error("FAILED getting user");
 
1112
                        return USERgroups;
 
1113
                }
 
1114
                return get_user_groups(user);
 
1115
        }
 
1116
        /** Get all actual users (UID > 1000) excluding nobody*/
 
1117
        std::vector <std::string> all_users(){
 
1118
                struct passwd *pw = getpwent();
 
1119
                std::vector <std::string> ALLusers;
 
1120
                if(pw != NULL){
 
1121
                        for(pw = getpwent();pw!=NULL;pw = getpwent()){
 
1122
                                std::string TMP=pw->pw_name;
 
1123
                                std::string shell=pw->pw_shell;
 
1124
                                if( (shell.compare("/usr/sbin/nologin")!=0)&&(shell.compare("/bin/false")!=0) ){
 
1125
                                        if(TMP.compare("sync")!=0){
 
1126
                                                ALLusers.push_back(TMP);
 
1127
                                        }
 
1128
                                }
 
1129
                        }
 
1130
                }
 
1131
                setpwent();
 
1132
                endpwent();
 
1133
                return ALLusers;
 
1134
        }
 
1135
        /** Get all the users including all processes*/
 
1136
        std::vector <std::string> all_users_including_processes(){
 
1137
                struct passwd *pw = getpwent();
 
1138
                std::vector <std::string> ALLusers;
 
1139
                if(pw != NULL){
 
1140
                        for(pw = getpwent();pw!=NULL;pw = getpwent()){
 
1141
                                std::string TMP=pw->pw_name;
 
1142
                                ALLusers.push_back(TMP);
 
1143
                        }
 
1144
                }
 
1145
                setpwent();
 
1146
                endpwent();
 
1147
                return ALLusers;
 
1148
        }
 
1149
        /** get a vector list of all USER's groups
 
1150
         * @param USER the user name to get the groups of
 
1151
         */
 
1152
        std::vector <std::string> get_user_groups(std::string USER){
 
1153
                std::vector <std::string> USERgroups;
 
1154
        //echo_error("Getting groups for:"+USER)l;
 
1155
                int ngroups=NGROUPS_MAX;
 
1156
                struct passwd *pw;
 
1157
                struct group *gr;
 
1158
                gid_t groups[NGROUPS_MAX];
 
1159
                pw = getpwnam(USER.c_str());
 
1160
                if(getgrouplist(USER.c_str(),pw->pw_gid,groups, &ngroups) == -1){
 
1161
                        echo_error("FAILED getting groups for:"+USER);
 
1162
                        return USERgroups;
 
1163
                }
 
1164
                for (int j = 0; j < ngroups; j++) {
 
1165
                        gr = getgrgid(groups[j]);
 
1166
                        if (gr != NULL)USERgroups.push_back(gr->gr_name);
 
1167
                }
 
1168
                return USERgroups;
 
1169
        }
932
1170
        /** turn a comma delimited string into vector, and join the current items to the vector sent in
933
1171
         * @param LINE the comma delimited line
934
1172
         * @param Vector the vector to append to (can be empty)
1199
1437
         * this returns save_string_to_file()
1200
1438
         */
1201
1439
        bool append_string_to_file(std::string STRING, std::string FILENAME){
 
1440
                if(!test_file(FILENAME))return false;
 
1441
                if(!file_is_writable(FILENAME))return false;
1202
1442
                std::string tmp=file_to_string(FILENAME);
1203
1443
                tmp+="\n";
1204
1444
                tmp+=STRING;
1212
1452
                std::string extention_check;
1213
1453
                std::transform(filename.begin(), filename.end(), filename.begin(), ::tolower);
1214
1454
                std::transform(extention.begin(), extention.end(), extention.begin(), ::tolower);
 
1455
                unsigned int period = extention.find(".");
 
1456
                //add period to extention
 
1457
                if(period != 0)
 
1458
                        extention="."+extention;
1215
1459
                unsigned int found=filename.find(extention);
1216
1460
                unsigned int ext_len=extention.length();
1217
1461
                unsigned int file_len=filename.length();
1220
1464
                }
1221
1465
                return false;
1222
1466
        }
 
1467
        bool file_is_readable(std::string filename){
 
1468
                if(!test_file(filename)){return false;}
 
1469
                struct stat info;
 
1470
                std::string file_owner,file_group;
 
1471
                if(stat(filename.c_str(), &info)==0){
 
1472
                        struct passwd *pw = getpwuid(info.st_uid);
 
1473
                        struct group  *gr = getgrgid(info.st_gid);
 
1474
                        if(pw!=0){file_owner=pw->pw_name;}
 
1475
                        if(gr!=0){file_group=gr->gr_name;}
 
1476
                        if(info.st_mode &  S_IROTH)return true;
 
1477
                        std::string user=get_user_name();
 
1478
                        if(file_owner.compare(user)==0){
 
1479
                                if(info.st_mode &  S_IRUSR)return true;
 
1480
                        }
 
1481
                        //TODO get group of user and check group permissions
 
1482
                }
 
1483
                return false;
 
1484
        }
 
1485
        bool file_is_writable(std::string filename){
 
1486
                if(!test_file(filename)){
 
1487
                        filename=get_directory_from_filename(filename);
 
1488
                }
 
1489
                struct stat info;
 
1490
                std::string file_owner,file_group;
 
1491
                if(stat(filename.c_str(), &info)==0){
 
1492
                        struct passwd *pw = getpwuid(info.st_uid);
 
1493
                        struct group  *gr = getgrgid(info.st_gid);
 
1494
                        if(pw!=0){file_owner=pw->pw_name;}
 
1495
                        if(gr!=0){file_group=gr->gr_name;}
 
1496
                        if(info.st_mode &  S_IWOTH)return true;
 
1497
                        std::string user=get_user_name();
 
1498
                        if(file_owner.compare(user)==0){
 
1499
                                if(info.st_mode &  S_IWUSR)return true;
 
1500
                        }
 
1501
                        //TODO get group of user and check group permissions
 
1502
                }
 
1503
                return false;
 
1504
        }
1223
1505
        /** see if a string exists in a vector
1224
1506
         * @param vector_to_check the vector to check
1225
1507
         * @param item_to_find the item to find
1320
1602
         * @param item (icon or gtk)
1321
1603
         * @param value the new value
1322
1604
         */
 
1605
        bool switch_gtk_setting(std::string item, std::string value){
 
1606
                std::string GTK2=sed_i(item,"-","_");
 
1607
                std::string tmpGTKconf="gtk-"+item;
 
1608
                std::string gtkrc2_result, gtkrc3_result,gtk2;
 
1609
                if(value.compare("")==0){return false;}
 
1610
                if((item.compare("")==0)){return false;}
 
1611
                int retval=0;
 
1612
                std::string GSETTINGS=term_out("which gsettings");
 
1613
                std::string GCONF2=term_out("which gconftool-2");
 
1614
                if(GSETTINGS.find("gsettings")<GSETTINGS.length()){
 
1615
                        retval=run_a_program(GSETTINGS+" set org.gnome.desktop.interface "+item+ " \"" +value+"\"");
 
1616
                        if(retval!=0){echo_error("Error setting the GSETTINGS theme");}
 
1617
                }
 
1618
                if(GCONF2.find("gconftool-2")<GCONF2.length()){
 
1619
                        retval=run_a_program(GCONF2+" --set --type string /desktop/gnome/interface/"+GTK2+" \""+value+"\"");
 
1620
                        if(retval!=0){echo_error("Error setting the GCONF2 theme");}
 
1621
                }
 
1622
                const char* home = getenv("HOME");
 
1623
                bool fileFix=true;
 
1624
                if(home==NULL){fileFix=false;}
 
1625
                else{
 
1626
                        std::string HOME=home;
 
1627
                //CHECK/SET GTKRC FILES
 
1628
                        std::string GTKRC2=HOME + "/.gtkrc-2.0";
 
1629
                        const char* xdg_config_home=getenv("XDG_CONFIG_HOME");
 
1630
                        std::string XDG_CONFIG_HOME;
 
1631
                        if (xdg_config_home!=NULL){XDG_CONFIG_HOME=xdg_config_home;}
 
1632
                        else{XDG_CONFIG_HOME=HOME +"/.config";}
 
1633
                        std::string GTKRC3=XDG_CONFIG_HOME + "/gtk-3.0/settings.ini";
 
1634
                        if(test_file(GTKRC3.c_str())){
 
1635
                                gtkrc3_result=get_line_with_equal(GTKRC3,tmpGTKconf);
 
1636
                                gtkrc3_result=remove_cruft(gtkrc3_result,tmpGTKconf);
 
1637
                                gtkrc3_result=remove_cruft(gtkrc3_result,"\"");
 
1638
                                gtkrc3_result=remove_cruft(gtkrc3_result,"\"");
 
1639
                                if(value.compare(gtkrc3_result)!=0){
 
1640
                                        fileFix=false;
 
1641
                                        std::string command="sed -i \"s#"+gtkrc3_result+"#"+value+"#\" "+GTKRC3;
 
1642
                                        //echo_error(command);
 
1643
                                        retval=run_a_program(command);
 
1644
                                        if(retval==0)fileFix=true;
 
1645
                                        //TODO don't use sed... actually do it :P
 
1646
                                }
 
1647
                        }
 
1648
                        if(test_file(GTKRC2.c_str())){
 
1649
                                gtkrc2_result=get_line_with_equal(GTKRC2,tmpGTKconf);
 
1650
                                gtkrc2_result=remove_cruft(gtkrc2_result,tmpGTKconf);
 
1651
                                gtkrc2_result=remove_cruft(gtkrc2_result,"\"");
 
1652
                                gtkrc2_result=remove_cruft(gtkrc2_result,"\"");
 
1653
                                if(value.compare(gtkrc2_result)!=0){
 
1654
                                //TODO
 
1655
                                        fileFix=false;
 
1656
                                        ///THIS
 
1657
                                        std::string command="sed -i \"s#"+gtkrc2_result+"#"+value+"#\" "+GTKRC2;
 
1658
                                        //echo_error(command);
 
1659
                                        retval=run_a_program(command);
 
1660
                                        if(retval==0)fileFix=true;
 
1661
                                }
 
1662
                        }
 
1663
                }
 
1664
                if((retval!=0)&&(!fileFix))return false;
 
1665
                return true;
 
1666
        }
1323
1667
        bool switch_gtk_item(std::string item, std::string value){
1324
1668
                std::string gtkrc2_result, gtkrc3_result,gtk2;
1325
1669
                if(value.compare("")==0){return false;}
1338
1682
                if(GCONF2.find("gconftool-2")<GCONF2.length()){
1339
1683
                        std::string temp="gtk";
1340
1684
                        if(item.compare("")!=0)temp=item;
1341
 
                        retval=run_a_program(GCONF2+" --get /desktop/gnome/interface/"+temp+"_theme");
 
1685
                        retval=run_a_program(GCONF2+" --set --type string /desktop/gnome/interface/"+temp+"_theme "+ value);
1342
1686
                        if(retval!=0){echo_error("Error setting the theme");}
1343
1687
                }
1344
1688
                const char* home = getenv("HOME");
1363
1707
                                gtkrc3_result=remove_cruft(gtkrc3_result,"\"");
1364
1708
                                if(value.compare(gtkrc3_result)!=0){
1365
1709
                                        fileFix=false;
1366
 
                                std::string command="sed -i s#"+gtkrc3_result+"#"+value+"# "+GTKRC3;
 
1710
                                std::string command="sed -i \"s#"+gtkrc3_result+"#"+value+"#\" "+GTKRC3;
1367
1711
                                retval=run_a_program(command);
1368
1712
                                if(retval==0)fileFix=true;
1369
1713
                                //TODO don't use sed... actually do it :P
1379
1723
                                if(value.compare(gtkrc2_result)!=0){
1380
1724
                                //TODO
1381
1725
                                        fileFix=false;
1382
 
                                std::string command="sed -i s#"+gtkrc2_result+"#"+value+"# "+GTKRC2;
 
1726
                                std::string command="sed -i \"s#"+gtkrc2_result+"#"+value+"#\" "+GTKRC2;
1383
1727
                                retval=run_a_program(command);
1384
1728
                                if(retval==0)fileFix=true;
1385
1729
                                }
1388
1732
                if((retval!=0)&&(!fileFix))return false;
1389
1733
                return true;
1390
1734
        }
 
1735
        /** switch a value out of a file in a Blank=thing setting.
 
1736
         * @param item  The ITEM=
 
1737
         * @param value what goes after the '=' (note... this can be blank and still succeed)
 
1738
         * @param filename the file to look in and overwrite
 
1739
         */
 
1740
        bool switch_equal_line_item(std::string item, std::string value,std::string filename){
 
1741
                if((item.compare("")==0) || (!test_file(filename)) || (!file_is_readable(filename)) || (!file_is_writable(filename)) ){return false;}
 
1742
                std::vector<std::string> fVec = file_to_vector(filename);
 
1743
                std::ifstream inputFileStream(filename.c_str(), std::ifstream::in);
 
1744
                std::string OUT,thisLine;
 
1745
                if(inputFileStream.is_open()){
 
1746
                        while (getline(inputFileStream,thisLine)){
 
1747
                                std::string LINE=thisLine;
 
1748
                                if(thisLine.find(item)<thisLine.length()){
 
1749
                                        unsigned int found =thisLine.find("=");
 
1750
                                        if(found < thisLine.length()){
 
1751
                                                LINE=item+"="+value;
 
1752
                                        }
 
1753
                                }
 
1754
                                if(OUT.compare("")!=0) OUT=OUT+"\n"+LINE;
 
1755
                                else OUT=LINE;
 
1756
                        }
 
1757
                }
 
1758
                if(OUT.compare("")!=0)return save_string_to_file(OUT,filename);
 
1759
                return false;
 
1760
        }
1391
1761
        /** test to see if a file exists
1392
1762
         * @param fileWithFullPATH the full path and filename to test
1393
1763
         */
1469
1839
         * @param num the number to convert
1470
1840
         */
1471
1841
        unsigned int convert_string_to_number(const char* num){
1472
 
                if(num==NULL){return 0;}
1473
 
                std::string empty="";
1474
 
                if(empty.compare(num)==0){return 0;}
1475
 
                std::stringstream out;
1476
 
                out << num;
1477
 
                unsigned int integer;
1478
 
                out >> integer;
1479
 
                return integer;
 
1842
                unsigned int NUM=0;
 
1843
                try{NUM=std::stoul(num);}
 
1844
                catch(std::invalid_argument e){return 0;}
 
1845
                catch(std::out_of_range e){return 0;}
 
1846
                return NUM;
1480
1847
        }
1481
1848
        /** count the items in the PATH environment variable*/
1482
1849
        unsigned int items_in_path(){
1546
1913
                }
1547
1914
        }
1548
1915
        #endif
 
1916
        unsigned int userUID(std::string user){
 
1917
                unsigned int ERROR=99999;
 
1918
                if(user.compare("")==0)return ERROR;
 
1919
                struct passwd *pw;
 
1920
                if(getpwnam(user.c_str())==NULL)return ERROR;
 
1921
                unsigned int UID=pw->pw_uid;
 
1922
                setpwent();
 
1923
                endpwent();
 
1924
                return UID;
 
1925
        }
 
1926
        unsigned int userGID(std::string user){
 
1927
                unsigned int ERROR=99999;
 
1928
                if(user.compare("")==0)return ERROR;
 
1929
                struct passwd *pw;
 
1930
                if(getpwnam(user.c_str())==NULL)return ERROR;
 
1931
                unsigned int UID=pw->pw_gid;
 
1932
                setpwent();
 
1933
                endpwent();
 
1934
                return UID;
 
1935
        }
 
1936
        unsigned int highest_user_UID(){
 
1937
                std::vector <std::string> ALLusers = all_users();
 
1938
                unsigned int TOTAL=0;
 
1939
                for( std::vector<std::string>::iterator itr = ALLusers.begin();
 
1940
                        itr!=ALLusers.end();
 
1941
                        ++itr){
 
1942
                        std::string tmpUser=*itr;
 
1943
                        unsigned int tmp=userUID(tmpUser);
 
1944
                        if(tmp>TOTAL)TOTAL=tmp;
 
1945
                }
 
1946
                return TOTAL;
 
1947
        }
1549
1948
        //INTEGER FUNCTIONS/////////////////////////////////////////////////
 
1949
        int convert_string_to_int(std::string num){
 
1950
                unsigned int NUM=0;
 
1951
                try{NUM=std::stoi(num);}
 
1952
                catch(std::invalid_argument e){return 0;}
 
1953
                catch(std::out_of_range e){return 0;}
 
1954
                return NUM;
 
1955
        }
1550
1956
        /** get a process ID from a name like 'pgrep'
1551
1957
         * @param  procName the process name to look for
1552
1958
         */