Skip to content

Instantly share code, notes, and snippets.

@uhziel
uhziel / apue.2e.fig3.2.c
Created October 15, 2011 02:15
apue.2e.fig3.2.c
#include "apue.h"
#include <fcntl.h>
#include <string.h>
char buf1[] = "abcdefghij";
char buf2[] = "ABCDEFGHIJ";
int main()
{
int fd = open("file.hole", O_WRONLY | O_CREAT | O_TRUNC, FILE_MODE);
@uhziel
uhziel / apue.2e.fig3.4.c
Created October 15, 2011 03:26
apue.2e.fig3.4.c
#include "apue.h"
#define BUFFSIZE 4096
int main()
{
int n;
char buf[BUFFSIZE];
while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
@uhziel
uhziel / apue.2e.fig3.10.c
Created October 16, 2011 02:26
apue.2e.fig3.10.c
#include "apue.h"
#include <fcntl.h>
int main(int argc, char *argv[])
{
int val;
if(argc != 2)
{
err_quit("usage: %s <filedescriptor>", argv[0]);
/*
* Write your own dup2 function that performs the same service as the dup2 function described in Section 3.12,
* without calling the fcntl function. Be sure to handle errors correctly.
*/
/* TODO */
@uhziel
uhziel / apue.2e.fig4.3.c
Created October 18, 2011 16:20
apue.2e.fig4.3.c
#include "apue.h"
int main(int argc, char *argv[])
{
int i;
struct stat buf;
char *ptr;
for (i = 1; i < argc; i++)
{
@uhziel
uhziel / apue.2e.fig4.8.c
Created October 20, 2011 15:39
apue.2e.fig4.8.c
#include "apue.h"
#include <fcntl.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
err_quit("usage: %s <pathname>", argv[0]);
}
if (access(argv[1], R_OK) < 0)
@uhziel
uhziel / apue.2e.fig4.9.c
Created October 20, 2011 16:12
apue.2e.fig4.9.c
#include <fcntl.h>
#define RWXRWXRWX (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH)
int main(void)
{
int old_umask = umask(0);
printf("old_umask %o\n", old_umask);
if (creat("foo", RWXRWXRWX) < 0)
{
@uhziel
uhziel / apue.2e.fig4.12.c
Created October 20, 2011 16:34
apue.2e.fig4.12.c
#include "apue.h"
int main()
{
struct stat statbuf;
if (stat("foo", &statbuf) < 0)
{
err_sys("stat err for foo");
}
@uhziel
uhziel / apue.2e.fig4.16.c
Created November 8, 2011 16:08
apue.2e.fig4.16.c
#include "apue.h"
#include <fcntl.h>
int main(void)
{
if (open("tmpfile", O_RDWR) < 0)
{
err_sys("open error");
}
if (unlink("tmpfile") < 0)
@uhziel
uhziel / apue.2e.fig4.21.c
Created November 10, 2011 16:07
apue.2e.fig4.21.c
#include "apue.h"
#include <fcntl.h>
#include <utime.h>
int main(int argc, char *argv[])
{
int i;
int fd;
struct stat statbuf;
struct utimbuf timebuf;