ttfqi.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. TrueType Fonts Quick Installer (aka "ttfqi") lets you quickly install TTF fonts
  3. on your computer!
  4. Version: 1.0
  5. Written by nokoru
  6. un-licensed, proudly free and open source work, see "LICENSE" <3
  7. */
  8. #include<stdio.h>
  9. #include<stdlib.h>
  10. #include<string.h>
  11. #include<unistd.h>
  12. typedef enum {
  13. CACHE_UPDATE, CACHE_IGNORE
  14. } cache_mode;
  15. int main(int argc, char **argv) {
  16. int c=0;
  17. cache_mode cm=CACHE_UPDATE;
  18. if (argc == 1) {
  19. printf("Error: No options! Use ttfqi -h for help.\n");
  20. return 1;
  21. }
  22. //read options
  23. while((c = getopt(argc, argv, "uih")) != -1) {
  24. switch(c) {
  25. case 'u':
  26. cm=CACHE_UPDATE;
  27. goto install;
  28. case 'i':
  29. cm=CACHE_IGNORE;
  30. goto install;
  31. case 'h':
  32. goto help;
  33. default:
  34. return 1;
  35. }
  36. }
  37. install:
  38. //check if root
  39. if (setuid(0) == -1) {
  40. printf("You must be root to do that.\n");
  41. return 1;
  42. }
  43. //check if no files
  44. if (argc <= 2) {
  45. printf("Error: No input files. Please type the filenames of the fonts you want to install.\n");
  46. return 1;
  47. }
  48. int i=0;
  49. //validate files
  50. printf("Checking files...\n");
  51. for(i=2; i<argc; i++) {
  52. //check if file exists
  53. if (access(argv[i], F_OK) == -1) {
  54. printf("Error: File \"%s\" doesn't exist! (Aborted.)\n", argv[i]);
  55. return 1;
  56. }
  57. //check if file is a TTF font
  58. char *ext;
  59. if ((ext = strrchr(argv[i], '.')) != NULL) {
  60. if (strcmp(ext, ".ttf") != 0 && strcmp(ext, ".TTF") != 0) {
  61. printf("Error: File \"%s\" is not a TrueType font! (Aborted.)\n", argv[i]);
  62. return 1;
  63. }
  64. } else {
  65. printf("Error: File \"%s\" is not a TrueType font! (Aborted.)\n", argv[i]);
  66. return 1;
  67. }
  68. }
  69. //Files ok, move font files to /usr/local/share/fonts/
  70. printf("Files OK. Moving fonts...\n");
  71. for(i=2; i<argc; i++) {
  72. char newfname[24 + strlen(argv[i])];
  73. sprintf(newfname,"/usr/local/share/fonts/%s", argv[i]);
  74. if (rename(argv[i], newfname) == -1) {
  75. printf("Error: Oops! Error while moving font files (Aborted.)\n");
  76. return 1;
  77. }
  78. }
  79. if (cm == CACHE_UPDATE) {
  80. printf("Installed %d font(s). Updating cache...\n", argc-2);
  81. system("fc-cache -f");
  82. printf("Cache updated.\n");
  83. printf("OK.\n");
  84. return 0;
  85. } else {
  86. printf("Installed %d font(s). Please reboot your system or use fc-cache -f\n", argc-2);
  87. printf("OK.\n");
  88. return 0;
  89. }
  90. help:
  91. printf("--- TrueType Fonts Quick Installer (aka ttfqi) ---\n");
  92. printf("Author: nokoru <nokoru@disroot.org> Version: 1.0\n");
  93. printf("\nUsage: ttfqi <options> [<file(s)>]\nOptions:\n");
  94. printf("-u\tInstall fonts and update font cache.\n-i\tInstall fonts without updating font cache (you'll need to reboot your system.)\n-h\tShow this information.\n\nttfqi is proudly FOSS and un-licensed; see LICENSE!.\n\nThank you for using ttfqi <3!\n");
  95. return 0;
  96. }