<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<atom:link href="https://dev1galaxy.org/extern.php?action=feed&amp;tid=3215&amp;type=rss" rel="self" type="application/rss+xml" />
		<title><![CDATA[Dev1 Galaxy Forum / Adjusting brightness in C]]></title>
		<link>https://dev1galaxy.org/viewtopic.php?id=3215</link>
		<description><![CDATA[The most recent posts in Adjusting brightness in C.]]></description>
		<lastBuildDate>Tue, 17 Dec 2019 23:36:22 +0000</lastBuildDate>
		<generator>FluxBB</generator>
		<item>
			<title><![CDATA[Re: Adjusting brightness in C]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=19051#p19051</link>
			<description><![CDATA[<div class="codebox"><pre class="vscroll"><code>#include &lt;errno.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;
 
int main(int argc, char **argv) {
	int opt;
	FILE *fp;
	char *fname = NULL;
	char *brightness = NULL;
	
	while ((opt = getopt(argc, argv, &quot;b:f:&quot;)) != -1) {
		switch (opt) {
		case &#039;b&#039;:
			brightness = optarg;
			break;
		case &#039;f&#039;:
			fname = optarg;
			break;
		default:
			fname = brightness = NULL;
		}
	}

	if (fname &amp;&amp; brightness) {
		if((fp = fopen(fname, &quot;w+&quot;)) != NULL) {
			if (fprintf(fp, &quot;%s&quot;, brightness) &gt; 0) {
				fclose(fp);
				exit(EXIT_SUCCESS);
			}
		}
		fprintf(stderr, &quot;%s: %s: &#039;%s&#039;\n&quot;, argv[0], strerror(errno), fname);
	} else {
		fprintf(stderr, &quot;Usage: %s -f PATH -b NUM\n&quot;
				&quot;Options: -f\t\tsysfs path\n&quot;
				&quot;         -b\t\tbrightness value\n&quot;, argv[0]);
	}
	exit(EXIT_FAILURE);
}</code></pre></div><p>Just for fun and to refresh my C coding skills. Improved version with command line arguments handling and error checking.<br />Ciao</p>]]></description>
			<author><![CDATA[dummy@example.com (farmatito)]]></author>
			<pubDate>Tue, 17 Dec 2019 23:36:22 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=19051#p19051</guid>
		</item>
		<item>
			<title><![CDATA[Re: Adjusting brightness in C]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=18993#p18993</link>
			<description><![CDATA[<p>siva, change <em>test</em> to <em>brightness</em> so that the source file looks like this:</p><div class="codebox"><pre><code>#include &lt;stdio.h&gt;
#include &lt;fcntl.h&gt;	// Use &quot;open&quot;
#include &lt;unistd.h&gt;	// For &quot;close&quot;

int main() {
	// Open the brightness file.
	FILE *fp = fopen(&quot;/sys/class/backlight/acpi_video0/brightness&quot;, &quot;w+&quot;);
	// Adjust brightness value.
	fprintf(fp, &quot;5&quot;);
	// End.
	fclose(fp);
	return 0;
}</code></pre></div><p>Then compile the file, change ownership to root, and set SUID:</p><div class="codebox"><pre><code>$ gcc -o testing testing.c
$ sudo chown root:root testing
$ sudo chmod u+s testing</code></pre></div><p>Now run the executable as regular user and it will change the brightness:</p><div class="codebox"><pre><code>$ ./testing # boom! brightness changes</code></pre></div><p>It was segfaulting because the binary did not have enough permissions to open the file.</p>]]></description>
			<author><![CDATA[dummy@example.com (GNUser)]]></author>
			<pubDate>Fri, 13 Dec 2019 14:57:39 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=18993#p18993</guid>
		</item>
		<item>
			<title><![CDATA[Re: Adjusting brightness in C]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=18988#p18988</link>
			<description><![CDATA[<p>might be some pointers here: <a href="https://www.linuxquestions.org/questions/linux-software-2/a-simple-c-program-to-control-monitor-brightness-4175521454/" rel="nofollow">https://www.linuxquestions.org/question … 175521454/</a></p>]]></description>
			<author><![CDATA[dummy@example.com (HevyDevy)]]></author>
			<pubDate>Fri, 13 Dec 2019 11:46:15 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=18988#p18988</guid>
		</item>
		<item>
			<title><![CDATA[Adjusting brightness in C]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=18975#p18975</link>
			<description><![CDATA[<p>I have a shell script that updates brightness using <span class="bbc">echo [number] &gt; /location/of/brightness</span>. It works okay when it&#039;s run as root. Also, as root, I can adjust the brightness value using an editor like nano.</p><p>A few minutes ago, I tried to translate this into a simple C program.&#160; It compiled fine.&#160; However, when I tried running the program as root, it gave a &quot;Segmentation fault&quot; as the result, and did not change the brightness.</p><p>Can anyone offer any pointers?</p><p>Here&#039;s the code:</p><div class="codebox"><pre><code>#include &lt;stdio.h&gt;
#include &lt;fcntl.h&gt;	// Use &quot;open&quot;
#include &lt;unistd.h&gt;	// For &quot;close&quot;

int main() {
	// Open the brightness file.
	FILE *fp = fopen(&quot;/sys/class/backlight/acpi_video0/test&quot;, &quot;w+&quot;);
	// Adjust brightness value.
	fprintf(fp, &quot;5&quot;);
	// End.
	fclose(fp);
	return 0;
}</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (siva)]]></author>
			<pubDate>Fri, 13 Dec 2019 03:55:27 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=18975#p18975</guid>
		</item>
	</channel>
</rss>
