1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#define STATUS_FILE "/var/lib/lowmem"
#define TEMPLATE_FILE_EXTENSION ".templates"
#define TEMPLATE_LINE_MAX_LENGTH 2048
#define DEFAULT_TO_REMOVE "Default-"
#define DESCRIPTION_TO_REMOVE "Description-"
#define CHOICES_TO_REMOVE "Choices-"
#define DEFAULT_TO_KEEP "Default-en"
#define DESCRIPTION_TO_KEEP "Description-en"
#define CHOICES_TO_KEEP "Choices-en"
#define FILENAME_LENGTH 256
int trimtemplate(char *filename) {
int ignore = 0;
FILE *fd = NULL;
FILE *tmpfd = NULL;
char template_line[TEMPLATE_LINE_MAX_LENGTH];
char tmpfilename[FILENAME_LENGTH];
if ((fd = fopen(filename, "r")) == NULL) {
perror("unable to open template file");
return 0;
}
snprintf(tmpfilename,
FILENAME_LENGTH,"%s.%s",
filename,"tmp");
if ((tmpfd = fopen(tmpfilename, "w")) == NULL) {
perror("unable to open temp file");
return 0;
}
ignore = 0;
/* parse template file */
while (fgets(template_line,
TEMPLATE_LINE_MAX_LENGTH, fd) != NULL) {
if (ignore &&
strstr(template_line, " ") == template_line) {
continue;
}
ignore = 0;
if (strstr(template_line, DESCRIPTION_TO_REMOVE)
|| strstr(template_line, CHOICES_TO_REMOVE)
|| strstr(template_line, DEFAULT_TO_REMOVE)
) {
if (strstr(template_line, DESCRIPTION_TO_KEEP)
|| strstr(template_line, CHOICES_TO_KEEP)
|| strstr(template_line, DEFAULT_TO_KEEP)
) {
ignore = 0;
continue;
} else {
ignore = 1;
continue;
}
}
if (fputs(template_line, tmpfd) == EOF) {
perror("unable to write to temp file");
return 0;
}
}
fclose(tmpfd);
fclose(fd);
if( rename(tmpfilename, filename) != 0) {
perror("unable to rename temp to template file");
return 0;
}
return 1;
}
int main(int argc, char** argv) {
struct stat buf;
if (argc < 2) {
return 0;
}
if ( stat(argv[1], &buf) == -1 ) {
perror("unable to get info about specified file or directory");
return 0;
}
/* test if lowmem is activated */
if( rename(STATUS_FILE, STATUS_FILE) != 0) {
return 0;
}
if (S_ISDIR(buf.st_mode)) {
DIR *dip = NULL;
struct dirent *dit = NULL;
char *dir = NULL;
char abs_path_file_name[FILENAME_LENGTH];
dir = argv[1];
if ((dip = opendir(dir)) == NULL) {
return 0;
}
while ((dit = readdir(dip)) != NULL) {
/* select only template files */
if (strstr(dit->d_name, TEMPLATE_FILE_EXTENSION) != NULL) {
snprintf(abs_path_file_name,
FILENAME_LENGTH,"%s/%s",dir,dit->d_name);
if (trimtemplate(abs_path_file_name) == 0) {
return 0;
}
}
}
if (closedir(dip) == -1) {
return 0;
}
return 1;
}
if (S_ISREG(buf.st_mode)) {
char *filename = NULL;
filename = argv[1];
if (trimtemplate(filename) == 0) {
return 0;
}
return 1;
}
return 0;
}
|