Initial commit
This commit is contained in:
17
kapitel11/call-by-ref.c
Normal file
17
kapitel11/call-by-ref.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void inc(int *tal);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int x = 5;
|
||||
printf("x är nu: %d\n", x);
|
||||
inc(&x);
|
||||
printf("x är nu: %d\n", x);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void inc(int *tal)
|
||||
{
|
||||
*tal = *tal + 3;
|
||||
}
|
19
kapitel11/falt-arg-ex1.c
Normal file
19
kapitel11/falt-arg-ex1.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void say(char *namn);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char namn01[] = "Kalle";
|
||||
char *namn02 = "Lisa";
|
||||
|
||||
say(namn01);
|
||||
say(namn02);
|
||||
say("Jack-Benny");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void say(char *namn)
|
||||
{
|
||||
printf("Hej %s, hur är läget?\n", namn);
|
||||
}
|
20
kapitel11/falt-arg-ex2.c
Normal file
20
kapitel11/falt-arg-ex2.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
float medel(int tal[], int antal);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int x[] = {40, 42, 39, 37, 28};
|
||||
printf("Medelvärdet är: %.1f\n", medel(x, 5));
|
||||
return 0;
|
||||
}
|
||||
|
||||
float medel(int tal[], int antal)
|
||||
{
|
||||
int i;
|
||||
int sum = 0;
|
||||
for (i = 0; i<antal; i++)
|
||||
sum = sum + tal[i];
|
||||
|
||||
return ((float)sum / antal);
|
||||
}
|
12
kapitel11/mainarg.c
Normal file
12
kapitel11/mainarg.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
printf("Du har angett %d st argument\n", argc);
|
||||
for (i = 0; i<argc; i++)
|
||||
{
|
||||
printf("Argument %d är: %s\n", i, argv[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
22
kapitel11/return-flt-ex1.c
Normal file
22
kapitel11/return-flt-ex1.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char *say(char namn[]);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *mening = say("Jack-Benny");
|
||||
printf("%s\n", mening);
|
||||
free(mening);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *say(char namn[])
|
||||
{
|
||||
char *str;
|
||||
str = calloc(50, sizeof(char));
|
||||
strncat(str, "Hej ", 4);
|
||||
strncat(str, namn, 45);
|
||||
return str;
|
||||
}
|
18
kapitel11/return-flt-fail.c
Normal file
18
kapitel11/return-flt-fail.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
char *say(char namn[]);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("%s\n", say("Jack-Benny"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *say(char namn[])
|
||||
{
|
||||
char str[50] = { 0 };
|
||||
strncat(str, "Hej ", 4);
|
||||
strncat(str, namn, 45);
|
||||
return str;
|
||||
}
|
100
kapitel11/simple-ls-ver1.c
Normal file
100
kapitel11/simple-ls-ver1.c
Normal file
@@ -0,0 +1,100 @@
|
||||
#define _XOPEN_SOURCE 500
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
void printUsage(char *arg);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
unsigned int typ;
|
||||
char opt[4] = { 0 };
|
||||
|
||||
/* Skapa pekare och struct */
|
||||
DIR *d;
|
||||
struct dirent *dir;
|
||||
struct stat s;
|
||||
|
||||
/* Läs in aktuell katalog */
|
||||
d = opendir(".");
|
||||
/* Om 'd' INTE är sant (! = inte sant) */
|
||||
if (!d)
|
||||
{
|
||||
printf("Kan inte läsa aktuell katalog\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Behandla arugment */
|
||||
if (argc == 1)
|
||||
{
|
||||
strncpy(opt, "-a", 3);
|
||||
}
|
||||
else if (argc == 2)
|
||||
{
|
||||
strncpy(opt, argv[1], 3);
|
||||
|
||||
/* Hjälptext */
|
||||
if ( strcmp(opt, "-h") == 0 )
|
||||
printUsage(argv[0]);
|
||||
|
||||
/* Visa endast filer */
|
||||
else if ( strcmp(opt, "-f") == 0 )
|
||||
{
|
||||
typ = S_IFREG;
|
||||
}
|
||||
|
||||
/* Visa endast kataloger */
|
||||
else if ( strcmp(opt, "-d") == 0 )
|
||||
{
|
||||
typ = S_IFDIR;
|
||||
}
|
||||
|
||||
/* Visa alla filer */
|
||||
else if ( strcmp(opt, "-a") == 0 )
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
/* Om vi angav ett felaktigt argument */
|
||||
else
|
||||
{
|
||||
printUsage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/* Om vi angav fel antal argument */
|
||||
else
|
||||
{
|
||||
printUsage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Skriv ut innehållet i katalogen */
|
||||
while ((dir = readdir(d)) != NULL)
|
||||
{
|
||||
if ( strcmp(opt, "-a") == 0 )
|
||||
{
|
||||
printf("%s\n", dir->d_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
stat(dir->d_name, &s);
|
||||
/* Se kapitlet om 'Bitvisa operationer' */
|
||||
if ((s.st_mode & S_IFMT) == typ)
|
||||
printf("%s\n", dir->d_name);
|
||||
}
|
||||
}
|
||||
|
||||
/* Stäng "d" och returnera noll */
|
||||
closedir(d);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void printUsage(char *arg)
|
||||
{
|
||||
printf("Usage: %s [-a] [-f] [-d]\n"
|
||||
" -a to list all types (default)\n"
|
||||
" -f to list only files\n"
|
||||
" -d to list only directories\n", arg);
|
||||
}
|
75
kapitel11/simple-ls-ver2.c
Normal file
75
kapitel11/simple-ls-ver2.c
Normal file
@@ -0,0 +1,75 @@
|
||||
#define _XOPEN_SOURCE 500
|
||||
#include <stdio.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
void printUsage(char *arg);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int opt;
|
||||
unsigned int typ = 0;
|
||||
char *path = ".";
|
||||
|
||||
/* Skapa pekare och struct */
|
||||
DIR *d;
|
||||
struct dirent *dir;
|
||||
struct stat s;
|
||||
|
||||
/* Behandla argument med getopt */
|
||||
while ((opt = getopt(argc, argv, "hafdp:")) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
case 'a':
|
||||
typ = -1;
|
||||
break;
|
||||
case 'f':
|
||||
typ = S_IFREG;
|
||||
break;
|
||||
case 'd':
|
||||
typ = S_IFDIR;
|
||||
break;
|
||||
case 'p':
|
||||
path = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
printUsage(argv[0]);
|
||||
return 0;
|
||||
default:
|
||||
printUsage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Byt katalog med chdir() för att stat() ska
|
||||
se filerna i rätt katalog */
|
||||
chdir(path);
|
||||
|
||||
d = opendir(path);
|
||||
while ((dir = readdir(d)) != NULL)
|
||||
{
|
||||
if ( typ == 0 )
|
||||
{
|
||||
printf("%s\n", dir->d_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
stat(dir->d_name, &s);
|
||||
/* Se kapitlet om 'Bitvisa operationer' */
|
||||
if ((s.st_mode & S_IFMT) == typ)
|
||||
printf("%s\n", dir->d_name);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void printUsage(char *arg)
|
||||
{
|
||||
printf("Usage: %s [-a] [-f] [-d] [-p path]\n"
|
||||
" -a to list all types (default)\n"
|
||||
" -f to list only files\n"
|
||||
" -d to list only directories\n"
|
||||
" -p to specify a directory\n", arg);
|
||||
}
|
Reference in New Issue
Block a user