©2004 by Marc J. Rochkind. All rights reserved. Portions marked "Open Source" may be copied under license.

 

Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

c3/slink.c

Go to the documentation of this file.
00001 /*
00002     Symbolic-link test program (uses stat and lstat)
00003     AUP2, Sec. 3.5.1 (not in book)
00004 
00005     Copyright 2003 by Marc J. Rochkind. All rights reserved.
00006     May be copied only for purposes and under conditions described
00007     on the Web page www.basepath.com/aup/copyright.htm.
00008 
00009     The Example Files are provided "as is," without any warranty;
00010     without even the implied warranty of merchantability or fitness
00011     for a particular purpose. The author and his publisher are not
00012     responsible for any damages, direct or incidental, resulting
00013     from the use or non-use of these Example Files.
00014 
00015     The Example Files may contain defects, and some contain deliberate
00016     coding mistakes that were included for educational reasons.
00017     You are responsible for determining if and how the Example Files
00018     are to be used.
00019 
00020 */
00021 #include "defs.h"
00022 
00023 const char *get_file_type(mode_t mode)
00024 {
00025     if (S_ISBLK(mode))
00026         return "block special file";
00027     if (S_ISCHR(mode))
00028         return "character special file";
00029     if (S_ISDIR(mode))
00030         return "directory";
00031     if (S_ISFIFO(mode))
00032         return "pipe or FIFO special file";
00033     if (S_ISREG(mode))
00034         return "regular file";
00035     if (S_ISLNK(mode))
00036         return "symbolic link";
00037     if (S_ISSOCK(mode))
00038         return "socket";
00039     return "unknown type";
00040 }
00041 
00042 int main(void)
00043 {
00044     struct stat sbuf;
00045 
00046     (void)unlink("a.tmp");
00047     (void)creat("b.tmp", PERM_FILE);
00048 
00049     if (symlink("b.tmp", "a.tmp") == -1)
00050         syserr("symlink");
00051     if (link("b.tmp", "c.tmp") == -1)
00052         syserr("link");
00053 
00054     if (stat("b.tmp", &sbuf) == -1)
00055         syserr("stat 1");
00056     printf("stat(\"b.tmp\"):\n");
00057     printf("\tst_ino = %u\n", (unsigned)sbuf.st_ino);
00058     printf("\tst_nlink = %u\n", (unsigned)sbuf.st_nlink);
00059     printf("\tst_mode = 0%o\n", (unsigned)sbuf.st_mode);
00060     printf("\tfile type: %s\n", get_file_type(sbuf.st_mode));
00061 
00062     if (stat("a.tmp", &sbuf) == -1)
00063         syserr("stat 2");
00064     printf("\nstat(\"a.tmp\"):\n");
00065     printf("\tst_ino = %u\n", (unsigned)sbuf.st_ino);
00066     printf("\tst_nlink = %u\n", (unsigned)sbuf.st_nlink);
00067     printf("\tst_mode = 0%o\n", (unsigned)sbuf.st_mode);
00068     printf("\tfile type: %s\n", get_file_type(sbuf.st_mode));
00069 
00070     if (lstat("a.tmp", &sbuf) == -1)
00071         syserr("lstat 1");
00072     printf("\nlstat(\"a.tmp\"):\n");
00073     printf("\tst_ino = %u\n", (unsigned)sbuf.st_ino);
00074     printf("\tst_nlink = %u\n", (unsigned)sbuf.st_nlink);
00075     printf("\tst_mode = 0%o\n", (unsigned)sbuf.st_mode);
00076     printf("\tfile type: %s\n", get_file_type(sbuf.st_mode));
00077 
00078     if (stat("c.tmp", &sbuf) == -1)
00079         syserr("stat 3");
00080     printf("\nstat(\"c.tmp\"):\n");
00081     printf("\tst_ino = %u\n", (unsigned)sbuf.st_ino);
00082     printf("\tst_nlink = %u\n", (unsigned)sbuf.st_nlink);
00083     printf("\tst_mode = 0%o\n", (unsigned)sbuf.st_mode);
00084     printf("\tfile type: %s\n", get_file_type(sbuf.st_mode));
00085 
00086     if (lstat("c.tmp", &sbuf) == -1)
00087         syserr("lstat 2");
00088     printf("\nlstat(\"c.tmp\"):\n");
00089     printf("\tst_ino = %u\n", (unsigned)sbuf.st_ino);
00090     printf("\tst_nlink = %u\n", (unsigned)sbuf.st_nlink);
00091     printf("\tst_mode = 0%o\n", (unsigned)sbuf.st_mode);
00092     printf("\tfile type: %s\n", get_file_type(sbuf.st_mode));
00093 
00094     exit(EXIT_SUCCESS);
00095 }

Generated on Fri Apr 23 10:56:55 2004 for AUP2 Example Source by doxygen 1.3.1