Just an assignment from my Linux programming course.
I have only done a small part of work of the ls command, after all, it’s a program with 400+ lines, if you want to study full source codes of ls command(in which there are 4000+ lines codes), you can get it from here.
Honestly, this program is so much different from the Linux source codes, because I have used STL, which has hardly appeared in the Linux source codes but very helpful to simple codes.
The functions I have finished:
With option -l: use a long listing format:
With option -a: do not ignore dotfiles:
With option -R: show subdirectories:
With option -f: show files without sorting:
Show files with using default symbols:
Besides, this program can also display files beautifully in vertical order, just like the ls command.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
/* * File Name: sls.cpp * Author: razrLeLe * Mail: razrlele@gmail.com */ #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <string.h> #include <sys/stat.h> #include <memory.h> #include <malloc.h> #include <malloc.h> #include <getopt.h> #include <grp.h> #include <pwd.h> #include <unistd.h> #include <fcntl.h> #include <time.h> #include <error.h> #include <sys/types.h> #include <linux/kdev_t.h> #include <sys/ioctl.h> #include <vector> #include <algorithm> #include <ctype.h> #define OPT_v 0x0001 /* -v check version */ #define OPT_h 0x0002 /* -h check help information */ #define OPT_l 0x0004 /* -l show files with a long listing format */ #define OPT_a 0x0008 /* -a show all files (including dotfiles)*/ #define OPT_R 0x0010 /* -R show sub directory */ #define OPT_f 0x0020 /* -f without sorting the results */ #define DONE_SUCCESS 0 using namespace std; struct winsize w; // the width and height of terminal windows typedef struct _Node { char path[1024]; char name[256]; int length; struct stat st; bool operator < ( const _Node b) const{ int lena = strlen(name), lenb = strlen(b.name); int len = min(lena, lenb); int i; for(i = 0; i < len; i++) if( name[i] != b.name[i]) return tolower(name[i]) < tolower(b.name[i]); return lena < lenb; } } nNode; bool cmp(nNode a, nNode b) { return a < b; } int createlslink(char *path, int flag, vector<nNode> &toShow) { DIR *dp; struct dirent *entry; struct stat statbuf; char abspath[1024]; if(( dp = opendir(path)) == NULL) { fprintf(stderr, "cannot open directory:%s\n", path); return -1; } if( chdir(path) == -1) { fprintf(stderr, "cannot cd directory:%s\n", path); return -2; } if( NULL == getcwd(abspath, 1024)) { fprintf(stderr, "getcwd error!\n"); return -3; } while(( entry = readdir(dp)) != NULL) { lstat(entry->d_name, &statbuf); if(( !(flag & OPT_a)) && (entry->d_name[0] == '.')) continue; else { nNode tmpNode; tmpNode.length = strlen(entry->d_name); strcpy(tmpNode.path, abspath); strcpy(tmpNode.name, entry->d_name); memcpy( &tmpNode.st, &statbuf, sizeof(struct stat)); toShow.push_back(tmpNode); } } if( -1 == chdir("..")) { fprintf(stderr, "cannot cd directory:..\n"); return -3; } if(( closedir(dp)) == -1) { fprintf(stderr, "cannot close dp\n"); return -4; } return DONE_SUCCESS; } void findRowNum(vector<nNode> toShow, int &rownum, vector<int> &colspace) //to show file order vertially { bool ok; rownum = 0; do { ok = true; rownum++; int col_count = 0; for( int i = 0; i < toShow.size(); i += rownum) { int tmpnum = 0; for(int j = i; j < i+rownum; j++)//find max strlen in current col tmpnum = max(tmpnum, (int)strlen(toShow[j].name)); col_count += tmpnum + 2; colspace.push_back(tmpnum + 2); if( col_count > w.ws_col) { colspace.clear(); //rownum is to small ok = false; break; } } }while(!ok); return ; } void format_normal_print(vector<nNode> toShow, int flag) { int rownum; vector<int> colspace; findRowNum(toShow, rownum, colspace); int colnum = colspace.size(); for(int i = 0; i < rownum; i++) { int order = 0, strspace; for(int j = i; j < toShow.size(); j += rownum) { strspace = colspace[order++]; if( ((strcmp(toShow[j].name, ".") == 0 ) || (strcmp(toShow[j].name, "..") == 0 )) && (flag & OPT_a)) { printf("%-*s", strspace, toShow[j].name); } else { if( S_ISDIR(toShow[j].st.st_mode)) printf("\033[;32m%-*s\033[0m", strspace, toShow[j].name); else printf("%-*s", strspace, toShow[j].name); } } printf("\n"); } return ; } void format_long_print( vector<nNode> toShow, int flag) { char buf[12]; struct passwd *password; struct group *grp; struct tm *tm_ptr; char linkfilebuf[1024]; char pathfilename[1024]; char filename[256]; if( toShow.empty()) return ; for( vector<nNode>::iterator p = toShow.begin(); p != toShow.end(); p++) { if( !strcmp(p->name, ".") || !strcmp(p->name, "..")) { printf("%s\n", p->name); } else { /*permission*/ strcpy(pathfilename, p->path); strcpy(filename, p->name); memset(buf, '-', 11); buf[11] = 0; if( S_ISDIR(p->st.st_mode)) buf[0] = 'd'; else if( S_ISCHR(p->st.st_mode)) buf[0] = 'c'; else if( S_ISBLK(p->st.st_mode)) buf[0] = 'b'; else if( S_ISFIFO(p->st.st_mode)) buf[0] = 'p'; else if( S_ISLNK(p->st.st_mode)) buf[0] = 'l'; else if( S_ISSOCK(p->st.st_mode)) buf[0] = 's'; if ( S_IRUSR & p->st.st_mode) buf[1] = 'r'; if ( S_IWUSR & p->st.st_mode) buf[2] = 'w'; if ( S_IXUSR & p->st.st_mode) buf[3] = 'x'; if (S_IRGRP & p->st.st_mode) buf[4] = 'r'; if (S_IRGRP & p->st.st_mode) buf[5] = 'w'; if (S_IXGRP & p->st.st_mode) buf[6] = 'x'; if (S_IROTH & p->st.st_mode) buf[7] = 'r'; if (S_IWOTH & p->st.st_mode) buf[8] = 'w'; if (S_IXOTH & p->st.st_mode) buf[9] = 'x'; printf("%s ", buf); /* link number*/ printf("4d ", p->st.st_nlink); /* owner */ password = getpwuid(p->st.st_uid); if( password) { printf("$s ", password->pw_name); } else { printf("%d ", p->st.st_uid); } /* group */ grp = getgrgid(p->st.st_gid); if( grp) { printf("%s ", grp->gr_name); } else { printf("%d ", p->st.st_gid); } /* file bytes or dev number */ if( S_ISBLK(p->st.st_mode) || S_ISCHR(p->st.st_mode)) { printf("%3u, ", MAJOR(p->st.st_rdev)); printf("%3u ", MINOR(p->st.st_mode)); } else { printf("%7u ", p->st.st_size); } /* time */ tm_ptr = localtime( &(p->st.st_mtime)); printf("%02d-%02d ", tm_ptr->tm_mon + 1, tm_ptr->tm_mday); printf("%02d:%02d ", tm_ptr->tm_hour, tm_ptr->tm_min); /* filename */ if (S_ISDIR(p->st.st_mode)) printf("\033[;34m%s\033[0m\n", filename); else printf("%s ", filename); if( S_ISLNK(p->st.st_mode)) { strcat(pathfilename, "/"); strcat(pathfilename, p->name); int rslt = readlink(pathfilename, linkfilebuf, 1024); if (rslt < 0) { printf("readlink error!\n"); exit(-1); } linkfilebuf[rslt] = '\0'; printf("-> \033[;32m%s\033[0m", linkfilebuf); } printf("\n"); } } return ; } void showlslink(vector<nNode> toShow, int flag) { if ( flag & OPT_l) format_long_print(toShow, flag); else format_normal_print(toShow, flag); return ; } void lspath( char *path, int flag) { char pathname[1024]; char filename[1024]; vector<nNode> toShow; if( (createlslink(path, flag, toShow)) != DONE_SUCCESS) { printf("createlslink error!\n"); return ; } if( !(flag & OPT_f)) sort(toShow.begin(), toShow.end(), cmp); showlslink(toShow, flag); for( vector<nNode>::iterator p = toShow.begin(); p != toShow.end(); p++) { if( S_ISDIR(p->st.st_mode) && (flag & OPT_R)) if( strcmp(p->name, ".") && strcmp(p->name, "..")) { strcpy(pathname, p->path); strcat(pathname, "/"); strcat(pathname, p->name); printf("\033[;32m%s:\033[0m\n", pathname); lspath(pathname, flag); } } return ; } int lspath_with_default(int argc, char** argv, int optind, int listflag, char *path) { vector<nNode> toShow; char pathname[1024]; char filename[256]; struct stat statbuf; for(; optind < argc; optind++){ if( lstat( argv[optind], &statbuf) < 0) { fprintf(stderr, "stat error\n"); return -1; } if( S_ISDIR(statbuf.st_mode)){ lspath(argv[optind], listflag); } else { DIR *dp; struct dirent *entry; if( ( dp = opendir(path)) == NULL) { fprintf(stderr, "cannot open directory(default):%s\n", path); return -1; } while (( entry = readdir(dp)) != NULL){ nNode tmpNode; bool flag = true; if ( strlen(entry->d_name) == strlen(argv[optind])) { int len = strlen(argv[optind]); for( int i = 0; i < len; i++) { if( argv[optind][i] == '?') continue; else if(argv[optind][i] != entry->d_name[i]) { flag = false; break; } } if( !flag) continue; } else continue; tmpNode.length = strlen(entry->d_name); strcpy(tmpNode.path, path); strcpy(tmpNode.name, entry->d_name); memcpy( &(tmpNode.st), &statbuf, sizeof(struct stat)); toShow.push_back(tmpNode); break; } } } if( !(listflag & OPT_f)) sort(toShow.begin(), toShow.end(), cmp); showlslink(toShow, listflag); for( vector<nNode>::iterator p = toShow.begin(); p!= toShow.end(); p++) { if( S_ISDIR(p->st.st_mode) && (listflag & OPT_R)) { if( strcmp(p->name, ".") && strcmp(p->name, "..")){ strcpy(pathname, p->path); strcat(pathname, "/"); strcat(pathname, p->name); printf("\033[;32m%s:\033[0m\n", pathname); lspath(pathname, listflag); } } } return 0; } int main(int argc, char *argv[]) { int opt; char path[1024]; int listflag = 0; struct stat statbuf; ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); // get the size of the window struct option longopts[] = {{"help", 0,NULL, 'h'}, {"version", 0, NULL, 'v'}, {0, 0, 0, 0}}; while ((opt = getopt_long(argc, argv, "afhlvR", longopts, NULL)) != -1) { switch (opt) { case 'a': listflag |= OPT_a; break; case 'f': listflag |= OPT_f; break; case 'h': listflag |= OPT_h; break; case 'l': listflag |= OPT_l; break; case 'v': listflag |= OPT_v; break; case 'R': listflag |= OPT_R; break; case '?': printf("Usage:\n %s -h \n or \n %s --help \nfor help\n", argv[0], argv[0]); exit(-1); } } if (listflag & OPT_h) { printf("Usage: %s -afhlvR [directory1 [directory2],...]\n", argv[0]); return 0; } else if (listflag & OPT_v) { printf("sls version 0.1\nBy razrLeLe\n"); return 0; } if (NULL == getcwd(path, 1024)) return -1; if (optind == argc) { lspath(path, listflag); } else { lspath_with_default(argc, argv, optind, listflag, path); } return 0; } |