EmbDev.net

Forum: ARM programming with GCC/GNU tools The output of file rights for some reason does not work correctly


von Kirkina A. (Company: no) (alya)


Attached files:

Rate this post
useful
not useful
I'm learning to program under android, and I'm having a little problem.
The code of the program I attached to the file can read.
The problem is that it does not correctly display the resolution of the 
files through the sperm function.
This is what this program will output:
=======
 rwxrwx--x  .
 rwxr-x--x  ..
 rwxr-x--x  lost+found
 rwxr-x--x  bugreports
 rwxr-x--x  .layout_version
 rwxr-x--x  .tp.settings
 rwxr-x--x  BT_Addr
 rwxr-x--x  piggybank
 rwxr-x--x  server
 rwxr-x--x  fusemap
 rwxrwx---  media
 rwxrwx---  lp
 rwxrwx---  acdapi
 rwxrwx---  tombstones
 rwxrwx---  dontpanic
 rwxrwx---  data
 rwxrwx---  system
 rwxrwx---  drm
 rwxrwx---  misc
 rwxrwx---  app-asec
 rwxrwx---  agps_supl
 rwxrwx---  nvram
 rwxrwx---  amit
 rwxrwx---  sec
 rwxrwx---  mdl
 rwxrwx---  ssh
 rwxrwx---  user
 rwxrwx---  property
 rwxrwx---  app
 rwxrwx---  resource-cache
 rwxrwx---  local
 rwxrwx---  @btmtk
 rwxrwx---  anr
 rwxrwx---  systest
 rwxrwx---  app-lib
 rwxrwx---  security
 rwxrwx---  nfc_socket
 rwxrwx---  backup
 rwxrwx---  dalvik-cache
 rwxrwx---  app-private
 rwxrwx---  x
=======
And now compare this with the output of the original program from 
coreutils:
======
drwxrwx--- bluetooth net_bt            2017-06-23 02:01 @btmtk
-rw-rw-r-- nvram    nvram          64 2017-06-30 00:40 BT_Addr
drwxrwx--- system   system            2017-03-23 13:44 acdapi
drwxrwx--x gps      system            2017-06-30 00:40 agps_supl
drwxrwx--- system   system            2017-03-23 13:44 amit
drwxrwxr-x system   system            2017-06-30 00:40 anr
drwxrwx--x system   system            2017-06-30 00:40 app
drwx------ root     root              2017-03-23 13:44 app-asec
drwxrwx--x system   system            2017-06-29 16:19 app-lib
drwxrwx--x system   system            2017-03-23 13:44 app-private
drwx------ system   system            2017-06-23 02:01 backup
lrwxrwxrwx root     root              2017-06-23 02:01 bugreports -> 
/data/data/com.android.shell/files/bugreports
drwxrwx--x system   system            2017-06-30 00:16 dalvik-cache
drwxrwx--x system   system            2017-06-30 00:16 data
drwxr-x--- root     log               2017-03-23 13:44 dontpanic
drwxr-xr-x drm      system            2017-06-23 02:01 drm
-rwxr-xr-x root     root        13516 2017-06-26 20:13 fusemap
drwxr-x--x root     root              2017-06-27 22:07 local
drwxrwx--- root     root              1970-01-01 03:00 lost+found
drwxrwxrwx root     root              2017-06-23 02:01 lp
drwxrwxr-x shell    shell             2017-06-23 02:02 mdl
drwxrwx--- media_rw media_rw          2017-06-26 20:13 media
drwxrwx--t system   misc              2017-06-29 22:03 misc
drwxrwx--x nfc      radio             2017-03-23 13:44 nfc_socket
drwxrws--- root     nvram             2017-06-27 17:18 nvram
-rw------- system   system    4194304 2017-06-30 00:41 piggybank
drwx------ root     root              2017-06-30 17:31 property
drwxrwx--x system   system            2017-03-23 13:44 resource-cache
drwxr-x--- nvram    system            2017-06-30 00:40 sec
drwx--x--x system   system            2017-03-23 13:44 security
srwxrw---- system   system            2017-06-29 22:03 server
drwxr-x--- root     shell             2017-06-23 02:01 ssh
drwxrwxr-x system   system            2017-06-30 17:13 system
drwx------ system   system            2017-06-23 02:01 systest
drwx------ system   system            2017-06-26 19:04 tombstones
drwx--x--x system   system            2017-06-23 02:02 user
d---rwxrwx root     root              2017-06-23 16:59 x
=====
The conclusions are completely different and my program has everything 
wrong. Help me!!!

von Rolf Magnus (Guest)


Rate this post
useful
not useful
What did you specify as directory? I guess it's not the current 
directory, which is the only one that your program will work correctly 
with.
You're calling stat on only file names without path. If you had checked 
the return value of stat() for errors, you would have noticed that it 
will report "file not found" errors as soon as you try to apply you 
program to anything but the current directory.

von Kirkina A. (Company: no) (alya)


Rate this post
useful
not useful
Rolf Magnus wrote:
> What did you specify as directory? I guess it's not the current
> directory, which is the only one that your program will work correctly
> with.
> You're calling stat on only file names without path. If you had checked
> the return value of stat() for errors, you would have noticed that it
> will report "file not found" errors as soon as you try to apply you
> program to anything but the current directory.

