• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: 2 problems.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 2 problems.


  • Subject: Re: 2 problems.
  • From: Steve Checkoway <email@hidden>
  • Date: Mon, 3 Jan 2005 08:01:57 -0800

On Jan 2, 2005, at 9:11 PM, D.Walsh wrote:
problem #1
<snip>

This really doesn't belong on on this list but this seems to work:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <grp.h>
#include <pwd.h>

int main(int argc, char *argv[])
{
	if (argc != 2)
		return 1;

	const char *const path = "/etc/hostconfig";
	FILE *fp = fopen(path, "r");
	char template[] = "/tmp/temp.XXXXXX";
	int fd;
	FILE *temp;

	if (!fp)
		return 2;

	fd = mkstemp(template);
	if (fd == -1)
	{
		fclose(fp);
		return 3;
	}
	temp = fdopen(fd, "w");
	if (!temp)
	{
		fclose(fp);
		close(fd);
		return 4;
	}

	char buffer[1024]; /* quite a long line */
	bool match = false;

	while (fgets(buffer, sizeof(buffer), fp))
	{
		if (strncmp("MAILTRANSPORT=", buffer, strlen("MAILTRANSPORT=")))
			fputs(buffer, temp);
		else
		{
			fprintf(temp, "MAILTRANSPORT=%s\n", argv[1]);
			match = true;
		}
	}
	if (!match)
		fprintf(temp, "MAILTRANSPORT=%s\n", argv[1]);

	fclose(temp); /* no need to close(fd) */
	fclose(fp);

	if (rename(template, path))
		return 5;
	if (chmod(path, 0644))
		return 6;

	struct group *grp = getgrnam("wheel");
	struct passwd *user = getpwnam("root");

	if (!grp || !user)
		return 7;
	if (chown(path, user->pw_uid, grp->gr_gid))
		return 8;
	return 0;
}

problem #2

Others answered this.

- Steve

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >2 problems. (From: "D.Walsh" <email@hidden>)

  • Prev by Date: Re: Cocoa Bindings - nondebuggable, non-obvious, procedural ???
  • Next by Date: Re: Invisible characters in NSTextView
  • Previous by thread: Re: 2 problems.
  • Next by thread: KVO and arrays
  • Index(es):
    • Date
    • Thread