Capitulo 1
Capitulo 1
/*
* hello.c Canonical "Hello, World!" program
*/
#include <stdio.h>
int main(void)
{
puts("Hello there, Linux programmer!");
return 0;
}
/*
* inline.c - Using the inline keyword
*/
#include <stdio.h>
int main(void)
{
int i = 0;
while(i < 100) {
inc(&i);
printf("%d ", i);
}
printf("\n");
return 0;
}
/*
* msg.c - Define function from msg.h
*/
#include <stdio.h>
#include "msg.h"
/*
* noret.c - Using the noreturn attribute
*/
#include <stdio.h>
int main(void)
{
int i = 0;
while(1) {
switch(i) {
case 0 ... 5:
printf("i = %d\n", i);
break;
default:
die_now();
}
++i;
}
return 0;
}
void die_now(void)
{
puts("dying now");
exit(1);
}
/*
* packme.c - Using gcc's packed attributed
*/
#include <stdio.h>
struct P {
short s[3];
long l;
char str[5];
} __attribute__ ((packed));
struct UP {
short s[3];
long l;
char str[5];
};
int main(void)
{
struct P packed;
struct UP unpacked;
return 0;
}
/*
* pedant.c - Compile with -ansi, -pedantic or -pedantic-errors
*/
#include <stdio.h>
void main(void)
{
long long int i = 0l;
puts("This is a non-conforming C program");
}
/*
* showit.c Display driver
*/
#include <stdio.h>
#include "msg.h"
int main(void)
{
char msg_hi[] = {"Hi there, programmer!"};
char msg_bye[] = {"Goodbye, programmer!"};
printf("%s\n", msg_hi);
prmsg(msg_bye);
return 0;
}
LDFLAGS :=
PROGS = \
hello \
pedant \
newhello \
newhello-lib \
noret \
inline \
packme
all: $(PROGS)
.c.o:
$(CC) $(CFLAGS) -c $*.c
.SUFFIXES : .c .o
hello: hello.c
pedant: pedant.c
libmsg.a: msg.c
$(CC) $(CFLAGS) -c $< -o libmsg.o
$(AR) rcs libmsg.a libmsg.o
noret: noret.c
inline: inline.c
$(CC) -O2 $< -o $@
packme: packme.c
clean:
$(RM) $(PROGS) *.o *.a *~ *.out
zip: clean
zip 215101cd.zip *.c *.h Makefile
/*
* msg.h - Header for msg.c
*/
#ifndef MSG_H_
#define MSG_H_
#endif /* MSG_H_ */
Capitulo 2
/*
* ids.c - Print UIDs and GIDs
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
printf("Real user ID: %d\n", getuid());
printf("Effective user ID: %d\n", geteuid());
printf("Real group ID: %d\n", getgid());
printf("Effective group ID: %d\n", getegid());
exit(EXIT_SUCCESS);
}