No, this is all right.
I started the command ./ls system
=====
 rwxrwx--x  .
 rwxr-x--x  ..
 rwxr-x--x  lost+found
 rwxr-x--x  app
 rwxr-x--x  appjar
 rwxr-x--x  bin
 rwxr-x--x  build.prop
 rwxrwx---  data
 rwxrwx---  db
 rwxrwx---  dolby
 rwxrwx---  etc
 rwxrwx---  fonts
 rwxrwx---  framework
 rwxrwx---  lib
 rwxrwx---  media
 rwxrwx---  mobile_toolkit
 rwxrwx---  priv-app
 rwxrwx---  res
 rwxrwx---  tts
 rwxrwx---  usr
 rwxrwx---  vendor
 rwxrwx---  xbin
======
As you can see everything works great. And here's another example of 
starting a command ./ls /system/priv-app /system/app
=======
 rwxrwx--x  .
 rwxr-x--x  ..
 rwxr-x--x  BackupRestoreConfirmation.apk
 rwxr-x--x  CDS_INFO.apk
 rwxr-x--x  Calendar.apk
 rwxr-x--x  CalendarProvider.apk
 rwxr-x--x  ConfigUpdater.apk
 rwxr-x--x  Contacts.apk
 rwxr-x--x  ContactsProvider.apk
 rwxr-x--x  DefaultContainerService.apk
 rwxr-x--x  Dialer.apk
 rwxr-x--x  DownloadProvider.apk
 rwxr-x--x  ExternalStorageProvider.apk
 rwxr-x--x  FusedLocation.apk
 rwxr-x--x  GmsCore.apk
 rwxr-x--x  GoogleBackupTransport.apk
 rwxr-x--x  GoogleFeedback.apk
 rwxr-x--x  GoogleLoginService.apk
 rwxr-x--x  GoogleOneTimeInitializer.apk
 rwxr-x--x  GooglePartnerSetup.apk
 rwxr-x--x  GoogleServicesFramework.apk
 rwxr-x--x  InputDevices.apk
 rwxr-x--x  Keyguard.apk
 rwxr-x--x  NovaLauncher.apk
 rwxr-x--x  MediaProvider.apk
 rwxr-x--x  Mms.apk
 rwxr-x--x  PrivacyProtectionLock.apk
 rwxr-x--x  ProxyHandler.apk
 rwxr-x--x  Settings.apk
 rwxr-x--x  SettingsProvider.apk
 rwxr-x--x  SetupWizard.apk
 rwxr-x--x  SharedStorageBackup.apk
 rwxr-x--x  SystemUI.apk
 rwxr-x--x  TeleService.apk
 rwxr-x--x  VpnDialogs.apk
 rwxr-x--x  WallpaperCropper.apk
 rwxrwx--x  .
 rwxr-x--x  ..
 rwxr-x--x  AppHide.apk
 rwxr-x--x  ApplicationsProvider.apk
 rwxr-x--x  BatteryWarning.apk
 rwxr-x--x  Bluetooth.apk
 rwxr-x--x  BrowserProviderProxy.apk
 rwxr-x--x  Superuser.apk
 rwxr-x--x  CalendarImporter.apk
 rwxr-x--x  CellConnService.apk
 rwxr-x--x  CertInstaller.apk
 rwxr-x--x  CountryList.apk
 rwxr-x--x  DeskClock.apk
 rwxr-x--x  DocumentsUI.apk
 rwxr-x--x  DownloadProviderUi.apk
 rwxr-x--x  DrmProvider.apk
 rwxr-x--x  EngineerMode.apk
 rwxr-x--x  EngineerModeSim.apk
 rwxr-x--x  FMRadioHQ.apk
 rwxr-x--x  FWUpgrade.apk
 rwxr-x--x  FWUpgradeProvider.apk
 rwxr-x--x  FaceLock.apk
 rwxr-x--x  FactoryKitTest.apk
 rwxr-x--x  FactoryServiceHQ.apk
 rwxr-x--x  FlashLight.apk
 rwxr-x--x  Gallery2.apk
 rwxr-x--x  GoogleCalendarSyncAdapter.apk
 rwxr-x--x  GoogleContactsSyncAdapter.apk
 rwxr-x--x  GoogleTTS.apk
 rwxr-x--x  HTMLViewer.apk
 rwxr-x--x  KeyChain.apk
 rwxr-x--x  LocationEM.apk
 rwxr-x--x  MTKAndroidSuiteDaemon.apk
 rwxr-x--x  MTKLogger.apk
 rwxr-x--x  MTKThermalManager.apk
 rwxr-x--x  MtkBt.apk
 rwxr-x--x  MusicFX.apk
 rwxr-x--x  Omacp.apk
 rwxr-x--x  PacProcessor.apk
 rwxr-x--x  PackageInstaller.apk
 rwxr-x--x  PartnerBookmarksProvider.apk
 rwxr-x--x  PermissionControl.apk
 rwxr-x--x  PhaseBeam.apk
 rwxr-x--x  RuntimeTest.apk
 rwxr-x--x  SchedulePowerOnOff.apk
 rwxr-x--x  Stk1.apk
 rwxr-x--x  StkSelection.apk
 rwxr-x--x  SystemUpdate.apk
 rwxr-x--x  SystemUpdateAssistant.apk
 rwxr-x--x  TelephonyProvider.apk
 rwxr-x--x  UserDictionaryProvider.apk
 rwxr-x--x  VoiceCommand.apk
 rwxr-x--x  VoiceUnlock.apk
 rwxr-x--x  YGPS.apk
