Creates new main.c with empty main boilerplate template

How to write a C Program to Creates new main.c with empty main boilerplate template in C Programming Language ?


Solution:

  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <stdbool.h>
  10. #include <inttypes.h>
  11. /**
  12.  * Prints an optional error message, and kills process.
  13.  * At the end of the error message, a newline is printed.
  14.  * If msg is a NULL pointer then nothing will be printed.
  15.  */
  16. void die(int status, const char *msg)
  17. {
  18.     // print error message
  19.     if (msg) {
  20.         fprintf(stderr, "%s: %s\n", "mkcmain", msg);
  21.     }
  22.     exit(status);
  23. }
  24. int main(void)
  25. {    
  26.     const char *comment_text =
  27.         "/***************************************************************************\n"
  28.         " * Created by mkcmain                                                      *\n"
  29.         " **************************************************************************/\n\n";
  30.     size_t      comment_text_size;
  31.     const char  *cmain_text = "int main(int argc, char *argv[])\n"
  32.                               "{\n\n"
  33.                               "\treturn 0;\n"
  34.                               "}\n";
  35.     size_t      cmain_text_size;
  36.     const char  *filename   = "main.c";
  37.     char        *cw_dir;
  38.     char        *filepath;
  39.     FILE        *file;
  40.     struct stat sb;
  41.     // attempt to get the current working dir
  42.     errno  = 0;
  43.     cw_dir = get_current_dir_name();
  44.     if(NULL == cw_dir) {
  45.         perror("get_current_dir_name()");
  46.         die(EXIT_FAILURE, "could not get current directory name");
  47.     }
  48.     // allocate memory for path
  49.     if (NULL == (filepath = malloc((strlen(cw_dir) + 1 + strlen(filename) + 1) * sizeof(char)))) {
  50.         die(EXIT_FAILURE, "could not allocate memory for file path name"); }
  51.     // construct file path
  52.     strcpy(filepath, cw_dir);
  53.     strcat(filepath, "/");
  54.     strcat(filepath, filename);
  55.     free(cw_dir); // just noticed this was a  memory leak .. havent tested yet
  56.     // check to see if file exists
  57.     if (0 == stat(filepath, &sb)) {
  58.         bool abort = false;
  59.         // see if this is a regular file
  60.         if (!(sb.st_mode & S_IFREG)) {
  61.             abort = true;
  62.             fprintf(stderr, "%s: %s is not a regular file\n", "mkcmain", filepath);
  63.         } else {
  64.             // prompt user to replace file
  65.             char response;
  66.             fprintf(stdout, "%s: %jd byte file %s exists. Replace file? [y/N?] ",
  67.                     "mkcmain", (intmax_t) sb.st_size, filepath);
  68.             response = fgetc(stdin);
  69.             if (response != 'y' && response != 'Y') {
  70.                 abort = true;
  71.             }
  72.         }
  73.         if (abort) {
  74.             free(filepath);
  75.             die(EXIT_FAILURE, "no file written");
  76.         }
  77.     }
  78.     // try to open file for writing
  79.     errno = 0;
  80.     if (NULL == (file = fopen(filepath, "w"))) {
  81.         perror("fopen");
  82.         fprintf(stderr, "%s: could not open %s for writing\n", "mkcmain", filepath);
  83.         free(filepath);
  84.         die(EXIT_FAILURE, NULL);
  85.     }
  86.     // try writing comment block
  87.     comment_text_size = strlen(comment_text);
  88.     if (fwrite(comment_text, sizeof(char), comment_text_size, file)
  89.             != comment_text_size * sizeof(char))
  90.     {
  91.         fprintf(stderr, "%s: could not write to open file %s\n", "mkcmain", filepath);
  92.         free(filepath);
  93.         die(EXIT_FAILURE, NULL);
  94.     }
  95.     // try to write C main file text to file
  96.     cmain_text_size = strlen(cmain_text);
  97.     if (fwrite(cmain_text, sizeof(char), cmain_text_size, file)
  98.             != cmain_text_size * sizeof(char))
  99.     {
  100.         fprintf(stderr, "%s: could not write to open file %s\n", "mkcmain", filepath);
  101.         free(filepath);
  102.         die(EXIT_FAILURE, NULL);
  103.     }
  104.     // try to close the file
  105.     errno = 0
  106.     if (fclose(file)) {
  107.         perror("fclose()");
  108.         free(filepath);
  109.         die(EXIT_FAILURE, "encountered error during attempt to close file");
  110.     }
  111.     fprintf(stdout, "%s: successfully created %s\n", "mkcmain", filepath);
  112.     free(filepath);
  113.     exit(EXIT_SUCCESS);
  114. }


Learn More :