8bc5f560d4b3b07c7153c107d2a0671a4ff391a0 — Rory Bradford a month ago
first version
7 files changed, 115 insertions(+), 36 deletions(-)

M .gitignore
A Dockerfile
M Makefile
A README.md
A control
M nini.c
M test.ini
M .gitignore => .gitignore +3 -0
@@ 1,1 1,4 @@ nini
+ nini_deb/
+ *.deb
+ 

A Dockerfile => Dockerfile +6 -0
@@ 0,0 1,6 @@
+ FROM debian:buster-slim
+ WORKDIR /root
+ COPY . .
+ RUN apt update
+ RUN dpkg -i nini_deb.deb; apt install -fy
+ RUN ./test

M Makefile => Makefile +13 -2
@@ 1,10 1,21 @@+name = nini_deb
+ prefix = $(name)/usr/local/bin/
+ 
  build:
  	gcc -o nini -linih -lseccomp -O2 nini.c -Wall
  
  test: build
  	./test
  
+ deb: test
+ 	mkdir -p $(prefix)
+ 	mkdir -p $(name)/DEBIAN
+ 	cp -v control $(name)/DEBIAN/
+ 	cp ./nini $(prefix)
+ 	dpkg-deb --build $(name)
+ 
  clean:
- 	@rm -v nini
+ 	@rm -fv nini
+ 	@rm -rfv $(name)
  
- .PHONY: clean test build
+ .PHONY: clean test build deb

A README.md => README.md +27 -0
@@ 0,0 1,27 @@
+ # nini
+ 
+ > nanoscopic ini file reader
+ 
+ ### Usage
+ 
+ ```shell
+ $ nini file section key
+ ```
+ 
+ `key` can be specified as `'_'` which will print a list of key/pairs in that section.
+ 
+ ### Examples
+ 
+ With `test.ini` as an example:
+ 
+ ```ini
+ [foo bar]
+ hello=world
+ ```
+ 
+ ```shell
+ $ nini test.ini 'foo bar' hello
+ world
+ $ nini test.ini 'foo bar' _
+ hello = world
+ ```

A control => control +7 -0
@@ 0,0 1,7 @@
+ Package: nini
+ Version: 1.0.0
+ Depends: libinih1
+ Maintainer: Rory Bradford <roryrjb@gmail.com>
+ Architecture: amd64
+ Description: nanoscopic ini file reader
+ 

M nini.c => nini.c +55 -34
@@ 1,59 1,80 @@ #include <stdio.h>
+ #include <unistd.h>
  #include <string.h>
  #include <seccomp.h>
  #include <ini.h>
  
+ #define USAGE \
+ "\nUsage: nini file section key\n\n- key can be '_' which will print " \
+ "key values pairs in specified section\n"
+ 
  struct match {
- 	const char* section;
- 	const char* key;
+         const char* section;
+         const char* key;
  };
  
- int get_value(void* _, const char* section, const char* key,
- 	      const char* value)
+ int get_value(void* m, const char* section, const char* key,
+               const char* value)
  {
- 	struct match* config = (struct match*) _;
+         struct match* config = (struct match*) m;
+ 
+         if (
+             strcmp(section, config->section) == 0 &&
+             strcmp(config->key, "_") == 0
+         ) {
+             printf("%s = %s\n", key, value);
+         }
  
- 	if (!strcmp(section, config->section) && !strcmp(key, config->key)) {
- 		printf("%s\n", value);
- 	}
+         if (
+             strcmp(section, config->section) == 0 &&
+             strcmp(key, config->key) == 0
+         ) {
+             printf("%s\n", value);
+         }
  
- 	return 0;
+         return 0;
+ }
+ 
+ int usage()
+ {
+         printf(USAGE);
+         return 1;
  }
  
  int main(int argc, char** argv)
  {
          scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_KILL);
- 	seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 0);
- 	seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group), 0);
- 	seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0);
- 	seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fstat), 0);
- 	seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(lseek), 0);
- 	seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(close), 0);
- 	seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0);
+         seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 0);
+         seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group), 0);
+         seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0);
+         seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fstat), 0);
+         seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(lseek), 0);
+         seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(close), 0);
+         seccomp_rule_add_exact(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0);
          seccomp_load(ctx);
  
- 	if (!argv[1]) {
- 		printf("Specify a file.\n");
- 		return 1;
- 	}
+         if (!argv[1]) {
+                 printf("Specify a file.\n");
+                 return usage();
+         }
  
- 	if (!argv[2] || !argv[3]) {
- 		printf("Section and/or key is empty.\n");
- 		return 1;
- 	}
+         if (!argv[2] || !argv[3]) {
+                 printf("Section and/or key is empty.\n");
+                 return usage();
+         }
  
- 	struct match m = {
- 		.section = argv[2],
- 		.key = argv[3]
- 	};
+         struct match m = {
+                 .section = argv[2],
+                 .key = argv[3]
+         };
  
- 	if (ini_parse(argv[1], get_value, &m) < 0) {
- 		printf("Cannot open \"%s\".\n", argv[1]);
- 		return 1;
- 	}
+         if (ini_parse(argv[1], get_value, &m) < 0) {
+                 printf("Cannot open \"%s\".\n", argv[0]);
+                 return usage();
+         }
  
- 	seccomp_release(ctx);
+         seccomp_release(ctx);
  
- 	return 0;
+         return 0;
  }
  

M test.ini => test.ini +4 -0
@@ 1,8 1,12 @@ [section]
  hello = world
  
+ ; a comment line
+ 
  [_section2]
  some_config=val
  
+ # another comment
+ 
  [Section 3]
  a b c = d