======
Rights are not shown correctly !!!!

von Rolf Magnus (Guest)


Rate this post
useful
not useful
Kirkina A. wrote:
> Rolf Magnus wrote:
>> What did you specify as directory? I guess it's not the current
>> directory, which is the only one that your program will work correctly
>> with.
>> You're calling stat on only file names without path. If you had checked
>> the return value of stat() for errors, you would have noticed that it
>> will report "file not found" errors as soon as you try to apply you
>> program to anything but the current directory.
>
> No, this is all right.

It isn't.

> I started the command ./ls /system/
> =====
>  rwxrwx--x  .
>  rwxr-x--x  ..
>  rwxr-x--x  lost+found
>  rwxr-x--x  app
>  rwxr-x--x  appjar
>  rwxr-x--x  bin
>  rwxr-x--x  build.prop
>  rwxrwx---  data
>  rwxrwx---  db
>  rwxrwx---  dolby
>  rwxrwx---  etc
>  rwxrwx---  fonts
>  rwxrwx---  framework
>  rwxrwx---  lib
>  rwxrwx---  media
>  rwxrwx---  mobile_toolkit
>  rwxrwx---  priv-app
>  rwxrwx---  res
>  rwxrwx---  tts
>  rwxrwx---  usr
>  rwxrwx---  vendor
>  rwxrwx---  xbin
> ======
> As you can see everything works great.

It just seems to, but it doesn't.

> Rights are not shown correctly !!!!

The reason is still the same.
Change your line that calls stat into:
1
       int ret = stat(ent->d_name, &statbuf); //получаем информацию
2
        if (ret < 0)
3
        {
4
            perror(ent->d_name);
5
        }
and see what happens.

Also, try
1
cd /system/priv-app
2
<mydir>/ls .
replacing <mydir> with the directory where your ls is installed.

von Alya (Guest)


Rate this post
useful
not useful
Rolf Magnus wrote:
> Kirkina A. wrote:
> Rolf Magnus wrote:
> What did you specify as directory? I guess it's not the current
> directory, which is the only one that your program will work correctly
> with.
> You're calling stat on only file names without path. If you had checked
> the return value of stat() for errors, you would have noticed that it
> will report "file not found" errors as soon as you try to apply you
> program to anything but the current directory.
>
> No, this is all right.
>
> It isn't.
>
> I started the command ./ls system
> =====
>  rwxrwx--x  .
>  rwxr-x--x  ..
>  rwxr-x--x  lost+found
>  rwxr-x--x  app
>  rwxr-x--x  appjar
>  rwxr-x--x  bin
>  rwxr-x--x  build.prop
>  rwxrwx---  data
>  rwxrwx---  db
>  rwxrwx---  dolby
>  rwxrwx---  etc
>  rwxrwx---  fonts
>  rwxrwx---  framework
>  rwxrwx---  lib
>  rwxrwx---  media
>  rwxrwx---  mobile_toolkit
>  rwxrwx---  priv-app
>  rwxrwx---  res
>  rwxrwx---  tts
>  rwxrwx---  usr
>  rwxrwx---  vendor
>  rwxrwx---  xbin
> ======
> As you can see everything works great.
>
> It just seems to, but it doesn't.
>
> Rights are not shown correctly !!!!
>
> The reason is still the same.
> Change your line that calls stat into:       int ret = stat(ent->d_name,
> &statbuf); //получаем информацию
>         if (ret < 0)
>         {
>             perror(ent->d_name);
>         }
>
> and see what happens.
>
> Also, trycd /system/priv-app
> <mydir>/ls .
>
> replacing <mydir> with the directory where your ls is installed.

