Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Тестовые примеры / ch7 / test_chown
.C #include <iostream.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
int main (int argc, char* argv[])
{
if (argc < 3)
{
cerr << "Usage: " << argv[0] << " <usr_name> <file> ...\n";
return 1;
}
struct passwd * pwd = getpwnam(argv[1]); /* convert user name to UID */
uid_t UID = pwd->pw_uid;
struct stat statv;
if (UID == (uid_t)-1)
cerr<< "Invalid user name\n";
else for (int i=2; i < argc; i++) /* do for each file specified */
if (stat(argv[i],&statv)==0)
{
if (chown(argv[i], UID, statv.st_gid)) perror("chown");
} else perror("stat");
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
int main (int argc, char* argv[])
{
if (argc < 3)
{
cerr << "Usage: " << argv[0] << " <usr_name> <file> ...\n";
return 1;
}
struct passwd * pwd = getpwnam(argv[1]); /* convert user name to UID */
uid_t UID = pwd->pw_uid;
struct stat statv;
if (UID == (uid_t)-1)
cerr<< "Invalid user name\n";
else for (int i=2; i < argc; i++) /* do for each file specified */
if (stat(argv[i],&statv)==0)
{
if (chown(argv[i], UID, statv.st_gid)) perror("chown");
} else perror("stat");
return 0;
}
Соседние файлы в папке ch7