Sfoglia il codice sorgente

Merge pull request #54 from troglobit/brillo-merges

Brillo merges
Joachim Nilsson 5 anni fa
parent
commit
a44ad60a66
No account linked to committer's email
2 ha cambiato i file con 14 aggiunte e 18 eliminazioni
  1. 12
    16
      src/light.c
  2. 2
    2
      src/light.h

+ 12
- 16
src/light.c Vedi File

@@ -205,7 +205,6 @@ static bool light_parse_args(int argc, char **argv)
205 205
 			ctx.field = LIGHT_MIN_CAP;
206 206
 			break;
207 207
 
208
-			/* -- Controller selection -- */
209 208
 		case 'a':
210 209
 			ASSERT_CTRLSET();
211 210
 			ctx.ctrl = LIGHT_AUTO;
@@ -216,10 +215,9 @@ static bool light_parse_args(int argc, char **argv)
216 215
 			ctx.ctrl = LIGHT_SPECIFY;
217 216
 			if (!light_check_ctrl(optarg))
218 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 220
 			break;
222
-			/* -- Value modes -- */
223 221
 
224 222
 		case 'p':
225 223
 			ASSERT_VALSET();
@@ -344,7 +342,7 @@ bool light_initialize(int argc, char **argv)
344 342
 	/* Make sure we have a valid controller before we proceed */
345 343
 	if (ctx.ctrl == LIGHT_AUTO) {
346 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 346
 			LIGHT_ERR("could not find suitable controller");
349 347
 			return false;
350 348
 		}
@@ -787,7 +785,7 @@ static bool light_ctrl_init(DIR **dir)
787 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 790
 	struct dirent *d;
793 791
 	bool found = false;
@@ -809,14 +807,13 @@ static bool light_ctrl_iterate(DIR *dir, char *current)
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 812
 	return true;
816 813
 }
817 814
 
818 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 818
 	DIR *dir;
822 819
 	unsigned long best = 0;
@@ -834,7 +831,7 @@ bool light_ctrl_probe(char *controller)
834 831
 		return false;
835 832
 	}
836 833
 
837
-	while (light_ctrl_iterate(dir, current)) {
834
+	while (light_ctrl_iterate(dir, current, sizeof(current))) {
838 835
 		unsigned long val = 0;
839 836
 
840 837
 		LIGHT_NOTE("found '%s' controller", current);
@@ -844,8 +841,7 @@ bool light_ctrl_probe(char *controller)
844 841
 				if (val > best) {
845 842
 					found = true;
846 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 845
 					ctx.has_cached_brightness_max = true;
850 846
 					ctx.cached_brightness_max = val;
851 847
 				} else {
@@ -871,8 +867,8 @@ bool light_ctrl_probe(char *controller)
871 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 872
 	return true;
877 873
 }
878 874
 
@@ -927,7 +923,7 @@ bool light_ctrl_set_cap_min(char const *controller, unsigned long val)
927 923
 
928 924
 bool light_ctrl_list(void)
929 925
 {
930
-	char controller[NAME_MAX + 1];
926
+	char controller[NAME_MAX];
931 927
 	bool found = false;
932 928
 	DIR *dir;
933 929
 
@@ -936,7 +932,7 @@ bool light_ctrl_list(void)
936 932
 		return false;
937 933
 	}
938 934
 
939
-	while (light_ctrl_iterate(dir, controller)) {
935
+	while (light_ctrl_iterate(dir, controller, sizeof(controller))) {
940 936
 		printf("%s\n", controller);
941 937
 		found = true;
942 938
 	}

+ 2
- 2
src/light.h Vedi File

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