Yes, I understood the error. I went to /bin and ran the command 
/mnt/programs/ls
=======
 rwxr-xr-x  znew
 rwxr-xr-x  zfgrep
 rwxr-xr-x  vdir
 rwxr-xr-x  lesspipe
 rwxr-xr-x  cat
 rwxr-xr-x  zforce
 rwxr-xr-x  run-parts
 rwxr-xr-x  zegrep
 rwxr-xr-x  nc
 rwxr-xr-x  mktemp
 rwxr-xr-x  dash
 rwxr-xr-x  netcat
 rwxr-xr-x  rm
 rwxr-xr-x  systemd-escape
 rwxr-xr-x  tar
 rwxr-xr-x  sh
 rwxr-xr-x  ..
 rwxr-xr-x  hostname
 rwxr-xr-x  systemd
 rwxr-xr-x  lessfile
 rwxr-xr-x  echo
 rwxr-xr-x  zless
 rwxr-xr-x  ps
 rwxr-xr-x  cpio
 rwxr-xr-x  rmdir
 rwxr-xr-x  unchroot
 rwxr-xr-x  pidof
 rwxr-xr-x  login
 rwxr-xr-x  kill
 rwxr-xr-x  domainname
 rwxr-xr-x  systemd-tty-ask-password-agent
 rwxr-xr-x  mkdir
 rwxr-xr-x  dmesg
 rwxr-xr-x  sed
 rwxr-xr-x  nc.traditional
 rwxr-xr-x  dir
 rwxr-xr-x  egrep
 rwxr-xr-x  nano
 rwxr-xr-x  cp
 rwxr-xr-x  zgrep
 rwxr-xr-x  sleep
 rwxr-xr-x  tailf
 rwxr-xr-x  systemd-machine-id-setup
 rwxr-xr-x  ss
 rwxr-xr-x  fgrep
 rwxr-xr-x  machinectl
 rwxr-xr-x  rnano
 rwxr-xr-x  lesskey
 rwxr-xr-x  false
 rwxr-xr-x  mount
 rwxr-xr-x  chacl
 rwxr-xr-x  netstat
 rwxr-xr-x  dnsdomainname
 rwxr-xr-x  dd
 rwxr-xr-x  uncompress
 rwxr-xr-x  touch
 rwxr-xr-x  systemd-ask-password
 rwxr-xr-x  readlink
 rwxr-xr-x  chown
 rwxr-xr-x  which
 rwxr-xr-x  lessecho
 rwxr-xr-x  ls
 rwxr-xr-x  grep
 rwxr-xr-x  chgrp
 rwxr-xr-x  kmod
 rwxr-xr-x  mt
 rwxr-xr-x  uname
 rwxr-xr-x  systemd-tmpfiles
 rwxr-xr-x  pwd
 rwxr-xr-x  mknod
 rwxr-xr-x  ln
 rwxr-xr-x  .
 rwxr-xr-x  mountpoint
 rwxr-xr-x  ping
 rwxr-xr-x  zdiff
 rwxr-xr-x  mt-gnu
 rwxr-xr-x  setfacl
 rwxr-xr-x  lsblk
 rwxr-xr-x  umount
 rwxr-xr-x  journalctl
 rwxr-xr-x  ypdomainname
 rwxr-xr-x  loginctl
 rwxr-xr-x  true
 rwxr-xr-x  gzip
 rwxr-xr-x  systemctl
 rwxr-xr-x  chmod
 rwxr-xr-x  ping6
 rwxr-xr-x  ip
 rwxr-xr-x  wdctl
 rwxr-xr-x  nisdomainname
 rwxr-xr-x  su
 rwxr-xr-x  mv
 rwxr-xr-x  udevadm
 rwxr-xr-x  stty
 rwxr-xr-x  gzexe
 rwxr-xr-x  systemd-inhibit
 rwxr-xr-x  tempfile
 rwxr-xr-x  date
 rwxr-xr-x  lsmod
 rwxr-xr-x  df
 rwxr-xr-x  systemd-notify
 rwxr-xr-x  bash
 rwxr-xr-x  rbash
 rwxr-xr-x  gunzip
 rwxr-xr-x  zcat
 rwxr-xr-x  zmore
 rwxr-xr-x  more
 rwxr-xr-x  findmnt
 rwxr-xr-x  less
 rwxr-xr-x  sh.distrib
 rwxr-xr-x  zcmp
 rwxr-xr-x  sync
 rwxr-xr-x  getfacl
