Commits

Ragnara  committed 4c3fdc4

Added silent parameter (-s)

  • Participants
  • Parent commits 20d2c64

Comments (0)

Files changed (2)

File buildcounter.c

 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "buildno.h"
 
-const char *format = "#define BUILDNO %d\n";
+static const char *format = "#define BUILDNO %d\n";
 
-void print_usage(const char *argv0)
+static void print_usage(const char *argv0)
 {
-    fprintf(stderr, "Usage: %s <filename>\n", argv0);
+    fprintf(stderr, "Usage: %s [-s] <filename>\n", argv0);
+    fprintf(stderr, "       -s: Silent mode\n\n");
     fprintf(stderr, "Build: %d (%s)\n", BUILDNO, __DATE__);
+    fprintf(stderr, "https://bitbucket.org/Ragnara/buildcounter\n");
     exit(1);
 }
 
-void read_buffer(int *build, FILE *file)
+////////////////////////////////////////////////////////////////////////////////
+
+static char *parse_parameters(int argc, char **argv, _Bool *silent)
 {
-    fscanf(file, format, build);
+    if (argc < 2 || argc > 3)
+        print_usage(argv[0]);
+
+    *silent = 0;
+    int filename_index = 1;
+
+    if(argc == 3)
+    {
+        if (strcmp(argv[1], "-s"))
+            print_usage(argv[0]);
+        *silent = 1;
+        ++filename_index;
+    }
+
+    return argv[filename_index];
 }
 
+////////////////////////////////////////////////////////////////////////////////
+
 int main(int argc, char **argv)
 {
-    if (argc != 2)
-        print_usage(argv[0]);
+    _Bool silent = 0;
+    char *filename = parse_parameters(argc, argv, &silent);
 
     FILE *file;
     int lastbuild = 0;
 
-    if ((file = fopen(argv[1], "r")))
+    if ((file = fopen(filename, "r")))
     {
-        read_buffer(&lastbuild, file);
+        fscanf(file, format, &lastbuild);
         fclose(file);
     }
 
-    printf(format, ++lastbuild);
-
-    if ((file = fopen(argv[1], "w")))
+    if ((file = fopen(filename, "w")))
     {
-        fprintf(file, format, lastbuild);
+        fprintf(file, format, ++lastbuild);
         fclose(file);
+
+        if (!silent)
+            printf(format, lastbuild);
+
     }
     else
     {
-#define BUILDNO 7
+#define BUILDNO 10