Beruflich Dokumente
Kultur Dokumente
• If buf is not NULL, getcwd copies the name into buf. If buf is
NULL, POSIX states that the behavior of getcwd is undefined.
• In some implementations, getcwd uses malloc to create a buffer
to hold the pathname. (!Do not rely on this behavior !)
• You should always supply getcwd with a buffer large enough to
fit a string containing the pathname.
• The PATH_MAX constant may or may not be defined in limits.h.
The optional POSIX constants can be omitted from limits.h if
their values are indeterminate but larger than the required POSIX
minimum. For PATH_MAX, the _POSIX_PATH_MAX constant
specifies that an implementation must accommodate pathname
lengths of at least 255.
• A vendor might allow PATH_MAX to depend on the amount of
available memory space on a specific instance of a specific
implementation.
BIL 244 – System Programming
A complete program to output the current working directory
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#ifndef PATH_MAX
#define PATH_MAX 255
#endif
int main(void) {
char mycwd[PATH_MAX];
if (getcwd(mycwd, PATH_MAX) == NULL) {
perror("Failed to get current working directory");
return 1;
}
printf("Current working directory: %s\n", mycwd);
return 0;
}
The current working directory
• A more flexible approach uses the pathconf function to determine the real
value for the maximum path length at run time. The pathconf function is
one of a family of functions that allows a program to determine system and
runtime limits in a platform-independent way.
• The sysconf function takes a single argument, which is the name of a
configurable systemwide limit such as the number of clock ticks per second
(_SC_CLK_TCK) or the maximum number of processes allowed per user
(_SC_CHILD_MAX).
• The pathconf and fpathconf functions report limits associated with a
particular file or directory.
• The fpathconf takes a file descriptor and the limit designator as
parameters, so the file must be opened before a call to fpathconf.
• The pathconf function takes a pathname and a limit designator as
parameters, so it can be called without the program actually opening the file.
• The sysconf function returns the current value of a configurable system
limit that is not associated with files. Its name parameter designates the limit.
#include <uninstd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
long maxpath;
char *mycwdp;
if ((maxpath = pathconf(".", _PC_PATH_MAX)) == -1) {
perror("Failed to determine the pathname length");
return 1;
}
if ((mycwdp = (char *) malloc(maxpath)) == NULL) {
perror("Failed to allocate space for pathname");
return 1;
}
#include <dirent.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
if (argc != 2) {
fprintf(stderr, "Usage: %s directory_name\n", argv[0]);
return 1;
}
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
....
if (link("/dirA/name1", "/dirB/name2") == -1)
perror("Failed to make a new link in /dirB")
.....
link and unlink
• The link function creates a new directory entry for the existing file
specified by path1 in the directory specified by path2.
#include <unistd.h>
int link(const char *path1, const char *path2);
#include <stdio.h>
#include <unistd.h>
....
if (symlink("/dirA/name1", "/dirB/name2") == -1)
perror("Failed to create symbolic link in /dirB");
.....