=====
But the result of the ls system command ls
=====
total 5504
-rwxr-xr-x. 1 root root 679084 Nov 13  2014 bash
-rwxr-xr-x. 1 root root  30464 Mar 14  2015 cat
-rwxr-xr-x. 1 root root   9728 Sep  8  2014 chacl
-rwxr-xr-x. 1 root root  38644 Mar 14  2015 chgrp
-rwxr-xr-x. 1 root root  34520 Mar 14  2015 chmod
-rwxr-xr-x. 1 root root  38652 Mar 14  2015 chown
-rwxr-xr-x. 1 root root  92216 Mar 14  2015 cp
-rwxr-xr-x. 1 root root  89704 Feb 14  2016 cpio
-rwxr-xr-x. 1 root root  75420 Nov  8  2014 dash
-rwxr-xr-x. 1 root root  42728 Mar 14  2015 date
-rwxr-xr-x. 1 root root  42788 Mar 14  2015 dd
-rwxr-xr-x. 1 root root  59548 Mar 14  2015 df
-rwxr-xr-x. 1 root root  79984 Mar 14  2015 dir
-rwxr-xr-x. 1 root root  38812 Mar 30  2015 dmesg
lrwxrwxrwx. 1 root root      8 Jul  3 00:04 dnsdomainname -> hostname
lrwxrwxrwx. 1 root root      8 Jul  3 00:04 domainname -> hostname
-rwxr-xr-x. 1 root root  22160 Mar 14  2015 echo
-rwxr-xr-x. 1 root root     29 Feb 14  2015 egrep
-rwxr-xr-x. 1 root root  18060 Mar 14  2015 false
-rwxr-xr-x. 1 root root     29 Feb 14  2015 fgrep
-rwxr-xr-x. 1 root root  35472 Mar 30  2015 findmnt
-rwxr-xr-x. 1 root root  18352 Sep  8  2014 getfacl
-rwxr-xr-x. 1 root root 128552 Feb 14  2015 grep
-rwxr-xr-x. 2 root root   2301 Sep 27  2014 gunzip
-rwxr-xr-x. 1 root root   5927 Sep 27  2014 gzexe
-rwxr-xr-x. 1 root root  63992 Sep 27  2014 gzip
-rwxr-xr-x. 1 root root   9768 Nov  3  2013 hostname
-rwxr-xr-x. 1 root root 223048 Sep  5  2014 ip
-rwxr-xr-x. 1 root root 161428 Mar 14  2016 journalctl
-rwxr-xr-x. 1 root root  18028 Mar  7  2015 kill
-rwxr-xr-x. 1 root root 104384 Sep 27  2014 kmod
-rwxr-xr-x. 1 root root 116292 Sep  8  2014 less
-rwxr-xr-x. 1 root root   9588 Sep  8  2014 lessecho
lrwxrwxrwx. 1 root root      8 Jul  3 00:04 lessfile -> lesspipe
-rwxr-xr-x. 1 root root  14352 Sep  8  2014 lesskey
-rwxr-xr-x. 1 root root   7758 Sep  8  2014 lesspipe
-rwxr-xr-x. 1 root root  34548 Mar 14  2015 ln
-rwxr-xr-x. 1 root root  34688 Nov 20  2015 login
-rwxr-xr-x. 1 root root 202476 Mar 14  2016 loginctl
-rwxr-xr-x. 1 root root  79984 Mar 14  2015 ls
-rwxr-xr-x. 1 root root  42936 Mar 30  2015 lsblk
lrwxrwxrwx. 1 root root      4 Jul  3 00:04 lsmod -> kmod
-rwxr-xr-x. 1 root root 186092 Mar 14  2016 machinectl
-rwxr-xr-x. 1 root root  46936 Mar 14  2015 mkdir
-rwxr-xr-x. 1 root root  38712 Mar 14  2015 mknod
-rwxr-xr-x. 1 root root  26328 Mar 14  2015 mktemp
-rwxr-xr-x. 1 root root  30460 Mar 30  2015 more
-rwsr-xr-x. 1 root root  30664 Mar 30  2015 mount
-rwxr-xr-x. 1 root root   5564 Apr  6  2015 mountpoint
lrwxrwxrwx. 1 root root     20 Jul  3 00:04 mt -> /etc/alternatives/mt
-rwxr-xr-x. 1 root root  42992 Feb 14  2016 mt-gnu
-rwxr-xr-x. 1 root root  79900 Mar 14  2015 mv
-rwxr-xr-x. 1 root root 125044 Jul 16  2014 nano
lrwxrwxrwx. 1 root root     20 Jul  3 00:04 nc -> /etc/alternatives/nc
-rwxr-xr-x. 1 root root  18092 Sep 11  2014 nc.traditional
lrwxrwxrwx. 1 root root     24 Jul  3 00:04 netcat -> 
/etc/alternatives/netcat
-rwxr-xr-x. 1 root root  84680 Nov  8  2014 netstat
lrwxrwxrwx. 1 root root      8 Jul  3 00:04 nisdomainname -> hostname
lrwxrwxrwx. 1 root root     14 Jul  3 00:04 pidof -> /sbin/killall5
-rwxr-xr-x. 1 root root  34752 Nov  8  2014 ping
-rwxr-xr-x. 1 root root  35056 Nov  8  2014 ping6
-rwxr-xr-x. 1 root root  67492 Mar  7  2015 ps
-rwxr-xr-x. 1 root root  22208 Mar 14  2015 pwd
lrwxrwxrwx. 1 root root      4 Jul  3 00:04 rbash -> bash
-rwxr-xr-x. 1 root root  26284 Mar 14  2015 readlink
-rwxr-xr-x. 1 root root  38640 Mar 14  2015 rm
-rwxr-xr-x. 1 root root  26288 Mar 14  2015 rmdir
lrwxrwxrwx. 1 root root      4 Jul  3 00:04 rnano -> nano
-rwxr-xr-x. 1 root root  14112 Nov  8  2014 run-parts
-rwxr-xr-x. 1 root root  47308 Nov  8  2014 sed
-rwxr-xr-x. 1 root root  22552 Sep  8  2014 setfacl
lrwxrwxrwx. 1 root root      4 Jul  3 00:04 sh -> dash
lrwxrwxrwx. 1 root root      4 Jul  3 00:04 sh.distrib -> dash
-rwxr-xr-x. 1 root root  18076 Mar 14  2015 sleep
-rwxr-xr-x. 1 root root  51800 Sep  5  2014 ss
-rwxr-xr-x. 1 root root  46796 Mar 14  2015 stty
-rwsr-xr-x. 1 root root  26588 Nov 20  2015 su
-rwxr-xr-x. 1 root root  18068 Mar 14  2015 sync
-rwxr-xr-x. 1 root root 366476 Mar 14  2016 systemctl
lrwxrwxrwx. 1 root root     20 Jul  3 00:04 systemd -> 
/lib/systemd/systemd
-rwxr-xr-x. 1 root root  30340 Mar 14  2016 systemd-ask-password
-rwxr-xr-x. 1 root root  22140 Mar 14  2016 systemd-escape
-rwxr-xr-x. 1 root root 157416 Mar 14  2016 systemd-inhibit
-rwxr-xr-x. 1 root root  22132 Mar 14  2016 systemd-machine-id-setup
-rwxr-xr-x. 1 root root  22128 Mar 14  2016 systemd-notify
-rwxr-xr-x. 1 root root  59000 Mar 14  2016 systemd-tmpfiles
-rwxr-xr-x. 1 root root  38524 Mar 14  2016 
systemd-tty-ask-password-agent
-rwxr-xr-x. 1 root root  13908 Mar 30  2015 tailf
-rwxr-xr-x. 1 root root 224444 Nov  9  2014 tar
-rwxr-xr-x. 1 root root   5580 Nov  8  2014 tempfile
-rwxr-xr-x. 1 root root  38668 Mar 14  2015 touch
-rwxr-xr-x. 1 root root  18060 Mar 14  2015 true
-rwxr-xr-x. 1 root root 202388 Mar 14  2016 udevadm
-rwsr-xr-x. 1 root root  22324 Mar 30  2015 umount
-rwxr-xr-x. 1 root root  18076 Mar 14  2015 uname
-rwxr-xr-x. 1 root root    323 Sep 13  2016 unchroot
-rwxr-xr-x. 2 root root   2301 Sep 27  2014 uncompress
-rwxr-xr-x. 1 root root  79984 Mar 14  2015 vdir
-rwxr-xr-x. 1 root root  22124 Mar 30  2015 wdctl
-rwxr-xr-x. 1 root root    946 Nov  8  2014 which
lrwxrwxrwx. 1 root root      8 Jul  3 00:04 ypdomainname -> hostname
-rwxr-xr-x. 1 root root   1937 Sep 27  2014 zcat
-rwxr-xr-x. 1 root root   1777 Sep 27  2014 zcmp
-rwxr-xr-x. 1 root root   5764 Sep 27  2014 zdiff
-rwxr-xr-x. 1 root root    140 Sep 27  2014 zegrep
-rwxr-xr-x. 1 root root    140 Sep 27  2014 zfgrep
-rwxr-xr-x. 1 root root   2131 Sep 27  2014 zforce
-rwxr-xr-x. 1 root root   5938 Sep 27  2014 zgrep
-rwxr-xr-x. 1 root root   2037 Sep 27  2014 zless
-rwxr-xr-x. 1 root root   1910 Sep 27  2014 zmore
-rwxr-xr-x. 1 root root   5047 Sep 27  2014 znew
=======
Now everything displays rules. I slightly changed the code so that it 
would output the files in the current folder if the parameters are not 
specified in the command line.
But here's what the program displays if you type ./ls ​​/ bin:
========
znew: No such file or directory
zfgrep: No such file or directory
vdir: No such file or directory
lesspipe: No such file or directory
cat: No such file or directory
zforce: No such file or directory
run-parts: No such file or directory
zegrep: No such file or directory
nc: No such file or directory
mktemp: No such file or directory
dash: No such file or directory
netcat: No such file or directory
rm: No such file or directory
systemd-escape: No such file or directory
tar: No such file or directory
sh: No such file or directory
hostname: No such file or directory
systemd: No such file or directory
lessfile: No such file or directory
echo: No such file or directory
zless: No such file or directory
ps: No such file or directory
cpio: No such file or directory
rmdir: No such file or directory
unchroot: No such file or directory
pidof: No such file or directory
login: No such file or directory
kill: No such file or directory
domainname: No such file or directory
systemd-tty-ask-password-agent: No such file or directory
mkdir: No such file or directory
dmesg: No such file or directory
sed: No such file or directory
nc.traditional: No such file or directory
dir: No such file or directory
egrep: No such file or directory
nano: No such file or directory
cp: No such file or directory
zgrep: No such file or directory
sleep: No such file or directory
tailf: No such file or directory
systemd-machine-id-setup: No such file or directory
ss: No such file or directory
fgrep: No such file or directory
machinectl: No such file or directory
rnano: No such file or directory
lesskey: No such file or directory
false: No such file or directory
mount: No such file or directory
chacl: No such file or directory
netstat: No such file or directory
dnsdomainname: No such file or directory
dd: No such file or directory
uncompress: No such file or directory
touch: No such file or directory
systemd-ask-password: No such file or directory
readlink: No such file or directory
chown: No such file or directory
which: No such file or directory
lessecho: No such file or directory
grep: No such file or directory
chgrp: No such file or directory
kmod: No such file or directory
mt: No such file or directory
uname: No such file or directory
systemd-tmpfiles: No such file or directory
pwd: No such file or directory
mknod: No such file or directory
ln: No such file or directory
mountpoint: No such file or directory
ping: No such file or directory
zdiff: No such file or directory
mt-gnu: No such file or directory
setfacl: No such file or directory
lsblk: No such file or directory
umount: No such file or directory
journalctl: No such file or directory
ypdomainname: No such file or directory
loginctl: No such file or directory
true: No such file or directory
gzip: No such file or directory
systemctl: No such file or directory
chmod: No such file or directory
ping6: No such file or directory
ip: No such file or directory
wdctl: No such file or directory
nisdomainname: No such file or directory
su: No such file or directory
mv: No such file or directory
udevadm: No such file or directory
stty: No such file or directory
gzexe: No such file or directory
systemd-inhibit: No such file or directory
tempfile: No such file or directory
date: No such file or directory
lsmod: No such file or directory
df: No such file or directory
systemd-notify: No such file or directory
bash: No such file or directory
rbash: No such file or directory
gunzip: No such file or directory
zcat: No such file or directory
zmore: No such file or directory
more: No such file or directory
findmnt: No such file or directory
less: No such file or directory
sh.distrib: No such file or directory
zcmp: No such file or directory
sync: No such file or directory
getfacl: No such file or directory
 ---------  znew
 ---------  zfgrep
 ---------  vdir
 ---------  lesspipe
 ---------  cat
 ---------  zforce
 ---------  run-parts
 ---------  zegrep
 ---------  nc
 ---------  mktemp
 ---------  dash
 ---------  netcat
 ---------  rm
 ---------  systemd-escape
 ---------  tar
 ---------  sh
 rwxrwx---  ..
 rwxrwx---  hostname
 rwxrwx---  systemd
 rwxrwx---  lessfile
 rwxrwx---  echo
 rwxrwx---  zless
 rwxrwx---  ps
 rwxrwx---  cpio
 rwxrwx---  rmdir
 rwxrwx---  unchroot
 rwxrwx---  pidof
 rwxrwx---  login
 rwxrwx---  kill
 rwxrwx---  domainname
 rwxrwx---  systemd-tty-ask-password-agent
 rwxrwx---  mkdir
 rwxrwx---  dmesg
 rwxrwx---  sed
 rwxrwx---  nc.traditional
 rwxrwx---  dir
 rwxrwx---  egrep
 rwxrwx---  nano
 rwxrwx---  cp
 rwxrwx---  zgrep
 rwxrwx---  sleep
 rwxrwx---  tailf
 rwxrwx---  systemd-machine-id-setup
 rwxrwx---  ss
 rwxrwx---  fgrep
 rwxrwx---  machinectl
 rwxrwx---  rnano
 rwxrwx---  lesskey
 rwxrwx---  false
 rwxrwx---  mount
 rwxrwx---  chacl
 rwxrwx---  netstat
 rwxrwx---  dnsdomainname
 rwxrwx---  dd
 rwxrwx---  uncompress
 rwxrwx---  touch
 rwxrwx---  systemd-ask-password
 rwxrwx---  readlink
 rwxrwx---  chown
 rwxrwx---  which
 rwxrwx---  lessecho
 rwxrwx---  ls
 rwxrwx---  grep
 rwxrwx---  chgrp
 rwxrwx---  kmod
 rwxrwx---  mt
 rwxrwx---  uname
 rwxrwx---  systemd-tmpfiles
 rwxrwx---  pwd
 rwxrwx---  mknod
 rwxrwx---  ln
 rwxrwx---  .
 rwxrwx---  mountpoint
 rwxrwx---  ping
 rwxrwx---  zdiff
 rwxrwx---  mt-gnu
 rwxrwx---  setfacl
 rwxrwx---  lsblk
 rwxrwx---  umount
 rwxrwx---  journalctl
 rwxrwx---  ypdomainname
 rwxrwx---  loginctl
 rwxrwx---  true
 rwxrwx---  gzip
 rwxrwx---  systemctl
 rwxrwx---  chmod
 rwxrwx---  ping6
 rwxrwx---  ip
 rwxrwx---  wdctl
 rwxrwx---  nisdomainname
 rwxrwx---  su
 rwxrwx---  mv
 rwxrwx---  udevadm
 rwxrwx---  stty
 rwxrwx---  gzexe
 rwxrwx---  systemd-inhibit
 rwxrwx---  tempfile
 rwxrwx---  date
 rwxrwx---  lsmod
 rwxrwx---  df
 rwxrwx---  systemd-notify
 rwxrwx---  bash
 rwxrwx---  rbash
 rwxrwx---  gunzip
 rwxrwx---  zcat
 rwxrwx---  zmore
 rwxrwx---  more
 rwxrwx---  findmnt
 rwxrwx---  less
 rwxrwx---  sh.distrib
 rwxrwx---  zcmp
 rwxrwx---  sync
 rwxrwx---  getfacl
