File: trimtemplates.c

package info (click to toggle)
lowmem 1.52
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 112 kB
  • sloc: ansic: 113; sh: 49; makefile: 12
file content (145 lines) | stat: -rw-r--r-- 3,175 bytes parent folder | download | duplicates (11)
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
143
144
145
#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 10000 /* languagechooser/country-name is
					* very very long */
#define FILENAME_LENGTH 256
#define TAG_LENGTH 30

#define LANG_TO_KEEP "en"
char tags_to_remove[][TAG_LENGTH] = {
	"Default-",
	"Description-",
	"Choices-",
	"Indices-"
};

int trimtemplate(char *filename) {
     int ignore = 0;
     FILE *fd = NULL;
     FILE *tmpfd = NULL;
     char template_line[TEMPLATE_LINE_MAX_LENGTH];
     char tmpfilename[FILENAME_LENGTH];
     int i=0;
     int nbr_tags = sizeof(tags_to_remove)/TAG_LENGTH;
     char tag_to_keep[TAG_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 == 1 && 
	      strstr(template_line, " ") == template_line) {
	       continue;
	  }
	  ignore = 0;
	  for( i=0; i<nbr_tags && ignore == 0; i++ ) {
		  snprintf(tag_to_keep,
			   TAG_LENGTH,"%s%s",
			   tags_to_remove[i], LANG_TO_KEEP);

		  if( strstr(template_line, tag_to_keep) == NULL &&
		      strstr(template_line, tags_to_remove[i]) != NULL &&
		      strstr(template_line, "Choices-C:") == NULL ) {
			  ignore = 1;
		  }
	  }
	  if (ignore == 0 && 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;
}