number example
authorThomas Walker Lynch <thomas.lynch@reasoningtechnology.com>
Sun, 24 Feb 2019 01:37:01 +0000 (02:37 +0100)
committerThomas Walker Lynch <thomas.lynch@reasoningtechnology.com>
Sun, 24 Feb 2019 01:37:01 +0000 (02:37 +0100)
22 files changed:
src/1_try/mh_main_prob/command1.c [new file with mode: 0644]
src/1_try/mh_main_prob/command2.c [new file with mode: 0644]
src/1_try/mh_main_prob/just_fun.c [new file with mode: 0644]
src/1_try/mh_main_prob/transcript1.txt [new file with mode: 0644]
src/1_try/mh_main_prob/transcript2.txt [new file with mode: 0644]
src/1_try/mh_main_prob/transcript3.txt [new file with mode: 0644]
src/3_documents/README.txt [new file with mode: 0644]
src/5_scratch/dispatch_f.lib.h
src/5_scratch/subu-config.lib.h
src/5_scratch/subu-init.cli.h
src/5_scratch/subu-mk-0.cli.h
src/5_scratch/subu-mk-0.lib.h
src/5_scratch/subu-number.cli.h
src/README.txt [deleted file]
src/dispatch_f.lib.c
src/makefile
src/subu-config.lib.c
src/subu-init.cli.c
src/subu-mk-0.cli.c
src/subu-mk-0.lib.c
src/subu-number.cli.c
src/subu_init [deleted file]