=======
My new code:
1
#include <stdio.h>
2
#include <sys/types.h>
3
#include <sys/stat.h>
4
#include <dirent.h>
5
#include <stdlib.h>
6
#include <string.h>
7
#include <pwd.h>
8
#include <grp.h>
9
#include <time.h>
10
#include <locale.h>
11
#include <langinfo.h>
12
#include <stdint.h>
13
14
typedef struct {
15
    char *arg;
16
} DATA; //динамический массив
17
18
struct stat statbuf; //информация о файле
19
struct passwd *pwd;
20
21
DATA *the_array = NULL;
22
int num_elements = 0; // Keeps track of the number of elements used
23
int num_allocated = 0; // This is essentially how large the array is
24
25
//==== Функции
26
int AddToArray (DATA item);
27
char const * sperm(__mode_t mode);
28
void printLine();
29
//========
30
31
int main(int argc, char *argv[])
32
{
33
  short int numFiles=0; //количество файлов/папок в аргументах
34
  short int key_a=0; //ключ -a
35
  for(int i=1; i<argc; i++) //Сканируем все аргументы
36
  {
37
    char ch = argv[i][0]; //берем первый символ
38
    if(ch == '-')
39
    {
40
      int start=1; //начало после '-'
41
      while(argv[i][start] != NULL) //проверяем ключи
42
      {
43
        if(argv[i][start] == 'a')
44
             key_a=1;
45
        start++;
46
      }
47
    }
48
    else
49
    {
50
       numFiles++;
51
         DATA temp;
52
         temp.arg = malloc((strlen(argv[i]) + 1) * sizeof(char));
53
         strncpy(temp.arg, argv[i], strlen(argv[i]) + 1); //копируем значение аргумента в переменную
54
         if(AddToArray(temp) == -1) //если возникли проблемы с памятью
55
               return 1;
56
    }
57
  }
58
  
59
  for(int i=0;i<numFiles;i++) //все каталоги из аргументов
60
  {
61
      printLine();
62
    }
63
    
64
    if(numFiles==0) //если не заданы папки в параметрах
65
    {
66
      DIR *dir = opendir(".");
67
        
68
      struct dirent *ent;
69
      while((ent = readdir(dir)) != NULL)
70
      {
71
       
72
      int ret = stat(ent->d_name, &statbuf);
73
      if (ret < 0)
74
      {
75
          perror(ent->d_name);
76
      }
77
        
78
      printf("%10.10s", sperm (statbuf.st_mode));
79
      printf("\t%s\n", ent->d_name);
80
      }
81
    
82
    }
83
    
84
    return 0;
85
}
86
87
void printLine() //печать файла папки с инфой
88
{
89
  DIR *dir = opendir(the_array->arg);
90
    *the_array++;
91
    if(dir)
92
    {
93
    struct dirent *ent;
94
    while((ent = readdir(dir)) != NULL)
95
    {
96
        
97
        int ret = stat(ent->d_name, &statbuf);
98
        if (ret < 0)
99
        {
100
            perror(ent->d_name);
101
        }
102
        
103
        printf("%10.10s", sperm (statbuf.st_mode));
104
        printf("\t%s\n", ent->d_name);
105
    }
106
    }
107
    else
108
    {
109
    fprintf(stderr, "Error opening directory\n");
110
    }
111
}
112
113
char const * sperm(__mode_t mode) {
114
    static char local_buff[16] = {0};
115
    int i = 0;
116
    // user permissions
117
    if ((mode & S_IRUSR) == S_IRUSR) local_buff[i] = 'r';
118
    else local_buff[i] = '-';
119
    i++;
120
    if ((mode & S_IWUSR) == S_IWUSR) local_buff[i] = 'w';
121
    else local_buff[i] = '-';
122
    i++;
123
    if ((mode & S_IXUSR) == S_IXUSR) local_buff[i] = 'x';
124
    else local_buff[i] = '-';
125
    i++;
126
    // group permissions
127
    if ((mode & S_IRGRP) == S_IRGRP) local_buff[i] = 'r';
128
    else local_buff[i] = '-';
129
    i++;
130
    if ((mode & S_IWGRP) == S_IWGRP) local_buff[i] = 'w';
131
    else local_buff[i] = '-';
132
    i++;
133
    if ((mode & S_IXGRP) == S_IXGRP) local_buff[i] = 'x';
134
    else local_buff[i] = '-';
135
    i++;
136
    // other permissions
137
    if ((mode & S_IROTH) == S_IROTH) local_buff[i] = 'r';
138
    else local_buff[i] = '-';
139
    i++;
140
    if ((mode & S_IWOTH) == S_IWOTH) local_buff[i] = 'w';
141
    else local_buff[i] = '-';
142
    i++;
143
    if ((mode & S_IXOTH) == S_IXOTH) local_buff[i] = 'x';
144
    else local_buff[i] = '-';
145
    return local_buff;
146
}
147
148
int AddToArray (DATA item) //функция добавления в динамический массив
149
{
150
        if(num_elements == num_allocated) // Are more refs required?
151
        { 
152
                // Feel free to change the initial number of refs
153
                // and the rate at which refs are allocated.
154
                if (num_allocated == 0)
155
                        num_allocated = 3; // Start off with 3 refs
156
                else
157
                        num_allocated *= 2; // Double the number 
158
                                                    // of refs allocated
159
160
                // Make the reallocation transactional 
161
                // by using a temporary variable first
162
                void *_tmp = realloc(the_array, (num_allocated * sizeof(DATA)));
163
164
                // If the reallocation didn't go so well,
165
                // inform the user  bail out
166
                if (!_tmp)
167
                {
168
                        fprintf(stderr, "ERROR: Couldn't realloc memory!\n");
169
                        return(-1);
170
                }
171
172
                // Things are looking good so far
173
                the_array = (DATA*)_tmp;
174
        }
175
176
        the_array[num_elements] = item;
177
        num_elements++;
178
179
        return num_elements;
180
}

