From: Thomas Walker Lynch Date: Sun, 24 Feb 2019 01:37:01 +0000 (+0100) Subject: number example X-Git-Url: https://git.reasoningtechnology.com/style/static/git-logo.png?a=commitdiff_plain;h=af789ab57fb54cbcfbbb752bd1cf56b50eb52f6a;p=subu number example --- diff --git a/src/1_try/mh_main_prob/command1.c b/src/1_try/mh_main_prob/command1.c new file mode 100644 index 0000000..637df55 --- /dev/null +++ b/src/1_try/mh_main_prob/command1.c @@ -0,0 +1,6 @@ +#include "command1.h" +#include +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 index 0000000..5d8c612 --- /dev/null +++ b/src/1_try/mh_main_prob/command2.c @@ -0,0 +1,6 @@ +#include "command2.h" +#include +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 index 0000000..67625f4 --- /dev/null +++ b/src/1_try/mh_main_prob/just_fun.c @@ -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 index 0000000..c5511fe --- /dev/null +++ b/src/1_try/mh_main_prob/transcript1.txt @@ -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 +int main(){ + printf("command1 %d\n", f()); + return 0; +} +> cat command2.c +#include "command2.h" +#include +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 index 0000000..77ec819 --- /dev/null +++ b/src/1_try/mh_main_prob/transcript2.txt @@ -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 +static int main(){ + printf("command1 %d\n", f()); + return 0; +} +> cat command2.c +#include "command2.h" +#include +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 index 0000000..b3a00b7 --- /dev/null +++ b/src/1_try/mh_main_prob/transcript3.txt @@ -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 +int main(int argc, char **argv){ + printf("command1 %d\n", f()); + return 0; +} +> cat command1.h +/* This 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 +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 index 0000000..a9a2b21 --- /dev/null +++ b/src/3_documents/README.txt @@ -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 diff --git a/src/5_scratch/dispatch_f.lib.h b/src/5_scratch/dispatch_f.lib.h index 994592d..85eaf80 100644 --- a/src/5_scratch/dispatch_f.lib.h +++ b/src/5_scratch/dispatch_f.lib.h @@ -1,5 +1,11 @@ /* This file was automatically generated. Do not edit! */ #undef INTERFACE +#include +#include 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 diff --git a/src/5_scratch/subu-config.lib.h b/src/5_scratch/subu-config.lib.h index c359b52..a63786d 100644 --- a/src/5_scratch/subu-config.lib.h +++ b/src/5_scratch/subu-config.lib.h @@ -2,8 +2,9 @@ #undef INTERFACE #include 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 diff --git a/src/5_scratch/subu-init.cli.h b/src/5_scratch/subu-init.cli.h index 46bfd20..eff6c3f 100644 --- a/src/5_scratch/subu-init.cli.h +++ b/src/5_scratch/subu-init.cli.h @@ -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); diff --git a/src/5_scratch/subu-mk-0.cli.h b/src/5_scratch/subu-mk-0.cli.h index 075b350..9c73c68 100644 --- a/src/5_scratch/subu-mk-0.cli.h +++ b/src/5_scratch/subu-mk-0.cli.h @@ -2,7 +2,9 @@ #undef INTERFACE #include 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); diff --git a/src/5_scratch/subu-mk-0.lib.h b/src/5_scratch/subu-mk-0.lib.h index 82c7e14..76a337a 100644 --- a/src/5_scratch/subu-mk-0.lib.h +++ b/src/5_scratch/subu-mk-0.lib.h @@ -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 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 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 diff --git a/src/5_scratch/subu-number.cli.h b/src/5_scratch/subu-number.cli.h index 759d808..91145ce 100644 --- a/src/5_scratch/subu-number.cli.h +++ b/src/5_scratch/subu-number.cli.h @@ -2,9 +2,10 @@ #undef INTERFACE #include 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 index a9a2b21..0000000 --- a/src/README.txt +++ /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 diff --git a/src/dispatch_f.lib.c b/src/dispatch_f.lib.c index cfa75a8..7edf41b 100644 --- a/src/dispatch_f.lib.c +++ b/src/dispatch_f.lib.c @@ -10,14 +10,21 @@ 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 +#include +#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 -#include #include #include #include @@ -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); diff --git a/src/makefile b/src/makefile index ddfcfdd..030a9f7 100755 --- a/src/makefile +++ b/src/makefile @@ -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 diff --git a/src/subu-config.lib.c b/src/subu-config.lib.c index 8706aaf..525e195 100644 --- a/src/subu-config.lib.c +++ b/src/subu-config.lib.c @@ -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 -int main(){ +int main(int argc, char **argv, char **envp){ sqlite3 *db; if( sqlite3_open(config_file, &db) diff --git a/src/subu-mk-0.cli.c b/src/subu-mk-0.cli.c index cc07a78..6ed3bed 100644 --- a/src/subu-mk-0.cli.c +++ b/src/subu-mk-0.cli.c @@ -2,15 +2,19 @@ subu-mk-0 command */ - -#include #include "subu-mk-0.lib.h" +#include -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); } diff --git a/src/subu-mk-0.lib.c b/src/subu-mk-0.lib.c index 5c1bc88..657dc80 100644 --- a/src/subu-mk-0.lib.c +++ b/src/subu-mk-0.lib.c @@ -33,15 +33,16 @@ #include #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 diff --git a/src/subu-number.cli.c b/src/subu-number.cli.c index cb9d32b..586115c 100644 --- a/src/subu-number.cli.c +++ b/src/subu-number.cli.c @@ -7,7 +7,7 @@ Currently it doesn't do the setting part. #include "subu-number.cli.h" #include -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 index 91cc8fa..0000000 Binary files a/src/subu_init and /dev/null differ