Makros "undeclared" in selbst definierten Funktionen ?
-
Hallo Leute , ich habe ein kleines Programm geschrieben in dem die Information von verschiedenen Dateien ausgegeben werden können .
Um das ganze übersichtlicher zu halten habe ich funktionalität ausgelagert.
Allerdings bekomme ich folgende Fehlermeldung innerhalb der Funktionen.FileInfo.c: In function ‘testFileType’: FileInfo.c:90: warning: implicit declaration of function ‘S_ISSOCK’ FileInfo.c: In function ‘printRights’: FileInfo.c:116: error: ‘S_ISVTX’ undeclared (first use in this function) FileInfo.c:116: error: (Each undeclared identifier is reported only once FileInfo.c:116: error: for each function it appears in.) FileInfo.c: In function ‘printDeviceInfo’: FileInfo.c:217: error: ‘S_IFCHR’ undeclared (first use in this function) FileInfo.c:217: error: ‘S_IFSOCK’ undeclared (first use in this function) FileInfo.c:217: error: ‘S_IFBLK’ undeclared (first use in this function) FileInfo.c: In function ‘showCloseOnExecFlag’: FileInfo.c:226: error: ‘S_IFIFO’ undeclared (first use in this function)
Innerhlab der main kennt er die Makros komischer Weise nicht ?
Könnte mir vieleicht jemand sagen woran das liegt ?
Ich kann die Fehlermeldung irgendwie nicht deuten.Greets
-
Hast du auch eine Headeratei gemacht und diese eingebunden?
-
Ja innerhalb der main kennt er die makros ja auch ...
Nur wenn ich sie in den Funktionen nutzte bekomme ich die besagten Fehlermeldungen./********HEADERS************/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <time.h> #include <grp.h> #include <pwd.h> #include <fcntl.h> #include <errno.h> #include <string.h> /***************************/
-
stehen diese Funktionen denn in der selben Datei wie die main()? Wenn ja, unterhalb der #include's? Wenn nein, hast du in der FileInfo.c die selben Header eingebunden wie in der main.c?
-
Ich zeig euch einfach mal den Code :
/* main.c DateiInfo Created by Sebastian Boldt on 20.04.11. Copyright 2011 All rights reserved. */ /********HEADERS************/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <time.h> #include <grp.h> #include <pwd.h> #include <fcntl.h> #include <errno.h> #include <string.h> /***************************/ /*******PROTOTYPES*********/ int lstat(const char *file_name, struct stat *buf); /*Prototyp lstat*/ void testFileType(struct stat*); void printRights(struct stat*); void clearScreen(void); void printUserAndGroupInfos(struct group *,struct passwd *); void printFileInfo(struct stat*); void printDeviceInfo(struct stat *); void showCloseOnExecFlag(struct stat*,int); char* getusershell(void); /**************************/ int main (int argc, const char * argv[]) { /****************LOCALS*****************/ struct stat info; struct group *MyGroup; struct passwd *MyPasswd; int i ; /***********CLEAR SCREEN****************/ clearScreen(); /***************************************/ /***********MAIN INFO*******************/ printf("*****************DATEI_INFO_AUSGABE***************\n"); printf("\nUserShell: (%s) ",getusershell()); for(i = 0 ; i < argc ; i++) /*Iteration durch Übergabeparameter*/ { int fd; printf("\n******************File Number : %i********************* ",i); printf("\nFilename %s ",argv[i]); lstat(argv[i], &info); testFileType(&info); printRights(&info); printUserAndGroupInfos(MyGroup, MyPasswd); printFileInfo(&info); printDeviceInfo(&info); /*********CLOSE ON EXEC FLAG************/ printf("\nBefore setting Close on exec()"); fd = open(argv[i], O_RDWR); showCloseOnExecFlag(&info,fd); printf("\nAfter setting the Close on Exec Flag"); fcntl(fd,F_SETFD,FD_CLOEXEC); showCloseOnExecFlag(&info,fd); close(fd); } /*****************************************/ return 0; } /****************************************************************************/ /******************This function detects the type of file********************/ /****************************************************************************/ void testFileType(struct stat* info) { if(S_ISDIR(info->st_mode))printf("\nFile is a directory"); else if(S_ISCHR (info->st_mode))printf("\nFile is a character special file"); else if(S_ISBLK (info->st_mode))printf("\nFile is a block special file"); else if(S_ISREG (info->st_mode))printf("\nFile is a regular file"); else if(S_ISFIFO(info->st_mode))printf("\nFile is a FIFO File or a pipe"); else if(S_ISLNK (info->st_mode))printf("\nFile is a symbolic link"); else if(S_ISSOCK(info->st_mode))printf("\nFile is a socket"); } /****************************************************************************/ /****************************************************************************/ /****************************************************************************/ /****************************************************************************/ /****************This function detects the righst of file********************/ /****************************************************************************/ void printRights(struct stat *info) { printf("\nRights Oktal :%o",info->st_mode); /***************SPECIAL RIGHTS******************/ if(((info->st_mode)&(S_ISUID))==S_ISUID)printf("\nSet User ID on exicution : TRUE "); else printf("\nSet User ID on execution : FALSE "); if(((info->st_mode)&(S_ISGID ))==S_ISGID )printf("\nSet Group ID on execution : TRUE"); printf("\nSet Grp. ID on execution : FALSE"); /***************OWNER**************/ printf("\n\n Usr.Grp.Oth."); /**************4->3***************/ if(((info->st_mode) & (S_IRWXU|S_ISVTX))==(S_IRWXU|S_ISVTX))printf("\n drwx"); else if(((info->st_mode) & S_IRWXU)==S_IRWXU)printf("\n -rwx"); else if(((info->st_mode) & (S_IRUSR|S_IWUSR |S_ISVTX))==(S_IRUSR|S_IWUSR|S_ISVTX))printf("\n drw-"); else if(((info->st_mode) & (S_IWUSR|S_IXUSR |S_ISVTX))==(S_IWUSR|S_IXUSR |S_ISVTX))printf("\n d-wx"); else if(((info->st_mode) & (S_IRUSR|S_IXUSR |S_ISVTX))==(S_IRUSR|S_IXUSR |S_ISVTX))printf("\n dr-x"); /***************2****************/ else if(((info->st_mode) & (S_IRUSR|S_ISVTX))==(S_IRUSR|S_ISVTX))printf("\n dr--"); else if(((info->st_mode) &(S_IWUSR|S_ISVTX))==(S_IWUSR|S_ISVTX))printf("\n d-w-"); else if(((info->st_mode) & (S_IXUSR|S_ISVTX))==(S_IXUSR|S_ISVTX))printf("\n d--x"); else if(((info->st_mode) & (S_IRUSR|S_IWUSR))==(S_IRUSR|S_IWUSR))printf("\n -rw-"); else if(((info->st_mode) & (S_IWUSR|S_IXUSR))==(S_IWUSR|S_IXUSR))printf("\n --wx"); else if(((info->st_mode) & (S_IRUSR|S_IXUSR))==(S_IRUSR|S_IXUSR))printf("\n -r-x"); /***************1****************/ else if(((info->st_mode) & (S_ISVTX))== S_ISVTX)printf("\n d---"); else if(((info->st_mode) & (S_IRUSR))== S_IRUSR)printf("\n -r--"); else if(((info->st_mode) & S_IWUSR)==S_IWUSR)printf("\n --w-"); else if(((info->st_mode) & S_IXUSR)==S_IXUSR) printf("\n ---x"); /***************0***************/ else printf("----"); /**************GROUP**************/ /**************4->3***************/ printf("|"); if(((info->st_mode) & S_IRWXG)==S_IRWXG)printf("rwx"); /***************2****************/ else if(((info->st_mode) & (S_IRGRP|S_IWGRP))==(S_IRGRP|S_IWGRP))printf("rw-"); else if(((info->st_mode) & (S_IWGRP|S_IXGRP))==(S_IWGRP|S_IXGRP))printf("-wx"); else if(((info->st_mode) & (S_IRGRP|S_IXGRP))==(S_IRGRP|S_IXGRP))printf("r-x"); /***************1****************/ else if(((info->st_mode) & (S_IRGRP))== S_IRGRP)printf("r-_"); else if(((info->st_mode) & S_IWGRP)==S_IWGRP)printf("-w-"); else if(((info->st_mode) & S_IXGRP)==S_IXGRP) printf("--x"); /***************0***************/ else printf("---"); /**************OTHER**************/ printf("|"); if(((info->st_mode) & S_IRWXO)==S_IRWXO)printf("\n rwx"); else if(((info->st_mode) & (S_IROTH|S_IWOTH))==(S_IROTH|S_IWOTH))printf("rw-"); else if(((info->st_mode) & (S_IWOTH|S_IXOTH))==(S_IWOTH|S_IXOTH))printf("-wx"); else if(((info->st_mode) & (S_IROTH|S_IXOTH))==(S_IROTH|S_IXOTH))printf("r-x"); /***************1****************/ else if(((info->st_mode) & (S_IROTH))== S_IROTH)printf("r--"); else if(((info->st_mode) & S_IWOTH)==S_IWOTH)printf("-w-"); else if(((info->st_mode) & S_IXOTH)==S_IXOTH) printf("--x"); /***************0***************/ else printf("---"); printf("\n"); } /****************************************************************************/ /****************************************************************************/ /****************************************************************************/ void clearScreen(void) { int k; for (k = 0 ; k < 100 ; k++)printf("\n"); } void printUserAndGroupInfos(struct group *MyGroup,struct passwd *MyPasswd) { MyGroup = getgrgid(getgid()); char *GroupName; GroupName = MyGroup->gr_name; MyPasswd = getpwuid(getuid()); char *UserName; UserName = MyPasswd->pw_name; printf("\nUserID: (%i) ",getuid()); printf(" UserName: (%s) ",UserName); printf("\nGroupID: (%i) ",getgid()); printf(" GroupName: (%s) ",GroupName); } void printFileInfo(struct stat *info) { printf("\nInode :%llu",info->st_ino); printf("\nReferencecount :%hu",info->st_nlink); printf("\nSize in Bytes :%llu",(long long unsigned)info->st_size); printf("\nSize in kB :%llu",(long long unsigned)(info->st_size/1000)); printf("\nSize in mB :%llu",(long long unsigned)(info->st_size/1000000)); printf("\nBufferSize :%d",info->st_blksize); printf("\nBlocks a 512 Byte :%llu",(long long unsigned)info->st_blocks); printf("\nTime of last access :%s",ctime(&info->st_atime)); printf("Time of last Modification :%s",ctime(&info->st_mtime)); } void printDeviceInfo(struct stat *info) { if( (((info->st_mode)&(S_IFCHR))==S_IFCHR) || (((info->st_mode)&(S_IFSOCK))==S_IFSOCK) || (((info->st_mode)&(S_IFBLK))==S_IFBLK) ) printf("dev = %2d /%2d",major(info->st_rdev),minor(info->st_rdev)); else printf("\nNo Major and Minor Device Number because file is no DeviceFile"); } void showCloseOnExecFlag(struct stat *info,int fd) { if((info->st_mode&S_IFIFO)==S_IFIFO) return; /*PROOFS FILE OF BEING A PIPE*/ if((fcntl(fd,F_GETFD)&FD_CLOEXEC)==(0)) { printf("\nThe file will remain open across exec()"); } else printf("\nthe file will be closed upon execution of exec()"); }
-
Hallo,
S_ISSOCK und S_ISVTX sind nirgendwo definiert (schau mal in die <sys/stat.h>).
Und bei den anderen Makros hast du dich verschrieben...P.S. Welchen Compiler (bzw. Linux-System) verwendest du? Denn unter http://www.mail-archive.com/debian-glibc@lists.debian.org/msg43068.html gibt es einen Bugeintrag diesbezüglich...
P.S. #2: Und deine printRights-Funktion ist äußerst umständlich... (s. z.B. http://www.mrunix.de/forums/showthread.php?t=31940 - statt 'str' dann einfach putc() benutzen) - mit einem Array und einer Schleife wäre es sogar noch eleganter
-
Wo habe ich mich verschrieben ?
-
Bei S_IFCHR, S_IFSOCK, S_IFBLK und S_IFIFO. Die Makros heißen S_ISCHR, S_ISSOCK, S_ISBLK und S_ISFIFO.
Edit: Oh, sorry, nehme alles zurück! Aber (aus der man page von stat):
POSIX beschreibt die Bits S_IFMT, S_IFSOCK, S_IFLNK, S_IFREG, S_IFBLK, S_IFDIR, S_IFCHR, S_IFIFO und S_ISVTX nicht, sondern verlangt stattdessen die Benutzung der Makros S_ISDIR() etc.
-
Dann bekomme ich genau die selben Fehlermeldungen für die anderern Makros ..
FileInfo.c: In function ‘testFileType’:
FileInfo.c:92: warning: implicit declaration of function ‘S_ISSOCK’
FileInfo.c: In function ‘printRights’:
FileInfo.c:118: error: ‘S_ISVTX’ undeclared (first use in this function)
FileInfo.c:118: error: (Each undeclared identifier is reported only once
FileInfo.c:118: error: for each function it appears in.)
FileInfo.c: In function ‘printDeviceInfo’:
FileInfo.c:219: error: ‘S_ISCHR’ undeclared (first use in this function)
FileInfo.c:219: error: ‘S_ISSOCK’ undeclared (first use in this function)
FileInfo.c:219: error: ‘S_ISBLK’ undeclared (first use in this function)
FileInfo.c:220: warning: implicit declaration of function ‘major’
FileInfo.c:220: warning: implicit declaration of function ‘minor’
FileInfo.c: In function ‘showCloseOnExecFlag’:
FileInfo.c:228: error: ‘S_ISFIFO’ undeclared (first use in this function)
-
S_ISCHR, S_ISSOCK, S_ISBLK und S_ISFIFO sind Makros, die als Argument den Wert des st_mode-Feldes erwarten, so, wie du es auch schon in der Funktion testFileType() gemacht hast.
-
Ok das hab ich jetzt auch gesehen ...
Allerdings findet er immernoch I_SVTX nicht :S ?!?