diff --git a/src/1_try/mh_main_prob/command1.c b/src/1_try/mh_main_prob/command1.c
new file mode 100644 (file)
index 0000000..637df55
--- /dev/null
@@ -0,0 +1,6 @@
+#include "command1.h"
+#include <stdio.h>
+int main(int argc, char **argv){
+  printf("command1 %d\n", f());
+  return 0;
+}
diff --git a/src/1_try/mh_main_prob/command2.c b/src/1_try/mh_main_prob/command2.c
new file mode 100644 (file)
index 0000000..5d8c612
--- /dev/null
@@ -0,0 +1,6 @@
+#include "command2.h"
+#include <stdio.h>
+int main(int argc, char **argv){
+  printf("command2 %d\n", f() + argc);
+  return 0;
+}
diff --git a/src/1_try/mh_main_prob/just_fun.c b/src/1_try/mh_main_prob/just_fun.c
new file mode 100644 (file)
index 0000000..67625f4
--- /dev/null
@@ -0,0 +1,4 @@
+#include "just_fun.h"
+int f(){
+  return 5;
+}
diff --git a/src/1_try/mh_main_prob/transcript1.txt b/src/1_try/mh_main_prob/transcript1.txt
new file mode 100644 (file)
index 0000000..c5511fe
--- /dev/null
@@ -0,0 +1,36 @@
+Various commmand files each with its own main for testing a library.  makeheaders gets confused and puts all the
+declarations in the headers, leading to a failure.
+
+
+> ls
+command1.c  command2.c just_fun.c
+> cat just_fun.c
+#include "just_fun.h"
+int f(){
+  return 5;
+}
+> cat command1.c
+#include "command1.h"
+#include <stdio.h>
+int main(){
+  printf("command1 %d\n", f());
+  return 0;
+}
+> cat command2.c
+#include "command2.h"
+#include <stdio.h>
+int main(int argc, char **argv){
+  printf("command2 %d\n", f() + argc);
+  return 0;
+}
+> makeheaders *.c
+> gcc -o command1 command1.c
+command1.c: In function ‘main’:
+command1.c:3:1: error: number of arguments doesn’t match prototype
+ int main(){
+ ^~~
+In file included from command1.c:1:
+command1.h:5:5: error: prototype declaration
+ int main(int argc,char **argv);
+     ^~~~
+> 
diff --git a/src/1_try/mh_main_prob/transcript2.txt b/src/1_try/mh_main_prob/transcript2.txt
new file mode 100644 (file)
index 0000000..77ec819
--- /dev/null
@@ -0,0 +1,20 @@
+Making each main call static so it won't be in the header.  gcc can't find main.
+
+> cat command1.c
+#include "command1.h"
+#include <stdio.h>
+static int main(){
+  printf("command1 %d\n", f());
+  return 0;
+}
+> cat command2.c
+#include "command2.h"
+#include <stdio.h>
+static int main(int argc, char **argv){
+  printf("command2 %d\n", f() + argc);
+  return 0;
+}
+> gcc -o command1 command1.c just_fun.c
+/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o: in function `_start':
+(.text+0x24): undefined reference to `main'
+collect2: error: ld returned 1 exit status
diff --git a/src/1_try/mh_main_prob/transcript3.txt b/src/1_try/mh_main_prob/transcript3.txt
new file mode 100644 (file)
index 0000000..b3a00b7
--- /dev/null
@@ -0,0 +1,27 @@
+This time making each main definition have the same prototype.  Still end up with multiple main declarations,
+it is just that they agree.
+
+> rm *.h
+> makeheaders *.c
+> cat command1.c
+#include "command1.h"
+#include <stdio.h>
+int main(int argc, char **argv){
+  printf("command1 %d\n", f());
+  return 0;
+}
+> cat command1.h
+/* \aThis file was automatically generated.  Do not edit! */
+#undef INTERFACE
+int f();
+int main(int argc,char **argv);
+int main(int argc,char **argv);
+> cat command2.c
+#include "command2.h"
+#include <stdio.h>
+int main(int argc, char **argv){
+  printf("command2 %d\n", f() + argc);
+  return 0;
+}
+> gcc -o command1 command1.c just_fun.c
+>  .. worked
diff --git a/src/3_documents/README.txt b/src/3_documents/README.txt
new file mode 100644 (file)
index 0000000..a9a2b21
--- /dev/null
@@ -0,0 +1,23 @@
+
+
+filename.tag.extension
+
+extension: 
+  .c for C source
+  .cc for C++ source
+  .h for C header file
+  .hh for C++ header file
+  .o an object file
+
+tag:
+  .lib. The resulting .o file to be placed in release library and is part of the
+        programming interface.
+  .aux. The resulting.o file not directly part of the programming interface, but
+        it might be called by functions that are.
+  .cli. The source file has a main call and is to be relased as part of the command line interface
+  .loc. file has a main call to be made into a local uitlity function
+
+We carry the source file tag and extension to the .o file.  We do not put tags
+nor extensions on command line executables.
+
+local_common.h should be included in all source files
index 994592d..85eaf80 100644 (file)
@@ -1,5 +1,11 @@
 /* \aThis file was automatically generated.  Do not edit! */
 #undef INTERFACE
+#include <sys/types.h>
+#include <unistd.h>
 int dispatch_f_euid_egid(char *fname,int(*f)(void *arg),void *f_arg,uid_t euid,gid_t egid);
 int dbprintf(const char *format,...);
 int dispatch_f(char *fname,int(*f)(void *arg),void *f_arg);
+#define ERR_DISPATCH_F_SETEGID 3
+#define ERR_DISPATCH_F_SETEUID 2
+#define ERR_DISPATCH_F_FORK 1
+#define INTERFACE 0
index c359b52..a63786d 100644 (file)
@@ -2,8 +2,9 @@
 #undef INTERFACE
 #include <sqlite3.h>
 typedef unsigned int uint;
-int subu_number(sqlite3 *db,uint **subu_number);
+int subu_number(sqlite3 *db,uint *subu_number);
 int schema(sqlite3 *db,uint max_subu_number);
 extern char config_file[];
+extern char config_file[];
 #define ERR_CONFIG_FILE -1
 #define INTERFACE 0
index 46bfd20..eff6c3f 100644 (file)
@@ -5,6 +5,7 @@
 typedef unsigned int uint;
 int schema(sqlite3 *db,uint max_subu_number);
 extern char config_file[];
-int main();
-int main();
-int main(int argc,char **argv,char **env);
+extern char config_file[];
+int main(int argc,char **argv,char **envp);
+int main(int argc,char **argv,char **envp);
+int main(int argc,char **argv,char **envp);
index 075b350..9c73c68 100644 (file)
@@ -2,7 +2,9 @@
 #undef INTERFACE
 #include <sqlite3.h>
 extern char config_file[];
+extern char config_file[];
 int subu_mk_0(char *subuname,char *config_file);
-int main();
-int main();
-int main(int argc,char **argv,char **env);
+#define ERR_SUBU_MK_0_ARG_CNT 1
+int main(int argc,char **argv,char **envp);
+int main(int argc,char **argv,char **envp);
+int main(int argc,char **argv,char **envp);
index 82c7e14..76a337a 100644 (file)
@@ -10,20 +10,23 @@ struct dispatch_useradd_ret_t {
   struct passwd *pw_record;  
 };
 struct dispatch_useradd_ret_t dispatch_useradd(char **argv,char **envp);
+#include <unistd.h>
 int dispatch_f_euid_egid(char *fname,int(*f)(void *arg),void *f_arg,uid_t euid,gid_t egid);
 int dbprintf(const char *format,...);
 #include <sqlite3.h>
 extern char config_file[];
+extern char config_file[];
 int subu_mk_0(char *subuname,char *config_file);
 int masteru_makes_subuhome(void *arg);
 int allowed_subuname(char *subuname);
-#define ERR_SUBU_MK_0_SETFACL 9
-#define ERR_SUBU_MK_0_FAILED_USERADD 8
-#define ERR_SUBU_MK_0_BUG_SSS 7
-#define ERR_SUBU_MK_0_FAILED_MKDIR_SUBU 6
-#define ERR_SUBU_MK_0_MK_SUBUHOME 5
-#define ERR_SUBU_MK_0_MALLOC 4
-#define ERR_SUBU_MK_0_BAD_MASTERU_HOME 3
-#define ERR_SUBU_MK_0_SETUID_ROOT 2
-#define ERR_SUBU_MK_0_CONFIG_FILE 1
+#define ERR_SUBU_MK_0_SETFACL 10
+#define ERR_SUBU_MK_0_FAILED_USERADD 9
+#define ERR_SUBU_MK_0_BUG_SSS 8
+#define ERR_SUBU_MK_0_FAILED_MKDIR_SUBU 7
+#define ERR_SUBU_MK_0_MK_SUBUHOME 6
+#define ERR_SUBU_MK_0_MALLOC 5
+#define ERR_SUBU_MK_0_BAD_MASTERU_HOME 4
+#define ERR_SUBU_MK_0_SETUID_ROOT 3
+#define ERR_SUBU_MK_0_CONFIG_FILE 2
+#define ERR_SUBU_MK_0_ARG_CNT 1
 #define INTERFACE 0
index 759d808..91145ce 100644 (file)
@@ -2,9 +2,10 @@
 #undef INTERFACE
 #include <sqlite3.h>
 typedef unsigned int uint;
-int subu_number(sqlite3 *db,uint **subu_number);
+int subu_number(sqlite3 *db,uint *subu_number);
 #define ERR_CONFIG_FILE -1
 extern char config_file[];
-int main();
-int main();
-int main(int argc,char **argv,char **env);
+extern char config_file[];
+int main(int argc,char **argv,char **envp);
+int main(int argc,char **argv,char **envp);
+int main(int argc,char **argv,char **envp);
diff --git a/src/README.txt b/src/README.txt
deleted file mode 100644 (file)
index a9a2b21..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-
-
-filename.tag.extension
-
-extension: 
-  .c for C source
-  .cc for C++ source
-  .h for C header file
-  .hh for C++ header file
-  .o an object file
-
-tag:
-  .lib. The resulting .o file to be placed in release library and is part of the
-        programming interface.
-  .aux. The resulting.o file not directly part of the programming interface, but
-        it might be called by functions that are.
-  .cli. The source file has a main call and is to be relased as part of the command line interface
-  .loc. file has a main call to be made into a local uitlity function
-
-We carry the source file tag and extension to the .o file.  We do not put tags
-nor extensions on command line executables.
-
-local_common.h should be included in all source files
index cfa75a8..7edf41b 100644 (file)
  command passed in, and wants better behavior, he or she can spin a special
  version of dispatch for that command.
 */
+#define _GNU_SOURCE
 
 #include "dispatch_f.lib.h"
+// we need the declaration for uid_t etc.
+#if INTERFACE
+#include <sys/types.h>
+#include <unistd.h>
+#define ERR_DISPATCH_F_FORK 1
+#define ERR_DISPATCH_F_SETEUID 2
+#define ERR_DISPATCH_F_SETEGID 3
+#endif
 
 // without this #define execvpe is undefined
 #define _GNU_SOURCE   
 
-#include <sys/types.h>
-#include <unistd.h>
 #include <wait.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -32,7 +39,7 @@ int dispatch_f(char *fname, int (*f)(void *arg), void *f_arg){
   if( pid == -1 ){
     perror(perror_src);
     fprintf(stderr, "%s %s\n", perror_src, fname);
-    return ERR_FORK;
+    return ERR_DISPATCH_F_FORK;
   }
   if( pid == 0 ){ // we are the child
     int status = (*f)(f_arg);
@@ -53,18 +60,18 @@ int dispatch_f_euid_egid(char *fname, int (*f)(void *arg), void *f_arg, uid_t eu
   if( pid == -1 ){
     perror(perror_src);
     fprintf(stderr, "%s %s %u %u\n", perror_src, fname, euid, egid);
-    return ERR_FORK;
+    return ERR_DISPATCH_F_FORK;
   }
   if( pid == 0 ){ // we are the child
     if( seteuid(euid) == -1 ){
       perror(perror_src);
       fprintf(stderr, "%s %s %u %u\n", perror_src, fname, euid, egid);
-      return ERR_SETEUID;
+      return ERR_DISPATCH_F_SETEUID;
     }
     if( setegid(egid) == -1 ){
       perror(perror_src);
       fprintf(stderr, "%s %s %u %u\n", perror_src, fname, euid, egid);
-      return ERR_SETEGID;
+      return ERR_DISPATCH_F_SETEGID;
     }
     int status = (*f)(f_arg);
     exit(status);
index ddfcfdd..030a9f7 100755 (executable)
@@ -16,10 +16,9 @@ ECHO= echo
 SHELL=/bin/bash
 SCRATCHDIR= 5_scratch # clean and others put things here
 CC=gcc
-CFLAGS=-std=c11 -fPIC -I. -ggdb -DDEBUG
+CFLAGS=-std=c11 -fPIC -I. -ggdb -DDEBUG -Werror
 LIB="libsubu.a"
-LIBPATH="." #no trailing slash
-LINKFLAGS="-L. -lsubu"
+LINKFLAGS=-L. -lsubu  -lsqlite3
 
 #these are the source files that exist
 SOURCES_LIB= $(wildcard *.lib.c)
@@ -46,7 +45,6 @@ version:
        @echo "CC: " $(CC)
        @echo "CFLAGS: " $(CFLAGS)
        @echo "LIB: " $(LIB)
-       @echo "LIBPATH: " $(LIBPATH)
        @echo "LINKFLAGS: " $(LINKFLAGS)
        @echo '______end make $@_____'
 
@@ -71,7 +69,7 @@ deps:
        $(CC) $(CFLAGS) -MM $(SOURCES) 1> 2_makefile_deps
        for i in $(EXECS) ; do\
            $(ECHO) >> 2_makefile_deps;\
-           $(ECHO) "$$i : $$i.cli.o $(LIBPATH)/$(LIB)" >> 2_makefile_deps;\
+           $(ECHO) "$$i : $$i.cli.o $(LIB)" >> 2_makefile_deps;\
            $(ECHO) "   $(CXX) -o $$i $$i.cli.o $(LINKFLAGS)" >> 2_makefile_deps;\
         done
        @echo '______end make $@_____'
@@ -86,7 +84,7 @@ lib:
 sub_lib: $(LIB)
 
 
-execs: $(LIBPATH)/$(LIB)
+execs: $(LIB)
        @echo '---- make $@:------------------------------------------------------------'
        @echo `pwd`'>' 
        if [ ! -e 2_makefile_deps ]; then make deps; fi
@@ -110,6 +108,7 @@ install: all
 clean:
        @echo '---- make $@:------------------------------------------------------------'
        @echo `pwd`'>' 
+       if [ -f subu.db ]; then rm subu.db; fi
        for i in $(wildcard *~); do mv $$i $(SCRATCHDIR); done
        for i in $(wildcard *.lib.o) $(wildcard *.cli.o); do rm $$i; done 
        for i in $(HFILES); do mv $$i 5_scratch; done # just in case someone wrote a header file
index 8706aaf..525e195 100644 (file)
@@ -50,28 +50,30 @@ int schema(sqlite3 *db, uint max_subu_number){
   return sqlite3_exec(db, sql, NULL, NULL, NULL);
 }
 
-int subu_number(sqlite3 *db, uint **subu_number){
+static uint count = 0;
+
+static int callback(void *NotUsed, int argc, char **argv, char **col_name){
+    int i;
+    printf("count: %u\n", count++);
+    for(i=0; i<argc; i++){
+      printf("%s = %s\n", col_name[i], argv[i] ? argv[i] : "NULL");
+    }
+    printf("\n");
+    return 0;
+  }
+
+int subu_number(sqlite3 *db, uint *subu_number){
   *subu_number = 0;
   char *sql = 
     "BEGIN TRANSACTION;"
     "UPDATE Key_Int SET value = value + 1 WHERE key = 'max_subu_number';"
     "SELECT value FROM Key_Int WHERE key = 'max_subu_number';"
     "COMMIT;";
-  size_t sql_len = strlen(sql);
   int ret;
-  sqlite3_stmt *stmt;
-  ret = sqlite3_prepare_v2(db, sql, sql_len, &stmt, NULL);
-  if( ret != SQLITE_OK ){
-    return ERR_CONFIG_FILE;
-  }      
-  sqlite3_stmt *res;
-  rc = sqlite3_step(res);
-  if( rc == SQLITE_ROW ){
-    printf("%u\n", sqlite3_column_int(res, NULL));
-  }
-  rc = sqlite3_step(res);
-  if( rc == SQLITE_ROW ){
-    printf("%u\n", sqlite3_column_int(res, NULL));
+  char *errmsg;
+  ret = sqlite3_exec(db, sql, callback, NULL, &errmsg);
+  if( errmsg ){
+    printf("exec failed: %s\n", errmsg);
+    sqlite3_free(errmsg);
   }
-  sqlite3_finalize(res);
 }
index 4ef7527..bf59be1 100644 (file)
@@ -5,7 +5,7 @@ This command initializes the configuration file.
 #include "subu-init.cli.h"
 #include <stdio.h>
 
-int main(){
+int main(int argc, char **argv, char **envp){
   sqlite3 *db;
   if(
      sqlite3_open(config_file, &db)
index cc07a78..6ed3bed 100644 (file)
@@ -2,15 +2,19 @@
   subu-mk-0 command
 
 */
-
-#include <stdio.h>
 #include "subu-mk-0.lib.h"
+#include <stdio.h>
 
-int main(int argc, char **argv, char **env){
+//  char *config_file = "/etc/subu.db";
+char config_file[] = "subu.db";
+
+int main(int argc, char **argv, char **envp){
   char *command = argv[0];
   if( argc != 2 ){
     fprintf(stderr, "usage: %s subu", command);
-    return ERR_ARG_CNT;
+    return ERR_SUBU_MK_0_ARG_CNT;
   }
-  return subu_mk_0(argv[1]);
+  char *subuname = argv[1];
+
+  return subu_mk_0(subuname, config_file);
 }
index 5c1bc88..657dc80 100644 (file)
 #include <stdbool.h>
 
 #if INTERFACE
-#define ERR_SUBU_MK_0_CONFIG_FILE 1
-#define ERR_SUBU_MK_0_SETUID_ROOT 2
-#define ERR_SUBU_MK_0_BAD_MASTERU_HOME 3
-#define ERR_SUBU_MK_0_MALLOC 4
-#define ERR_SUBU_MK_0_MK_SUBUHOME 5
-#define ERR_SUBU_MK_0_FAILED_MKDIR_SUBU 6
-#define ERR_SUBU_MK_0_BUG_SSS 7
-#define ERR_SUBU_MK_0_FAILED_USERADD 8
-#define ERR_SUBU_MK_0_SETFACL 9
+#define ERR_SUBU_MK_0_ARG_CNT 1
+#define ERR_SUBU_MK_0_CONFIG_FILE 2
+#define ERR_SUBU_MK_0_SETUID_ROOT 3
+#define ERR_SUBU_MK_0_BAD_MASTERU_HOME 4
+#define ERR_SUBU_MK_0_MALLOC 5
+#define ERR_SUBU_MK_0_MK_SUBUHOME 6
+#define ERR_SUBU_MK_0_FAILED_MKDIR_SUBU 7
+#define ERR_SUBU_MK_0_BUG_SSS 8
+#define ERR_SUBU_MK_0_FAILED_USERADD 9
+#define ERR_SUBU_MK_0_SETFACL 10
 #endif
 
 
index cb9d32b..586115c 100644 (file)
@@ -7,7 +7,7 @@ Currently it doesn't do the setting part.
 #include "subu-number.cli.h"
 #include <stdio.h>
 
-int main(){
+int main(int argc, char **argv, char **envp){
   int ret;
   sqlite3 *db;
   ret = sqlite3_open(config_file, &db);
@@ -15,8 +15,8 @@ int main(){
     fprintf(stderr, "error exit, could not build schema\n");
     return ERR_CONFIG_FILE;
   }
-  uint subu_number;
-  ret = subu_number(db, subu_number);
+  uint msn;
+  ret = subu_number(db, &msn);
   if( sqlite3_close(db) != SQLITE_OK ){
     fprintf(stderr, "error exit, strange, we could not close the db\n");
     return ERR_CONFIG_FILE;
diff --git a/src/subu_init b/src/subu_init
deleted file mode 100755 (executable)
index 91cc8fa..0000000
Binary files a/src/subu_init and /dev/null differ