Guest User

Untitled

a guest
Feb 20th, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. ## main.c
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <getopt.h>
  5. #include "main.h"
  6. #include "list.h"
  7.  
  8.  
  9. int objectx_abi = 1; /* Object X ABI ver */
  10. char *ox_app_name = "elf32.dlm.so"; /* Application Name */
  11. char *ox_app_auth = "The Object X Group"; /* Application Author */
  12.  
  13. void show_version() {
  14. fprintf(stdout, "%s version %d.%d%d\n", APP_NAME, APP_VER_MAJOR,
  15. APP_VER_MINOR_1, APP_VER_MINOR_2);
  16. return;
  17. }
  18.  
  19. void show_usage() {
  20.  
  21. fprintf(stdout,
  22. "%s- %s\n"
  23. "version %d.%d%d\n"
  24. "Usage: %s [options] rc_file\n"
  25. "\n"
  26. "Options: \n"
  27. " -f, --temp-file Specify the temporary file %s will create.\n"
  28. " -v, --version Displays %s version information. \n"
  29. " -h, --help Displays this message. \n"
  30. " \n"
  31. " \n"
  32. " %s is a part of the Object X compiler suite, created by Robert Butler. \n"
  33. " \n"
  34. " DISCLAIMER- \n"
  35. " The Object X compiler suite is partially based from the GNU C Compiler \n"
  36. " Suite. This compiler suite is currently maintained by Robert M. Butler.\n"
  37. " Please email bug reports to the Object X build team <build@objectx.org>\n"
  38. " \n",
  39. APP_NAME, APP_DESC, APP_VER_MAJOR, APP_VER_MINOR_1, APP_VER_MINOR_2,APP_NAME,
  40. APP_NAME, APP_NAME, APP_NAME );
  41.  
  42. return;
  43. }
  44.  
  45. int main( int argc, char *argv[] ) {
  46.  
  47. FILE *handle;
  48. static char *rc_file, *tmp_file;
  49. int opt, option, arg_count;
  50. static struct option long_options[] = {
  51. /* Name argument flag value */
  52. { "help", no_argument, NULL, 'h' },
  53. { "temp-file", required_argument, &tmp_file, 'f' },
  54. { "version", no_argument, NULL, 'v' },
  55. { 0, 0, 0, 0 }
  56. };
  57.  
  58. while ((opt = getopt_long( argc, argv, "hvf:o:012",
  59. long_options,&option)) != -1) {
  60.  
  61. switch (opt) {
  62. case 'h':
  63. show_usage();
  64. return 0;
  65. break;
  66. case 'v':
  67. show_version();
  68. return 0;
  69. break;
  70. case 'f':
  71. fprintf(stdout, "%s[%d]: %s\n", __FILE__,__LINE__-1,rc_file);
  72. break;
  73. case '?':
  74. show_usage();
  75. return 0;
  76. break;
  77. case ':':
  78. fprintf(stdout, "%s[%d]: Option `%c' is missing a value.\n",
  79. __FILE__, __LINE__, opt);
  80. show_usage();
  81. return 0;
  82. break;
  83. }
  84. }
  85.  
  86. fprintf(stdout, "%s[%d]: %s\n", __FILE__,__LINE__-1,rc_file);
  87. fprintf(stdout, "%s[%d]: Resource script filename: %s \n",
  88. __FILE__, __LINE__, argv[ argc-1 ]);
  89.  
  90. struct version e_version[] = {
  91. { "Application Name", ox_app_name, sizeof( ox_app_name ) },
  92. { "Application Vendor",ox_app_auth, sizeof( ox_app_auth ) },
  93. { "Object X Version", &objectx_abi, sizeof( objectx_abi ) },
  94. { NULL, NULL, 0 }
  95. };
  96.  
  97. if ((handle=file_open(argv[argc-1]))==FALSE) {
  98. printf("%s[%d]: Failed to open %s.\n",
  99. __FILE__, __LINE__-1, argv[1] );
  100. return -1;
  101. }
  102.  
  103. int x; struct verList *list, *head, *lp;
  104. for (x=0; e_version[x].e_name != NULL; x++) {
  105.  
  106. if (x < 1) {
  107. head = list_create(e_version[0]);
  108. list = head;
  109. }
  110. else {
  111. list = listItem_add(list, e_version[x]);
  112. }
  113. }
  114.  
  115. lp = head;
  116. while (lp != NULL)
  117. {
  118. if (do_serialize(handle, lp) != 0)
  119. {
  120. // Abort, abort, abort!
  121. break;
  122. }
  123. }
  124.  
  125.  
  126. fclose( handle );
  127. }
  128.  
  129. ## main.h
  130. #ifndef _OBJECTX_MAIN
  131. #define _OBJECTX_MAIN
  132. #include <fcntl.h>
  133. #include <stdio.h>
  134. #include <stdlib.h>
  135. #include <sys/stat.h>
  136. #include <sys/types.h>
  137. #include <linux/errno.h>
  138. #include <error.h>
  139.  
  140. int errno;
  141. #define FALSE -1
  142. #define TRUE 0
  143. #define BOOL int
  144.  
  145. #define APP_NAME "rvc"
  146. #define APP_DESC "The Object X resource version compile and linker"
  147. #define APP_VER_MAJOR 1
  148. #define APP_VER_MINOR_1 0
  149. #define APP_VER_MINOR_2 0
  150.  
  151.  
  152. struct version {
  153. char *e_name;
  154. void *e_data;
  155. unsigned int e_size;
  156. };
  157.  
  158. #endif // _OBJECTX_MAIN
  159.  
  160. ## type.h
  161. #ifndef _TYPE_H
  162. #define _TYPE_H
  163. #define FALSE -1
  164. #define TRUE 0
  165. #define BOOL int
  166. #endif // _TYPE_H
  167.  
  168. ## file.c
  169. #include "main.h"
  170. #include "list.h"
  171. #include "type.h"
  172.  
  173. BOOL file_write(FILE *handle, struct verList *root, int size)
  174. {
  175. // Write our records
  176. struct verList *list;
  177. list = root;
  178. while (list != 0) {
  179. struct version *cur_record = (struct version*)list->e_data;
  180. do_serialize(handle, cur_record);
  181. list = list->next;
  182. }
  183. }
  184.  
  185. FILE *file_open(char *path)
  186. {
  187. // Open our file
  188. FILE *handle;
  189. if (0 == (handle=fopen(path, "wb+")))
  190. {
  191. printf("%s[%d]: Failed to open %s.\n", __FILE__, __LINE__-1, APP_NAME );
  192. return -1;
  193. }
  194.  
  195. return handle;
  196. }
  197.  
  198. ## list.c
  199. #include "main.h"
  200. #include "list.h"
  201. #include "type.h"
  202.  
  203. int do_serialize(FILE *handle, struct verList **segment) {
  204. if ( NULL == segment )
  205. return -1;
  206.  
  207. if ( NULL == (*segment)->e_name )
  208. return -1;
  209.  
  210. if ( NULL == (*segment)->e_data )
  211. return -1;
  212.  
  213. if ( NULL == (*segment)->e_size )
  214. return -1;
  215.  
  216. char *name = (*segment)->e_name;
  217. char *value = (*segment)->e_data;
  218. int *e_tmp = (*segment)->e_size;
  219.  
  220. char *d_tmp = value;
  221. char *s_tmp = name;
  222.  
  223. int val_len= 0, str_len= 0;
  224. while ( *d_tmp++ != '\0') val_len++;
  225. while ( *s_tmp++ != '\0') str_len++;
  226.  
  227. int ret;
  228.  
  229. if (ret = (fwrite(name, str_len, 1, handle)) != 1)
  230. return -1;
  231.  
  232. if (ret = (fwrite(value, val_len, 1, handle)) != 1)
  233. return -1;
  234.  
  235. if (ret = (fwrite(e_tmp,sizeof(int),1,handle)) != 1)
  236. return -1;
  237.  
  238. segment = segment->next;
  239. return 0;
  240. }
  241.  
  242. struct verList* list_create(void *data) {
  243. struct verList *root = (struct verList*)malloc( sizeof( struct verList ));
  244. root->next= 0;
  245. root->e_data= data;
  246.  
  247. return root;
  248. }
  249.  
  250. void list_destroy(struct verList* root) {
  251. struct verList *cur = root;
  252. struct verList *list = root;
  253.  
  254. while (cur != 0) {
  255. list = cur;
  256. cur = cur->next;
  257. free(list);
  258. list = 0;
  259. }
  260. }
  261.  
  262. struct verList* listItem_add(struct verList *list, void *data) {
  263. if (0 == list)
  264. return 0;
  265.  
  266. struct verList *listItem = (struct verList*)malloc(sizeof(struct verList));
  267.  
  268. list->next = listItem;
  269. listItem->next = 0;
  270. listItem->e_data = data;
  271. //listItem->e_size = Before->e_size;
  272.  
  273. return listItem;
  274. }
  275.  
  276. struct verList* listItem_Insert(struct verList *list, void *data) {
  277. if (0 == list)
  278. return 0;
  279.  
  280. struct verList *listItem = (struct verList*)malloc(sizeof(struct verList));
  281.  
  282. listItem->e_data = data;
  283. listItem->next = list->next;
  284. list->next = listItem;
  285.  
  286. return listItem;
  287. }
  288.  
  289. ## list.h
  290. #ifndef _LIST_H
  291. #define _LIST_H
  292. struct verList {
  293. struct verList *next;
  294.  
  295. char *e_name;
  296. void *e_data;
  297. unsigned int e_size;
  298. };
  299.  
  300. /* Methods */
  301. struct verList *create_version_table( void *list );
  302. void destroy_version_table( struct verLIst *root );
  303. struct verList *do_version_insert( struct verList *list, void *e_data );
  304. struct verList *do_version_create( struct verList *list, void *e_data );
  305. #endif // _LIST_H
Add Comment
Please, Sign In to add comment