0% found this document useful (0 votes)
5 views6 pages

Lab8 1

This document is a lab report for CSE-302L Systems Programming Lab, submitted by Anees Ur Rehman. It includes lab assessment rubrics, tasks related to implementing the 'ls' command in C, and a sample code for the implementation. The report is submitted to Dr. Madiha Sher at the University of Engineering and Technology, Peshawar.

Uploaded by

mranees5323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Lab8 1

This document is a lab report for CSE-302L Systems Programming Lab, submitted by Anees Ur Rehman. It includes lab assessment rubrics, tasks related to implementing the 'ls' command in C, and a sample code for the implementation. The report is submitted to Dr. Madiha Sher at the University of Engineering and Technology, Peshawar.

Uploaded by

mranees5323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

FILES AND

DIRECTORIES

LAB # 08

Fall 2024

CSE-302L
Systems Programming Lab

Submitted by: Anees Ur Rehman


Registration No.: 22PWCSE2168
Class Section: B

“On my honor, as student of University of Engineering and Technology, I have neither given
nor received unauthorized assistance on this academic work.”

Student Signature: ______________

Submitted to:
Dr. Madiha Sher
Friday, November 29, 2024

Department of Computer Systems Engineering


University of Engineering and Technology, Peshawar
CSE 302L: SYSTEMS PROGRAMMING LAB

LAB ASSESSMENT RUBRICS

Criteria & Point Outstanding Acceptable Considerable Below Score


Assigned 2 1.5 1 Expectations
0.5
Attendance and Attended in Attended in Attended late but Attended late not
Attentiveness in Lab proper Time and proper Time but attentive in Lab attentive in Lab
PLO08 attentive in Lab not attentive in
Lab

Capability of writing Right attempt/ no Right attempt/ Right attempt/


Program/Algorithm/Drawing errors and well no errors but not minor errors and Wrong attempt
Flow Chart formatted well formatted not well
PLO1, PLO2, PLO3, PLO5 formatted

Result or Output/ 100% target has 75% target has 50% target has None of the
Completion of target in Lab been completed been completed been completed outputs are
PLO9 and well and well but not well correct.
formatted. formatted. formatted.

Overall, Knowledge Demonstrates Demonstrates Has partial idea Has poor idea
PLO10, excellent good knowledge about the Lab and about the Lab and
knowledge of lab of lab procedure procedure
followed followed

Attention to Lab Report Submission of Lab Submission of Late Submission Late Submission
PLO4, Report in Proper Lab Report in with proper very poor
Time i.e., in next proper time but documentation. documentation
day of lab, with not with proper
proper documentation.
documentation.

Instructor:

Name: _________________________ Signature: ________________________


FILES AND DIRECTORIES
Tasks:
Task 1: Implement ls command
Code in C:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
#include <grp.h>
#include <pwd.h>

int displayFileStatistics(struct dirent *directoryEntry)


{
struct stat entryStatistics;
struct group *g;
struct passwd *p;
char *ctime_no_newline;

if (stat(directoryEntry->d_name, &entryStatistics) == -1)


{
perror("Error in statistics of the directory
contents.\n");
return -1;
}

if (!strcmp(directoryEntry->d_name, ".") ||
!strcmp(directoryEntry->d_name, ".."))
return 0;

printf("\t");
// print if entry is a directory or a file
S_ISDIR(entryStatistics.st_mode) ? printf("d ") :
printf("- ");

// printing permissions by taking bitwise and of


permission bit with mode.
S_IRUSR & entryStatistics.st_mode ? printf("r") :
printf("-");
S_IWUSR & entryStatistics.st_mode ? printf("w") :
printf("-");
S_IXUSR & entryStatistics.st_mode ? printf("x") :
printf("-");
S_IRGRP & entryStatistics.st_mode ? printf("r") :
printf("-");
S_IWGRP & entryStatistics.st_mode ? printf("w") :
printf("-");
S_IXGRP & entryStatistics.st_mode ? printf("x") :
printf("-");
S_IROTH & entryStatistics.st_mode ? printf("r") :
printf("-");
S_IWOTH & entryStatistics.st_mode ? printf("w") :
printf("-");
S_IXOTH & entryStatistics.st_mode ? printf("x") :
printf("-");

// No of links pointing to file or directory


printf(" %ld", entryStatistics.st_nlink);

// User Name from uid


p = getpwuid(entryStatistics.st_uid);
printf(" %s", p->pw_name);

// Group Name from gid


g = getgrgid(entryStatistics.st_gid);
printf(" %s", g->gr_name);

// Time and date of last access


ctime_no_newline =
strtok(ctime(&entryStatistics.st_ctime), "\n");
printf(" %s", ctime_no_newline);

// Entry name
printf(" %s\n", directoryEntry->d_name);
return 0;
}

int main(int argc, char *argv[])


{
if (argc > 3)
{
fprintf(stderr, "Need at most three args. Usage:\n%s
[OPTIONAL] [-l | FILE_NAME]\n", argv[0]);
return 1;
}

DIR *thisDirectory = opendir(".");


struct dirent *directoryEntries;
// ls
if (argc == 1)
{
printf("Directory Content:\n");
while ((directoryEntries = readdir(thisDirectory)) !=
NULL)
{
if (!strcmp(directoryEntries->d_name, ".") ||
!strcmp(directoryEntries->d_name, ".."))
continue;
printf("\t%s\n", directoryEntries->d_name);
}
}

// ls -l
else if ((argc == 2) && (!strcmp(argv[1], "-l")))
{
printf("Directory Content Statistics:\n");
while ((directoryEntries = readdir(thisDirectory)) !=
NULL)
if (displayFileStatistics(directoryEntries) == -1)
return 1;
}

// ls file.xyz
else if (argc == 2)
{
while ((directoryEntries = readdir(thisDirectory)) !=
NULL)
if (!strcmp(directoryEntries->d_name, argv[1]))
{
printf("File Found:\n\t%s\n",
directoryEntries->d_name);
break;
}
}

// ls -l file.xyz
else if ((argc == 3) && (!strcmp(argv[1], "-l")))
{
while ((directoryEntries = readdir(thisDirectory)) !=
NULL)
if (!strcmp(argv[2], directoryEntries->d_name))
{
printf("File Found. Statistics:\n");
if (displayFileStatistics(directoryEntries) ==
-1)
return 1;
break;
}
}

return 0;
}

Output:

You might also like