Browse Source

Merge pull request #54 from troglobit/brillo-merges

Brillo merges
Joachim Nilsson 6 years ago
parent
commit
a44ad60a66
No account linked to committer's email
2 changed files with 14 additions and 18 deletions
  1. 12
    16
      src/light.c
  2. 2
    2
      src/light.h

+ 12
- 16
src/light.c View File

205
 			ctx.field = LIGHT_MIN_CAP;
205
 			ctx.field = LIGHT_MIN_CAP;
206
 			break;
206
 			break;
207
 
207
 
208
-			/* -- Controller selection -- */
209
 		case 'a':
208
 		case 'a':
210
 			ASSERT_CTRLSET();
209
 			ASSERT_CTRLSET();
211
 			ctx.ctrl = LIGHT_AUTO;
210
 			ctx.ctrl = LIGHT_AUTO;
216
 			ctx.ctrl = LIGHT_SPECIFY;
215
 			ctx.ctrl = LIGHT_SPECIFY;
217
 			if (!light_check_ctrl(optarg))
216
 			if (!light_check_ctrl(optarg))
218
 				return false;
217
 				return false;
219
-			strncpy(ctx.ctrl_name, optarg, NAME_MAX);
220
-			ctx.ctrl_name[NAME_MAX] = '\0';
218
+
219
+			snprintf(ctx.ctrl_name, sizeof(ctx.ctrl_name), "%s", optarg);
221
 			break;
220
 			break;
222
-			/* -- Value modes -- */
223
 
221
 
224
 		case 'p':
222
 		case 'p':
225
 			ASSERT_VALSET();
223
 			ASSERT_VALSET();
344
 	/* Make sure we have a valid controller before we proceed */
342
 	/* Make sure we have a valid controller before we proceed */
345
 	if (ctx.ctrl == LIGHT_AUTO) {
343
 	if (ctx.ctrl == LIGHT_AUTO) {
346
 		LIGHT_NOTE("Automatic mode -- finding best controller");
344
 		LIGHT_NOTE("Automatic mode -- finding best controller");
347
-		if (!light_ctrl_probe(ctx.ctrl_name)) {
345
+		if (!light_ctrl_probe(ctx.ctrl_name, sizeof(ctx.ctrl_name))) {
348
 			LIGHT_ERR("could not find suitable controller");
346
 			LIGHT_ERR("could not find suitable controller");
349
 			return false;
347
 			return false;
350
 		}
348
 		}
787
 	return true;
785
 	return true;
788
 }
786
 }
789
 
787
 
790
-static bool light_ctrl_iterate(DIR *dir, char *current)
788
+static bool light_ctrl_iterate(DIR *dir, char *current, size_t len)
791
 {
789
 {
792
 	struct dirent *d;
790
 	struct dirent *d;
793
 	bool found = false;
791
 	bool found = false;
809
 		}
807
 		}
810
 	}
808
 	}
811
 
809
 
812
-	strncpy(current, d->d_name, NAME_MAX);
813
-	current[NAME_MAX] = '\0';
810
+	snprintf(current, len, "%s", d->d_name);
814
 
811
 
815
 	return true;
812
 	return true;
816
 }
813
 }
817
 
814
 
818
 /* WARNING: `controller` HAS to be at most NAME_MAX, otherwise fails */
815
 /* WARNING: `controller` HAS to be at most NAME_MAX, otherwise fails */
819
-bool light_ctrl_probe(char *controller)
816
+bool light_ctrl_probe(char *controller, size_t len)
820
 {
817
 {
821
 	DIR *dir;
818
 	DIR *dir;
822
 	unsigned long best = 0;
819
 	unsigned long best = 0;
834
 		return false;
831
 		return false;
835
 	}
832
 	}
836
 
833
 
837
-	while (light_ctrl_iterate(dir, current)) {
834
+	while (light_ctrl_iterate(dir, current, sizeof(current))) {
838
 		unsigned long val = 0;
835
 		unsigned long val = 0;
839
 
836
 
840
 		LIGHT_NOTE("found '%s' controller", current);
837
 		LIGHT_NOTE("found '%s' controller", current);
844
 				if (val > best) {
841
 				if (val > best) {
845
 					found = true;
842
 					found = true;
846
 					best = val;
843
 					best = val;
847
-					strncpy(best_name, current, NAME_MAX);
848
-					best_name[NAME_MAX] = '\0';
844
+					snprintf(best_name, sizeof(best_name), "%s", current);
849
 					ctx.has_cached_brightness_max = true;
845
 					ctx.has_cached_brightness_max = true;
850
 					ctx.cached_brightness_max = val;
846
 					ctx.cached_brightness_max = val;
851
 				} else {
847
 				} else {
871
 		return false;
867
 		return false;
872
 	}
868
 	}
873
 
869
 
874
-	strncpy(controller, best_name, NAME_MAX);
875
-	controller[NAME_MAX] = '\0';
870
+	snprintf(controller, len, "%s", best_name);
871
+
876
 	return true;
872
 	return true;
877
 }
873
 }
878
 
874
 
927
 
923
 
928
 bool light_ctrl_list(void)
924
 bool light_ctrl_list(void)
929
 {
925
 {
930
-	char controller[NAME_MAX + 1];
926
+	char controller[NAME_MAX];
931
 	bool found = false;
927
 	bool found = false;
932
 	DIR *dir;
928
 	DIR *dir;
933
 
929
 
936
 		return false;
932
 		return false;
937
 	}
933
 	}
938
 
934
 
939
-	while (light_ctrl_iterate(dir, controller)) {
935
+	while (light_ctrl_iterate(dir, controller, sizeof(controller))) {
940
 		printf("%s\n", controller);
936
 		printf("%s\n", controller);
941
 		found = true;
937
 		found = true;
942
 	}
938
 	}

+ 2
- 2
src/light.h View File

61
 
61
 
62
 typedef struct {
62
 typedef struct {
63
 	/* Cache file prefix */
63
 	/* Cache file prefix */
64
-	char               prefix[NAME_MAX + 1];
64
+	char               prefix[NAME_MAX];
65
 
65
 
66
 	/* Which controller to use */
66
 	/* Which controller to use */
67
 	light_ctrl_mode_t  ctrl;
67
 	light_ctrl_mode_t  ctrl;
89
 void light_free                    (void);
89
 void light_free                    (void);
90
 
90
 
91
 bool light_ctrl_list               (void);
91
 bool light_ctrl_list               (void);
92
-bool light_ctrl_probe              (char *controller);
92
+bool light_ctrl_probe              (char *controller, size_t len);
93
 bool light_ctrl_exist              (char const *controller);
93
 bool light_ctrl_exist              (char const *controller);
94
 
94
 
95
 bool light_ctrl_get_brightness     (char const *controller, unsigned long *v);
95
 bool light_ctrl_get_brightness     (char const *controller, unsigned long *v);