Initial commit
This commit is contained in:
39
kapitel13/filinfo-portabel.c
Normal file
39
kapitel13/filinfo-portabel.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#if __STDC_VERSION__ >= 199901L
|
||||
#define C99
|
||||
#include <inttypes.h>
|
||||
#endif
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct stat fileinfo;
|
||||
if (argc != 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s path/to/file\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
stat(argv[1], &fileinfo);
|
||||
|
||||
#ifdef C99
|
||||
printf("C99 (or newer)\n");
|
||||
printf("Inode number: %ju\n",
|
||||
(uintmax_t)fileinfo.st_ino);
|
||||
printf("Size in bytes: %jd (%.2f kilobytes)\n",
|
||||
(intmax_t)fileinfo.st_size,
|
||||
(double)fileinfo.st_size / 1024);
|
||||
#else
|
||||
printf("C89\n");
|
||||
printf("Inode number: %lu\n", (unsigned long int)fileinfo.st_ino);
|
||||
printf("Size in bytes: %lu (%.2f kilobytes)\n",
|
||||
(unsigned long int)fileinfo.st_size,
|
||||
(float)fileinfo.st_size / 1024);
|
||||
#endif
|
||||
printf("User-ID: %d\n", fileinfo.st_uid);
|
||||
printf("Group-ID: %d\n", fileinfo.st_gid);
|
||||
|
||||
return 0;
|
||||
}
|
25
kapitel13/filinfo.c
Normal file
25
kapitel13/filinfo.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct stat fileinfo;
|
||||
if (argc != 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s path/to/file\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
stat(argv[1], &fileinfo);
|
||||
|
||||
printf("Inode number: %lu\n", (unsigned long int)fileinfo.st_ino);
|
||||
printf("Size in bytes: %lu (%.2f kilobytes)\n",
|
||||
(unsigned long int)fileinfo.st_size,
|
||||
(float)fileinfo.st_size / 1024);
|
||||
printf("User-ID: %d\n", fileinfo.st_uid);
|
||||
printf("Group-ID: %d\n", fileinfo.st_gid);
|
||||
|
||||
return 0;
|
||||
}
|
42
kapitel13/mychown-felhantering-ex1.c
Normal file
42
kapitel13/mychown-felhantering-ex1.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#define _XOPEN_SOURCE 500
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <file> <uid> <gid>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uid = (uid_t)atoi(argv[2]);
|
||||
gid = (gid_t)atoi(argv[3]);
|
||||
|
||||
if (chown(argv[1], uid, gid) == -1)
|
||||
{
|
||||
switch (errno)
|
||||
{
|
||||
case EPERM:
|
||||
fprintf(stderr, "Operation not permitted\n");
|
||||
break;
|
||||
case ENOENT:
|
||||
fprintf(stderr, "No such file or directory\n");
|
||||
break;
|
||||
case ENAMETOOLONG:
|
||||
fprintf(stderr, "Filename too long\n");
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Unknown error\n");
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
30
kapitel13/mychown-felhantering-ex2.c
Normal file
30
kapitel13/mychown-felhantering-ex2.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#define _XOPEN_SOURCE 500
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <file> <uid> <gid>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uid = (uid_t)atoi(argv[2]);
|
||||
gid = (gid_t)atoi(argv[3]);
|
||||
|
||||
if (chown(argv[1], uid, gid) == -1)
|
||||
{
|
||||
printf("%i\n", errno); /* Visar vad errno innehåller */
|
||||
printf("%s\n", strerror(errno)); /* Meddelandet i klartext */
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
27
kapitel13/mychown-felhantering-ex3.c
Normal file
27
kapitel13/mychown-felhantering-ex3.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#define _XOPEN_SOURCE 500
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <file> <uid> <gid>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uid = (uid_t)atoi(argv[2]);
|
||||
gid = (gid_t)atoi(argv[3]);
|
||||
|
||||
if (chown(argv[1], uid, gid) == -1)
|
||||
{
|
||||
perror("chown");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
27
kapitel13/mychown.c
Normal file
27
kapitel13/mychown.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#define _XOPEN_SOURCE 500
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <file> <uid> <gid>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uid = (uid_t)atoi(argv[2]);
|
||||
gid = (gid_t)atoi(argv[3]);
|
||||
|
||||
if (chown(argv[1], uid, gid) == -1)
|
||||
{
|
||||
fprintf(stderr, "Could not change UID or GID\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
22
kapitel13/mytouch.c
Normal file
22
kapitel13/mytouch.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
/* Rättigheterna nedan blir 644, Read/Write user,
|
||||
Read group, Read others */
|
||||
creat(argv[1], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
||||
if (errno != 0)
|
||||
{
|
||||
perror("creat");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
8
kapitel13/write-ex1.c
Normal file
8
kapitel13/write-ex1.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
write(1, "Hejsan\n", 7);
|
||||
return 0;
|
||||
}
|
8
kapitel13/write-ex2.c
Normal file
8
kapitel13/write-ex2.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
write(STDOUT_FILENO, "Hejsan\n", 7);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user