Beispiel #1
0
int main(int argc, char* argv[])
{
	if(argc != 3) {
		return error("Usage: program_name text_file_name vocabulary_file_name");
	}

	text_file_name = argv[1];
	vocab_file_name = argv[2];

	 /* open streams for all of the files */
	text_file = fopen(text_file_name, "r");
	vocab_file = fopen(vocab_file_name, "r");
	temp_file = fopen(temp_file_name, "w");
	if(text_file == NULL || vocab_file == NULL || temp_file == NULL) {
		return error("text file, vocab file or temp file open error.");
	}

	if(scan_file(text_file) != 0) { /* documentation next to the function */
		return ERROR;
	}

	if(rewrite_file() != 0) { /* documentation next to the function */
		return ERROR;
	}

	if(remove(temp_file_name) != 0) { /* removes the temp file */
		return error("couldn't delete tempfile");
	}

	return 0;
}
static void
prepare_files(int nfiles)
{
	int i, j;
	char fname[1024];

	for (i = 0; i < nfiles; i++)
	{
		int fd;

		sprintf(fname, "%d", i);

		if ((fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
		{
			printf("open failed");
			exit(7);
		}

		rewrite_file(fd);

		/* fdatasync is default on linux */
		if (SYNC(fd) != 0)
		{
			printf("sync failed");
			exit(8);
		}

		if (close(fd) != 0)
		{
			printf("close failed");
			exit(9);
		}
	}
}
int
file_passwd(char *uname, char *locn)
{
	char *ne, *oc, *nc;
	int fd;
	FILE *fp;
	uid_t uid;
	char *fname;
	struct passwd *pw;
	struct passwd newpw;
	
	fname = _PASSWD_FILE;
	if (locn != NULL) fname = locn;
	
	fd = open(fname, O_RDONLY | O_EXLOCK);
	if (fd == -1) {
		err(EXIT_FAILURE, "%s", fname);
	}

	fp = fdopen(fd, "r");
	if (fp == NULL) {
		err(EXIT_FAILURE, "%s", fname);
	}

	pw = find_user(fp, uname);
	if (pw == NULL) {
		errx(EXIT_FAILURE, "user %s not found in %s", uname, fname);
	}

	uid = getuid();
	if (uid != 0 && uid != pw->pw_uid) {
		errno = EACCES;
		err(EXIT_FAILURE, "%s", uname);
	}

	// Get the password
	getpasswd(uname, (uid == 0), 5, 0, 0, pw->pw_passwd, &ne, &oc, &nc);

	newpw.pw_name = strdup(pw->pw_name);
	newpw.pw_passwd = strdup(ne);
	newpw.pw_uid = pw->pw_uid;
	newpw.pw_gid = pw->pw_gid;
	newpw.pw_class = strdup(pw->pw_class);
	newpw.pw_change = pw->pw_change;
	newpw.pw_expire = pw->pw_expire;
	newpw.pw_gecos = strdup(pw->pw_gecos);
	newpw.pw_dir = strdup(pw->pw_dir);
	newpw.pw_shell = strdup(pw->pw_shell);

	// Rewrite the file
	rewind(fp);
	rewrite_file(fname, fp, &newpw);

	fclose(fp);

	return 0;
}
void main()
{
	int i, f;

	prepare_files(NFILES);
	f = NFILES;

	/* now create a new file by renaming the oldest one, rewrite and sync */
	while (1)
	{
		int fd;
		char fname_old[1024];
		char fname_new[1024];

		sprintf(fname_old, "%d", f-NFILES);
		sprintf(fname_new, "%d", f);

		if (rename(fname_old, fname_new) != 0)
		{
			printf("rename failed");
			exit(1);
		}

		if ((fd = open(fname_new, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
		{
			printf("open failed");
			exit(2);
		}

		rewrite_file(fd);

		/* fdatasync is default on linux */
		if (SYNC(fd) != 0)
		{
			printf("sync failed");
			exit(3);
		}

		if (close(fd) != 0)
		{
			printf("close failed");
			exit(4);
		}

		printf("file %s\n", fname_new);

		/* sleep for 1 second */
		sleep(1);

		/* next file */
		f += 1;
	}

}
Beispiel #5
0
int
file_passwd(char *uname, int isroot, const char *old_pw, const char *new_pw)
{
	FILE *fp=NULL;
	char *fname;
	struct passwd *pw;
	struct passwd newpw={0};
	struct stat sb;
	int error_id = 0;
	char salt[9];
	static char nbuf[_PASSWORD_LEN+1]={0};
	
	fname = _PASSWD_FILE;
	
	umask((S_IRWXG | S_IRWXO));
        
	if ( lstat(fname, &sb) != 0 )
	{
		return ERR_PASSWD_FILE_NO_EXIST;
	}
	
	fp = fopen(fname, "a+");
	
	if (fp == NULL)
	{
		return ERR_PASSWD_FILE_WRITE;
	}
	if (fchmod(fileno(fp), (S_IRUSR | S_IWUSR)) != 0)
	{
		error_id = ERR_PASSWD_FILE_PERMISSON;
		goto err;
	}
	
	pw = find_user(uname, fp);
	if (pw == (struct passwd *)NULL)
	{
		error_id = ERR_USER_NO_EXIST;
		goto err;
	}
	if (isroot == 0)
	{
        if (strcmp(crypt(old_pw, pw->pw_passwd), pw->pw_passwd))
        {
            error_id = ERR_OLD_PW_WRONG;
            goto err;
        }
	}

	nbuf[0] = '\0';
	snprintf( nbuf, sizeof(nbuf), "%s", new_pw );
	/*
	 * Create a random salt
	 */
	srandom((int)time((time_t *)NULL));
	salt[0] = saltchars[random() % strlen(saltchars)];
	salt[1] = saltchars[random() % strlen(saltchars)];
	salt[2] = '\0';
	new_pw = crypt(nbuf, salt);

	newpw.pw_name = copyString(pw->pw_name);
	newpw.pw_passwd = copyString(new_pw);
	newpw.pw_class = copyString(pw->pw_class);
	newpw.pw_gecos = copyString(pw->pw_gecos);
	newpw.pw_dir = copyString(pw->pw_dir);
	newpw.pw_shell = copyString(pw->pw_shell);
	newpw.pw_uid = pw->pw_uid;
	newpw.pw_gid = pw->pw_gid;
	newpw.pw_change = pw->pw_change;
	newpw.pw_expire = pw->pw_expire;

	/*
	 * Re-write the file
	 */
	error_id = rewrite_file(fname, fp, &newpw);

	/*
	 * Clean up memory
	 */
	free(newpw.pw_name);
	free(newpw.pw_passwd);
	free(newpw.pw_class);
	free(newpw.pw_gecos);
	free(newpw.pw_dir);
	free(newpw.pw_shell);

err:
	pw = parse_user(NULL);
	fclose(fp);

	return error_id;
}