iotivity 0.9.0
[platform/upstream/iotivity.git] / service / protocol-plugin / lib / cpluff / examples / cpfile / plugins / special / special.c
1 /*
2  * Copyright 2007 Johannes Lehtinen
3  * This file is free software; Johannes Lehtinen gives unlimited
4  * permission to copy, distribute and modify it.
5  */
6
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <cpluff.h>
11 #include <core.h>
12
13 #if defined(HAVE_LSTAT)
14 #define STAT lstat
15 #elif defined(HAVE_STAT)
16 #define STAT stat
17 #endif
18
19
20 /* ------------------------------------------------------------------------
21  * Internal functions
22  * ----------------------------------------------------------------------*/
23
24 /**
25  * Classifies a file by using stat(2). This classifier does not need
26  * any classifier data so we use NULL as dummy data pointer. Therefore
27  * we do not need a plug-in instance either as there is no data to be
28  * initialized.
29  */
30 static int classify(void *dummy, const char *path) {
31 #ifdef STAT
32         struct stat s;
33         const char *type;
34         
35         // Stat the file
36         if (STAT(path, &s)) {
37                 fflush(stdout);
38                 perror("stat failed");
39                 
40                 // No point for other classifiers to classify this
41                 return 1;
42         }
43         
44         // Check if this is a special file
45         if ((s.st_mode & S_IFMT) == S_IFDIR) {
46                 type = "directory";
47 #ifdef S_IFCHR
48         } else if ((s.st_mode & S_IFMT) == S_IFCHR) {
49                 type = "character device";
50 #endif
51 #ifdef S_IFBLK
52         } else if ((s.st_mode & S_IFMT) == S_IFBLK) {
53                 type = "block device";
54 #endif
55 #ifdef S_IFLNK
56         } else if ((s.st_mode & S_IFMT) == S_IFLNK) {
57                 type = "symbolic link";
58 #endif
59         } else {
60                 
61                 // Did not recognize it, let other plug-ins try
62                 return 0;
63         }
64                 
65         // Print recognized file type
66         fputs(type, stdout);
67         putchar('\n');
68         return 1;
69 #else
70         return 0;
71 #endif
72 }
73
74
75 /* ------------------------------------------------------------------------
76  * Exported classifier information
77  * ----------------------------------------------------------------------*/
78
79 CP_EXPORT classifier_t cp_ex_cpfile_special_classifier = { NULL, classify };