von Rolf Magnus (Guest)


Rate this post
useful
not useful
Alya wrote:
> Now everything displays rules. I slightly changed the code so that it
> would output the files in the current folder if the parameters are not
> specified in the command line.
> But here's what the program displays if you type ./ls ​​/ bin:
> ========
> znew: No such file or directory
[...]

Yes. The issue still didn't change.  You open /bin in opendir. Then you 
get a list of all the files within it - but only file names, without 
path!
So you get e.g. "znew". You just give that name to stat, but because 
your current working directory is "/mnt/programs", stat will look for a 
file "/mnt/programs/znew", which does not exist. That's why you get the 
error "No such file or directory".
You would have to specify "/bin/znew" to stat and not just "znew". How 
would stat know that it has to search in /bin for this file?

von Kirkina A. (Company: no) (alya)


Rate this post
useful
not useful
Rolf Magnus wrote:
> Alya wrote:
> Now everything displays rules. I slightly changed the code so that it
> would output the files in the current folder if the parameters are not
> specified in the command line.
> But here's what the program displays if you type ./ls ​​/ bin:
> ========
> znew: No such file or directory
>
> [...]
>
> Yes. The issue still didn't change.  You open /bin in opendir. Then you
> get a list of all the files within it - but only file names, without
> path!
> So you get e.g. "znew". You just give that name to stat, but because
> your current working directory is "/mnt/programs", stat will look for a
> file "/mnt/programs/znew", which does not exist. That's why you get the
> error "No such file or directory".
> You would have to specify "/bin/znew" to stat and not just "znew". How
> would stat know that it has to search in /bin for this file?

All now I have managed to solve a problem here such code:
1
char txt[128];
2
        snprintf(txt, sizeof txt, "%s/%s", the_array->arg, next->d_name);
3
        int ret = stat(txt, &statbuf);
4
        if (ret < 0)
5
        {
6
            perror(txt);
7
        }

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.